/* Сказитель. Тема по умолчанию: тёплая чернильная ночь. Каждая история перекрашивает переменные. */
:root {
  --bg: #121110;
  --surface: #1c1a18;
  --text: #ece6dc;
  --muted: #a59d91;
  --accent: #d9a441;
  --font-head: "Alegreya", Georgia, serif;
  --font-body: "Manrope", system-ui, sans-serif;
  --font-ui: "Manrope", system-ui, sans-serif;
  --r-sm: 6px;
  --r-md: 12px;
  --r-lg: 18px;
  --line: rgba(255, 255, 255, 0.08);
  --lift: rgba(255, 255, 255, 0.04);
  --ease-out: cubic-bezier(0.16, 1, 0.3, 1);
  --stage-h: clamp(150px, 36dvh, 380px);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overscroll-behavior: none; } /* в приложении без «резинки» и pull-to-refresh */
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 var(--font-ui);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  touch-action: manipulation;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- кнопки ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px; padding: 0 20px; border-radius: var(--r-md);
  border: 1px solid var(--line); background: var(--lift);
  font-weight: 600; transition-property: transform, background-color; transition-duration: 140ms;
}
.btn:active { transform: scale(0.97); }
.btn.primary { background: var(--accent); border-color: transparent; color: var(--bg); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); padding: 0 12px; }
.btn[disabled] { opacity: 0.5; pointer-events: none; }
.icon-btn {
  width: 40px; height: 40px; display: grid; place-items: center; border-radius: 50%;
  border: 0; background: rgba(0, 0, 0, 0.35); color: var(--text);
  transition-property: transform; transition-duration: 140ms;
}
.icon-btn:active { transform: scale(0.92); }
.icon-btn svg { width: 20px; height: 20px; }
@media (hover: hover) {
  .btn:hover { background: rgba(255, 255, 255, 0.07); }
  .btn.primary:hover { background: color-mix(in oklch, var(--accent) 88%, white); }
}

