/* ==========================================================================
   Coloring Book Studio — design tokens
   ========================================================================== */
:root{
  --paper: #FAF8F3;
  --paper-panel: #FFFFFF;
  --ink: #211F1B;
  --ink-soft: #6B6558;
  --line: #E6E0D2;
  --line-strong: #D8D0BC;
  --accent: #2F5FE0;          /* crayon blue — primary action */
  --accent-ink: #1E3FA0;
  --accent-soft: #E8EDFC;
  --marigold: #FFB100;        /* preset / highlight accent */
  --marigold-ink: #8A5A00;
  --mint: #3FA980;            /* success / status */
  --mint-soft: #E4F5EE;
  --danger: #D6483A;

  --font-display: 'Fredoka', ui-rounded, 'Segoe UI', sans-serif;
  --font-body: 'Plus Jakarta Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;

  --radius-sm: 8px;
  --radius-md: 14px;
  --radius-lg: 22px;

  --shadow-sm: 0 1px 2px rgba(33,31,27,0.06);
  --shadow-md: 0 6px 20px rgba(33,31,27,0.09);
  --shadow-lg: 0 18px 44px rgba(33,31,27,0.14);

  --header-h: 68px;
}

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

/* The `hidden` attribute must always win over any .display: flex class etc.
   (Otherwise .empty-state { display:flex } etc. would keep hidden elements on
   screen, since author styles beat the UA default for [hidden].) */
[hidden]{ display: none !important; }

html, body{ height: 100%; }

body{
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

h1,h2,h3{ font-family: var(--font-display); font-weight: 600; margin: 0; }

button, select, input{ font-family: inherit; color: inherit; }

.visually-hidden{
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0,0,0,0); white-space: nowrap; border: 0; padding: 0; margin: -1px;
}

.skip-link{
  position: absolute; left: 12px; top: -50px; z-index: 100;
  background: var(--ink); color: #fff; padding: 10px 16px; border-radius: var(--radius-sm);
  transition: top .15s ease;
}
.skip-link:focus{ top: 12px; }

:focus-visible{
  outline: 2.5px solid var(--accent);
  outline-offset: 2px;
  border-radius: 6px;
}

/* ==========================================================================
   Header
   ========================================================================== */
.app-header{
  height: var(--header-h);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 22px;
  background: var(--paper-panel);
  border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 20;
}
.brand{ display: flex; align-items: center; gap: 12px; }
.brand-mark{ flex-shrink: 0; }
.brand-text h1{ font-size: 19px; letter-spacing: 0.1px; }
.brand-text .tagline{ margin: 0; font-size: 12.5px; color: var(--ink-soft); }
.header-actions{ display: flex; align-items: center; gap: 8px; }

/* ==========================================================================
   Buttons & form basics
   ========================================================================== */
.btn{
  display: inline-flex; align-items: center; gap: 7px;
  font-family: var(--font-body); font-weight: 600; font-size: 13.5px;
  padding: 9px 16px; border-radius: 999px; border: 1px solid transparent;
  cursor: pointer; transition: transform .08s ease, background .15s ease, border-color .15s ease;
  white-space: nowrap;
}
.btn:active{ transform: translateY(1px); }
.btn:disabled{ opacity: .45; cursor: not-allowed; }

.btn-primary{ background: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled){ background: var(--accent-ink); }

.btn-secondary{ background: var(--paper-panel); border-color: var(--line-strong); color: var(--ink); }
.btn-secondary:hover:not(:disabled){ border-color: var(--accent); color: var(--accent-ink); }

.btn-ghost{ background: transparent; color: var(--ink-soft); border-color: transparent; }
.btn-ghost:hover:not(:disabled){ background: var(--accent-soft); color: var(--accent-ink); }

.btn-small{ padding: 7px 12px; font-size: 12.5px; }

