:root {
  --paper: #f4e4c3;
  --paper-2: #ead6a8;
  --desk: #3d2e22;
  --ink: #2a1f18;
  --muted: #6b5344;
  --crayon-red: #e23d28;
  --crayon-blue: #2f6fed;
  --crayon-yellow: #f0c418;
  --crayon-green: #3aa35a;
  --tape: rgba(255, 236, 170, 0.72);
  --shadow: 3px 4px 0 rgba(42, 31, 24, 0.18);
  /* Recessed tray — top/bottom only (side inset was clipping brush sprites) */
  --well-inset: inset 0 5px 12px rgba(42, 31, 24, 0.22), inset 0 -2px 6px rgba(42, 31, 24, 0.1), inset 0 -1px 0 rgba(255, 248, 231, 0.28);
  --well-inset-soft: inset 0 2px 6px rgba(42, 31, 24, 0.14), inset 0 -1px 2px rgba(255, 248, 231, 0.2);
  --font-display: "Fredoka", system-ui, sans-serif;
  --font-hand: "Gaegu", "Comic Sans MS", cursive;

  /* Distinct panel tones (less flat) */
  --panel-top: #f3e2b8;
  --panel-tools: #f0dcad;
  --panel-colors: #e5cfa0;
  --panel-paper: #f8e9cb;
  --panel-size: #ebd5a4;
  --tools-bg: var(--panel-tools);

  /* ── Brush rack ──
     Idle: solo se ve ~mitad superior (base cortada por el marco).
     Activo: sube con --brush-lift.
     --brush-sink = cuánto se hunden (más alto = más abajo; evita corte al seleccionar).
     --brush-slot-w = ancho del “hueco” (juntar/separar). El PNG puede ser más ancho. */
  --brush-w: 100px;
  --brush-h: 180px;
  --brush-lift: 22%;
  --brush-sink: 0.65; /* ← bajar idle aquí (0.52 → más arriba, 0.65 → más abajo) */
  --brush-slot-w: 44px; /* ← juntar aquí (más chico = más juntas) */
  --brush-gap: 8px;
  --brush-rack-h: calc(var(--brush-h) * 0.48);
  /* Modest gutter — must leave room for 4 centered slots in the tools column */
  --brush-side-pad: 14px;
  /* Free sprites look a bit chunkier in-art than Pro — dial Free down to match */
  --brush-free-scale: 0.88;

  /* Folder tabs 1 / 2 */
  --folder-tab-h: 16px;
  --folder-tab-h-active: 17px;
  --folder-tab-w: 28px;
  /* Move ONLY the 1/2 labels up (negative = higher). Does not move the tools panel. */
  --folder-tab-lift: -2px;

  /* Desk chrome (room around the app) — themed via body[data-desk] */
  --desk-glow: rgba(240, 196, 24, 0.12);
  --desk-stripe-a: #3a2b20;
  --desk-stripe-b: #422f23;
  --desk-base: #3d2e22;
}

* { box-sizing: border-box; }

html {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  overflow-x: hidden;
  overscroll-behavior: none;
  max-width: 100%;
}

html, body {
  color: var(--ink);
  font-family: var(--font-hand);
  background: var(--desk);
  touch-action: manipulation;
}

body {
  margin: 0;
  width: 100%;
  max-width: 100%;
  height: 100%;
  min-height: 100%;
  min-height: 100dvh;
  overflow: hidden;
  overflow-x: hidden;
  position: relative;
  overscroll-behavior: none;
  background:
    radial-gradient(ellipse at 20% 10%, var(--desk-glow), transparent 40%),
    repeating-linear-gradient(90deg, var(--desk-stripe-a) 0px, var(--desk-stripe-a) 2px, var(--desk-stripe-b) 2px, var(--desk-stripe-b) 28px),
    var(--desk-base);
}

/* Desk room tints — only chrome, not the drawing ink */
body[data-desk="gray"] {
  --desk: #2e2e2e;
  --desk-glow: rgba(200, 200, 200, 0.1);
  --desk-stripe-a: #3a3a3a;
  --desk-stripe-b: #454545;
  --desk-base: #2e2e2e;
  --paper: #e8e6e2;
  --paper-2: #dcd9d4;
  --panel-top: #e4e1db;
  --panel-tools: #dedad4;
  --panel-colors: #d5d1cb;
  --panel-paper: #ebe8e3;
  --panel-size: #ddd9d3;
  --tools-bg: var(--panel-tools);
  --crayon-yellow: #c8c4b8;
  --tape: rgba(210, 210, 205, 0.75);
}

body[data-desk="slate"] {
  --desk: #1e252e;
  --desk-glow: rgba(140, 160, 180, 0.12);
  --desk-stripe-a: #2a323c;
  --desk-stripe-b: #343e4a;
  --desk-base: #1e252e;
  --paper: #d8dee6;
  --paper-2: #c9d1db;
  --panel-top: #d0d7e0;
  --panel-tools: #c5ced8;
  --panel-colors: #bac4d0;
  --panel-paper: #dfe5ec;
  --panel-size: #c8d0da;
  --tools-bg: var(--panel-tools);
  --crayon-yellow: #a8b4c4;
  --tape: rgba(180, 195, 210, 0.7);
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
  border: none;
  background: none;
}

.app {
  position: relative;
  height: 100%;
  height: var(--vvh, 100%);
  width: 100%;
  max-width: 100%;
  margin: 0;
  padding: 6px 8px 8px;
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 6px;
  overscroll-behavior: none;
  touch-action: manipulation;
  isolation: isolate;
  overflow-x: hidden;
  box-sizing: border-box;
}

/* Stickers in the empty gutter around the paper — never on the drawing surface */
.canvas-decor {
  position: absolute;
  inset: 0;
  z-index: 2;
  pointer-events: none;
  overflow: hidden;
}

