/* =========================================================
   Image Template Builder
   Style language matched to color.horoma.school
   Accent: #8F5CF5
   ========================================================= */

:root {
  /* Light theme tokens */
  --bg: #ffffff;
  --bg-elev: #ffffff;
  --bg-soft: #fafafa;
  --bg-stage: #f4f4f5;
  --border: #e6e6e6;
  --border-strong: #d4d4d4;
  --text: #0a0a0a;
  --text-muted: #6b6b6b;
  --text-subtle: #9a9a9a;
  --accent: #8F5CF5;
  --accent-soft: #f3edff;
  --accent-strong: #7a45e8;
  --danger: #c0392b;
  --shadow-sm: 0 1px 2px rgba(0,0,0,.04);
  --shadow-md: 0 8px 30px rgba(0,0,0,.08);
  --shadow-lg: 0 30px 80px rgba(0,0,0,.12);
  --radius: 8px;
  --radius-lg: 12px;
  --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --font-serif: 'Fraunces', Georgia, serif;
  --topbar-h: 56px;
  --footer-h: 44px;
  --sidebar-w: 320px;
}

[data-theme="dark"] {
  --bg: #0c0c0d;
  --bg-elev: #141416;
  --bg-soft: #18181b;
  --bg-stage: #101012;
  --border: #25262b;
  --border-strong: #34353b;
  --text: #f1f1f3;
  --text-muted: #a0a0a8;
  --text-subtle: #6c6c74;
  --accent-soft: #2a1f47;
  --shadow-sm: 0 1px 2px rgba(0,0,0,.5);
  --shadow-md: 0 8px 30px rgba(0,0,0,.5);
  --shadow-lg: 0 30px 80px rgba(0,0,0,.6);
}

*, *::before, *::after { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

a { color: inherit; }
a:hover { color: var(--accent); }

button { font-family: inherit; }

/* Accent line on top */
body::before {
  content: '';
  position: fixed;
  top: 0; left: 0; right: 0;
  height: 2px;
  background: linear-gradient(90deg, transparent, var(--accent), transparent);
  z-index: 100;
  pointer-events: none;
  opacity: .6;
}

/* ===== TOPBAR ===== */
.topbar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  height: var(--topbar-h);
  padding: 0 24px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  flex-shrink: 0;
}
.topbar-left { display: flex; align-items: center; gap: 12px; }
.topbar-center { display: flex; justify-content: center; }
.topbar-right { display: flex; justify-content: flex-end; align-items: center; gap: 8px; }

.brand {
  font-family: var(--font-serif);
  font-weight: 700;
  font-size: 22px;
  letter-spacing: -0.01em;
}

.step-pill {
  font-size: 13px;
  color: var(--text-muted);
  padding: 6px 14px;
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: 999px;
}

.icon-btn {
  width: 34px; height: 34px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text-muted);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: all .15s ease;
}
.icon-btn:hover { color: var(--text); border-color: var(--border-strong); }

/* ===== LAYOUT ===== */
.layout {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  flex: 1;
  min-height: 0;
}

/* ===== SIDEBAR ===== */
.sidebar {
  border-right: 1px solid var(--border);
  background: var(--bg);
  padding: 20px 18px 28px;
  overflow-y: auto;
}

.section { margin-bottom: 28px; }
.section-actions { margin-top: 8px; margin-bottom: 8px; }

.section-title {
  font-size: 13px;
  font-weight: 600;
  margin: 0 0 4px;
  letter-spacing: -0.005em;
}
.section-sub {
  font-size: 12px;
  color: var(--text-muted);
  margin: 0 0 12px;
}

/* Dropzone */
.dropzone {
  display: block;
  border: 1.5px dashed var(--border-strong);
  border-radius: var(--radius);
  padding: 22px 14px;
  cursor: pointer;
  transition: all .15s ease;
  background: var(--bg-soft);
}
.dropzone:hover {
  border-color: var(--accent);
  background: var(--accent-soft);
}
.dropzone.is-drag {
  border-color: var(--accent);
  background: var(--accent-soft);
  border-style: solid;
}
.dropzone-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 4px;
  color: var(--text-muted);
}
.dropzone-inner strong {
  color: var(--text);
  font-weight: 600;
  font-size: 13px;
  margin-top: 4px;
}
.dropzone-inner .muted { font-size: 12px; color: var(--text-subtle); }
.dropzone-inner .counter {
  font-size: 11px;
  color: var(--text-subtle);
  background: var(--bg);
  border: 1px solid var(--border);
  padding: 2px 8px;
  border-radius: 999px;
  margin-top: 8px;
}