.icon-btn{
  width: 30px; height: 30px; border-radius: 50%;
  border: 1px solid var(--line-strong); background: var(--paper-panel);
  font-size: 16px; line-height: 1; cursor: pointer; color: var(--ink);
}
.icon-btn:hover{ border-color: var(--accent); color: var(--accent-ink); }

select{
  appearance: none;
  background: var(--paper-panel) url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="10" height="6"><path d="M0 0l5 6 5-6z" fill="%236B6558"/></svg>') no-repeat right 12px center;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  padding: 8px 30px 8px 12px;
  font-size: 13.5px; font-weight: 500;
  min-width: 140px;
}

/* ==========================================================================
   Layout
   ========================================================================== */
.app-layout{
  display: grid;
  grid-template-columns: 250px minmax(0,1fr) 320px;
  min-height: calc(100vh - var(--header-h));
}

.panel{ min-height: 0; }
.panel-inner{ padding: 18px; display: flex; flex-direction: column; gap: 18px; }

.queue-panel{ border-right: 1px solid var(--line); background: var(--paper-panel); }
.controls-panel{ border-left: 1px solid var(--line); background: var(--paper-panel); overflow-y: auto; }
.preview-panel{ display: flex; flex-direction: column; min-width: 0; }

/* ==========================================================================
   Upload
   ========================================================================== */
.upload-drop{
  width: 100%; display: flex; flex-direction: column; align-items: center; gap: 6px;
  text-align: center; padding: 22px 12px;
  border: 2px dashed var(--line-strong); border-radius: var(--radius-md);
  background: var(--paper);
  cursor: pointer; transition: border-color .15s ease, background .15s ease;
}
.upload-drop span{ font-weight: 600; font-size: 13.5px; }
.upload-drop .upload-hint{ font-weight: 400; color: var(--ink-soft); font-size: 12px; }
.upload-drop:hover, .upload-drop.is-dragover{
  border-color: var(--accent); background: var(--accent-soft);
}

/* ==========================================================================
   Image queue
   ========================================================================== */
.image-queue{ list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }

.queue-item{
  display: flex; align-items: center; gap: 10px;
  padding: 8px; border-radius: var(--radius-md);
  border: 1.5px solid transparent; cursor: pointer; position: relative;
  background: var(--paper);
}
.queue-item:hover{ border-color: var(--line-strong); }
.queue-item.is-active{ border-color: var(--accent); background: var(--accent-soft); }

.queue-thumb{
  width: 46px; height: 46px; border-radius: 8px; object-fit: cover;
  background: var(--line); flex-shrink: 0; display: block;
}
.queue-info{ min-width: 0; flex: 1; }
.queue-name{
  font-size: 12.5px; font-weight: 600; white-space: nowrap; overflow: hidden;
  text-overflow: ellipsis; display: block;
}
.queue-status{ font-size: 11px; color: var(--ink-soft); }
.queue-status.is-ready{ color: var(--mint); }

