/* Layout: one column "pencil case" — header, how-to strip, crayon tabs, grid of paper sheets.
   Trace screen: full-bleed camera, glass controls floating at top and bottom. */
:root {
  --paper: #f3f4f9;
  --card: #ffffff;
  --sheet: #ffffff;          /* paper sheets stay white in both themes, drawings are graphite */
  --graphite: #2a2d36;
  --ink: #1d2030;
  --muted: #5c6378;
  --line: #dde1ec;
  --pastelka: #ff6b3d;
  --sky: #3b78ff;
  --mint: #12a594;
  --grape: #8a5cf6;
  --sun: #ffc531;
  --sun-ink: #d9930b;
  --shadow: 0 1px 0 rgba(30, 34, 52, .06), 0 8px 24px -12px rgba(30, 34, 52, .28);
  --display: "Baloo 2", "Nunito", ui-rounded, system-ui, sans-serif;
  --body: "Nunito", ui-rounded, system-ui, -apple-system, "Segoe UI", sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #13151c; --card: #1c1f29; --ink: #eef0f7; --muted: #a2a8ba; --line: #2b2f3d;
    --pastelka: #ff8357; --sky: #6e9bff; --mint: #2cc4b2; --grape: #a98bff; --sun: #ffd15c; --sun-ink: #ffc23d;
    --shadow: 0 1px 0 rgba(0, 0, 0, .3), 0 10px 26px -14px rgba(0, 0, 0, .8);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --paper: #13151c; --card: #1c1f29; --ink: #eef0f7; --muted: #a2a8ba; --line: #2b2f3d;
  --pastelka: #ff8357; --sky: #6e9bff; --mint: #2cc4b2; --grape: #a98bff; --sun: #ffd15c; --sun-ink: #ffc23d;
  --shadow: 0 1px 0 rgba(0, 0, 0, .3), 0 10px 26px -14px rgba(0, 0, 0, .8);
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font: 500 16px/1.5 var(--body);
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; cursor: pointer; }
button:focus-visible, input:focus-visible, .file-drop:focus-within { outline: 3px solid var(--sky); outline-offset: 2px; }
svg { display: block; }

