/* ============================= StoryPass ============================= */

:root,
:root[data-theme="light"] {
  --bg: #f6f2ea;
  --bg-glow: rgba(124, 92, 255, 0.08);
  --surface: #ffffff;
  --surface-2: #f1ece2;
  --text: #26211b;
  --text-dim: #6f675c;
  --border: #e3dbcd;
  --accent: #6d4fe0;
  --accent-soft: rgba(109, 79, 224, 0.12);
  --accent-ink: #ffffff;
  --danger: #c2402f;
  --danger-soft: rgba(194, 64, 47, 0.12);
  --ok: #2e8f57;
  --shadow: 0 1px 2px rgba(38, 33, 27, 0.05), 0 8px 24px rgba(38, 33, 27, 0.06);
  --font-ui: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-story: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --font-mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, monospace;
}

:root[data-theme="dark"] {
  --bg: #14151c;
  --bg-glow: rgba(157, 134, 255, 0.09);
  --surface: #1d1f29;
  --surface-2: #272a37;
  --text: #edeaf6;
  --text-dim: #9b97ab;
  --border: #32354355;
  --accent: #9d86ff;
  --accent-soft: rgba(157, 134, 255, 0.16);
  --accent-ink: #171320;
  --danger: #ff7a68;
  --danger-soft: rgba(255, 122, 104, 0.14);
  --ok: #58c98a;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.35), 0 10px 30px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }

html, body { margin: 0; padding: 0; }

body {
  font-family: var(--font-ui);
  color: var(--text);
  background:
    radial-gradient(1000px 480px at 50% -120px, var(--bg-glow), transparent 70%),
    var(--bg);
  min-height: 100vh;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

#app {
  max-width: 640px;
  margin: 0 auto;
  padding: 48px 20px 96px;
}

.hidden { display: none !important; }

/* ----------------------------- Type & bits ----------------------------- */

h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 42px; letter-spacing: -0.02em; }
h2 { font-size: 32px; letter-spacing: -0.02em; }
h3 { font-size: 17px; }

.hint { color: var(--text-dim); font-size: 13px; margin: 8px 0 0; }
.hint.center, .center { text-align: center; }
.eyebrow {
  font-size: 12px; text-transform: uppercase; letter-spacing: 0.14em;
  color: var(--text-dim); margin: 0 0 6px;
}

.pill {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--accent-soft); color: var(--accent);
  font-size: 13px; font-weight: 600;
  padding: 4px 12px; border-radius: 999px; white-space: nowrap;
}
.pill.subtle { background: var(--surface-2); color: var(--text-dim); font-weight: 500; }

/* ------------------------------- Cards -------------------------------- */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 20px;
  margin-top: 16px;
}

.card-title-row {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 12px;
}

/* ------------------------------ Buttons ------------------------------- */

.btn {
  font: inherit; font-weight: 600; font-size: 14px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 9px 16px;
  cursor: pointer;
  transition: transform 0.06s ease, background 0.15s ease, border-color 0.15s ease;
}
.btn:hover { border-color: var(--accent); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; transform: none; }
.btn.primary {
  background: var(--accent); color: var(--accent-ink); border-color: transparent;
}
.btn.primary:hover:not(:disabled) { filter: brightness(1.07); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--text-dim); }
.btn.ghost:hover { color: var(--text); border-color: var(--border); }
.btn.big { font-size: 16px; padding: 13px 22px; width: 100%; }
.btn.small { font-size: 13px; padding: 6px 12px; }
.btn.danger { color: var(--danger); }
.btn.danger:hover { background: var(--danger-soft); border-color: var(--danger); }

:is(.btn, input, select, textarea, .theme-toggle):focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ------------------------------- Inputs -------------------------------- */

input, select, textarea {
  font: inherit; font-size: 16px;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 14px;
  width: 100%;
}
textarea { resize: none; line-height: 1.5; }
input::placeholder, textarea::placeholder { color: var(--text-dim); opacity: 0.7; }

.field-label {
  display: block; font-size: 13px; font-weight: 600;
  margin-bottom: 6px; color: var(--text);
}

/* ------------------------------- Home ---------------------------------- */

.brand { text-align: center; margin-bottom: 8px; }
.brand-mark { font-size: 56px; line-height: 1; margin-bottom: 8px; }
.tagline { color: var(--text-dim); margin: 8px 0 0; font-size: 16px; }

.home-card { display: flex; flex-direction: column; gap: 14px; margin-top: 28px; }
.home-card .field-label { margin-bottom: -8px; }