.decor {
  position: absolute;
  display: block;
  height: auto;
  width: 56px;
  max-width: none;
  user-select: none;
  -webkit-user-drag: none;
  touch-action: none;
  pointer-events: auto;
  cursor: grab;
  filter: drop-shadow(2px 3px 0 rgba(42, 31, 24, 0.2));
  opacity: 0.95;
  transform-origin: center center;
  will-change: transform, left, top, width;
}

.decor:active,
.decor.is-dragging {
  cursor: grabbing;
  z-index: 5;
  opacity: 1;
}

/* Default placements (overridden by JS + localStorage) */
.d-tape-tl { top: 2%; left: 6%; width: 72px; }
.d-star { top: 34%; left: 1%; width: 44px; }
.d-smudge { top: 56%; left: 0%; width: 50px; }
.d-note { bottom: 5%; left: 2%; top: auto; width: 54px; }
.d-tape-tr { top: 1%; right: 4%; left: auto; width: 76px; }
.d-deer { top: 24%; right: 1%; left: auto; width: 46px; }
.d-heart { top: 46%; right: 1%; left: auto; width: 42px; }
.d-eye { top: 68%; right: 0%; left: auto; width: 44px; }
.d-tape-br { bottom: 2%; right: 12%; left: auto; top: auto; width: 68px; }