/* ---------- HOME ---------- */
#home {
  max-width: 1120px;
  margin: 0 auto;
  padding-block: max(20px, env(safe-area-inset-top)) 40px;
  padding-left: max(16px, env(safe-area-inset-left));
  padding-right: max(16px, env(safe-area-inset-right));
  display: grid;
  gap: 22px;
}
.brand { display: flex; align-items: center; gap: 14px; }
.brand-mark { width: 64px; height: 64px; flex: none; }
.bm-paper { fill: var(--sheet); stroke: var(--line); stroke-width: 2; }
.bm-line { fill: none; stroke: var(--graphite); stroke-width: 3; stroke-linecap: round; stroke-dasharray: 3 5; }
.bm-pencil { fill: var(--pastelka); }
.bm-wood { fill: #f4d3a6; }
.bm-tip { fill: var(--graphite); }
h1 {
  margin: 0;
  font: 800 clamp(2rem, 6vw, 3rem)/1 var(--display);
  letter-spacing: -.01em;
}
.tagline { margin: 4px 0 0; color: var(--muted); font-weight: 700; }

.howto {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
}
.step {
  position: relative;
  display: flex; align-items: center; gap: 12px;
  background: var(--card);
  border-radius: 18px;
  padding: 14px 16px;
  box-shadow: var(--shadow);
}
.step .num {
  position: absolute; top: -8px; left: -6px;
  width: 26px; height: 26px; border-radius: 50%;
  display: grid; place-items: center;
  background: var(--sun); color: #3b2a00;
  font: 800 .95rem/1 var(--display);
}
.step svg { width: 44px; height: 44px; flex: none; fill: none; stroke: var(--muted); stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }
.step p { margin: 0; font-size: .95rem; font-weight: 700; color: var(--muted); }
@media (max-width: 720px) {
  .howto { grid-template-columns: 1fr; gap: 10px; }
  .step { padding: 10px 14px; }
  .step svg { width: 36px; height: 36px; }
}

.tabs {
  display: flex; flex-wrap: wrap; gap: 10px;
  position: sticky; top: env(safe-area-inset-top, 0px); z-index: 5;
  padding-block: 8px;
  background: var(--paper);
}
.tab {
  display: inline-flex; align-items: center; gap: 8px;
  min-height: 52px;
  padding: 6px 18px 6px 12px;
  border: 0;
  border-radius: 999px;
  background: var(--card);
  box-shadow: var(--shadow);
  font: 700 1.15rem/1 var(--display);
  transition: transform .15s, background .15s, color .15s;
}
.tab svg { width: 30px; height: 30px; fill: none; stroke: var(--tab); stroke-width: 2.6; stroke-linecap: round; stroke-linejoin: round; }
.tab[aria-selected="true"] { background: var(--tab); color: #fff; }
.tab[aria-selected="true"] svg { stroke: #fff; }
.tab:active { transform: scale(.96); }
@media (max-width: 720px) {
  .tabs {
    flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none;
    margin-left: calc(-1 * max(16px, env(safe-area-inset-left)));
    margin-right: calc(-1 * max(16px, env(safe-area-inset-right)));
    padding-left: max(16px, env(safe-area-inset-left));
    padding-right: max(16px, env(safe-area-inset-right));
    padding-bottom: 14px;
  }
  .tabs::-webkit-scrollbar { display: none; }
  .tab { flex: none; min-height: 48px; font-size: 1.05rem; }
}
#tab-fotka[aria-selected="true"] { color: #2b1e00; }
#tab-fotka[aria-selected="true"] svg { stroke: #2b1e00; }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 16px;
}
@media (max-width: 420px) { .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; } }
.card {
  display: flex; flex-direction: column; align-items: stretch;
  gap: 8px;
  padding: 0;
  border: 0;
  background: none;
  text-align: center;
  font: 700 1.1rem/1.2 var(--display);
}
.sheet {
  position: relative;
  aspect-ratio: 1;
  max-width: 100%;
  background: var(--sheet);
  border-radius: 14px;
  box-shadow: var(--shadow);
  overflow: hidden;
  transition: transform .2s cubic-bezier(.3, 1.6, .5, 1), box-shadow .2s;
}
/* faint lines of a school notebook */
.sheet::before {
  content: "";
  position: absolute; inset: 0;
  background: repeating-linear-gradient(to bottom, transparent 0 23px, rgba(59, 120, 255, .09) 23px 24px);
  pointer-events: none;
}
@supports not (aspect-ratio: 1) {
  .sheet { height: 0; padding-top: 100%; }
  .sheet img, .sheet .glyph { position: absolute !important; inset: 0; }
}
.sheet img, .sheet .glyph { position: relative; width: 100%; height: 100%; padding: 10%; object-fit: contain; }
.card:nth-child(4n+1) .sheet { transform: rotate(-1.2deg); }
.card:nth-child(4n+2) .sheet { transform: rotate(.8deg); }
.card:nth-child(4n+3) .sheet { transform: rotate(-.4deg); }
.card:nth-child(4n) .sheet { transform: rotate(1.3deg); }
.card:hover .sheet, .card:focus-visible .sheet { transform: rotate(0) translateY(-4px) scale(1.03); }
.card:active .sheet { transform: scale(.97); }
.card:focus-visible { outline: none; }
.card:focus-visible .sheet { outline: 3px solid var(--sky); outline-offset: 3px; }
.card .label { color: var(--ink); }

.grid-glyph { grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); }
.glyph {
  display: grid; place-items: center;
  font: 700 clamp(3.4rem, 9vw, 4.6rem)/1 var(--display);
  color: transparent;
  -webkit-text-stroke: 2.5px var(--graphite);
  padding: 0 !important;
}