.divider {
  display: flex; align-items: center; gap: 12px;
  color: var(--text-dim); font-size: 12px; text-transform: uppercase; letter-spacing: 0.1em;
}
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--border); }

.invite-banner { text-align: center; margin-top: 26px; }
.invite-banner .invite-code { font-size: 30px; }
.invite-banner .hint { min-height: 20px; }

.join-row { display: flex; gap: 10px; }
.join-row input {
  font-family: var(--font-mono); text-transform: uppercase; letter-spacing: 0.25em;
  text-align: center; flex: 1;
}
.join-row .btn { flex: 0 0 auto; width: auto; }

/* ------------------------------- Lobby --------------------------------- */

.lobby-head { text-align: center; margin-bottom: 4px; }
.code-row { display: flex; flex-direction: column; align-items: center; gap: 12px; }
.code-display {
  font-family: var(--font-mono); font-size: 40px; font-weight: 700;
  letter-spacing: 0.3em; text-indent: 0.3em;
}
.code-actions { display: flex; gap: 10px; }

.player-list { list-style: none; margin: 0; padding: 0; }
.player-row {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 4px;
  border-bottom: 1px solid var(--border);
}
.player-row:last-child { border-bottom: none; }
.player-row.away { opacity: 0.55; }
.player-order { color: var(--text-dim); font-size: 13px; width: 18px; text-align: right; }
.avatar {
  width: 34px; height: 34px; border-radius: 50%;
  background: var(--accent-soft); color: var(--accent);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 15px; flex: 0 0 auto;
}
.player-name { font-weight: 600; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.player-name .you-tag { color: var(--text-dim); font-weight: 500; font-size: 13px; }
.badge {
  font-size: 11px; font-weight: 700; letter-spacing: 0.08em;
  padding: 3px 8px; border-radius: 6px;
  background: var(--accent-soft); color: var(--accent);
}
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); flex: 0 0 auto; }
.dot.off { background: var(--text-dim); opacity: 0.5; }
.kick-btn { margin-left: 4px; }

fieldset { border: none; margin: 0; padding: 0; }
fieldset:disabled { opacity: 0.72; }
fieldset:disabled .btn, fieldset:disabled select, fieldset:disabled input { cursor: not-allowed; }

.field { margin-top: 14px; min-width: 0; }
.field:first-child { margin-top: 0; }
.field-row { display: flex; gap: 20px; }
.field-row .field { flex: 1; }

.radio-card {
  display: flex; gap: 12px; align-items: flex-start;
  border: 1px solid var(--border); border-radius: 12px;
  padding: 12px 14px; margin-top: 8px; cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease;
}
.radio-card:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.radio-card input { width: auto; margin-top: 3px; accent-color: var(--accent); }
.radio-body strong { display: block; font-size: 14px; }
.radio-body small { color: var(--text-dim); font-size: 12.5px; }

.stepper { display: flex; align-items: center; gap: 12px; }
.stepper-val { font-weight: 700; font-size: 18px; min-width: 28px; text-align: center; }
.stepper .btn { width: 36px; }

.start-row { margin-top: 20px; text-align: center; }
.start-row .btn.ghost { margin-top: 14px; }

/* -------------------------------- Game --------------------------------- */