.top {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  background: var(--paper);
  border: 3px solid var(--ink);
  border-radius: 6px 16px 8px 12px / 12px 8px 14px 6px;
  padding: 4px 8px;
  box-shadow: var(--shadow);
  min-height: 0;
  background: var(--panel-top);
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.logo {
  width: 34px;
  height: 34px;
  flex-shrink: 0;
  border: 2.5px solid var(--ink);
  border-radius: 40% 55% 45% 50% / 50% 40% 55% 45%;
  background: var(--crayon-yellow);
  display: grid;
  place-items: center;
  box-shadow: 2px 2px 0 var(--ink);
  overflow: hidden;
}

.logo canvas {
  width: 100%;
  height: 100%;
  display: block;
}

.brand h1 {
  margin: 0;
  font-family: var(--font-display);
  font-size: 1.45rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  line-height: 1;
  transform: rotate(-1deg);
}

.tag {
  margin: 0;
  font-size: 0.95rem;
  color: var(--muted);
  line-height: 1;
}

.top-hints {
  margin: 0;
  justify-self: center;
  text-align: center;
  font-family: var(--font-hand);
  font-size: 0.75rem;
  color: var(--muted);
  line-height: 1.35;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
  padding: 2px 4px 0;
}

.top-hints kbd {
  display: inline-block;
  font-family: var(--font-display);
  font-size: 0.68rem;
  font-weight: 700;
  color: var(--ink);
  background: #fff8e7;
  border: 1.5px solid var(--ink);
  border-radius: 4px;
  padding: 1px 4px 0;
  line-height: 1.2;
  box-shadow: 1px 1px 0 var(--ink);
  vertical-align: baseline;
  position: relative;
  top: 0;
}

.hint-sep {
  opacity: 0.45;
  margin: 0 2px;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.gif-size {
  display: flex;
  align-items: center;
  gap: 4px;
  font-family: var(--font-display);
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--ink);
}

.gif-size select {
  font-family: var(--font-display);
  font-size: 0.72rem;
  font-weight: 700;
  border: 2px solid var(--ink);
  border-radius: 8px;
  background: #fff8e7;
  padding: 3px 4px;
  box-shadow: 1px 1px 0 var(--ink);
  color: var(--ink);
  cursor: pointer;
}

.btn.crayon {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 0.85rem;
  padding: 6px 12px;
  border: 2.5px solid var(--ink);
  border-radius: 40% 55% 48% 52% / 52% 45% 55% 48%;
  box-shadow: 2px 2px 0 var(--ink);
}

.btn.crayon:active {
  transform: translate(1px, 1px);
  box-shadow: 1px 1px 0 var(--ink);
}

.crayon-red { background: var(--crayon-red); color: #fff8e8; }
.crayon-blue { background: var(--crayon-blue); color: #fff8e8; }
.crayon-gray { background: #d9cbb0; color: var(--ink); }

.btn:disabled { opacity: 0.5; cursor: wait; }

.stage {
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 248px;
  gap: 6px;
  align-items: stretch;
}

.canvas-column {
  min-height: 0;
  min-width: 0;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  gap: 4px;
  overflow: visible;
}

.canvas-wrap {
  position: relative;
  min-height: 0;
  background: var(--paper);
  border: 3px solid var(--ink);
  border-radius: 8px 16px 8px 14px / 14px 8px 16px 8px;
  /* Gutter for stickers outside the 4:3 paper */
  padding: 40px 48px;
  box-shadow: var(--shadow), var(--well-inset-soft);
  display: grid;
  place-items: center;
  overflow: hidden;
  container-type: size;
}

.canvas-stage {
  position: relative;
  z-index: 1;
  display: block;
  line-height: 0;
  aspect-ratio: 4 / 3;
  /* Largest 4:3 that fits the wrap content box */
  width: min(100cqw, calc(100cqh * 4 / 3));
  height: min(100cqh, calc(100cqw * 3 / 4));
  max-width: 100%;
  max-height: 100%;
  overflow: hidden;
}

.tape {
  position: absolute;
  top: -8px;
  width: 56px;
  height: 16px;
  background: var(--tape);
  border: 2px solid rgba(42, 31, 24, 0.2);
  z-index: 2;
}

.tape-l { left: 20px; transform: rotate(-8deg); }
.tape-r { right: 28px; transform: rotate(6deg); }

/* legacy CSS tapes removed — using assets/decor PNGs */

#view {
  display: block;
  width: 100%;
  height: 100%;
  max-width: 100%;
  max-height: 100%;
  border: 2.5px solid var(--ink);
  border-radius: 4px;
  touch-action: none;
  background: #fffaf0;
  outline: none;
  /* fallback until JS sets adaptive cursor */
  cursor: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24'%3E%3Ccircle cx='12' cy='12' r='4.5' fill='none' stroke='%23fff' stroke-width='3'/%3E%3Ccircle cx='12' cy='12' r='4.5' fill='none' stroke='%231a1a1a' stroke-width='1.5'/%3E%3Cline x1='12' y1='1' x2='12' y2='7' stroke='%23fff' stroke-width='3'/%3E%3Cline x1='12' y1='17' x2='12' y2='23' stroke='%23fff' stroke-width='3'/%3E%3Cline x1='1' y1='12' x2='7' y2='12' stroke='%23fff' stroke-width='3'/%3E%3Cline x1='17' y1='12' x2='23' y2='12' stroke='%23fff' stroke-width='3'/%3E%3Cline x1='12' y1='1' x2='12' y2='7' stroke='%231a1a1a' stroke-width='1.5'/%3E%3Cline x1='12' y1='17' x2='12' y2='23' stroke='%231a1a1a' stroke-width='1.5'/%3E%3Cline x1='1' y1='12' x2='7' y2='12' stroke='%231a1a1a' stroke-width='1.5'/%3E%3Cline x1='17' y1='12' x2='23' y2='12' stroke='%231a1a1a' stroke-width='1.5'/%3E%3C/svg%3E") 12 12, crosshair;
}

.rail {
  min-height: 0;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  gap: 6px;
  align-content: start;
  overflow: visible;
}

.panel.sticker {
  background: var(--paper-2);
  border: 2.5px solid var(--ink);
  border-radius: 10px 14px 8px 12px / 12px 8px 14px 10px;
  padding: 8px;
  box-shadow: var(--shadow), var(--well-inset-soft);
  min-height: 0;
}

.tools-block {
  position: relative;
  z-index: 3;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  min-width: 0;
  overflow: visible;
  padding-top: 4px;
}

/* Tabs sit ON the box — only the labels move (--folder-tab-lift), panel stays put */
.folder-tabs {
  display: flex;
  align-items: flex-end;
  gap: 3px;
  padding-left: 8px;
  margin: 0 0 -3px;
  height: var(--folder-tab-h-active);
  transform: translateY(var(--folder-tab-lift));
  z-index: 5;
  pointer-events: auto;
  position: relative;
  filter: drop-shadow(1px 2px 0 rgba(42, 31, 24, 0.18));
}

.folder-tab {
  font-family: var(--font-display);
  font-weight: 800;
  font-size: 0.72rem;
  line-height: 1;
  width: var(--folder-tab-w);
  height: var(--folder-tab-h);
  border: 2.5px solid var(--ink);
  border-bottom: none;
  border-radius: 7px 7px 0 0;
  background: #e8d4a4;
  color: var(--ink);
  cursor: pointer;
  padding: 0;
  box-sizing: border-box;
}

.folder-tab.active {
  background: var(--crayon-yellow);
  height: var(--folder-tab-h-active);
  z-index: 2;
}

.folder-tab:nth-child(2).active {
  background: #7ec8ff;
}

.folder-tab:not(.active) {
  filter: brightness(0.96);
}

.tools-panel {
  background: var(--panel-tools) !important;
  border: 3px solid var(--ink) !important;
  border-radius: 14px !important;
  padding: 0 !important;
  overflow: hidden;
  /* Outer lift + top/bottom well (no side crush on edge brushes) */
  box-shadow:
    3px 4px 0 rgba(42, 31, 24, 0.22),
    var(--well-inset) !important;
  display: flex;
  flex-direction: column;
  gap: 0;
  position: relative;
  min-height: var(--brush-rack-h);
  flex-shrink: 0;
}

.brush-rack-viewport {
  width: 100%;
  height: var(--brush-rack-h);
  min-height: var(--brush-rack-h);
  flex: 0 0 var(--brush-rack-h);
  overflow: hidden;
  position: relative;
  padding: 0;
  box-sizing: border-box;
}

.brush-rack-track {
  display: flex;
  width: 200%;
  height: 100%;
  transition: transform 0.24s cubic-bezier(0.4, 0.1, 0.2, 1);
  will-change: transform;
}

.brush-rack-track[data-folder="0"] { transform: translateX(0); }
.brush-rack-track[data-folder="1"] { transform: translateX(-50%); }

.brush-rack {
  display: flex;
  justify-content: center;
  align-items: flex-end;
  gap: var(--brush-gap);
  height: 100%;
  min-height: var(--brush-rack-h);
  /* Gutter per folder — keeps Free/Pro from bleeding into each other */
  padding: 0 var(--brush-side-pad);
  overflow: hidden;
  flex: 0 0 50%;
  width: 50%;
  box-sizing: border-box;
}

.brush-slot.pro-lock {
  opacity: 0.55;
  filter: grayscale(0.35);
}

.brush-slot .slot-pro {
  position: absolute;
  top: 3px;
  left: 50%;
  right: auto;
  transform: translateX(-50%);
  z-index: 6;
  font-family: var(--font-display);
  font-size: 0.5rem;
  font-weight: 800;
  padding: 1px 4px;
  border: 1.5px solid var(--ink);
  border-radius: 3px;
  background: var(--crayon-yellow);
  line-height: 1;
  pointer-events: none;
  white-space: nowrap;
}

.colors-panel {
  background: var(--panel-colors) !important;
  box-shadow: var(--shadow), var(--well-inset-soft) !important;
}

.paper-panel {
  display: flex;
  flex-direction: column;
  gap: 4px;
  overflow: hidden;
  min-height: 0;
  background: var(--panel-paper) !important;
  box-shadow: var(--shadow), var(--well-inset-soft) !important;
}

.desk-tint {
  display: flex;
  gap: 5px;
  align-items: center;
  padding: 0 1px 2px;
}

.desk-chip {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 2px solid var(--ink);
  padding: 0;
  cursor: pointer;
  box-shadow: 1px 1px 0 var(--ink);
  flex-shrink: 0;
}

.desk-chip[data-desk="warm"] {
  background: #3d2e22;
}
.desk-chip[data-desk="gray"] {
  background: linear-gradient(135deg, #888, #444);
}
.desk-chip[data-desk="slate"] {
  background: linear-gradient(135deg, #6a7a8c, #2a323c);
}

.desk-chip.active {
  outline: 2px solid var(--crayon-blue);
  outline-offset: 1px;
}

.size-bar {
  padding: 3px 8px !important;
  background: var(--panel-size) !important;
  box-shadow: var(--shadow), var(--well-inset-soft) !important;
}

.brush-slot {
  position: relative;
  flex: 0 0 var(--brush-slot-w);
  width: var(--brush-slot-w);
  height: 100%;
  border: none;
  background: transparent;
  padding: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  transition: transform 0.2s cubic-bezier(0.34, 1.45, 0.64, 1);
  overflow: visible;
}

/* Idle ya está “hundido”; al seleccionar sube para mostrar más */
.brush-slot.active {
  transform: translateY(calc(-1 * var(--brush-lift)));
  z-index: 4;
}

.brush-slot.active .brush-sprite,
.brush-slot.active .brush-fallback {
  filter: drop-shadow(2px 4px 2px rgba(42, 31, 24, 0.35));
}

.brush-sprite {
  display: none;
  width: var(--brush-w);
  height: var(--brush-h);
  background: center top / contain no-repeat;
  /* tip arriba visible; la base sobresale abajo y el panel la corta */
  pointer-events: none;
  margin-bottom: calc(var(--brush-h) * -1 * var(--brush-sink));
  filter: drop-shadow(1px 2px 1px rgba(42, 31, 24, 0.18));
  transition: filter 0.2s ease;
}

/* Folder Free (tab 1): slightly smaller so they match Pro art weight */
#tools-folder-0 .brush-sprite {
  width: calc(var(--brush-w) * var(--brush-free-scale));
  height: calc(var(--brush-h) * var(--brush-free-scale));
  margin-bottom: calc(var(--brush-h) * var(--brush-free-scale) * -1 * var(--brush-sink));
}

.brush-slot.has-sprite .brush-sprite { display: block; }
.brush-slot.has-sprite .brush-fallback { display: none; }
.brush-slot.has-sprite .brush-name { display: none; }

.brush-fallback {
  width: 22px;
  height: 48px;
  border: 2px solid var(--ink);
  border-radius: 8px 8px 4px 4px / 10px 10px 4px 4px;
  background: linear-gradient(180deg, #fff8e7 0%, #f0c418 40%, #e23d28 100%);
  box-shadow: 2px 2px 0 var(--ink);
  display: grid;
  place-items: end center;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 0.7rem;
  padding-bottom: 4px;
  color: var(--ink);
}

.brush-slot:nth-child(2) .brush-fallback {
  background: linear-gradient(180deg, #fff8e7 0%, #e85a9b 45%, #2f6fed 100%);
}
.brush-slot:nth-child(3) .brush-fallback {
  background: linear-gradient(180deg, #fff8e7 0%, #3aa35a 100%);
  border-radius: 50% 50% 6px 6px;
}
.brush-slot:nth-child(4) .brush-fallback {
  background: linear-gradient(180deg, #ddd 0%, #888 100%);
  border-radius: 4px;
}

.brush-name {
  display: none; /* captura: solo sprites, sin labels */
}

.spectrum-wrap {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

#sv-canvas,
#hue-canvas {
  width: 100%;
  border: 2.5px solid var(--ink);
  border-radius: 8px 12px 6px 10px / 10px 6px 12px 8px;
  box-shadow: 1.5px 1.5px 0 var(--ink);
  cursor: crosshair;
  touch-action: none;
  display: block;
}

#hue-canvas {
  height: 14px;
  border-radius: 999px;
}

.color-row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 2px;
}

.color-preview {
  width: 28px;
  height: 28px;
  border: 2.5px solid var(--ink);
  border-radius: 40% 55% 45% 50%;
  box-shadow: 1.5px 1.5px 0 var(--ink);
  flex-shrink: 0;
}

.btn-eyedrop {
  width: 28px;
  height: 28px;
  padding: 2px;
  border: 2.5px solid var(--ink);
  border-radius: 40% 55% 45% 50% / 55% 45% 50% 45%;
  background: #fff8e7;
  box-shadow: 1.5px 1.5px 0 var(--ink);
  display: grid;
  place-items: center;
  flex-shrink: 0;
}

.btn-eyedrop img {
  width: 18px;
  height: 22px;
  object-fit: contain;
  object-position: center bottom;
  pointer-events: none;
  filter: drop-shadow(0.5px 0.5px 0 #2a1f18);
}

.btn-eyedrop.active {
  background: var(--crayon-yellow);
  outline: 2px solid var(--crayon-red);
  outline-offset: 1px;
}

.btn-tiny {
  font-size: 0.72rem !important;
  padding: 4px 8px !important;
}

.swatch-row {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 4px;
  margin-top: 6px;
}

.swatch-row.compact {
  grid-template-columns: repeat(8, 1fr);
}

.swatch-chip {
  aspect-ratio: 1;
  width: 100%;
  border: 2px solid var(--ink);
  border-radius: 42% 58% 48% 52% / 55% 42% 58% 45%;
  box-shadow: 1.5px 1.5px 0 var(--ink);
  min-height: 0;
  padding: 0;
}

.swatch-chip.light {
  box-shadow: 1.5px 1.5px 0 var(--ink), inset 0 0 0 1px rgba(42, 31, 24, 0.15);
}

.swatch-chip.slot-active {
  outline: 2px dashed var(--crayon-blue);
  outline-offset: 1px;
}

.swatch-chip.color-active {
  outline: 2.5px solid var(--crayon-red);
  outline-offset: 1px;
  transform: scale(1.08) rotate(-4deg);
}

.swatch-hint {
  margin: 4px 0 0;
  font-size: 0.8rem;
  color: var(--muted);
  line-height: 1.1;
}

/* kill old tool-grid look if leftover */
.tool-grid { display: none; }

.layer-toggle {
  display: flex;
  gap: 4px;
  margin-bottom: 6px;
}

.layer-btn {
  flex: 1;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 0.75rem;
  padding: 4px 6px;
  border: 2px solid var(--ink);
  border-radius: 40% 55% 45% 50% / 50% 42% 55% 48%;
  background: #fff8e7;
  box-shadow: 1.5px 1.5px 0 var(--ink);
}

.layer-btn.active { background: var(--crayon-yellow); }

.crayon-box {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 4px;
}

.crayon-chip {
  aspect-ratio: 1;
  width: 100%;
  border: 2px solid var(--ink);
  border-radius: 42% 58% 48% 52% / 55% 42% 58% 45%;
  box-shadow: 1.5px 1.5px 0 var(--ink);
  min-height: 0;
}

.crayon-chip.light {
  box-shadow: 1.5px 1.5px 0 var(--ink), inset 0 0 0 1px rgba(42, 31, 24, 0.15);
}

.crayon-chip.active {
  outline: 2.5px solid var(--crayon-red);
  outline-offset: 1px;
  transform: scale(1.1) rotate(-5deg);
}

.paper-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
}

.paper-cell {
  aspect-ratio: 1.15;
  border: 2px solid var(--ink);
  border-radius: 8px 12px 6px 10px / 10px 6px 12px 8px;
  background: #fff8e7 center/cover no-repeat;
  box-shadow: 1.5px 1.5px 0 var(--ink);
  position: relative;
  overflow: hidden;
  padding: 0;
}

.paper-cell .paper-label {
  position: absolute;
  left: 1px;
  right: 1px;
  bottom: 1px;
  font-family: var(--font-display);
  font-size: 0.52rem;
  font-weight: 700;
  text-align: center;
  background: rgba(244, 228, 195, 0.92);
  border-radius: 3px;
  line-height: 1.1;
  padding: 1px;
}

.paper-cell.active {
  outline: 2.5px solid var(--crayon-blue);
  outline-offset: 1px;
}

.paper-cell.trace-on {
  outline: 2.5px solid var(--crayon-yellow);
  outline-offset: 1px;
}

.paper-cell.trace-place-active {
  outline: 2.5px solid var(--crayon-red);
  outline-offset: 1px;
}

.paper-clear {
  background:
    linear-gradient(45deg, #ccc 25%, #fff 25%, #fff 50%, #ccc 50%, #ccc 75%, #fff 75%);
  background-size: 10px 10px;
}

.paper-trace {
  background:
    linear-gradient(135deg, rgba(47, 111, 237, 0.2), rgba(242, 223, 176, 0.9)),
    repeating-linear-gradient(-12deg, transparent, transparent 6px, rgba(42, 31, 24, 0.06) 6px, rgba(42, 31, 24, 0.06) 7px);
}

.paper-add {
  position: relative;
  background: #ead6a8;
  display: grid;
  place-items: center;
}

.paper-add .paper-label {
  position: static;
  background: transparent;
  font-size: 0.7rem;
}

/* ── Trace Place overlay ── */
.trace-place {
  position: absolute;
  inset: 0;
  z-index: 5;
  pointer-events: auto;
  touch-action: none;
  overflow: hidden; /* keep huge frames / bar inside the drawing frame */
}

.trace-place::before,
.trace-place::after {
  content: "";
  position: absolute;
  z-index: 4;
  pointer-events: none;
  background: rgba(47, 111, 237, 0.45);
  opacity: 0;
  transition: opacity 0.08s ease;
}

.trace-place.snap-x::before {
  opacity: 1;
  left: 50%;
  top: 0;
  bottom: 0;
  width: 1px;
  transform: translateX(-50%);
}

.trace-place.snap-y::after {
  opacity: 1;
  top: 50%;
  left: 0;
  right: 0;
  height: 1px;
  transform: translateY(-50%);
}

.trace-frame {
  position: absolute;
  box-sizing: border-box;
  border: 2.5px dashed var(--ink);
  border-radius: 4px;
  box-shadow: 0 0 0 9999px rgba(42, 31, 24, 0.18);
  pointer-events: auto;
  cursor: grab;
  touch-action: none;
}

.trace-frame:active { cursor: grabbing; }

.th {
  position: absolute;
  width: 12px;
  height: 12px;
  background: var(--crayon-yellow);
  border: 2px solid var(--ink);
  border-radius: 3px;
  box-shadow: 1px 1px 0 var(--ink);
  z-index: 2;
}

.th-nw { left: -7px; top: -7px; cursor: nwse-resize; }
.th-ne { right: -7px; top: -7px; cursor: nesw-resize; }
.th-sw { left: -7px; bottom: -7px; cursor: nesw-resize; }
.th-se { right: -7px; bottom: -7px; cursor: nwse-resize; }

.trace-place-bar {
  position: absolute;
  left: 50%;
  bottom: 8px;
  transform: translateX(-50%);
  z-index: 6;
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 4px 6px;
  background: var(--paper);
  border: 2.5px solid var(--ink);
  border-radius: 10px 14px 8px 12px / 12px 8px 14px 10px;
  box-shadow: 2px 2px 0 var(--ink);
  white-space: nowrap;
  pointer-events: auto;
}

.place-fade {
  display: flex;
  align-items: center;
  gap: 4px;
  font-family: var(--font-display);
  font-size: 0.7rem;
  font-weight: 600;
}

.place-fade input {
  width: 72px;
  accent-color: var(--crayon-blue);
}

.crayon-yellow { background: var(--crayon-yellow); color: var(--ink); }

.slider-row {
  display: grid;
  grid-template-columns: 40px 1fr 36px auto;
  align-items: center;
  gap: 6px;
  margin: 2px 0;
}

.slider-row #btn-brush-pop {
  padding: 4px 8px;
  font-size: 0.72rem;
  white-space: nowrap;
}

.slider-row label,
.slider-row .val {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 0.78rem;
}

.slider-row .val { text-align: right; cursor: text; }

.val-editable {
  cursor: text;
  outline: none;
  border-radius: 4px;
  padding: 0 2px;
}

.val-editable:focus,
.val-editable[contenteditable="true"] {
  background: #fff8e7;
  box-shadow: inset 0 0 0 1.5px var(--ink);
  color: var(--ink);
}

.slider-row input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  height: 8px;
  border-radius: 5px;
  background: #fff8e7;
  border: 2px solid var(--ink);
}

.slider-row input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 15px;
  height: 15px;
  border-radius: 40% 60% 45% 55%;
  background: var(--crayon-red);
  border: 2px solid var(--ink);
}

.pen-hint {
  margin: 2px 0 0;
  font-size: 0.78rem;
  color: var(--muted);
  line-height: 1.1;
}

.status-line {
  margin: 0;
  font-size: 0.95rem;
  color: var(--crayon-green);
  font-weight: 700;
  line-height: 1.15;
  min-height: 1.2em;
}

.brush-pop {
  position: fixed;
  z-index: 50;
  width: 220px;
  max-width: calc(100vw - 16px);
  padding: 10px 12px;
  background: var(--paper);
  border: 3px solid var(--ink);
  border-radius: 14px 18px 12px 16px / 16px 12px 18px 14px;
  box-shadow: 3px 4px 0 var(--ink);
  display: flex;
  flex-direction: column;
  gap: 8px;
  box-sizing: border-box;
  overflow: hidden;
}

.brush-pop[hidden] { display: none !important; }

.brush-pop .pop-title {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 700;
}

.pop-size-row {
  display: grid;
  grid-template-columns: 44px 1fr;
  gap: 8px;
  align-items: center;
  min-width: 0;
}

.pop-preview {
  width: 44px;
  height: 44px;
  border: 2px solid var(--ink);
  border-radius: 10px;
  background: #fff8e7;
  display: block;
}

.pop-size-sliders {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
}

.pop-field {
  display: grid;
  grid-template-columns: 52px minmax(0, 1fr) 36px;
  align-items: center;
  gap: 6px;
  font-family: var(--font-display);
  font-size: 0.78rem;
  font-weight: 600;
  min-width: 0;
  margin: 0;
}

.pop-val {
  font-variant-numeric: tabular-nums;
  font-size: 0.72rem;
  text-align: right;
  color: var(--muted);
  cursor: text;
  border-radius: 4px;
  padding: 0 2px;
}

.brush-pop input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  min-width: 0;
  max-width: 100%;
  height: 7px;
  border-radius: 4px;
  border: 2px solid var(--ink);
  background: #fff8e7;
  box-sizing: border-box;
}

.brush-pop input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 13px;
  height: 13px;
  border-radius: 50%;
  background: var(--crayon-blue);
  border: 2px solid var(--ink);
  cursor: pointer;
}

.brush-pop input[type="range"]::-moz-range-thumb {
  width: 13px;
  height: 13px;
  border-radius: 50%;
  background: var(--crayon-blue);
  border: 2px solid var(--ink);
  cursor: pointer;
}

.pop-mirror {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.pop-mirror-label {
  font-family: var(--font-display);
  font-size: 0.78rem;
  font-weight: 600;
}

.pop-mirror-btns {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
}

.pop-m-btn {
  font-family: var(--font-display);
  font-size: 0.72rem;
  font-weight: 700;
  padding: 4px 0;
  border: 2px solid var(--ink);
  border-radius: 8px;
  background: #fff8e7;
  color: var(--ink);
  box-shadow: 1px 1px 0 var(--ink);
  cursor: pointer;
}

.pop-m-btn.active {
  background: var(--crayon-yellow);
}

.pop-history {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

.pop-history .btn {
  width: 100%;
  justify-content: center;
}

.wave-pop-fields {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding-top: 4px;
  border-top: 2px dashed rgba(42, 31, 24, 0.2);
}

.wave-pop-fields[hidden] {
  display: none !important;
}

.pop-wave-title {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 0.8rem;
}

.pop-wave-tag {
  margin: 0;
  font-size: 0.72rem;
  color: var(--muted);
  line-height: 1.2;
}

.pop-hint {
  margin: 0;
  font-size: 0.78rem;
  color: var(--muted);
  line-height: 1.25;
}

.paper-pro-lock {
  position: relative;
  opacity: 0.85;
}

.pro-badge {
  position: absolute;
  top: 2px;
  right: 2px;
  font-family: var(--font-display);
  font-size: 0.55rem;
  font-weight: 800;
  line-height: 1;
  padding: 2px 4px;
  border-radius: 4px;
  border: 1.5px solid var(--ink);
  background: var(--crayon-yellow);
  color: var(--ink);
  box-shadow: 1px 1px 0 var(--ink);
  pointer-events: none;
}

.unlock-modal {
  position: fixed;
  inset: 0;
  z-index: 80;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(42, 31, 24, 0.55);
  box-sizing: border-box;
}

.unlock-modal[hidden] {
  display: none !important;
}

.unlock-card {
  width: min(340px, 100%);
  background: var(--paper);
  border: 3px solid var(--ink);
  border-radius: 16px 20px 14px 18px / 18px 14px 20px 16px;
  box-shadow: 4px 5px 0 var(--ink);
  padding: 14px 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.unlock-card h2 {
  margin: 0;
  font-family: var(--font-display);
  font-size: 1.15rem;
  font-weight: 700;
}

.unlock-copy {
  margin: 0;
  font-size: 0.95rem;
  line-height: 1.3;
  color: var(--muted);
}

.unlock-perks {
  margin: 0;
  padding-left: 1.1em;
  font-size: 0.9rem;
  line-height: 1.35;
}

.unlock-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 0.8rem;
}

.unlock-field input {
  font: inherit;
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
  font-size: 1.02rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  padding: 8px 10px;
  border: 2.5px solid var(--ink);
  border-radius: 8px;
  background: #fff8e7;
  color: var(--ink);
}

.unlock-field input.is-pro-key {
  text-transform: uppercase;
}

.unlock-msg {
  margin: 0;
  font-size: 0.85rem;
  min-height: 1.2em;
}

.unlock-msg.is-bad {
  color: var(--crayon-red);
}

.unlock-msg.is-ok {
  color: var(--crayon-green);
}

.unlock-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.unlock-hint {
  margin: 0;
  font-size: 0.75rem;
  color: var(--muted);
}

#btn-unlock[hidden] {
  display: none !important;
}

.btn-pro {
  font-weight: 700;
  box-shadow: 2px 2px 0 var(--ink);
  letter-spacing: 0.01em;
}

.unlock-buy {
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  text-decoration: none;
  width: 100%;
  box-sizing: border-box;
  padding: 10px 12px;
  font-weight: 700;
  font-size: 1.02rem;
  box-shadow: 2px 3px 0 var(--ink);
}

.unlock-buy:hover {
  filter: brightness(1.05);
}

.unlock-buy:active {
  transform: translate(1px, 1px);
  box-shadow: 1px 1px 0 var(--ink);
}

.unlock-or {
  margin: 4px 0 0;
  font-size: 0.88rem;
  color: var(--muted);
  text-align: center;
}

.unlock-pro-key {
  display: block;
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-align: center;
  padding: 10px 8px;
  border: 2.5px dashed var(--ink);
  border-radius: 10px;
  background: #fff8e7;
  color: var(--ink);
  user-select: all;
  word-break: break-all;
}

#unlock-save-panel[hidden],
#unlock-buy-panel[hidden] {
  display: none !important;
}

.pro-confetti {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 120;
}

.pro-thanks {
  position: fixed;
  left: 50%;
  top: 18%;
  transform: translate(-50%, -12px) scale(0.96);
  z-index: 121;
  max-width: min(360px, calc(100vw - 32px));
  padding: 14px 18px;
  background: var(--paper);
  border: 3px solid var(--ink);
  border-radius: 16px 20px 14px 18px / 18px 14px 20px 16px;
  box-shadow: 4px 5px 0 var(--ink);
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.35s ease, transform 0.35s ease;
}

.pro-thanks.is-show {
  opacity: 1;
  transform: translate(-50%, 0) scale(1);
}

.pro-thanks strong {
  display: block;
  font-family: var(--font-display);
  font-size: 1.15rem;
  margin-bottom: 6px;
}

.pro-thanks span {
  display: block;
  font-size: 0.95rem;
  line-height: 1.35;
  color: var(--muted);
}

/* Film button while recording live canvas → WebM */
#btn-record.is-filming {
  background: var(--crayon-red);
  color: #fffaf0;
}

/* First-run tip — Film / Crop / Esc */
.boil-tip {
  position: fixed;
  left: 50%;
  bottom: max(16px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 280;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 10px;
  max-width: min(420px, calc(100vw - 24px));
  padding: 10px 12px;
  background: var(--paper);
  border: 2.5px solid var(--ink);
  border-radius: 12px 14px 10px 12px / 12px 10px 14px 12px;
  box-shadow: 3px 3px 0 var(--ink);
  font-family: var(--font-hand);
  font-size: 0.95rem;
  line-height: 1.3;
  color: var(--ink);
}

.boil-tip[hidden] {
  display: none !important;
}

.boil-tip-text {
  margin: 0;
  flex: 1 1 200px;
}

.unlock-retry {
  align-self: flex-start;
}

.unlock-storage-hint {
  color: var(--crayon-red);
  font-weight: 700;
}

/* Tablet / narrow: canvas first, tools below */
@media (max-width: 820px) {
  :root {
    --brush-w: 88px;
    --brush-h: 110px;
    --brush-lift: 18%;
    --brush-sink: 0.58;
    --brush-slot-w: 40px;
    --brush-gap: 10px;
    --brush-side-pad: 12px;
    --brush-rack-h: calc(var(--brush-h) * 0.42);
  }

  .app {
    padding: 4px;
    gap: 4px;
    padding-bottom: max(4px, env(safe-area-inset-bottom));
    overflow-x: hidden;
    max-width: 100%;
  }

  .top {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 4px;
    padding: 4px 6px;
    min-width: 0;
    max-width: 100%;
    overflow: hidden;
  }

  /* Phone/tablet: GIF only — Film is desktop (use OS screen recorder for video) */
  #btn-record {
    display: none !important;
  }

  .brand {
    gap: 6px;
  }

  .brand h1 {
    font-size: 1.05rem;
    line-height: 1;
  }

  .tag { display: none; }
  .top-hints { display: none; }

  .top-actions {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: flex-start;
    gap: 4px;
    flex-shrink: 1;
    min-width: 0;
    max-width: 100%;
  }

  .top-actions .btn.crayon {
    padding: 4px 7px;
    font-size: 0.68rem;
    box-shadow: 1px 1px 0 var(--ink);
  }

  .top-actions .btn-pro {
    padding: 4px 8px;
    font-size: 0.65rem;
  }

  .gif-size {
    gap: 2px;
    font-size: 0.65rem;
  }

  .gif-size select {
    font-size: 0.65rem;
    padding: 2px 3px;
    max-width: 5.5rem;
  }

  .gif-size span { display: none; }

  .stage {
    grid-template-columns: minmax(0, 1fr);
    /* Paper sizes to content; rail takes leftover — no giant empty gutters */
    grid-template-rows: auto minmax(0, 1fr);
    gap: 4px;
    min-height: 0;
    min-width: 0;
    max-width: 100%;
    overflow: hidden;
  }

  .canvas-column {
    min-height: 0;
    min-width: 0;
    max-width: 100%;
    overflow: hidden;
    /* Wrap + size-bar hug the paper; don't stretch wrap to 1fr */
    grid-template-rows: auto auto;
  }

  .canvas-wrap {
    padding: 4px;
    place-items: center;
    justify-items: center;
    align-items: center;
    width: 100%;
    max-width: 100%;
    min-width: 0;
    height: fit-content;
    max-height: min(48dvh, 100%);
    box-sizing: border-box;
    /* Width-only container — height must not invent empty gutters */
    container-type: inline-size;
  }

  .canvas-stage {
    justify-self: center;
    align-self: center;
    margin-inline: auto;
    width: min(100%, calc(48dvh * 4 / 3)) !important;
    height: auto !important;
    max-width: 100%;
    max-height: 48dvh;
    aspect-ratio: 4 / 3;
  }

  .desk-decor .decor { display: none; }
  .canvas-decor {
    display: none;
  }

  .size-bar {
    padding: 2px 6px !important;
    min-width: 0;
    max-width: 100%;
    overflow: hidden;
    position: sticky;
    bottom: 0;
    z-index: 6;
    background: var(--panel-size);
  }

  .slider-row {
    display: flex;
    flex-wrap: nowrap;
    align-items: center;
    gap: 4px;
    min-width: 0;
    max-width: 100%;
  }

  .slider-row input[type="range"] {
    flex: 1 1 auto;
    min-width: 0;
  }

  .slider-row #btn-brush-pop {
    flex: 0 0 auto;
    padding: 3px 6px !important;
    font-size: 0.65rem !important;
  }

  .rail {
    display: flex;
    flex-direction: column;
    gap: 3px;
    min-height: 0;
    min-width: 0;
    max-width: 100%;
    /* Fill leftover viewport under the paper */
    max-height: none;
    height: 100%;
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-y;
  }

  .brush-rack-viewport {
    height: var(--brush-rack-h);
    min-height: var(--brush-rack-h);
    flex: 0 0 var(--brush-rack-h);
    max-width: 100%;
    overflow-x: auto;
    overflow-y: hidden;
  }

  .brush-rack {
    justify-content: center;
    height: 100%;
    min-height: var(--brush-rack-h);
  }

  .tools-panel {
    padding: 0 !important;
    flex-shrink: 0;
    min-height: var(--brush-rack-h);
    max-width: 100%;
    min-width: 0;
    overflow: hidden;
  }

  .colors-panel {
    flex-shrink: 0;
    padding: 4px 6px !important;
  }

  #sv-canvas {
    height: 56px !important;
  }

  .paper-panel {
    flex-shrink: 0;
    overflow: visible;
  }

  .paper-row {
    display: flex;
    flex-wrap: nowrap;
    gap: 4px;
    overflow-x: auto;
    overscroll-behavior-x: contain;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 2px;
    grid-template-columns: none;
    touch-action: pan-x;
  }

  .paper-cell {
    flex: 0 0 48px;
    width: 48px;
    aspect-ratio: 1;
  }

  .status-line {
    font-size: 0.75rem;
    flex-shrink: 0;
    min-height: 0;
  }

  .pen-hint { display: none; }
}