.seg {
  display: inline-flex;
  padding: 4px;
  gap: 4px;
  border-radius: 999px;
  background: var(--card);
  box-shadow: var(--shadow);
  justify-self: start;
}
.seg-btn {
  min-height: 44px;
  min-width: 64px;
  padding: 0 16px;
  border: 0;
  border-radius: 999px;
  background: transparent;
  font: 700 1.05rem/1 var(--display);
  color: var(--muted);
}
.seg-btn[aria-pressed="true"] { background: var(--ink); color: var(--paper); }
#panel-pismena { display: grid; gap: 16px; }

.sheet-form {
  display: grid; gap: 14px;
  max-width: 560px;
  background: var(--card);
  border-radius: 22px;
  padding: 22px;
  box-shadow: var(--shadow);
}
.sheet-form label { font: 700 1.1rem/1.2 var(--display); }
#name-input {
  width: 100%;
  min-height: 60px;
  padding: 8px 16px;
  border: 2px solid var(--line);
  border-radius: 14px;
  background: var(--paper);
  color: var(--ink);
  font: 700 1.8rem/1 var(--display);
  letter-spacing: .04em;
}
.check { display: flex; align-items: center; gap: 10px; font: 700 1rem/1.2 var(--body) !important; color: var(--muted); }
.check input { width: 22px; height: 22px; accent-color: var(--grape); }
.name-preview {
  min-height: 120px;
  display: grid; place-items: center;
  background: var(--sheet);
  border-radius: 14px;
  border: 2px dashed var(--line);
  font: 700 clamp(2.6rem, 10vw, 4.4rem)/1 var(--display);
  color: transparent;
  -webkit-text-stroke: 2px var(--graphite);
  overflow-wrap: anywhere;
  padding: 8px;
  text-align: center;
}
.primary {
  min-height: 56px;
  padding: 0 28px;
  border: 0;
  border-radius: 999px;
  background: var(--pastelka);
  color: #fff;
  font: 800 1.3rem/1 var(--display);
  box-shadow: 0 6px 0 -1px #a8401f;
  transition: transform .1s, box-shadow .1s;
}
.primary:active { transform: translateY(4px); box-shadow: 0 2px 0 -1px #a8401f; }
.primary.small { min-height: 46px; font-size: 1.1rem; }

.file-drop {
  display: grid; justify-items: center; gap: 6px;
  padding: 28px 16px;
  border: 3px dashed var(--line);
  border-radius: 18px;
  cursor: pointer;
  text-align: center;
}
.file-drop svg { width: 56px; height: 56px; fill: none; stroke: var(--sun-ink); stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.file-drop span { font: 800 1.3rem/1.2 var(--display); }
.file-drop small { color: var(--muted); font-weight: 700; }
#photo-edit { display: grid; gap: 14px; }
.photo-preview {
  background: var(--sheet);
  border-radius: 14px;
  padding: 10px;
  height: min(52vh, 380px);
  color: var(--graphite);
}
#photo-preview-inner { width: 100%; height: 100%; }
#photo-preview-inner .mask, #photo-preview-inner img { width: 100%; height: 100%; object-fit: contain; }
#photo-detail-label { display: grid; gap: 6px; }
input[type="range"] { width: 100%; accent-color: var(--pastelka); height: 32px; }

.foot { color: var(--muted); font-size: .92rem; font-weight: 700; text-align: center; }

/* ---------- TRACE ---------- */
#trace {
  position: fixed; inset: 0;
  background: #fdfdfb repeating-linear-gradient(to bottom, transparent 0 31px, rgba(59, 120, 255, .12) 31px 32px);
  overflow: hidden;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  color: #fff;
}
#video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
#video.mirror { transform: scaleX(-1); }
#nocam {
  position: absolute; inset: 0;
  background: #fdfdfb repeating-linear-gradient(to bottom, transparent 0 31px, rgba(59, 120, 255, .12) 31px 32px);
}
.nocam-card {
  position: absolute; left: 50%; top: calc(env(safe-area-inset-top, 0px) + 84px);
  transform: translateX(-50%);
  width: min(92vw, 420px);
  display: grid; gap: 12px; justify-items: center; text-align: center;
  background: #1d2030; color: #fff;
  padding: 16px 18px; border-radius: 18px;
  box-shadow: 0 10px 30px -10px rgba(0, 0, 0, .5);
  z-index: 4;
}
.nocam-card p { margin: 0; font-weight: 700; }
#stage { position: absolute; inset: 0; z-index: 1; }
#overlay {
  position: absolute; left: 50%; top: 50%;
  z-index: 2;
  pointer-events: none;
  color: #1a1a1a;
  --o: 7; --d: 4.5; --t: 3;
  transform-origin: 50% 50%;
  will-change: transform;
}
#overlay-inner { width: 100%; height: 100%; }
#overlay-inner svg { width: 100%; height: 100%; overflow: visible; }
#overlay.no-detail .t { display: none; }
.mask {
  width: 100%; height: 100%;
  background: currentColor;
  -webkit-mask-repeat: no-repeat; mask-repeat: no-repeat;
  -webkit-mask-position: center; mask-position: center;
  -webkit-mask-size: contain; mask-size: contain;
}
#overlay-inner img { width: 100%; height: 100%; object-fit: contain; }
.txt text { font-family: "Baloo 2", "Nunito", sans-serif; font-weight: 700; fill: currentColor; fill-opacity: .1; stroke: currentColor; stroke-width: var(--d); stroke-linejoin: round; }