.queue-remove{
  width: 22px; height: 22px; border-radius: 50%; border: none; background: transparent;
  color: var(--ink-soft); font-size: 15px; line-height: 1; cursor: pointer; flex-shrink: 0;
}
.queue-remove:hover{ background: #FBE7E4; color: var(--danger); }

.queue-drag-handle{ cursor: grab; color: var(--ink-soft); font-size: 12px; flex-shrink: 0; }

.batch-actions{ display: flex; flex-direction: column; gap: 8px; padding-top: 4px; border-top: 1px solid var(--line); }

/* ==========================================================================
   Preview
   ========================================================================== */
.preview-toolbar{
  display: flex; align-items: center; justify-content: space-between; gap: 14px;
  padding: 14px 22px; border-bottom: 1px solid var(--line); flex-wrap: wrap;
}

.compare-toggle{ display: flex; background: var(--paper); border: 1px solid var(--line-strong); border-radius: 999px; padding: 3px; }
.toggle-seg{
  border: none; background: transparent; padding: 6px 14px; border-radius: 999px;
  font-size: 12.5px; font-weight: 600; color: var(--ink-soft); cursor: pointer;
}
.toggle-seg.is-active{ background: var(--ink); color: #fff; }

.zoom-controls{ display: flex; align-items: center; gap: 8px; }
.zoom-label{ font-size: 12.5px; font-weight: 600; color: var(--ink-soft); min-width: 34px; text-align: center; }

.preview-stage{
  flex: 1; min-height: 0; position: relative;
  display: flex; align-items: center; justify-content: center;
  overflow: auto;
  background:
    linear-gradient(135deg, #EFEAE0 25%, transparent 25%) -10px 0/20px 20px,
    linear-gradient(225deg, #EFEAE0 25%, transparent 25%) -10px 0/20px 20px,
    linear-gradient(45deg, #EFEAE0 25%, transparent 25%) 0 0/20px 20px,
    linear-gradient(315deg, #EFEAE0 25%, #E4DECF 25%) 0 0/20px 20px;
  padding: 36px;
}

.empty-state{
  display: flex; flex-direction: column; align-items: center; gap: 12px;
  color: var(--ink-soft); font-size: 14px; text-align: center;
  background: var(--paper-panel); padding: 44px 56px; border-radius: var(--radius-lg);
  border: 1px solid var(--line); box-shadow: var(--shadow-sm);
}

/* the "clipboard" holding the page */
.page-mat{
  position: relative;
  padding: 26px 22px 34px;
  background: linear-gradient(180deg, #C99C63, #B98650);
  border-radius: 14px;
  box-shadow: var(--shadow-lg);
}
.page-mat .clip{
  position: absolute; top: -14px; left: 50%; transform: translateX(-50%);
  width: 64px; height: 26px; border-radius: 8px;
  background: linear-gradient(180deg, #DADFE6, #ACB4BE);
  box-shadow: 0 3px 6px rgba(0,0,0,0.25), inset 0 1px 0 rgba(255,255,255,0.5);
}
.page-mat .clip::after{
  content: ""; position: absolute; left: 50%; top: 50%; width: 10px; height: 10px;
  background: #7B828C; border-radius: 50%; transform: translate(-50%,-50%);
}

.page-boundary{
  position: relative;
  background: #fff;
  box-shadow: 0 2px 10px rgba(0,0,0,0.18);
  line-height: 0;
  overflow: hidden;
}

#resultCanvas, #originalCanvas{
  display: block;
  max-width: 78vw;
  max-height: 62vh;
  width: auto; height: auto;
}
#originalCanvas{
  position: absolute; top: 0; left: 0;
  clip-path: inset(0 50% 0 0);
}

.compare-handle{
  position: absolute; inset: 0; pointer-events: none;
}
.compare-handle input[type="range"]{
  position: absolute; inset: 0; width: 100%; height: 100%;
  margin: 0; opacity: 0; cursor: ew-resize; pointer-events: auto;
  -webkit-appearance: none; appearance: none;
}
.compare-line{
  position: absolute; top: 0; bottom: 0; left: 50%;
  width: 2px; background: var(--accent);
  transform: translateX(-1px);
  pointer-events: none;
}
.compare-grip{
  position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%);
  width: 34px; height: 34px; border-radius: 50%; background: var(--accent);
  box-shadow: 0 3px 8px rgba(0,0,0,0.3);
  display: flex; align-items: center; justify-content: center;
}
.compare-grip::before{
  content: "◂ ▸"; color: #fff; font-size: 11px; letter-spacing: 2px;
}

.status-message{
  margin: 0; padding: 8px 22px; font-size: 12.5px; color: var(--ink-soft);
  min-height: 34px; display: flex; align-items: center;
  border-top: 1px solid var(--line);
}

/* ==========================================================================
   Controls
   ========================================================================== */
.control-section{
  padding-bottom: 16px; border-bottom: 1px solid var(--line);
  display: flex; flex-direction: column; gap: 12px;
}
.control-section:last-of-type{ border-bottom: none; }
.control-section h3{
  font-size: 12.5px; text-transform: uppercase; letter-spacing: 0.06em;
  color: var(--ink-soft); font-family: var(--font-body); font-weight: 700;
}

details.control-section > summary{
  font-size: 12.5px; text-transform: uppercase; letter-spacing: 0.06em;
  color: var(--ink-soft); font-weight: 700; cursor: pointer; list-style: none;
  display: flex; align-items: center; gap: 6px; margin-bottom: 2px;
}
details.control-section > summary::-webkit-details-marker{ display: none; }
details.control-section > summary::before{
  content: "▸"; font-size: 10px; transition: transform .15s ease;
}
details.control-section[open] > summary::before{ transform: rotate(90deg); }
details.control-section[open] > summary{ margin-bottom: 10px; }
details.control-section > *:not(summary){ margin-top: 12px; }
details.control-section > *:not(summary):first-of-type{ margin-top: 0; }

.preset-row{ display: flex; flex-wrap: wrap; gap: 8px; }
.preset-chip{
  border: 1.5px solid var(--line-strong); background: var(--paper);
  padding: 8px 13px; border-radius: 999px; font-size: 12.5px; font-weight: 600;
  cursor: pointer; color: var(--ink);
}
.preset-chip:hover{ border-color: var(--accent); }
.preset-chip.is-active{ background: var(--marigold); border-color: var(--marigold); color: #4A3000; }

.slider-field{ display: flex; flex-direction: column; gap: 6px; }
.slider-field label{
  display: flex; justify-content: space-between; font-size: 13px; font-weight: 600;
}
.slider-field .slider-value{
  font-variant-numeric: tabular-nums; color: var(--accent-ink); font-weight: 700;
  background: var(--accent-soft); border-radius: 6px; padding: 1px 7px; font-size: 12px;
}

input[type="range"]{
  -webkit-appearance: none; appearance: none;
  width: 100%; height: 6px; border-radius: 999px;
  background: var(--line-strong); outline-offset: 4px;
}
input[type="range"]::-webkit-slider-thumb{
  -webkit-appearance: none; width: 18px; height: 18px; border-radius: 50%;
  background: var(--accent); border: 3px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,0.3);
  cursor: pointer; margin-top: -6px;
}
input[type="range"]::-moz-range-thumb{
  width: 18px; height: 18px; border-radius: 50%;
  background: var(--accent); border: 3px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,0.3);
  cursor: pointer;
}
input[type="range"]::-moz-range-track{ height: 6px; border-radius: 999px; background: var(--line-strong); }

.field-row{ display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.field-row label{ font-size: 13px; font-weight: 600; }

input[type="checkbox"]{ width: 18px; height: 18px; accent-color: var(--accent); }

.export-section{ padding-bottom: 4px; }
.export-buttons{ display: flex; flex-direction: column; gap: 9px; }
.export-buttons .btn{ justify-content: center; width: 100%; }

/* ==========================================================================
   Toast
   ========================================================================== */
.toast{
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%);
  background: var(--ink); color: #fff; padding: 11px 20px; border-radius: 999px;
  font-size: 13px; font-weight: 600; box-shadow: var(--shadow-md); z-index: 90;
}
.toast.is-error{ background: var(--danger); }
.toast.is-success{ background: var(--mint); }

/* ==========================================================================
   Mobile app tab bar (base shell; shown only on small screens)
   ========================================================================== */
.mobile-nav{
  display: none;
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 50;
  background: var(--paper-panel);
  border-top: 1px solid var(--line);
  box-shadow: 0 -6px 18px rgba(33,31,27,0.08);
  padding-bottom: 0px;
}
.mobile-tab{
  flex: 1; min-height: 46px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  border: none; background: transparent; cursor: pointer;
  font-family: var(--font-body); font-size: 10.5px; font-weight: 600; color: var(--ink-soft);
  -webkit-tap-highlight-color: transparent;
}
.mobile-tab.is-active{ color: var(--accent); }
.mobile-tab svg{ flex-shrink: 0; }

.adjust-peek{ display: none; }

/* ==========================================================================
   Responsive
   ========================================================================== */
@media (max-width: 1100px){
  .app-layout{ grid-template-columns: 220px minmax(0,1fr) 280px; }
}

@media (min-width: 861px) and (max-width: 1100px){
  .app-layout{ grid-template-columns: 230px minmax(0,1fr) 300px; }
}

/* ── Mobile: app-style, one screen at a time with a bottom tab bar ─────── */
@media (max-width: 860px){
  html, body{ height: auto; min-height: 100vh; }

  /* Fixed bottom tab bar, safe-area aware for notched phones */
  .mobile-nav{
    display: flex;
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }
  .mobile-nav .mobile-tab{ font-size: 10.5px; }

  /* Compact header — keep the primary actions large for touch */
  .app-header{
    height: 58px; padding: 0 12px;
    padding-top: env(safe-area-inset-top, 0px);
  }
  .brand-text .tagline{ display: none; }
  .brand-text h1{ font-size: 16px; }
  .header-actions{ gap: 4px; }
  .header-actions .btn{
    min-width: 44px; min-height: 44px; padding: 0 11px; justify-content: center;
  }

  /* One screen at a time, filling the space above the tab bar */
  .app-layout{
    display: flex; flex-direction: column;
    min-height: calc(100vh - 58px - 46px);
    min-height: calc(100svh - 58px - env(safe-area-inset-bottom, 0px) - 46px);
  }
  .panel{ display: none; min-height: 0; border: none; }
  .preview-panel[data-active]{ display: flex; flex-direction: column; flex: 1 1 auto; }
  .queue-panel[data-active], .controls-panel[data-active]{
    display: block; flex: 1 1 auto;
    overflow-y: auto; -webkit-overflow-scrolling: touch;
  }

  /* ---- Photos screen ---- */
  .queue-panel .panel-inner{ padding: 14px 12px; gap: 14px; }
  .upload-drop{ padding: 18px 12px; }
  .image-queue{ flex-direction: row; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .queue-item{ min-width: 170px; flex: 0 0 auto; }
  .queue-drag-handle{ display: none; }

  /* ---- Editor screen ---- */
  .preview-toolbar{ padding: 12px 14px; gap: 10px; }
  .preview-toolbar .zoom-controls{ gap: 6px; }
  .preview-stage{ padding: 18px 14px 28px; }
  #resultCanvas, #originalCanvas{ max-width: 90vw; max-height: 46vh; }

  /* ---- Adjust screen ---- */
  .adjust-peek{ display: flex; margin-bottom: 4px; border-bottom: 1px solid var(--line); padding-bottom: 10px; }
  .controls-panel .panel-inner{ padding: 16px 14px; }
  .controls-panel{ overflow-y: auto; -webkit-overflow-scrolling: touch; }
  select, input:not([type="range"]){ font-size: 16px; }   /* avoid iOS auto-zoom */
  input[type="range"]{ height: 8px; }
  input[type="range"]::-webkit-slider-thumb{ width: 28px; height: 28px; margin-top: -11px; }
  input[type="range"]::-moz-range-thumb{ width: 28px; height: 28px; }
  .field-row{ gap: 8px; }
  .export-buttons{ gap: 12px; }
  .export-buttons .btn{ padding: 13px 16px; }

  /* ---- Touch polish across the app ---- */
  *{ -webkit-tap-highlight-color: transparent; }
  input[type="range"], select, textarea, button{ touch-action: manipulation; }
}

@media (prefers-reduced-motion: reduce){
  *{ transition: none !important; animation: none !important; }
}

@media print{
  body *{ visibility: hidden; }
  .page-boundary, .page-boundary *{ visibility: visible; }
  .page-boundary{ position: fixed; inset: 0; box-shadow: none; }
}
