:root {
  --bg: #fff7ee;
  --grid: rgba(120, 150, 200, 0.13);
  --paper: #ffffff;
  --ink: #2b2240;
  --muted: #6f6680;
  --line: #eadfce;
  --primary: #ff5c8a;
  --primary-dark: #e2416f;
  --accent: #22b3a3;
  --accent-dark: #148f82;
  --danger: #e5484d;
  --ok: #2fa86b;
  --warn-bg: #fff1c9;
  --radius: 18px;
  --shadow: 0 4px 0 rgba(43, 34, 64, 0.22);
  --font: "Trebuchet MS", "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background-color: var(--bg);
  background-image:
    linear-gradient(var(--grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 22px 22px;
  color: var(--ink);
  font-family: var(--font);
  font-size: 18px;
  line-height: 1.35;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  min-height: 100dvh;
}
button, input, textarea, select { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; line-height: 1.15; }
a { color: inherit; }
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.loading { padding: 40px; text-align: center; font-size: 22px; }
.warn { background: var(--warn-bg); border: 2px solid #f0c75e; border-radius: 12px; padding: 10px 14px; margin: 8px 0; }
.hint { color: var(--muted); font-size: 15px; margin: 6px 0 0; }

/* ---------- кнопки ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 52px; padding: 0 18px;
  border: 3px solid var(--ink); border-radius: 16px;
  background: var(--paper); color: var(--ink);
  font-weight: 700; font-size: 18px; text-decoration: none;
  box-shadow: var(--shadow); cursor: pointer; user-select: none;
  transition: transform 0.08s, box-shadow 0.08s;
}
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(43, 34, 64, 0.22); }
.btn:disabled { opacity: 0.45; pointer-events: none; }
.btn.primary { background: var(--primary); color: #fff; }
.btn.accent { background: var(--accent); color: #fff; }
.btn.danger { background: var(--danger); color: #fff; }
.btn.ghost { background: rgba(255, 255, 255, 0.7); box-shadow: none; border-color: rgba(43, 34, 64, 0.35); }
.btn.big { min-height: 68px; font-size: 22px; padding: 0 26px; border-radius: 20px; }
.btn.small { min-height: 44px; font-size: 16px; }
.btn.round { width: 52px; padding: 0; border-radius: 50%; }
.danger-text { color: var(--danger); }
:focus-visible { outline: 4px solid #6c4cff; outline-offset: 3px; }

/* ---------- верхняя панель ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  background: rgba(255, 247, 238, 0.94); border-bottom: 2px solid var(--line);
  backdrop-filter: blur(6px);
}
.topbar-title { flex: 1; text-align: center; font-size: 24px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar-spacer { width: 110px; }
.topbar-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.name-field { flex: 1 1 220px; min-width: 0; }
.name-field input {
  width: 100%; min-height: 52px; padding: 0 14px;
  border: 3px solid var(--ink); border-radius: 14px; background: #fff;
  font-size: 22px; font-weight: 700;
}
.shake { animation: shake 0.5s; border-color: var(--danger) !important; }
@keyframes shake { 20%, 60% { transform: translateX(-8px); } 40%, 80% { transform: translateX(8px); } }

/* ---------- главная ---------- */
.home { max-width: 1100px; margin: 0 auto; padding: 18px 16px 40px; }
.hero { display: flex; align-items: center; gap: 18px; margin: 10px 0 18px; }
.hero h1 { font-size: 48px; letter-spacing: 0.5px; }
.hero p { margin: 4px 0 0; color: var(--muted); font-size: 20px; }
.logo { width: 96px; height: 96px; flex: none; transform: rotate(-6deg); }
.hero-actions { display: flex; gap: 14px; flex-wrap: wrap; margin-bottom: 26px; }
.hero-actions .btn { flex: 1 1 240px; }
.section-title { font-size: 26px; margin: 20px 0 12px; }
.section-title small { font-size: 16px; color: var(--muted); font-weight: 400; }
.empty { background: #fff; border: 3px dashed var(--line); border-radius: var(--radius); padding: 28px; text-align: center; font-size: 20px; }
.empty .btn { margin-top: 16px; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 16px; }
.card {
  background: #fff; border: 3px solid var(--ink); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 12px; display: flex; flex-direction: column; gap: 10px;
}
.card.draft { border-style: dashed; }
.card-preview { display: block; border-radius: 10px; overflow: hidden; }
.card-preview svg { width: 100%; height: auto; display: block; }
.card-body h3 { font-size: 21px; overflow-wrap: anywhere; }
.badge-ok, .badge-draft { display: inline-block; margin-top: 6px; padding: 3px 10px; border-radius: 999px; font-size: 15px; font-weight: 700; }
.badge-ok { background: #d8f5e5; color: #17663f; }
.badge-draft { background: var(--warn-bg); color: #7a5a00; }
.card-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.card-actions .btn { flex: 1 1 auto; min-height: 48px; padding: 0 12px; font-size: 17px; }
.backup { margin-top: 30px; background: #fff; border: 2px solid var(--line); border-radius: var(--radius); padding: 6px 18px 18px; }
.backup p { color: var(--muted); }
.row { display: flex; gap: 10px; flex-wrap: wrap; }
.cloud-box { margin-top: 18px; padding-top: 6px; border-top: 2px dashed var(--line); }
.cloud-box h3 { font-size: 21px; margin: 8px 0 4px; }
.cloud-box #cloud-status { font-weight: 700; margin: 6px 0; color: var(--ink); }
.text-input {
  width: 100%; min-height: 54px; padding: 0 14px; border: 3px solid var(--ink); border-radius: 14px;
  background: #fff; font-size: 22px; font-weight: 700;
}
.text-input:focus { border-color: var(--primary); outline: none; box-shadow: 0 0 0 4px rgba(255, 92, 138, 0.25); }
.modal .field { margin-top: 12px; }

/* ---------- редактор ---------- */
.editor { display: flex; flex-direction: column; min-height: 100dvh; }
.editor-body { flex: 1; display: grid; grid-template-columns: minmax(0, 1fr) minmax(360px, 440px); min-height: 0; }
.board-wrap { display: flex; align-items: center; justify-content: center; padding: 14px; min-width: 0; }
.board { width: min(100%, calc(100dvh - 170px)); aspect-ratio: 1; }
.board-svg { width: 100%; height: 100%; display: block; filter: drop-shadow(0 6px 0 rgba(43, 34, 64, 0.18)); }
.board-svg .tgt { cursor: pointer; }
.panel {
  background: rgba(255, 255, 255, 0.92); border-left: 2px solid var(--line);
  padding: 14px 16px 24px; overflow-y: auto; display: flex; flex-direction: column; gap: 14px;
  max-height: calc(100dvh - 140px);
}
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  position: relative; width: 46px; height: 46px; border-radius: 12px;
  border: 2px solid rgba(43, 34, 64, 0.4); background: #fff; font-weight: 700; font-size: 18px; cursor: pointer;
}
.chip.corner { font-size: 22px; color: var(--muted); }
.chip.done::after { content: ""; position: absolute; right: 3px; top: 3px; width: 9px; height: 9px; border-radius: 50%; background: var(--ok); }
.chip.on { background: var(--primary); color: #fff; border-color: var(--ink); }
.panel-head { display: flex; align-items: center; gap: 10px; }
.panel-head h2 { flex: 1; text-align: center; font-size: 26px; }
.field label { display: block; font-weight: 700; font-size: 19px; margin-bottom: 6px; }
.field label small { font-weight: 400; color: var(--muted); font-size: 15px; }
.field textarea {
  width: 100%; resize: none; padding: 12px 14px; min-height: 92px;
  border: 3px solid var(--ink); border-radius: 14px; background: #fffef9; font-size: 24px; font-weight: 700;
}
.field textarea:focus { border-color: var(--primary); outline: none; box-shadow: 0 0 0 4px rgba(255, 92, 138, 0.25); }
.field .hint { text-align: right; }
.btn-stickers {
  display: flex; align-items: center; gap: 12px; width: 100%; min-height: 68px; padding: 8px 16px; margin-bottom: 14px;
  border: 3px solid var(--ink); border-radius: 18px; background: #ffe27a; box-shadow: var(--shadow);
  font-size: 22px; font-weight: 800; cursor: pointer; text-align: left;
}
.btn-stickers:active { transform: translateY(3px); box-shadow: none; }
.btn-stickers.on { background: #fff0f5; border-color: var(--primary); }
.btn-stickers .star { font-size: 34px; line-height: 1; }
.btn-stickers .arrow { margin-left: auto; font-size: 34px; line-height: 1; }
.picker-backdrop {
  position: fixed; inset: 0; z-index: 25; background: rgba(43, 34, 64, 0.5);
  display: flex; align-items: stretch; justify-content: center; padding: 12px;
}
.picker {
  width: min(1100px, 100%); display: flex; flex-direction: column; min-height: 0;
  background: #fff; border: 4px solid var(--ink); border-radius: 22px; overflow: hidden;
}
.picker-head { display: flex; align-items: center; gap: 12px; padding: 12px 16px; border-bottom: 2px solid var(--line); }
.picker-head h2 { flex: 1; font-size: 26px; }
.picker-nav { padding: 10px 16px 8px; border-bottom: 2px solid var(--line); flex: none; }
.picker-body { flex: 1; overflow-y: auto; padding: 4px 16px 24px; overscroll-behavior: contain; }
.pk-sec h3 { font-size: 22px; margin: 16px 0 10px; }
.picker .sticker-grid { grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); }
.pal-title { margin: 4px 0 8px; font-weight: 700; font-size: 17px; }
.pal-title small { font-weight: 400; color: var(--muted); font-size: 15px; }
.swatches + .pal-title { margin-top: 16px; }
.swatches { display: grid; grid-template-columns: repeat(auto-fill, minmax(54px, 1fr)); gap: 10px; }
.swatch {
  position: relative; aspect-ratio: 1; border-radius: 50%; border: 3px solid var(--ink);
  background: var(--c); cursor: pointer; padding: 0;
}
.swatch.on { outline: 5px solid var(--primary); outline-offset: 3px; }
.swatch.custom {
  display: flex; align-items: center; justify-content: center; font-size: 24px; overflow: hidden;
  background: conic-gradient(#ff4d4d, #ffd23f, #8bd346, #3ec5f0, #6c4cff, #ff6fb5, #ff4d4d);
}
.swatch.custom span { background: #fff; border-radius: 50%; width: 60%; height: 60%; display: flex; align-items: center; justify-content: center; font-size: 18px; }
.swatch.custom input { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; cursor: pointer; border: 0; padding: 0; }
.themes { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 6px; scrollbar-width: thin; }
.theme {
  flex: none; display: flex; align-items: center; gap: 6px; min-height: 50px; padding: 0 12px;
  border: 2px solid rgba(43, 34, 64, 0.4); border-radius: 999px; background: #fff; font-weight: 700; font-size: 16px; cursor: pointer;
}
.theme.on { background: var(--ink); color: #fff; border-color: var(--ink); }
.theme-ico { font-size: 22px; }
.sticker-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(82px, 1fr)); gap: 10px; }
.sticker-cell { position: relative; }
.sticker-btn {
  width: 100%; aspect-ratio: 1; border: 2px solid rgba(43, 34, 64, 0.3); border-radius: 16px; background: #fff;
  padding: 6px; cursor: pointer; display: flex; flex-direction: column; align-items: center; justify-content: center;
}
.sticker-btn svg { width: 100%; height: 100%; }
.sticker-btn.on { border: 4px solid var(--primary); background: #fff0f5; }
.sticker-btn.upload { border-style: dashed; font-size: 30px; font-weight: 700; color: var(--muted); }
.sticker-btn.upload small { font-size: 13px; }
.del-sticker {
  position: absolute; top: -6px; right: -6px; width: 30px; height: 30px; border-radius: 50%; padding: 0;
  border: 2px solid #b9b0c4; background: #fff; color: #8a8199; font-size: 14px; font-weight: 700; cursor: pointer;
}
.status-bar { padding: 10px 16px; background: var(--warn-bg); border-top: 2px solid #f0c75e; font-weight: 700; font-size: 17px; }
.status-bar.ok { background: #d8f5e5; border-color: #8fd9b1; color: #17663f; }

/* ---------- выбор для игры ---------- */
.select-body { max-width: 1100px; margin: 0 auto; padding: 18px 16px 40px; }

/* ---------- игра ---------- */
.game { min-height: 100dvh; display: flex; flex-direction: column; }
.game-body { flex: 1; display: grid; grid-template-columns: minmax(0, 1fr) minmax(340px, 420px); min-height: 0; }
.stage { display: flex; align-items: center; justify-content: center; padding: 12px; min-width: 0; }
.stage-inner { position: relative; width: min(100%, calc(100dvh - 110px)); aspect-ratio: 1; }
.game-svg { width: 100%; height: 100%; display: block; overflow: visible; }
.game .face-wrap { transition: opacity 0.45s, transform 0.55s; transform-box: fill-box; transform-origin: center; }
.game[data-phase="choose"] .face-wrap { cursor: pointer; }
.game .face-wrap.dim { opacity: 0.2; }
.game .face-wrap.chosen { transform: scale(1.12); }
.game .badge { opacity: 0; transition: opacity 0.3s; }
.game[data-phase="choose"] .badge, .game[data-phase="reveal"] .badge { opacity: 1; }
.counter {
  position: absolute; top: -6px; right: -6px; min-width: 96px; height: 96px; padding: 0 10px;
  display: flex; align-items: center; justify-content: center; border-radius: 48px;
  background: #fff; border: 4px solid var(--ink); box-shadow: var(--shadow);
  font-size: 56px; font-weight: 800; pointer-events: none;
}
.counter:empty { display: none; }
.counter.pop { animation: pop 0.3s ease-out; }
@keyframes pop { 0% { transform: scale(0.6); } 70% { transform: scale(1.15); } 100% { transform: scale(1); } }
.game-side { padding: 18px 16px 28px; background: rgba(255, 255, 255, 0.9); border-left: 2px solid var(--line); overflow-y: auto; }
.step h2 { font-size: 28px; margin-bottom: 14px; display: flex; align-items: center; gap: 10px; }
.step-n { display: inline-flex; width: 42px; height: 42px; align-items: center; justify-content: center; border-radius: 50%; background: var(--primary); color: #fff; font-size: 24px; }
.big-hint { font-size: 20px; color: var(--muted); margin: 0 0 14px; }
.num-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 10px; }
.num {
  min-height: 64px; border: 3px solid var(--ink); border-radius: 16px; background: #fff;
  font-size: 28px; font-weight: 800; box-shadow: var(--shadow); cursor: pointer;
}
.num:active { transform: translateY(3px); box-shadow: none; }
.choice-list { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.choice {
  display: flex; align-items: center; gap: 12px; min-height: 76px; padding: 8px 14px;
  border: 3px solid var(--ink); border-radius: 18px; background: #fff; box-shadow: var(--shadow);
  font-size: 22px; font-weight: 800; cursor: pointer;
}
.reveal {
  position: fixed; inset: 0; z-index: 20; display: flex; align-items: center; justify-content: center;
  padding: 16px; background: rgba(43, 34, 64, 0.55);
}
.reveal-card {
  width: min(760px, 100%); max-height: 100%; overflow-y: auto;
  background: #fff; border: 4px solid var(--ink); border-radius: 26px; box-shadow: 0 10px 0 rgba(43, 34, 64, 0.3);
  padding: 22px 22px 24px; text-align: center;
}
.reveal-card.pop-in { animation: popin 0.45s cubic-bezier(.2, 1.4, .4, 1); }
@keyframes popin { from { transform: scale(0.4) rotate(-6deg); opacity: 0; } to { transform: none; opacity: 1; } }
.reveal-head { display: flex; align-items: center; justify-content: center; gap: 12px; font-size: 20px; color: var(--muted); font-weight: 700; }
.reveal-text { margin: 22px 0 26px; font-weight: 800; line-height: 1.15; overflow-wrap: anywhere; color: var(--ink); }
.reveal-text.xl { font-size: clamp(44px, 9vw, 96px); }
.reveal-text.l { font-size: clamp(36px, 6.5vw, 70px); }
.reveal-text.m { font-size: clamp(28px, 5vw, 52px); }
.reveal-actions { display: flex; gap: 12px; flex-wrap: wrap; justify-content: center; }
.reveal-actions .btn { flex: 1 1 200px; }

/* ---------- окна и подсказки ---------- */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 30; background: rgba(43, 34, 64, 0.5);
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.modal {
  width: min(600px, 100%); max-height: calc(100dvh - 32px); overflow-y: auto;
  background: #fff; border: 4px solid var(--ink); border-radius: 22px; padding: 20px; box-shadow: 0 8px 0 rgba(43, 34, 64, 0.3);
}
.modal h2 { font-size: 26px; margin-bottom: 10px; }
.modal-text { font-size: 19px; }
.modal-actions { display: flex; gap: 10px; flex-wrap: wrap; justify-content: flex-end; margin-top: 18px; }
.modal-actions .btn { flex: 1 1 auto; }
.pdf-box { display: flex; flex-direction: column; gap: 10px; }
.pdf-preview { min-height: 200px; display: flex; flex-direction: column; align-items: center; justify-content: center; background: #f4efe7; border-radius: 12px; padding: 10px; }
.pdf-preview img { max-width: 100%; max-height: 52dvh; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2); background: #fff; }
.pdf-box .row .btn { flex: 1 1 160px; }
.spinner { width: 46px; height: 46px; border: 5px solid #e3d8c8; border-top-color: var(--primary); border-radius: 50%; animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.toast {
  position: fixed; left: 50%; bottom: max(18px, env(safe-area-inset-bottom)); z-index: 40;
  transform: translate(-50%, 160%); max-width: calc(100% - 32px);
  background: var(--ink); color: #fff; padding: 14px 20px; border-radius: 16px; font-weight: 700; font-size: 18px;
  transition: transform 0.25s, visibility 0s 0.25s; pointer-events: none; text-align: center; visibility: hidden;
}
.toast.show { transform: translate(-50%, 0); visibility: visible; transition: transform 0.25s; }
.toast.error { background: var(--danger); }
.toast.ok { background: var(--ok); }

/* ---------- портретная ориентация ---------- */
@media (orientation: portrait), (max-width: 760px) {
  .editor-body, .game-body { display: flex; flex-direction: column; }
  .board { width: min(100%, 58dvh); }
  .panel { border-left: 0; border-top: 2px solid var(--line); max-height: none; overflow: visible; }
  .stage-inner { width: min(100%, 54dvh); }
  .game-side { border-left: 0; border-top: 2px solid var(--line); }
  .topbar-spacer { display: none; }
  .name-field { order: 3; flex-basis: 100%; }
  .topbar-actions { margin-left: auto; }
}
@media (max-width: 520px) {
  .hero h1 { font-size: 38px; }
  .logo { width: 72px; height: 72px; }
  .num-grid { grid-template-columns: repeat(4, 1fr); }
  .topbar-actions .btn { padding: 0 12px; font-size: 16px; }
}

/* ---------- меньше движения ---------- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
.cloud-author { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin: 8px 0; }