/* ---------- вход ---------- */
.auth {
  min-height: 100dvh; display: grid; align-content: center;
  padding: 32px max(20px, env(safe-area-inset-left)); max-width: 440px; margin: 0 auto;
}
.wordmark { font: 600 44px/1 var(--font-head); letter-spacing: -0.022em; margin: 0 0 10px; }
.wordmark span { color: var(--accent); }
.auth p.lead { color: var(--muted); margin: 0 0 32px; max-width: 32ch; }
.field { display: grid; gap: 6px; margin-bottom: 14px; }
.field label { font-size: 14px; color: var(--muted); }
.field input, .wizard textarea, .free textarea {
  width: 100%; min-height: 46px; padding: 11px 14px; border-radius: var(--r-md);
  border: 1px solid var(--line); background: var(--surface);
}
.field input:focus, .wizard textarea:focus, .free textarea:focus { outline: none; border-color: var(--accent); }
.auth .btn.primary { width: 100%; margin-top: 8px; }
.auth .switch { margin-top: 16px; text-align: left; }
.auth .switch .btn { padding-left: 0; }
.err { color: #f0a08a; font-size: 14px; min-height: 20px; margin: 6px 0 0; }

/* ---------- каталог ---------- */
.shell { max-width: 1120px; margin: 0 auto; padding: 18px max(16px, env(safe-area-inset-left)) 48px; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 18px; }
.topbar .wordmark { font-size: 30px; margin: 0; }
.who { display: flex; align-items: center; gap: 4px; color: var(--muted); font-size: 14px; }
.who .icon-btn { background: transparent; }
.install-hint {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; width: 100%;
  margin: 0 0 16px; padding: 12px 8px 12px 14px; border-radius: var(--r-md);
  background: color-mix(in oklch, var(--accent) 12%, var(--bg)); color: var(--text);
  font-size: 14px; line-height: 1.4; text-align: left; border: 0;
}
.install-hint b { display: block; font-weight: 700; }
.install-hint .icon-btn { background: transparent; flex: none; }
.install-hint.as-button { padding-right: 14px; transition-property: transform; transition-duration: 140ms; }
.install-hint.as-button:active { transform: scale(0.98); }
.install-hint .plus { font-size: 22px; color: var(--accent); }
.tabs { display: flex; gap: 4px; margin: 0 -8px 20px; overflow-x: auto; scrollbar-width: none; }
.tab {
  position: relative; border: 0; background: none; padding: 10px 12px 14px; white-space: nowrap;
  color: var(--muted); font-weight: 600;
}
.tab[aria-selected="true"] { color: var(--text); }
.tab[aria-selected="true"]::after {
  content: ""; position: absolute; left: 50%; bottom: 4px; width: 5px; height: 5px; margin-left: -2.5px;
  border-radius: 50%; background: var(--accent);
}
.shelf { display: grid; gap: 14px; grid-template-columns: 1fr; }
@media (min-width: 640px) { .shelf { grid-template-columns: 1fr 1fr; } }
@media (min-width: 1000px) { .shelf { grid-template-columns: repeat(3, 1fr); } }

/* админка */
a.icon-btn { text-decoration: none; }
.admin .topbar { justify-content: flex-start; }
.admin .topbar .icon-btn { background: transparent; flex: none; }
.admin .topbar .btn { margin-left: auto; text-decoration: none; color: var(--text); padding: 0 14px; font-size: 14px; }
.admin-title { margin: 0; font: 400 26px/1.1 var(--font-head); letter-spacing: -0.012em; }
.tiles { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
@media (min-width: 640px) { .tiles { grid-template-columns: repeat(4, 1fr); } }
.tile { padding: 14px; border-radius: var(--r-md); background: var(--surface); }
.tile small, .adm-row small { display: block; color: var(--muted); font-size: 13px; }
.tile b { display: block; margin-top: 4px; font-size: 22px; font-variant-numeric: tabular-nums; }
.adm-note { color: var(--muted); font-size: 14px; margin: 10px 0 0; }
.adm-h { margin: 28px 0 10px; font: 400 21px/1.2 var(--font-head); }
.adm-list { display: grid; gap: 1px; border-radius: var(--r-md); overflow: hidden; background: var(--line); }
.adm-row { display: flex; justify-content: space-between; gap: 12px; padding: 12px 14px; background: var(--surface); }
.adm-row > div { min-width: 0; }
.adm-row b { font-weight: 600; overflow-wrap: anywhere; }
.adm-row .sum { text-align: right; flex: none; font-variant-numeric: tabular-nums; }

/* обложка в цветах своей истории */
.poster {
  position: relative; display: block; width: 100%; aspect-ratio: 4 / 3.4; padding: 0; border: 0;
  border-radius: var(--r-lg); overflow: hidden; text-align: left;
  background: radial-gradient(120% 90% at 80% 0%, color-mix(in oklch, var(--p-accent) 28%, var(--p-bg)), var(--p-bg));
  color: var(--p-text); outline: 1px solid rgba(255, 255, 255, 0.08); outline-offset: -1px;
  transition-property: transform; transition-duration: 160ms;
}
.poster:active { transform: scale(0.985); }
.poster img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.poster::after {
  content: ""; position: absolute; inset: 0;
  background: linear-gradient(to bottom, transparent 30%, color-mix(in srgb, var(--p-bg) 82%, transparent) 62%, var(--p-bg));
}
.poster .meta { position: absolute; left: 0; right: 0; bottom: 0; z-index: 1; padding: 18px 18px 16px; }
.poster .genre { display: flex; align-items: center; gap: 8px; font: 600 12px/1.3 var(--font-ui); color: var(--p-muted); }
.poster .genre::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: var(--p-accent); flex: none; }
.poster h3 { margin: 6px 0 6px; font: 400 26px/1.08 var(--p-head); letter-spacing: -0.012em; }
.poster p {
  margin: 0; font-size: 14px; line-height: 1.4; color: var(--p-muted);
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.poster .badge {
  position: absolute; top: 12px; right: 12px; z-index: 1; padding: 5px 10px; border-radius: 999px;
  background: rgba(0, 0, 0, 0.55); font: 600 12px/1.2 var(--font-ui); color: var(--p-text);
}
.create-cta {
  display: flex; align-items: center; justify-content: space-between; gap: 16px; width: 100%;
  padding: 18px 18px; margin-bottom: 14px; border-radius: var(--r-lg); border: 1px dashed rgba(255, 255, 255, 0.18);
  background: transparent; text-align: left;
}
.create-cta b { display: block; font: 400 22px/1.15 var(--font-head); }
.create-cta span { color: var(--muted); font-size: 14px; }
.create-cta .plus { font-size: 28px; color: var(--accent); line-height: 1; }
.empty { color: var(--muted); padding: 32px 0; max-width: 40ch; }

.runs { display: grid; gap: 8px; }
.run-row {
  display: grid; grid-template-columns: 64px 1fr auto; gap: 14px; align-items: center; width: 100%;
  padding: 10px; border: 0; border-radius: var(--r-md); background: var(--lift); text-align: left;
}
.run-row .thumb { width: 64px; height: 64px; border-radius: var(--r-sm); object-fit: cover; background: var(--surface); }
.run-row b { display: block; font: 400 19px/1.2 var(--font-head); }
.run-row small { color: var(--muted); font-size: 13px; }
.bar { height: 3px; border-radius: 3px; background: var(--line); margin-top: 8px; overflow: hidden; }
.bar i { display: block; height: 100%; background: var(--accent); transform-origin: left; }

/* ---------- лист выбора режима ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 20; background: rgba(0, 0, 0, 0.6);
  display: grid; align-items: end; justify-items: center; overscroll-behavior: contain;
  animation: fade 200ms var(--ease-out);
}
.sheet {
  width: 100%; max-width: 560px; max-height: 92dvh; overflow-y: auto; overscroll-behavior: contain;
  background: var(--p-bg, var(--surface)); color: var(--p-text, var(--text));
  border-radius: var(--r-lg) var(--r-lg) 0 0; padding: 22px 20px calc(20px + env(safe-area-inset-bottom));
  animation: rise 280ms var(--ease-out);
}
@media (min-width: 640px) { .overlay { align-items: center; } .sheet { border-radius: var(--r-lg); } }
.sheet h2 { margin: 0 0 8px; font: 400 30px/1.1 var(--p-head, var(--font-head)); letter-spacing: -0.012em; }
.sheet .facts { color: var(--p-muted, var(--muted)); font-size: 14px; margin: 0 0 14px; }
.sheet .logline { margin: 0 0 20px; line-height: 1.55; }
.modes { display: grid; gap: 10px; margin-bottom: 12px; }
.mode {
  display: grid; gap: 2px; width: 100%; padding: 14px 16px; text-align: left; border-radius: var(--r-md);
  border: 1px solid rgba(255, 255, 255, 0.1); background: rgba(255, 255, 255, 0.04);
  transition-property: transform; transition-duration: 140ms;
}
.mode:active { transform: scale(0.98); }
.mode b { font-weight: 700; }
.mode span { font-size: 14px; color: var(--p-muted, var(--muted)); }
.mode.continue { background: var(--p-accent, var(--accent)); color: var(--p-bg, var(--bg)); border-color: transparent; }
.mode.continue span { color: inherit; opacity: 0.8; }
.sheet-label { font-size: 13px; color: var(--p-muted, var(--muted)); margin: 16px 0 8px; }

/* ---------- анкета ---------- */
.wizard {
  min-height: 100dvh; max-width: 600px; margin: 0 auto; display: grid; grid-template-rows: auto 1fr auto;
  padding: 16px max(16px, env(safe-area-inset-left)) calc(16px + env(safe-area-inset-bottom));
}
.wizard header { display: flex; align-items: center; gap: 12px; color: var(--muted); font-size: 14px; }
.wizard header .bar { flex: 1; margin: 0; }
.wizard .icon-btn { background: transparent; }
.wizard .q { align-self: center; padding: 24px 0; }
.wizard h2 { font: 400 32px/1.1 var(--font-head); letter-spacing: -0.012em; margin: 0 0 10px; }
.wizard .hint { color: var(--muted); margin: 0 0 20px; max-width: 46ch; }
.wizard textarea { min-height: 132px; resize: none; line-height: 1.5; }
.chips { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.chip {
  min-height: 52px; padding: 10px 14px; border-radius: var(--r-md); text-align: left;
  border: 1px solid var(--line); background: var(--lift); font-weight: 600;
  transition-property: transform; transition-duration: 140ms;
}
.chip:active { transform: scale(0.97); }
.chip[aria-pressed="true"] { border-color: var(--accent); background: color-mix(in oklch, var(--accent) 16%, transparent); }
.wizard footer { display: flex; justify-content: space-between; gap: 12px; }
.count { text-align: right; font-size: 13px; color: var(--muted); margin-top: 6px; font-variant-numeric: tabular-nums; }
.brewing { display: grid; gap: 14px; align-content: center; min-height: 60dvh; }
.brewing p { color: var(--muted); max-width: 36ch; margin: 0; }

/* ---------- экран игры ---------- */
/* Телефон: шапка, под ней кадр 4:3 целиком (своя неизменная область), ниже текст.
   После последней страницы — отдельный экран выбора с той же картинкой. */
.play {
  position: relative; height: 100dvh; display: grid; overflow: hidden;
  grid-template-areas: "hud" "stage" "text" "actions";
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  background: var(--bg);
}
.play.choose-screen { grid-template-rows: auto auto 0 minmax(0, 1fr); }
.play.choose-screen .text { visibility: hidden; }
.play.choose-screen .actions { align-content: start; padding-top: 16px; overflow-y: auto; }
.choose-title { margin: 0 2px 4px; font: 400 22px/1.2 var(--font-head); letter-spacing: -0.012em; }
.reread { justify-self: start; margin-top: 2px; padding-left: 2px; gap: 4px; }
.reread svg { width: 16px; height: 16px; }

.stage {
  grid-area: stage; position: relative; overflow: hidden; background: var(--surface);
  width: 100%; aspect-ratio: 4 / 3; max-height: 46dvh;
}
@media (max-height: 620px) {
  .stage { max-height: 40dvh; }
  .hud { padding-top: max(4px, env(safe-area-inset-top)); padding-bottom: 4px; }
  .play .choice { min-height: 40px; padding: 7px 10px; font-size: 14px; }
  .play.choose-screen .actions { padding-top: 10px; gap: 5px; }
  .choose-title { font-size: 19px; }
}
.stage img {
  position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain;
  opacity: 0; filter: blur(3px); transition-property: opacity, filter; transition-duration: 700ms;
  transition-timing-function: var(--ease-out);
}
.stage img.on { opacity: 1; filter: blur(0); }
/* если экран низкий и кадр не влез по высоте — поля заполняет размытая копия картинки */
.stage img.backdrop { object-fit: cover; opacity: 1; filter: blur(24px) brightness(0.5); transform: scale(1.15); transition: none; }
.stage::before { content: none; }
.stage .pending {
  position: absolute; z-index: 2; right: 14px; bottom: 12px; font-size: 12px; color: var(--muted);
  display: flex; align-items: center; gap: 6px; opacity: 0; transition-property: opacity; transition-duration: 300ms;
}
.stage .pending.on { opacity: 1; }
.stage .pending::before {
  content: ""; width: 6px; height: 6px; border-radius: 50%; background: var(--accent);
  animation: pulse 1.4s ease-in-out infinite;
}

.hud {
  grid-area: hud; position: relative; z-index: 3;
  display: flex; align-items: center; gap: 10px;
  padding: max(8px, env(safe-area-inset-top)) 10px 8px;
}
.hud .icon-btn { background: transparent; }
.hud .title { flex: 1; min-width: 0; }
.hud .title b { display: block; font: 400 17px/1.2 var(--font-head); white-space: nowrap; overflow: hidden; }
.hud .title small { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.progress { position: absolute; left: 0; right: 0; top: 0; height: 2px; z-index: 4; background: rgba(255, 255, 255, 0.08); }
.progress i { display: block; height: 100%; background: var(--accent); transform-origin: left; transition: transform 600ms var(--ease-out); }

.text { grid-area: text; position: relative; padding: 14px 22px 0; min-height: 0; display: grid; grid-template-rows: minmax(0, 1fr) auto; }
.page {
  margin: 0; overflow: hidden; font-family: var(--font-body); font-size: 17px; line-height: 1.58;
  white-space: pre-line; transition-property: opacity; transition-duration: 160ms; cursor: pointer;
}
.page.fading { opacity: 0; }
.page.wait { opacity: 0.45; }
.page em.input { display: block; font-style: normal; color: var(--accent); font-size: 0.86em; margin-bottom: 6px; }
.pager { display: flex; align-items: center; gap: 6px; height: 22px; }
.pager i { width: 6px; height: 6px; border-radius: 50%; background: var(--line); }
.pager i.on { background: var(--accent); }

.actions { grid-area: actions; padding: 8px 14px calc(12px + env(safe-area-inset-bottom)); display: grid; gap: 6px; }
.choice {
  display: grid; grid-template-columns: 22px 1fr; gap: 10px; align-items: baseline; width: 100%;
  min-height: 44px; padding: 9px 12px; border-radius: var(--r-md); text-align: left;
  border: 1px solid var(--line); background: var(--lift); font-family: var(--font-body); font-size: 14.5px; line-height: 1.3;
  transition-property: transform, background-color; transition-duration: 130ms;
}
.choice:active { transform: scale(0.98); background: rgba(255, 255, 255, 0.08); }
.choice .n { font: 400 17px/1 var(--font-head); color: var(--accent); }
@media (hover: hover) { .choice:hover { background: rgba(255, 255, 255, 0.07); } }
.next { justify-self: end; }
.next svg { width: 18px; height: 18px; }
.free { display: grid; grid-template-columns: 1fr 48px; gap: 8px; align-items: end; }
.free textarea { min-height: 48px; max-height: 120px; resize: none; line-height: 1.4; font-family: var(--font-body); }
.free .send { width: 48px; height: 48px; padding: 0; border-radius: var(--r-md); }
.free .send svg { width: 20px; height: 20px; }
.writing { display: flex; align-items: center; gap: 10px; min-height: 46px; color: var(--muted); font-size: 15px; }
.writing .dots { display: inline-flex; gap: 4px; }
.writing .dots i { width: 5px; height: 5px; border-radius: 50%; background: var(--accent); animation: pulse 1.2s ease-in-out infinite; }
.writing .dots i:nth-child(2) { animation-delay: 0.15s; }
.writing .dots i:nth-child(3) { animation-delay: 0.3s; }
.ending { display: grid; gap: 6px; padding: 4px 2px 6px; }
.ending small { color: var(--muted); }
.ending b { font: 400 24px/1.15 var(--font-head); color: var(--accent); }

/* журнал */
.journal {
  position: fixed; inset: 0 0 0 auto; z-index: 30; width: min(440px, 100%); background: var(--surface);
  display: grid; grid-template-rows: auto 1fr; animation: slide 260ms var(--ease-out);
  padding-bottom: env(safe-area-inset-bottom);
}
.journal header { display: flex; align-items: center; justify-content: space-between; padding: 14px 12px 10px 20px; }
.journal header b { font: 400 22px/1.2 var(--font-head); }
.journal .icon-btn { background: transparent; }
.journal .entries { overflow-y: auto; padding: 0 20px 32px; overscroll-behavior: contain; }
.entry { padding: 14px 0; border-top: 1px solid var(--line); font-family: var(--font-body); line-height: 1.55; }
.entry .you { color: var(--accent); font-size: 14px; margin-bottom: 6px; }
.entry p { margin: 0 0 8px; }

/* десктоп: кадр слева на всю высоту, шапка поверх кадра, текст и варианты справа (без отдельного экрана выбора) */
@media (min-width: 900px) and (min-aspect-ratio: 5/4) {
  .play, .play.choose-screen {
    grid-template-areas: "stage text" "stage actions";
    grid-template-columns: minmax(0, 1.2fr) minmax(400px, 580px);
    grid-template-rows: minmax(0, 1fr) auto;
  }
  .hud { grid-area: stage; align-self: start; padding: 10px 12px; }
  .hud .icon-btn { background: rgba(0, 0, 0, 0.35); }
  .hud .title small { color: rgba(255, 255, 255, 0.75); }
  .stage { aspect-ratio: auto; max-height: none; height: 100%; }
  .stage img { object-fit: cover; }
  .stage img.backdrop { display: none; }
  .stage::before {
    content: ""; position: absolute; inset: 0; z-index: 1; pointer-events: none;
    background:
      linear-gradient(to bottom, rgba(0, 0, 0, 0.5), transparent 22%),
      linear-gradient(to right, transparent 70%, var(--bg));
  }
  .text { padding: 48px 44px 0 32px; }
  .page { font-size: 19px; }
  .actions { padding: 12px 44px 32px 32px; }
}

@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { transform: translateY(24px); opacity: 0; } }
@keyframes slide { from { transform: translateX(32px); opacity: 0; } }
@keyframes pulse { 50% { opacity: 0.25; } }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 1ms !important; animation-iteration-count: 1 !important; transition-duration: 1ms !important; }
}