/* Thumbs list */
.thumbs {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.thumb {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 8px;
  border-radius: 6px;
  border: 1px solid transparent;
  cursor: pointer;
  background: var(--bg-soft);
  transition: all .12s ease;
}
.thumb:hover { background: var(--bg); border-color: var(--border); }
.thumb.is-active {
  border-color: var(--accent);
  background: var(--accent-soft);
}
.thumb-img {
  width: 36px;
  height: 36px;
  object-fit: cover;
  border-radius: 4px;
  background: var(--border);
  flex-shrink: 0;
}
.thumb-meta {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}
.thumb-name {
  font-size: 12px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.thumb-orient {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-subtle);
}
.thumb-orient.h { color: var(--accent); }

/* Field inputs */
.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 10px;
}
.field span {
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.field input,
.field textarea {
  width: 100%;
  padding: 8px 10px;
  font-size: 13px;
  font-family: inherit;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  resize: vertical;
  transition: border-color .12s ease;
}
.field input:focus,
.field textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

/* Color picker rows */
.color-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 14px;
}
.color-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.color-field span {
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.color-field input[type="color"] {
  width: 100%;
  height: 32px;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 2px;
  background: var(--bg);
  cursor: pointer;
}

.bg-row { display: flex; flex-direction: column; gap: 6px; }
.bg-label {
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.bg-controls {
  display: flex;
  align-items: center;
  gap: 8px;
}
.bg-controls input[type="color"] {
  width: 40px;
  height: 32px;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 2px;
  cursor: pointer;
  background: var(--bg);
  flex-shrink: 0;
}
.swatches {
  display: flex;
  gap: 6px;
  flex: 1;
}
.sw {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 1px solid var(--border-strong);
  cursor: pointer;
  padding: 0;
  transition: transform .1s ease;
}
.sw:hover { transform: scale(1.1); }

/* Buttons */
.ghost-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 12px;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  transition: all .12s ease;
}
.ghost-btn:hover {
  border-color: var(--border-strong);
  background: var(--bg-soft);
}
.ghost-btn.small { padding: 5px 10px; font-size: 12px; }
.ghost-btn.danger { color: var(--danger); }
.ghost-btn.danger:hover { border-color: var(--danger); background: rgba(192,57,43,.06); }
.ghost-btn.primary {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}
.ghost-btn.primary:hover {
  background: var(--accent-strong);
  border-color: var(--accent-strong);
}
.ghost-btn[disabled] {
  opacity: 0.5;
  cursor: not-allowed;
  pointer-events: none;
}

/* Format toggle */
.format-toggle {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}
.fmt-btn {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 1px;
  padding: 8px 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  text-align: left;
  transition: all .12s ease;
  color: var(--text);
}
.fmt-btn strong { font-size: 12px; font-weight: 600; }
.fmt-btn span { font-size: 10px; color: var(--text-muted); }
.fmt-btn:hover { border-color: var(--border-strong); }
.fmt-btn.is-active {
  border-color: var(--accent);
  background: var(--accent-soft);
}

/* Watermark tabs */
.wm-tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  margin-bottom: 10px;
  padding: 3px;
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: 7px;
}
.wm-tab {
  padding: 5px 8px;
  font-size: 12px;
  background: transparent;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  color: var(--text-muted);
  transition: all .12s ease;
  font-family: inherit;
}
.wm-tab:hover { color: var(--text); }
.wm-tab.is-active {
  background: var(--bg);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}
.wm-panel { display: flex; flex-direction: column; gap: 8px; }
.wm-drop { padding: 14px 12px; }
.wm-drop .dropzone-inner strong { font-size: 12px; }
#wmLogoClear { align-self: flex-start; }