.game-top {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  margin-bottom: 10px;
}
.time-text {
  margin-left: auto;
  font-family: var(--font-mono); font-size: 22px; font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.time-text.low { color: var(--danger); animation: pulse 1s ease infinite; }

.timerbar {
  height: 6px; border-radius: 999px; overflow: hidden;
  background: var(--surface-2); margin-bottom: 14px;
}
.timerbar-fill {
  height: 100%; width: 100%;
  background: var(--accent); border-radius: 999px;
  transition: width 0.2s linear;
}
.timerbar-fill.low { background: var(--danger); }

.turn-strip { display: flex; flex-wrap: wrap; gap: 8px; margin: 4px 0 10px; }
.turn-chip {
  display: inline-flex; align-items: center; gap: 7px;
  font-size: 13px; font-weight: 600;
  border: 1px solid var(--border); border-radius: 999px;
  padding: 5px 12px; background: var(--surface);
}
.turn-chip .mini { font-size: 10.5px; font-weight: 700; letter-spacing: 0.08em; color: var(--text-dim); }
.turn-chip.active { background: var(--accent); border-color: transparent; color: var(--accent-ink); }
.turn-chip.active .mini { color: var(--accent-ink); opacity: 0.8; }
.turn-chip.next { border-color: var(--accent); color: var(--accent); }
.turn-chip.away { opacity: 0.5; border-style: dashed; }
.chip-kick {
  border: none; background: transparent; color: inherit;
  font: inherit; font-size: 11px; font-weight: 700;
  opacity: 0.55; cursor: pointer; padding: 0 0 0 2px; line-height: 1;
}
.chip-kick:hover { opacity: 1; color: var(--danger); }
.turn-chip.active .chip-kick:hover { color: inherit; }

.status { margin: 0 0 4px; font-size: 15px; min-height: 22px; }
.status .warn { color: var(--danger); font-weight: 600; }

.story-zone {
  min-height: 180px; max-height: 42vh; overflow-y: auto;
  display: flex; flex-direction: column;
}

.story-line {
  font-family: var(--font-story); font-size: 17px; line-height: 1.65;
  margin: 0 0 14px;
}
.story-line:last-child { margin-bottom: 0; }
.story-author {
  display: block;
  font-family: var(--font-ui); font-size: 11px; font-weight: 700;
  text-transform: uppercase; letter-spacing: 0.1em;
  color: var(--text-dim); margin-bottom: 1px;
}
.story-author .auto-mark { cursor: help; }

.story-empty, .waiting-view {
  margin: auto; text-align: center; color: var(--text-dim); padding: 24px 12px;
}
.waiting-view .big { font-size: 40px; margin-bottom: 10px; }
.waiting-view .who { color: var(--text); font-size: 17px; font-weight: 600; }
.waiting-view .note { font-size: 13px; margin-top: 10px; }

.typing-dots span {
  display: inline-block; width: 5px; height: 5px; margin: 0 1.5px;
  border-radius: 50%; background: var(--text-dim); vertical-align: middle;
  animation: blink 1.2s infinite both;
}
.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }

.prev-line-card { margin: auto 0; }
.prev-line-label {
  font-size: 11px; font-weight: 700; text-transform: uppercase;
  letter-spacing: 0.12em; color: var(--accent); margin-bottom: 8px;
}

.input-card { transition: border-color 0.2s ease, box-shadow 0.2s ease; }
.input-card.my-turn {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft), var(--shadow);
}
.input-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 8px; }
.char-count { font-size: 12px; color: var(--text-dim); font-variant-numeric: tabular-nums; }
.input-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 10px; }

/* ------------------------------- Reveal --------------------------------- */

.reveal-head { text-align: center; margin-bottom: 6px; }
.reveal-head h2 { font-family: var(--font-story); font-style: italic; }

.story-reveal {
  padding: 28px 26px; min-height: 140px;
  display: flex; flex-direction: column; justify-content: center;
}
.story-reveal .story-line { animation: riseIn 0.55s ease both; animation-delay: calc(var(--i) * 100ms); }

.row { display: flex; gap: 10px; margin-top: 18px; flex-wrap: wrap; }
.center-row { justify-content: center; }

/* ---------------------------- Global chrome ----------------------------- */

.theme-toggle {
  position: fixed; top: 16px; right: 16px; z-index: 50;
  width: 42px; height: 42px; border-radius: 50%;
  border: 1px solid var(--border); background: var(--surface);
  font-size: 19px; cursor: pointer; box-shadow: var(--shadow);
  display: flex; align-items: center; justify-content: center;
  transition: transform 0.1s ease;
}
.theme-toggle:hover { transform: scale(1.08); }

.conn-banner {
  position: fixed; top: 16px; left: 50%; transform: translateX(-50%); z-index: 60;
  background: var(--danger); color: #fff; font-size: 13px; font-weight: 600;
  padding: 7px 16px; border-radius: 999px; box-shadow: var(--shadow);
}

.toasts {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 70;
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  width: max-content; max-width: min(90vw, 480px);
}
.toast {
  background: var(--text); color: var(--bg);
  font-size: 14px; font-weight: 500;
  padding: 10px 18px; border-radius: 12px; box-shadow: var(--shadow);
  animation: riseIn 0.25s ease both;
  max-width: 100%;
}
.toast.error { background: var(--danger); color: #fff; }
.toast.fade { opacity: 0; transition: opacity 0.4s ease; }

/* ------------------------------ Animations ------------------------------ */

@keyframes riseIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.45; }
}
@keyframes blink {
  0%, 80%, 100% { opacity: 0.25; }
  40% { opacity: 1; }
}

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

/* ------------------------------ Responsive ------------------------------ */

@media (max-width: 480px) {
  #app { padding: 32px 14px 72px; }
  h1 { font-size: 34px; }
  .code-display { font-size: 30px; }
  .field-row { flex-direction: column; gap: 0; }
  .story-zone { max-height: 36vh; }
  .theme-toggle { top: 10px; right: 10px; }
}
