:root {
  --paper: #f6efe1;
  --paper-2: #ece0c8;
  --ink: #2b2236;
  --ink-soft: #5b4f68;
  --ultra: #2f4bd8;
  --vermilion: #e8590c;
  --err: #b02a37;
  --font-hand: "Caveat", "Segoe Print", "Comic Sans MS", cursive;
  --font-ui: "Nunito", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --paper-tex: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='180' height='180'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='.85' numOctaves='3' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 .35  0 0 0 0 .28  0 0 0 0 .18  0 0 0 .22 0'/></filter><rect width='100%' height='100%' filter='url(%23n)'/></svg>");
  color-scheme: light;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--paper);
  color: var(--ink);
  font: 15px/1.35 var(--font-ui);
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
}
canvas#gl {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: manipulation;
}
.feed {
  position: fixed;
  top: 0;
  left: 0;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}
.sr {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* Мазок: паперовий фон з рваним краєм, текст лишається чистим */
.paint, .chip, .btn { position: relative; isolation: isolate; }
.paint::before, .chip::before, .btn::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  background: var(--paper) var(--paper-tex);
  border-radius: 26px 34px 28px 38px / 32px 24px 36px 28px;
  box-shadow: 4px 6px 0 rgba(43, 34, 54, 0.22);
  filter: url(#brush);
}

.top, .bottom {
  position: fixed;
  left: max(14px, env(safe-area-inset-left, 0px));
  right: max(14px, env(safe-area-inset-right, 0px));
  z-index: 10;
  transition: opacity 0.25s;
}
.top {
  top: calc(env(safe-area-inset-top, 0px) + 12px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.bottom {
  bottom: calc(env(safe-area-inset-bottom, 0px) + 28px);
  display: flex;
  flex-direction: column;
  gap: 18px;
}
.brand {
  font-family: var(--font-hand);
  font-weight: 700;
  font-size: 28px;
  line-height: 1;
  color: var(--paper);
  text-shadow: 0 2px 0 var(--ink), 0 0 14px rgba(43, 34, 54, 0.5);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}
.actions { display: flex; gap: 10px; flex: 0 0 auto; }
.iconbtn {
  width: 46px;
  height: 46px;
  flex: 0 0 auto;
  border: 0;
  background: none;
  color: var(--ink);
  display: grid;
  place-items: center;
  cursor: pointer;
  padding: 0;
}
.iconbtn svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ---- колесо-палітра ---- */
.dial {
  position: relative;
  width: var(--dial);
  aspect-ratio: 1;
  margin: 0 auto;
  touch-action: none;
  cursor: grab;
  border-radius: 50%;
}
.dial:active { cursor: grabbing; }
.dial:focus-visible { outline: 3px solid var(--ultra); outline-offset: 6px; }
.ring {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: visible;
  transition: transform 0.42s cubic-bezier(0.2, 0.8, 0.2, 1);
  filter: url(#brush) drop-shadow(0 8px 10px rgba(43, 34, 54, 0.25));
}
.ring.dragging { transition: none; }
.pointer {
  position: absolute;
  left: 50%;
  top: -10px;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 11px solid transparent;
  border-right: 11px solid transparent;
  border-top: 20px solid var(--ink);
  z-index: 2;
  pointer-events: none;
}
.hub {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 46%;
  aspect-ratio: 1;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 8px;
  text-align: center;
  color: var(--ink);
  pointer-events: none;
  isolation: isolate;
}
.hub::before {
  content: "";
  position: absolute;
  inset: -4%;
  z-index: -1;
  background: var(--paper) var(--paper-tex);
  border-radius: 52% 48% 50% 50% / 50% 52% 48% 50%;
  box-shadow: 0 4px 0 rgba(43, 34, 54, 0.2);
  filter: url(#brush);
}
.hub-glyph { display: block; line-height: 0; }
.hub-glyph svg {
  width: 30px;
  height: 30px;
  display: block;
  margin: 0 auto;
  fill: none;
  stroke: var(--ink);
  stroke-width: 3;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.hub-name {
  font-family: var(--font-hand);
  font-weight: 700;
  font-size: 22px;
  line-height: 0.95;
  text-wrap: balance;
}
.hub-idx {
  font-family: var(--font-ui);
  font-size: 11px;
  font-weight: 800;
  color: var(--ink-soft);
  font-variant-numeric: tabular-nums;
}

.row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px 8px 10px;
  border-radius: 22px;
}
.row input[type="range"] {
  flex: 1;
  min-width: 0;
  accent-color: var(--vermilion);
}
.val {
  min-width: 3em;
  text-align: right;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}
.chip {
  flex: 0 0 auto;
  padding: 9px 14px;
  border: 0;
  background: none;
  color: var(--ink);
  font-family: var(--font-ui);
  font-weight: 800;
  font-size: 14px;
  white-space: nowrap;
  cursor: pointer;
}
.chip[aria-pressed="true"] { color: #fff; }
.chip[aria-pressed="true"]::before { background: var(--ultra) var(--paper-tex); }
body.ui-off .top, body.ui-off .bottom { opacity: 0; pointer-events: none; }

.toast {
  position: fixed;
  left: 50%;
  top: 50%;
  z-index: 15;
  width: min(90vw, 520px);
  transform: translate(-50%, -50%) scale(0.96) rotate(-2deg);
  text-align: center;
  font-family: var(--font-hand);
  font-weight: 700;
  font-size: clamp(34px, 10vw, 56px);
  line-height: 1.05;
  color: var(--paper);
  text-shadow: 0 3px 0 var(--ink), 0 0 24px rgba(43, 34, 54, 0.6);
  opacity: 0;
  transition: opacity 0.3s, transform 0.3s;
  pointer-events: none;
}
.toast.long { font-size: 26px; }
.toast.show { opacity: 1; transform: translate(-50%, -50%) scale(1) rotate(-2deg); }

/* ---- стартові екрани ---- */
.start, .sheet {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding:
    max(20px, env(safe-area-inset-top, 0px))
    max(20px, env(safe-area-inset-right, 0px))
    max(20px, env(safe-area-inset-bottom, 0px))
    max(20px, env(safe-area-inset-left, 0px));
  background: rgba(43, 34, 54, 0.38);
}
.sheet { z-index: 25; }
.card {
  width: min(100%, 380px);
  display: grid;
  gap: 12px;
  padding: 26px 22px;
  text-align: center;
  color: var(--ink);
}
.card h1, .sheet-title {
  margin: 0;
  font-family: var(--font-hand);
  font-weight: 700;
  font-size: 42px;
  line-height: 1;
}
.card p {
  margin: 0;
  color: var(--ink-soft);
  text-wrap: pretty;
}
.btn {
  width: 100%;
  padding: 14px 16px;
  border: 0;
  background: none;
  color: var(--ink);
  font-family: var(--font-ui);
  font-weight: 800;
  font-size: 16px;
  cursor: pointer;
}
.btn.primary { color: #fff; }
.btn.primary::before { background: var(--ultra) var(--paper-tex); }
.btn.ghost::before { background: var(--paper-2) var(--paper-tex); box-shadow: none; }
.btn:disabled { opacity: 0.45; cursor: default; }
.note { font-size: 13px; }
.err {
  min-height: 1.2em;
  color: var(--err);
  font-weight: 800;
  font-size: 14px;
}
.gate-in {
  width: 100%;
  padding: 12px;
  font: inherit;
  font-size: 24px;
  font-weight: 800;
  text-align: center;
  color: var(--ink);
  background: var(--paper);
  border: 2px solid var(--ink);
  border-radius: 14px;
}
#modes { display: grid; gap: 10px; }

button:focus-visible, input:focus-visible {
  outline: 3px solid var(--ultra);
  outline-offset: 3px;
}

/* ---- вікові режими ---- */
.mode-kid .brand { display: none; }
.mode-kid .iconbtn { width: 72px; height: 72px; }
.mode-kid .iconbtn svg { width: 30px; height: 30px; }
.mode-kid .chip { min-height: 64px; padding: 0 18px; font-size: 19px; }
.mode-kid .row { padding: 10px 14px; }
.mode-kid .dial { width: var(--dial); }
.mode-kid .hub-glyph svg { width: 66px; height: 66px; }
.mode-kid .hub-name, .mode-kid .hub-idx { display: none; }
.mode-kid .btn { min-height: 64px; font-size: 19px; }

.mode-adult .hub-glyph { display: none; }
.mode-adult .hub-name { font-size: 22px; }
.mode-adult .dial { width: var(--dial); }

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

/* ---- v3: вік, мітки, «Картина», стрілки ---- */
.ico { width: 22px; height: 22px; flex: 0 0 auto; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.chip { display: inline-flex; align-items: center; gap: 6px; }
.btn { display: flex; align-items: center; justify-content: center; gap: 10px; }
.tools {
  position: fixed;
  left: max(14px, env(safe-area-inset-left, 0px));
  right: max(14px, env(safe-area-inset-right, 0px));
  top: calc(env(safe-area-inset-top, 0px) + 70px);
  z-index: 10;
  transition: opacity 0.25s;
}
body.ui-off .tools { opacity: 0; pointer-events: none; }
.dial-row { position: relative; display: flex; justify-content: center; }
/* коло, повернуте на кут, не має перехоплювати дотики до стрілок: усі жести висять на .dial */
.ring { pointer-events: none; }
.arrow {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 52px;
  height: 52px;
  border: 0;
  background: none;
  color: var(--ink);
  display: grid;
  place-items: center;
  padding: 0;
  cursor: pointer;
}
.arrow { z-index: 3; }
.arrow[data-side="l"] { left: 0; }
.arrow[data-side="r"] { right: 0; }
.arrow .ico { width: 24px; height: 24px; stroke-width: 2.6; }
.age { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.age-btn {
  min-height: 64px;
  padding: 8px 4px;
  border: 0;
  background: none;
  color: var(--ink);
  font-family: var(--font-hand);
  font-size: 24px;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
  position: relative;
  isolation: isolate;
}
.age-btn small { display: block; margin-top: 3px; font-family: var(--font-ui); font-size: 11px; font-weight: 800; color: var(--ink-soft); }
.age-btn::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  background: var(--paper) var(--paper-tex);
  border-radius: 26px 34px 28px 38px / 32px 24px 36px 28px;
  box-shadow: 4px 6px 0 rgba(43, 34, 54, 0.22);
  filter: url(#brush);
}
.age-btn[aria-pressed="true"] { color: #fff; }
.age-btn[aria-pressed="true"]::before { background: var(--ultra) var(--paper-tex); }
.age-btn[aria-pressed="true"] small { color: #dfe6ff; }
.age-btn:disabled { opacity: 0.5; }
/* натиск: мазок стискається й змінює форму */
.paint:active::before, .chip:active::before, .btn:active::before, .age-btn:active::before {
  border-radius: 40px 22px 36px 26px / 24px 36px 22px 32px;
  transform: scale(0.97);
}
.lbl { white-space: nowrap; }
/* верхній рядок: слайдер із відсотками на першому рядку, три мітки кнопок — на другому */
.tools .row { flex-wrap: wrap; justify-content: space-between; row-gap: 6px; }
.tools input[type="range"] { flex: 1 1 calc(100% - 64px); }
.tools #btnAuto, .tools #btnPaint, .tools #btnMode { order: 3; }

/* дитячий режим: цілі від 76 px, мітки лише іконками, без кнопок на нижньому краї */
.mode-kid { --top-h: 100px; }
.mode-kid .tools { top: calc(env(safe-area-inset-top, 0px) + 100px); }
.mode-kid .iconbtn { width: 76px; height: 76px; }
.mode-kid .iconbtn svg { width: 30px; height: 30px; }
.mode-kid .chip { min-width: 76px; min-height: 76px; padding: 0 12px; justify-content: center; }
.mode-kid .chip .ico { width: 30px; height: 30px; }
.mode-kid .lbl { display: none; }
.mode-kid .arrow { width: 72px; height: 72px; }
.mode-kid .arrow .ico { width: 34px; height: 34px; }
.mode-kid .row input[type="range"] { height: 44px; }
.mode-kid .btn, .mode-kid .age-btn { min-height: 76px; font-size: 19px; }
.mode-kid .dial { width: var(--dial); }
.mode-kid .hub-glyph svg { width: 62px; height: 62px; }
.mode-kid .hub-name, .mode-kid .hub-idx { display: none; }

/* підлітковий режим: стандартні цілі, мітки і жести */
.mode-teen .iconbtn { width: 48px; height: 48px; }

/* дорослий: тихіший, без авто-режиму */
.mode-adult .hub-glyph { display: none; }
.mode-adult .dial { width: var(--dial); }
.mode-adult .arrow { display: none; }

/* ---- v4: розпізнавання сміху ---- */
.laugh { position: relative; padding: 9px 11px; }
.laugh-dot {
  position: absolute;
  top: 5px;
  right: 5px;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--vermilion);
  opacity: 0;
  transform: scale(var(--lvl, 0.2));
  transition: transform 0.12s, opacity 0.2s;
  pointer-events: none;
}
.laugh[data-state="listening"] .laugh-dot { opacity: 0.55; }
.laugh[data-state="laughing"] .laugh-dot { opacity: 1; }
/* слайдер займає місце, що лишилося після кнопки сміху і відсотків */
.tools input[type="range"] { flex: 1 1 calc(100% - 120px); }
.mode-kid .tools input[type="range"] { flex: 1 1 calc(100% - 180px); }
.mode-kid .laugh { padding: 0 12px; }
[hidden]{display:none!important}

/* ---- v6: фото видно більше, колесо внизу, панелі напівпрозорі ---- */
:root { --dial: min(50vw, 190px); }
.mode-kid { --dial: min(56vw, 220px); }
.mode-adult { --dial: min(46vw, 180px); }
/* колесо опущене: третина ховається під нижній край, решта видна і тягнеться пальцем */
.bottom { bottom: calc(env(safe-area-inset-bottom, 0px) - var(--dial) * 0.35); opacity: 0.85; }
.tools { opacity: 0.85; }
.tools .chip::before, .tools .row.paint::before { opacity: 0.72; }
.ring { opacity: 0.9; }
/* планшет і ширші екрани: колесо збоку по центру, панель вгорі зліва — фото нічим не закрито */
@media (min-width: 700px) {
  .bottom { left: auto; right: 28px; bottom: 50%; transform: translateY(50%); }
  .tools { right: auto; width: min(460px, 56vw); top: calc(env(safe-area-inset-top, 0px) + 84px); }
}