.export-grid {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.export-grid .ghost-btn {
  justify-content: flex-start;
  width: 100%;
}
.export-status {
  font-size: 11px;
  color: var(--text-muted);
  margin: 8px 0 0;
  min-height: 14px;
}
.export-status.is-done { color: var(--accent); }

/* ===== CANVAS / STAGE ===== */
.canvas {
  background: var(--bg-stage);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding: 24px;
  overflow-y: auto;
  position: relative;
}

.empty {
  margin: auto;
  text-align: center;
  max-width: 480px;
  color: var(--text-muted);
}
.empty h1 {
  font-family: var(--font-serif);
  font-size: 32px;
  font-weight: 600;
  color: var(--text);
  margin: 0 0 12px;
  letter-spacing: -0.02em;
}
.empty p {
  font-size: 15px;
  line-height: 1.6;
  margin: 0 0 24px;
}
.empty-hint {
  font-size: 12px;
  color: var(--text-subtle);
}
.kbd {
  display: inline-block;
  padding: 2px 6px;
  border: 1px solid var(--border-strong);
  border-bottom-width: 2px;
  border-radius: 4px;
  font-size: 11px;
  font-family: ui-monospace, 'SFMono-Regular', monospace;
  background: var(--bg);
  color: var(--text-muted);
  margin: 0 1px;
}

/* Stage */
.stage {
  width: 100%;
  max-width: 1100px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.stage-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 999px;
  box-shadow: var(--shadow-sm);
}
.stage-toolbar .divider {
  width: 1px;
  height: 18px;
  background: var(--border);
  margin: 0 4px;
}
.stage-counter {
  font-size: 12px;
  color: var(--text-muted);
  padding: 0 8px;
  font-variant-numeric: tabular-nums;
}

.page-wrap {
  width: 100%;
  display: flex;
  justify-content: center;
  padding: 8px 0 40px;
}

/* =========================================================
   PAGE — the actual template
   Ratios:
     A4      portrait  = 210 / 297 = 0.7071  → aspect 1 : 1.4142
     A4      landscape = 297 / 210                     1.4142 : 1
     Letter  portrait  = 8.5 / 11   = 0.7727  → aspect 1 : 1.2941
     Letter  landscape = 11 / 8.5                       1.2941 : 1
   Margins fixed at ~1.5 cm on A4 = ~5% of width.
   Layout is FIXED: image area = 70% of page height, text = 30% (vertical).
   For horizontal: image area on the upper portion, text underneath.
   ========================================================= */
.page {
  background: #ffffff;
  color: #1a1a1a;
  box-shadow: var(--shadow-lg);
  position: relative;
  font-family: var(--font-sans);
  display: grid;
  box-sizing: border-box;
  /* Custom properties set inline by JS */
  --tpl-title: #8F5CF5;
  --tpl-text: #1a1a1a;
  --tpl-bg: #ffffff;
  --tpl-pad: 3.75%;       /* Homogeneous on all 4 sides */
  background: var(--tpl-bg);
  color: var(--tpl-text);
  padding: var(--tpl-pad);
  /* Two-row grid: image (70%) + text block (30%). Heights are calculated
     as a fraction of the (page height - 2 × padding), so the bottom
     padding is preserved — no overflow on the text side. */
  grid-template-rows: 70fr 30fr;
  gap: 2.5%;
}
.page.vertical { width: min(640px, 90vw); }
.page.horizontal { width: min(960px, 95vw); }

.page.format-a4.vertical    { aspect-ratio: 210 / 297; }
.page.format-a4.horizontal  { aspect-ratio: 297 / 210; }
.page.format-letter.vertical   { aspect-ratio: 8.5 / 11; }
.page.format-letter.horizontal { aspect-ratio: 11 / 8.5; }

.page-image {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
}
/* The frame contains the image AND its watermark. We size the frame to match
   the natural rendered size of the img using width/height set via JS based
   on the image's intrinsic ratio and the frame's bounding box. CSS-only
   fallback: img uses object-fit + max sizing. */
.image-frame {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  max-width: 100%;
  max-height: 100%;
  line-height: 0;
  /* Set inline by JS — width/height matching the actual displayed image */
}
.page-image img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  object-position: center center;
}
.image-frame .watermark {
  position: absolute;
  right: 8px;
  bottom: 6px;
  font-family: var(--font-serif);
  font-weight: 700;
  color: var(--wm-color, rgba(255, 200, 100, 0.95));
  text-shadow: 0 1px 3px rgba(0,0,0,.35);
  letter-spacing: -0.01em;
  line-height: 1;
  pointer-events: none;
}
.image-frame .watermark.is-logo {
  text-shadow: none;
  filter: drop-shadow(0 1px 2px rgba(0,0,0,.35));
}
.image-frame .watermark img {
  display: block;
  max-height: 5%;
  height: auto;
  width: auto;
}
.page.vertical .image-frame .watermark { font-size: 12px; }
.page.horizontal .image-frame .watermark { font-size: 13px; }
.page.vertical .image-frame .watermark img { max-height: 24px; }
.page.horizontal .image-frame .watermark img { max-height: 26px; }

