:root {
  --paper: #f3f0e8;
  --paper-deep: #e8e3d8;
  --ink: #22292f;
  --muted: #6e7477;
  --blue: #75899a;
  --blue-deep: #263746;
  --blue-pale: #c7d0d6;
  --line: #cbc5b9;
  --warm: #a9845c;
  --danger: #795c55;
  --max-width: 1040px;
}

* { box-sizing: border-box; }

html {
  scroll-behavior: smooth;
  background: var(--blue-deep);
}

body {
  min-width: 300px;
  margin: 0;
  color: var(--ink);
  background: var(--paper);
  font-family: "Noto Serif SC", "Songti SC", "STSong", Georgia, serif;
  line-height: 1.8;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

button { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }
button:focus-visible, a:focus-visible {
  outline: 2px solid var(--warm);
  outline-offset: 4px;
}

.page-shell {
  width: min(100%, 1260px);
  min-height: 100vh;
  margin: 0 auto;
  background: var(--paper);
}

.topbar {
  min-height: 76px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 28px;
  padding: 14px clamp(20px, 4vw, 54px);
  border-bottom: 1px solid var(--line);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  color: var(--blue-deep);
  text-decoration: none;
}

.brand-mark {
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border: 1px solid var(--blue);
  color: var(--blue-deep);
}

.brand strong,
.brand small { display: block; }
.brand strong { font-size: 15px; letter-spacing: .18em; }
.brand small { margin-top: 1px; color: var(--muted); font-size: 10px; letter-spacing: .12em; }

.top-actions { display: flex; align-items: center; gap: 24px; }
.text-button {
  padding: 6px 0;
  border: 0;
  border-bottom: 1px solid transparent;
  color: var(--muted);
  background: transparent;
  cursor: pointer;
  font-size: 12px;
  letter-spacing: .08em;
  transition: color .2s ease, border-color .2s ease;
}
.text-button:hover { color: var(--blue-deep); border-color: currentColor; }
.text-button.danger:hover { color: var(--danger); }

.reading-progress { height: 2px; background: var(--paper-deep); }
.reading-progress span {
  display: block;
  width: 0;
  height: 100%;
  background: var(--blue);
  transition: width .35s ease;
}

.game {
  width: min(calc(100% - 40px), var(--max-width));
  margin: clamp(28px, 5vw, 62px) auto 80px;
  border: 1px solid var(--line);
  background: #f7f4ec;
}

.scene-visual {
  position: relative;
  aspect-ratio: 16 / 8.2;
  overflow: hidden;
  background: var(--blue-deep);
}

.scene-visual img {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: cover;
  opacity: 0;
  transition: opacity .55s ease, transform 1.2s ease;
}
.scene-visual img.is-loaded { opacity: .88; transform: scale(1.012); }

.visual-shade {
  position: absolute;
  inset: 0;
  background: linear-gradient(180deg, rgba(13, 21, 28, .05) 20%, rgba(13, 21, 28, .82) 100%);
  pointer-events: none;
}

.scene-heading {
  position: absolute;
  right: clamp(24px, 5vw, 58px);
  bottom: clamp(24px, 5vw, 48px);
  left: clamp(24px, 5vw, 58px);
  color: var(--paper);
}
.scene-index {
  margin: 0 0 8px;
  color: var(--blue-pale);
  font-size: 10px;
  letter-spacing: .24em;
}
.scene-heading h1 {
  max-width: 780px;
  margin: 0;
  font-size: clamp(28px, 5vw, 52px);
  font-weight: 500;
  line-height: 1.25;
  letter-spacing: .08em;
}

.save-indicator {
  position: absolute;
  top: 18px;
  right: 18px;
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 5px 9px;
  color: var(--blue-pale);
  background: rgba(24, 36, 45, .72);
  font-size: 10px;
  letter-spacing: .08em;
}
.save-indicator span { width: 5px; height: 5px; border-radius: 50%; background: #a7b99f; }
.save-indicator.is-saving span { animation: pulse .75s ease infinite alternate; }

.story-panel { padding: clamp(28px, 6vw, 64px); }
.story-meta {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 24px;
  min-height: 28px;
  padding-bottom: 24px;
  border-bottom: 1px solid var(--line);
}

.variables { display: flex; flex-wrap: wrap; gap: 8px; }
.variable {
  padding: 3px 9px;
  border: 1px solid var(--line);
  color: var(--muted);
  font-size: 11px;
  letter-spacing: .06em;
}
.variable strong { color: var(--blue-deep); font-weight: 600; }
.paragraph-counter { color: var(--muted); font-size: 11px; letter-spacing: .14em; white-space: nowrap; }

.story-copy {
  position: relative;
  min-height: 190px;
  display: grid;
  align-content: center;
  padding: 40px 0 28px;
}
.story-copy p {
  max-width: 790px;
  margin: 0;
  font-size: clamp(17px, 2vw, 20px);
  line-height: 2.05;
  letter-spacing: .035em;
  white-space: pre-wrap;
}
.story-copy p::first-letter { color: var(--blue-deep); font-size: 1.4em; }
.typing-cursor::after { content: "│"; color: var(--blue); animation: blink .7s steps(1) infinite; }
.skip-typing {
  justify-self: start;
  margin-top: 16px;
  padding: 0;
  border: 0;
  border-bottom: 1px solid var(--blue);
  color: var(--blue-deep);
  background: transparent;
  cursor: pointer;
  font-size: 11px;
}

.paragraph-actions {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  padding-top: 24px;
  border-top: 1px solid var(--line);
}

.primary-button,
.secondary-button {
  min-height: 46px;
  padding: 10px 20px;
  border: 1px solid var(--blue-deep);
  cursor: pointer;
  font-size: 13px;
  letter-spacing: .06em;
  transition: background-color .2s ease, color .2s ease, transform .2s ease;
}
.primary-button { color: var(--paper); background: var(--blue-deep); }
.secondary-button { color: var(--blue-deep); background: transparent; }
.primary-button:hover, .secondary-button:hover { transform: translateY(-2px); }
.secondary-button:hover { color: var(--paper); background: var(--blue-deep); }
.primary-button:disabled, .secondary-button:disabled { opacity: .42; cursor: default; transform: none; }

.choices-section { padding-top: 34px; }
.choice-heading span,
.ending-label,
.dialog-kicker,
.dialog-head p {
  color: var(--warm);
  font-size: 10px;
  letter-spacing: .2em;
  text-transform: uppercase;
}
.choice-heading h2,
.ending h2,
.dialog-head h2,
.confirm-dialog h2 {
  margin: 4px 0 24px;
  color: var(--blue-deep);
  font-size: clamp(23px, 3vw, 31px);
  font-weight: 500;
  letter-spacing: .06em;
}
.choices { display: grid; gap: 10px; }
.choice {
  width: 100%;
  display: grid;
  grid-template-columns: 38px minmax(0, 1fr) auto;
  align-items: center;
  gap: 14px;
  padding: 16px 18px;
  border: 1px solid var(--line);
  color: var(--ink);
  background: var(--paper);
  text-align: left;
  cursor: pointer;
  transition: border-color .2s ease, background-color .2s ease, transform .2s ease;
}
.choice:hover:not(:disabled) { border-color: var(--blue); background: var(--paper-deep); transform: translateX(4px); }
.choice-number { color: var(--blue); font-size: 11px; letter-spacing: .12em; }
.choice-text { font-size: 15px; }
.choice-arrow { color: var(--blue); }
.choice:disabled { color: #8d8b85; cursor: not-allowed; }
.choice-lock { grid-column: 2 / -1; color: var(--danger); font-size: 11px; }

.ending { padding: 34px 0 8px; text-align: center; }
.ending h2 { margin-bottom: 12px; font-size: clamp(30px, 5vw, 48px); }
.ending > p:not(.ending-label) { max-width: 620px; margin: 0 auto 30px; color: var(--muted); font-size: 16px; }

.footer {
  display: flex;
  justify-content: space-between;
  gap: 20px;
  padding: 0 clamp(20px, 4vw, 54px) 34px;
  color: var(--muted);
  font-size: 10px;
  letter-spacing: .08em;
}

dialog {
  width: min(calc(100% - 32px), 620px);
  max-height: min(720px, calc(100vh - 40px));
  padding: 0;
  border: 1px solid var(--line);
  color: var(--ink);
  background: var(--paper);
}
dialog::backdrop { background: rgba(23, 31, 37, .68); }
.dialog-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 20px;
  padding: 26px 28px 18px;
  border-bottom: 1px solid var(--line);
}
.dialog-head p, .dialog-head h2 { margin-top: 0; }
.dialog-head h2 { margin-bottom: 0; }
.icon-button {
  width: 38px;
  height: 38px;
  border: 1px solid var(--line);
  color: var(--blue-deep);
  background: transparent;
  cursor: pointer;
  font-size: 24px;
  line-height: 1;
}
.history-dialog ol { margin: 0; padding: 10px 28px 26px 58px; overflow-y: auto; }
.history-dialog li { padding: 14px 0; border-bottom: 1px solid var(--line); }
.history-dialog li strong { display: block; color: var(--blue-deep); font-size: 13px; }
.history-dialog li span { color: var(--muted); font-size: 12px; }
.empty-history { margin: 0; padding: 32px 28px; color: var(--muted); }

.confirm-dialog { padding: 30px; text-align: center; }
.confirm-dialog h2 { margin-bottom: 10px; }
.confirm-dialog > p:not(.dialog-kicker) { margin: 0; color: var(--muted); }
.confirm-actions { display: flex; justify-content: center; gap: 12px; margin-top: 28px; }

.noscript { padding: 20px; color: var(--paper); background: var(--danger); text-align: center; }

@keyframes blink { 50% { opacity: 0; } }
@keyframes pulse { to { opacity: .2; } }

@media (max-width: 720px) {
  .topbar { align-items: flex-start; padding: 12px 16px; }
  .brand small { display: none; }
  .top-actions { gap: 12px; padding-top: 6px; }
  .top-actions .text-button { font-size: 11px; }
  #typewriterButton { display: none; }
  .game { width: 100%; margin: 0; border-width: 0 0 1px; }
  .scene-visual { aspect-ratio: 4 / 3.25; }
  .scene-heading { right: 20px; bottom: 22px; left: 20px; }
  .save-indicator { top: 12px; right: 12px; }
  .story-panel { padding: 26px 20px 34px; }
  .story-meta { flex-direction: column; gap: 12px; }
  .story-copy { min-height: 230px; padding-top: 30px; }
  .story-copy p { font-size: 17px; line-height: 2; }
  .choice { grid-template-columns: 30px minmax(0, 1fr) auto; padding: 15px 13px; }
  .footer { flex-direction: column; gap: 2px; padding: 26px 20px; }
}

@media (max-width: 430px) {
  .brand-mark { width: 34px; height: 34px; }
  .brand strong { font-size: 13px; }
  .top-actions { gap: 10px; }
  .paragraph-actions { display: grid; grid-template-columns: 1fr 1.3fr; }
  .primary-button, .secondary-button { padding-inline: 12px; font-size: 12px; }
  .confirm-actions { flex-direction: column; }
}

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