.glass {
  display: inline-flex; align-items: center; gap: 8px;
  min-height: 52px;
  padding: 0 18px 0 12px;
  border: 0;
  border-radius: 999px;
  background: rgba(16, 18, 26, .72);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  color: #fff;
  font: 700 1.1rem/1 var(--display);
}
.glass svg { width: 26px; height: 26px; fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }
.tbar {
  position: absolute; z-index: 5;
  top: calc(env(safe-area-inset-top, 0px) + 12px);
  left: max(12px, env(safe-area-inset-left));
  right: max(12px, env(safe-area-inset-right));
  display: flex; align-items: flex-start; justify-content: space-between; gap: 10px;
  pointer-events: none;
}
.tbar > * { pointer-events: auto; }
.hint {
  margin: 0;
  max-width: 360px;
  padding: 10px 14px;
  border-radius: 14px;
  background: rgba(255, 197, 49, .95);
  color: #2b1e00;
  font-weight: 800;
  font-size: .95rem;
  line-height: 1.3;
  text-align: center;
  transition: opacity .4s;
}
.hint.gone { opacity: 0; pointer-events: none; }
@media (max-width: 560px) { .hint { position: absolute; top: 64px; left: 0; right: 0; margin: 0 auto; } }

.lock { position: relative; overflow: hidden; }
.lock .ico-closed { display: none; }
.lock[aria-pressed="true"] { background: var(--sun); color: #2b1e00; }
.lock[aria-pressed="true"] .ico-open { display: none; }
.lock[aria-pressed="true"] .ico-closed { display: block; }
.hold-bar {
  position: absolute; left: 0; bottom: 0; height: 5px; width: 0;
  background: #2b1e00;
}
.lock.holding .hold-bar { width: 100%; transition: width .9s linear; }

.glass-panel {
  background: rgba(16, 18, 26, .78);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
}
.dock {
  position: absolute; z-index: 5;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom, 0px) + 12px);
  transform: translateX(-50%);
  width: min(calc(100% - 24px), 640px);
  display: grid; gap: 10px;
  grid-template-columns: minmax(0, 1fr);
  padding: 12px 14px;
  border-radius: 24px;
  transition: transform .3s, opacity .3s;
}
#trace.locked .dock, #trace.dock-hidden .dock { transform: translate(-50%, 140%); opacity: 0; pointer-events: none; }
.show-dock {
  position: absolute; z-index: 5;
  left: 50%; transform: translateX(-50%);
  bottom: calc(env(safe-area-inset-bottom, 0px) + 14px);
  padding: 0 20px 0 14px;
}
#trace:not(.dock-hidden) .show-dock, #trace.locked .show-dock { display: none; }
#trace.locked #back, #trace.locked .hint { opacity: 0; pointer-events: none; }
#trace.nocam .hint { display: none; }
.dock-row { display: flex; align-items: center; gap: 12px; min-width: 0; }
.dock-row input[type="range"] { flex: 1 1 auto; width: auto; min-width: 0; }
.dock-row.wrap { flex-wrap: wrap; justify-content: space-between; row-gap: 10px; }
.dock-ico { width: 26px; height: 26px; flex: none; fill: none; stroke: #fff; stroke-width: 2; }
.dock-ico .fillme { fill: #fff; stroke: none; }
.dock input[type="range"] { accent-color: var(--sun); }
.swatches { display: flex; gap: 8px; flex-wrap: wrap; }
.swatch {
  width: 40px; height: 40px;
  border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, .25);
  background: var(--c);
  padding: 0;
}
.swatch[aria-checked="true"] { border-color: #fff; box-shadow: 0 0 0 3px var(--sun); }
.seg.dark { background: rgba(255, 255, 255, .1); box-shadow: none; }
.seg.dark .seg-btn { min-width: 48px; padding: 0 12px; color: #fff; }
.seg.dark .seg-btn i { display: block; width: 22px; border-radius: 4px; background: currentColor; }
.seg.dark .seg-btn[aria-pressed="true"] { background: #fff; color: #10121a; }
.chip {
  min-height: 44px; padding: 0 16px;
  border-radius: 999px;
  border: 2px solid rgba(255, 255, 255, .35);
  background: transparent; color: #fff;
  font: 700 1rem/1 var(--display);
}
.chip[aria-pressed="true"] { background: #fff; color: #10121a; border-color: #fff; }
.tools { justify-content: flex-start; gap: 8px; flex-wrap: nowrap; }
.tools .tool { flex: none; }
@media (max-width: 480px) {
  .dock { padding: 10px 12px; gap: 8px; }
  .swatch { width: 36px; height: 36px; }
  .tool { width: 44px; height: 44px; }
  .done .done-long { display: none; }
  .done { text-transform: capitalize; padding: 0 14px 0 10px; min-width: 0; }
}
.tool {
  width: 48px; height: 48px;
  display: grid; place-items: center;
  border: 0; border-radius: 50%;
  background: rgba(255, 255, 255, .1);
}
.tool[aria-pressed="true"] { background: var(--sun); color: #2b1e00; }
.tool svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.done {
  margin-left: auto;
  display: inline-flex; align-items: center; gap: 8px;
  min-height: 50px; padding: 0 18px 0 12px;
  border: 0; border-radius: 999px;
  background: var(--sun); color: #2b1e00;
  font: 800 1.1rem/1 var(--display);
  white-space: nowrap;
}
.done svg { width: 24px; height: 24px; fill: currentColor; }

#cheer {
  position: absolute; inset: 0; z-index: 10;
  display: grid; place-items: center;
  background: rgba(10, 12, 18, .55);
}
.cheer-card {
  display: grid; justify-items: center; gap: 10px;
  padding: 28px 26px 22px;
  border-radius: 28px;
  background: #fff; color: #1d2030;
  width: min(90vw, 380px);
  text-align: center;
  animation: pop .5s cubic-bezier(.3, 1.6, .5, 1);
}
.cheer-card > svg { width: 96px; height: 96px; fill: var(--sun); stroke: #d9930b; stroke-width: 1; animation: spin 1.2s ease-out; }
.cheer-card p { margin: 0; font: 800 2.4rem/1 var(--display); }
.cheer-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; margin-top: 6px; }
.cheer-actions .glass { background: #eef0f6; color: #1d2030; padding: 0 20px; }
.confetti { position: absolute; top: -20px; width: 12px; height: 18px; border-radius: 3px; z-index: 11; pointer-events: none; animation: fall linear forwards; }
@keyframes pop { from { transform: scale(.6); opacity: 0; } to { transform: scale(1); opacity: 1; } }
@keyframes spin { from { transform: rotate(-200deg) scale(.3); } to { transform: rotate(0) scale(1); } }
@keyframes fall { to { transform: translateY(110vh) rotate(720deg); } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
