:root {
  color-scheme: dark;
  --bg: #12100e;
  --surface: #1c1916;
  --surface-2: #26221d;
  --line: #352f28;
  --text: #ebe3d5;
  --muted: #a1978a;
  --accent: #dca64d;
  --accent-ink: #1d1508;
  --secret: #b39cf0;
  --secret-bg: #1f1b2c;
  --secret-line: #3e3560;
  --danger: #e27d6d;
  --story: "Noto Serif", Georgia, "Times New Roman", serif;
  --ui: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 var(--ui);
  -webkit-tap-highlight-color: transparent;
}

button, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; }
button:disabled { opacity: .5; cursor: default; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.primary {
  min-height: 44px; padding: 0 18px;
  background: var(--accent); color: var(--accent-ink);
  border-radius: 10px; font-weight: 650;
}
.ghost {
  min-height: 44px; padding: 0 14px;
  border: 1px solid var(--line); border-radius: 10px; color: var(--text);
}
.link { color: var(--accent); font-weight: 600; min-height: 44px; padding: 0 4px; white-space: nowrap; }
.wide { width: 100%; }
.danger { color: var(--danger); }
.muted { color: var(--muted); margin: 0; }

/* Вхід */
.login {
  min-height: 100dvh; padding: 24px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center;
}
.login img { border-radius: 22px; }
.login h1 { font: 600 34px/1.2 var(--story); margin: 20px 0 6px; }
.tagline { font: italic 17px/1.5 var(--story); color: var(--muted); margin: 0 0 22px; }

/* Каркас */
.app { height: 100dvh; display: flex; flex-direction: column; max-width: 720px; margin: 0 auto; }
.top {
  display: flex; align-items: center; justify-content: space-between;
  padding: max(6px, env(safe-area-inset-top)) 6px 6px 16px;
  border-bottom: 1px solid var(--line);
}
.top h1 { font: 600 19px/1.2 var(--story); margin: 0 auto 0 0; letter-spacing: .02em; }
.icon-button { width: 44px; height: 44px; display: grid; place-items: center; }
.icon-button svg { fill: none; stroke: var(--muted); stroke-width: 1.8; stroke-linecap: round; }

.banner {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 2px 16px; background: var(--surface); border-bottom: 1px solid var(--line);
  font-size: 14px; color: var(--muted);
}

.view { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.feed, .chat-list, .settings { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding: 0 16px; }

/* Стрічка */
.older { display: block; margin: 16px auto 0; color: var(--muted); }
.scene { padding: 24px 0 8px; }
.scene + .scene { border-top: 1px solid var(--line); margin-top: 16px; }
/* Остання сцена займає щонайменше весь екран, щоб її початок можна було підняти до верху. */
.scene:last-child { min-height: var(--feed-height, 100%); }
.scene-title {
  font: 600 13px/1.3 var(--ui); letter-spacing: .08em; text-transform: uppercase;
  color: var(--accent); margin: 0 0 12px;
}
.scene-art {
  display: block; width: 100%; height: auto; margin: 0 0 16px;
  border-radius: 6px; background: var(--line);
}
.scene-art + .art-redraw, .scene-art + .art-note { display: block; margin: -10px 0 10px auto; }
.art-redraw { min-height: 36px; font-size: 14px; }
.art-note { font: 14px/36px var(--ui); color: var(--muted); text-align: right; }
.scene-body { font: 16px/1.62 var(--story); }
.library { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding: 0 16px 24px; }
.person { display: flex; gap: 14px; padding: 18px 0; border-bottom: 1px solid var(--line); align-items: flex-start; }
.person-portrait { flex: 0 0 96px; }
.person-portrait img { display: block; width: 96px; height: auto; border-radius: 6px; background: var(--line); }
.person-text { flex: 1; min-width: 0; }
.person-name { font: 600 19px/1.3 var(--story); margin: 0 0 6px; }
.person-body p { margin: 0 0 .7em; font: 15px/1.55 var(--story); white-space: pre-line; overflow-wrap: anywhere; }
.gallery-section { padding-top: 18px; }
.gallery-title { font: 600 13px/1.3 var(--ui); letter-spacing: .08em; text-transform: uppercase; color: var(--accent); margin: 0 0 10px; }
.gallery-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 12px; }
.gallery-item { margin: 0; min-width: 0; }
.gallery-item img { display: block; width: 100%; height: auto; aspect-ratio: 1; object-fit: cover; border-radius: 6px; background: var(--line); }
.gallery-item figcaption { font: 13px/1.35 var(--ui); color: var(--muted); margin-top: 5px; overflow-wrap: anywhere; }
.scene-map { margin: 4px 0 16px; }
.scene-map img { display: block; width: 100%; height: auto; border-radius: 6px; background: var(--line); }
.scene-map figcaption { font: 14px/1.4 var(--ui); color: var(--muted); margin-top: 6px; }
.scene-body p { margin: 0 0 .85em; white-space: pre-line; overflow-wrap: anywhere; }

.scene-secret {
  margin: 8px 0 14px; padding: 12px 14px;
  background: var(--secret-bg); border: 1px solid var(--secret-line); border-radius: 10px;
}
.scene-secret h3 {
  margin: 0 0 6px; font: 600 12px/1.3 var(--ui); letter-spacing: .08em; text-transform: uppercase; color: var(--secret);
}
.scene-secret p { margin: 0; font: 15px/1.55 var(--story); white-space: pre-line; overflow-wrap: anywhere; }

.moves { display: flex; flex-direction: column; gap: 8px; margin: 12px 0 8px; }
.move { padding: 10px 12px; background: var(--surface); border-left: 3px solid var(--line); border-radius: 0 8px 8px 0; }
.move.mine { border-left-color: var(--accent); }
.move.secret { background: var(--secret-bg); border-left-color: var(--secret); }
.move-who { font-size: 13px; font-weight: 650; color: var(--muted); display: flex; gap: 8px; align-items: baseline; }
.move-tag { color: var(--secret); font-weight: 500; }
.move-text { margin: 2px 0 0; white-space: pre-wrap; overflow-wrap: anywhere; }
.move-hidden { margin: 2px 0 0; color: var(--muted); font-style: italic; }
.move-hidden-part { margin: 8px 0 0; padding: 6px 10px; background: var(--secret-bg); border-left: 2px solid var(--secret); border-radius: 0 6px 6px 0; }
.move-hidden-part .move-tag { font-size: 13px; }
textarea.move-hidden-input { background: var(--secret-bg); border-color: var(--secret-line); }
textarea.move-hidden-input:focus { border-color: var(--secret); }

.question { padding: 10px 12px; background: var(--surface); border: 1px solid var(--line); border-radius: 8px; }
.question.secret { background: var(--secret-bg); border-color: var(--secret-line); }
.question-answer { margin: 8px 0 0; font: 15px/1.55 var(--story); white-space: pre-line; overflow-wrap: anywhere; }

.empty { color: var(--muted); text-align: center; font: italic 17px/1.5 var(--story); padding: 56px 12px; margin: 0; }

/* Хід */
.dock { border-top: 1px solid var(--line); background: var(--surface); padding: 10px 16px; }
.dock-status { display: flex; flex-direction: column; gap: 8px; }
.dock-status p { margin: 0; font-size: 15px; color: var(--muted); }
/* Кнопки панелі в один ряд: менше висоти, більше тексту сцени на екрані. */
.dock-row { display: flex; gap: 8px; }
.dock-row:not(:has(button:not([hidden]))) { display: none; }
.dock-row button { flex: 1; min-width: 0; padding: 0 8px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dock-row .main { flex: 2; }
.lock-bar { height: 5px; border-radius: 3px; background: var(--surface-2); overflow: hidden; }
.lock-bar span { display: block; height: 100%; width: 100%; background: var(--accent); transform-origin: left; }

.move-form { display: flex; flex-direction: column; gap: 8px; }
textarea {
  width: 100%; padding: 10px 12px; resize: none;
  background: var(--bg); border: 1px solid var(--line); border-radius: 10px; line-height: 1.45;
}
#move-input, #ask-input { max-height: 38dvh; }
.move-actions { display: flex; align-items: center; gap: 8px; }
.move-actions .primary { margin-left: auto; }
.secret-toggle { display: inline-flex; align-items: center; gap: 8px; min-height: 44px; padding-right: 4px; color: var(--muted); }
.secret-toggle input { width: 20px; height: 20px; margin: 0; accent-color: var(--secret); }
.secret-toggle:has(input:checked) { color: var(--secret); }
.secret-hint { margin: 0; font-size: 13px; color: var(--secret); }

/* Чат */
.chat-list { padding-top: 12px; padding-bottom: 12px; }
#messages { display: flex; flex-direction: column; gap: 6px; }
.message { max-width: 84%; align-self: flex-start; padding: 7px 11px; background: var(--surface); border-radius: 12px 12px 12px 3px; }
.message.mine { align-self: flex-end; background: var(--surface-2); border-radius: 12px 12px 3px 12px; }
.message-text { white-space: pre-wrap; overflow-wrap: anywhere; }
.message-time { font-size: 11px; color: var(--muted); text-align: right; margin-top: 1px; }
.chat-form { display: flex; gap: 8px; align-items: flex-end; padding: 10px 16px; border-top: 1px solid var(--line); background: var(--surface); }
#chat-input { flex: 1; max-height: 30dvh; }
.message { cursor: pointer; transition: box-shadow .3s; }
.message.flash { animation: flash 1.2s ease-out; }
@keyframes flash { from { box-shadow: 0 0 0 2px var(--accent); } to { box-shadow: 0 0 0 2px transparent; } }
.message-reply {
  margin: 1px 0 5px; padding: 3px 8px; border-left: 2px solid var(--accent); border-radius: 0 6px 6px 0;
  background: rgba(255, 255, 255, .04); font-size: 13px;
}
.message-reply strong { display: block; color: var(--accent); font-weight: 600; }
.message-reply span, .chat-reply-text span {
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; color: var(--muted);
  overflow-wrap: anywhere;
}
.chat-reply {
  display: flex; align-items: center; gap: 8px; padding: 6px 8px 6px 16px;
  border-top: 1px solid var(--line); background: var(--surface);
}
.chat-reply-text { flex: 1; min-width: 0; padding-left: 8px; border-left: 2px solid var(--accent); font-size: 13px; }
.chat-reply-text strong { display: block; color: var(--accent); font-weight: 600; }
.chat-reply-cancel { min-width: 44px; min-height: 44px; color: var(--muted); font-size: 18px; }
.chat-reply:not([hidden]) + .chat-form { border-top: 0; }
.message.selected { box-shadow: 0 0 0 2px var(--accent); }
.message-reply.deleted { color: var(--muted); font-style: italic; }
.chat-typing { margin: 0; padding: 4px 16px; font-size: 13px; color: var(--muted); font-style: italic; }
.chat-actions { display: flex; gap: 8px; padding: 8px 16px; border-top: 1px solid var(--line); background: var(--surface); }
.chat-actions button { flex: 1; min-width: 0; padding: 0 8px; white-space: nowrap; }

/* Налаштування */
.settings h2 { font: 600 22px/1.2 var(--story); margin: 22px 0 6px; }
.settings > .ghost { width: 100%; margin-top: 12px; }
.card {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  margin-top: 18px; padding: 14px; background: var(--surface); border-radius: 12px;
}
.card p { font-size: 14px; margin-top: 2px; }

/* Сейви й відкат */
.save-form input {
  flex: 1; min-width: 0; min-height: 44px; padding: 0 12px;
  background: var(--bg); color: var(--text); border: 1px solid var(--line); border-radius: 10px; font: inherit;
}
.save-hint { margin-top: 8px; }
.saves-head { font: 600 17px/1.2 var(--story); margin: 26px 0 0; }
.saves-head + div .card:first-child { margin-top: 10px; }
.card > div { min-width: 0; }
.card strong { overflow-wrap: anywhere; }
.restore-card {
  margin: 0 0 10px; padding: 12px 14px; border-radius: 12px;
  background: var(--surface-2); border: 1px solid var(--accent);
}
.settings .restore-card { margin-top: 16px; }
.restore-card p { margin: 0 0 10px; font-size: 15px; color: var(--text); }
.restore-actions { display: flex; gap: 8px; }
.restore-actions button { flex: 1; }
.scene-chapter {
  margin: 28px 0 0; font: 600 13px/1.3 var(--ui); letter-spacing: .08em; text-transform: uppercase; color: var(--accent);
}

/* Вкладки */
.tabs { display: flex; border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom); }
.tab { flex: 1; height: 50px; color: var(--muted); font-weight: 600; position: relative; }
.tab.active { color: var(--accent); }
.dot {
  display: inline-block; width: 8px; height: 8px; margin-left: 6px; vertical-align: 2px;
  border-radius: 50%; background: var(--accent);
}

.toast {
  position: fixed; left: 16px; right: 16px; bottom: 76px; z-index: 5; margin: 0 auto; max-width: 480px;
  padding: 12px 14px; border-radius: 10px; background: var(--surface-2); border: 1px solid var(--line);
  text-align: center;
}

/* Анкета героя */
.profile-form { display: flex; flex-direction: column; gap: 14px; padding-bottom: 24px; }
.profile-form h2 { margin-bottom: 0; }
.profile-form label { display: flex; flex-direction: column; gap: 6px; font-weight: 600; font-size: 15px; }
.profile-form input {
  min-height: 44px; padding: 0 12px; font: inherit; font-weight: 400;
  background: var(--bg); color: var(--text); border: 1px solid var(--line); border-radius: 10px;
}
.profile-form textarea { font-weight: 400; }

/* Картка PvP */
.consent-card { border-color: var(--secret); }
.consent-card strong { display: block; margin-bottom: 6px; }
.consent-card .consent-text { margin: 0 0 8px; font-size: 15px; color: var(--text); }
.consent-card .muted { margin: 0 0 10px; font-size: 13px; }

/* перегляд картинки на весь екран */
.viewer { position: fixed; inset: 0; z-index: 1000; background: rgba(0, 0, 0, 0.94); display: flex; align-items: center; justify-content: center; overflow: auto; touch-action: pinch-zoom pan-x pan-y; }
.viewer img { max-width: 100%; max-height: 100%; object-fit: contain; }
.viewer-close { position: fixed; top: calc(env(safe-area-inset-top) + 12px); right: 12px; width: 44px; height: 44px; border-radius: 50%; border: 1px solid var(--line); background: var(--surface); color: var(--text); font-size: 20px; }
body.viewer-open { overflow: hidden; }
/* індикатор майстра в шапці */
.gm-presence { display: inline-flex; align-items: center; gap: 6px; margin-right: auto; margin-left: 10px; font-size: 13px; color: var(--muted); white-space: nowrap; }
.gm-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--danger); flex: none; }
.gm-presence.online .gm-dot { background: #6cbf6a; }.top h1:has(+ .gm-presence:not([hidden])) { margin-right: 0; }