.page-image .empty-img {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: rgba(0,0,0,.25);
  background: repeating-linear-gradient(
    45deg,
    rgba(0,0,0,.02),
    rgba(0,0,0,.02) 10px,
    rgba(0,0,0,.04) 10px,
    rgba(0,0,0,.04) 20px
  );
  font-size: 12px;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.page-text {
  display: grid;
  align-content: start;
  gap: 4px;
  min-height: 0;
  overflow: hidden;
  /* Reserve a piece at the bottom of this 30% block for the process line */
  grid-template-rows: auto auto auto auto 1fr auto;
}

.tpl-title {
  font-family: var(--font-sans);
  color: var(--tpl-title);
  font-size: 14px;
  font-weight: 600;
  letter-spacing: -0.005em;
  margin: 0 0 2px;
  outline: none;
  border-radius: 2px;
  padding: 1px 2px;
  margin-left: -2px;
  transition: background .15s ease;
}
.tpl-title:focus,
[contenteditable]:focus {
  background: rgba(143,92,245,0.08);
}
.tpl-meta {
  font-size: 9.5px;
  color: var(--tpl-text);
  opacity: 0.8;
  letter-spacing: 0.005em;
  outline: none;
  border-radius: 2px;
  padding: 1px 2px;
  margin-left: -2px;
}
.tpl-desc {
  font-size: 9.5px;
  line-height: 1.45;
  color: var(--tpl-text);
  margin-top: 6px;
  outline: none;
  border-radius: 2px;
  padding: 2px;
  margin-left: -2px;
  white-space: pre-wrap;
}
.tpl-process {
  font-size: 8px;
  line-height: 1.4;
  color: var(--tpl-text);
  opacity: 0.45;
  grid-row: 6;
  margin: 0;
  padding-top: 4%;
  outline: none;
  border-radius: 2px;
  white-space: pre-wrap;
}

[contenteditable][data-empty="true"]::before {
  content: attr(data-placeholder);
  color: currentColor;
  opacity: 0.4;
  pointer-events: none;
}

/* Horizontal: image on top, text below — SAME structure as vertical.
   On landscape, give the image more height (75fr) and text 25fr so the text
   stays compact and the image keeps its prominence. */
.page.horizontal { grid-template-rows: 75fr 25fr; }
.page.horizontal .tpl-title { font-size: 15px; }
.page.horizontal .tpl-meta { font-size: 10px; }
.page.horizontal .tpl-desc { font-size: 10px; }
.page.horizontal .tpl-process { font-size: 8.5px; }

/* ===== FULLSCREEN MODE ===== */
:fullscreen .stage,
:fullscreen body {
  background: #1a1a1d;
}
:fullscreen .topbar,
:fullscreen .sidebar,
:fullscreen .footer,
:fullscreen .stage-toolbar {
  display: none !important;
}
:fullscreen .layout {
  display: block;
}
:fullscreen .canvas {
  height: 100vh;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
:fullscreen .stage {
  width: 100%;
  max-width: none;
  height: 100vh;
  justify-content: center;
}
:fullscreen .page-wrap {
  padding: 0;
  height: 100%;
  align-items: center;
}
:fullscreen .page.vertical {
  width: auto;
  height: 92vh;
  max-height: 92vh;
}
:fullscreen .page.horizontal {
  width: 95vw;
  height: auto;
  max-height: 92vh;
}

/* Fullscreen nav hint */
.fs-nav {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  display: none;
  gap: 8px;
  padding: 8px 12px;
  background: rgba(20,20,22,0.85);
  color: #f0f0f0;
  border-radius: 999px;
  backdrop-filter: blur(10px);
  font-size: 12px;
  z-index: 9999;
}
:fullscreen .fs-nav { display: flex; align-items: center; }
.fs-nav .kbd {
  background: rgba(255,255,255,0.12);
  border-color: rgba(255,255,255,0.2);
  color: #f0f0f0;
}

/* ===== FOOTER ===== */
.footer {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  height: var(--footer-h);
  padding: 0 24px;
  border-top: 1px solid var(--border);
  background: var(--bg);
  font-size: 12px;
  color: var(--text-muted);
  flex-shrink: 0;
}
.foot-left { text-align: left; }
.foot-center { text-align: center; }
.foot-right { text-align: right; color: var(--text-subtle); }

.footer a {
  color: var(--text-muted);
  text-decoration: underline;
  text-decoration-color: var(--border-strong);
  text-underline-offset: 2px;
}
.footer a:hover { color: var(--accent); }

/* ===== RESPONSIVE ===== */
@media (max-width: 900px) {
  .layout {
    grid-template-columns: 1fr;
  }
  .sidebar {
    border-right: none;
    border-bottom: 1px solid var(--border);
    max-height: 50vh;
  }
  .footer {
    grid-template-columns: 1fr;
    height: auto;
    padding: 12px 16px;
    text-align: center;
    gap: 4px;
  }
  .foot-left, .foot-center, .foot-right { text-align: center; }
}

/* Scrollbars */
.sidebar::-webkit-scrollbar,
.canvas::-webkit-scrollbar { width: 8px; }
.sidebar::-webkit-scrollbar-thumb,
.canvas::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 4px;
}
.sidebar::-webkit-scrollbar-thumb:hover,
.canvas::-webkit-scrollbar-thumb:hover {
  background: var(--border-strong);
}