@media (max-width: 560px) {
  :root {
    --brush-w: 72px;
    --brush-h: 96px;
    --brush-slot-w: 34px;
    --brush-gap: 8px;
    --brush-side-pad: 10px;
    --brush-rack-h: calc(var(--brush-h) * 0.4);
  }

  .logo {
    width: 26px;
    height: 26px;
  }

  .brand h1 {
    font-size: 0.95rem;
  }

  /* Logo only — frees horizontal room; title already in tab */
  .brand > div {
    display: none;
  }

  .top-actions .btn.crayon {
    padding: 4px 6px;
    font-size: 0.64rem;
  }

  /* Compact Pro chip */
  .top-actions .btn-pro {
    max-width: 3.2rem;
    overflow: hidden;
    text-overflow: clip;
    white-space: nowrap;
    padding: 4px 6px;
    font-size: 0.6rem;
  }

  .rail {
    /* Extra room on small phones — paper already capped via 42dvh */
    min-height: 0;
  }

  .canvas-wrap {
    max-height: min(42dvh, 100%);
    padding: 3px;
  }

  .canvas-stage {
    width: min(100%, calc(42dvh * 4 / 3)) !important;
    max-height: 42dvh;
  }

  .layer-toggle {
    font-size: 0.75rem;
  }

  #sv-canvas {
    height: 48px !important;
  }

  .swatch-row {
    gap: 3px;
  }

  .paper-cell {
    flex: 0 0 44px;
    width: 44px;
  }

  .paper-cell .paper-label {
    font-size: 0.45rem;
  }

  .colors-panel,
  .paper-panel,
  .tools-panel {
    max-width: 100%;
    min-width: 0;
  }
}
