/* Celadon-Paper & Tactile Key UI inspired by Way of Xian + 1in8B */
:root {
  --paper: #e9efea;
  --surface: #f7faf7;
  --ink: #1d2724;
  --muted: #5a6b65;
  --line: #c7d4cd;
  --jade: #2c6b59;
  --cinnabar: #a8352a;
  --gold: #9a6e14;
  --tie: #2c63b0;
  --up: #257a3e;
  --down: #b03328;

  --btn-bg: #ffffff;
  --btn-border: #6f877d;
  --btn-edge: #a3b5ac;
  --btn-hover: #e3eee8;

  --font-display: "Noto Serif Thai", Georgia, serif;
  --font-body: "IBM Plex Sans Thai", -apple-system, BlinkMacSystemFont, sans-serif;
}

html[data-theme="dark"] {
  --paper: #111715;
  --surface: #18211e;
  --ink: #e4ede9;
  --muted: #93a8a0;
  --line: #2a3934;
  --jade: #388871;
  --cinnabar: #d14c3e;
  --gold: #e0ad38;
  --tie: #5b93e0;
  --up: #41b866;
  --down: #e35d50;

  --btn-bg: #202b27;
  --btn-border: #3a4e46;
  --btn-edge: #0c1210;
  --btn-hover: #283631;
}

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

html, body {
  height: 100%;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  cursor: pointer;
}

/* Tactile Key Button (Way of Xian style) */
.key-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 14px;
  border-radius: 8px;
  background: var(--btn-bg);
  border: 1.5px solid var(--btn-border);
  box-shadow: 0 3px 0 var(--btn-edge);
  font-weight: 600;
  font-size: 0.88rem;
  transition: transform 0.08s ease, box-shadow 0.08s ease, background 0.12s;
}

.key-btn:hover:not(:disabled) { background: var(--btn-hover); }
.key-btn:active:not(:disabled) {
  transform: translateY(2px);
  box-shadow: 0 1px 0 var(--btn-edge);
}
.key-btn:disabled { opacity: 0.45; cursor: not-allowed; }

.key-btn.sm { padding: 6px 10px; font-size: 0.78rem; }
.key-btn.full { width: 100%; padding: 12px 16px; font-size: 0.96rem; }

.key-btn.primary {
  background: var(--jade);
  color: #ffffff;
  border-color: #1c4a3d;
  box-shadow: 0 3px 0 #15382e;
}
.key-btn.primary:hover:not(:disabled) { background: #23594a; }

.key-btn.active {
  background: var(--ink);
  color: var(--surface);
  border-color: var(--ink);
  box-shadow: 0 2px 0 #000000;
}

.key-btn.playing {
  background: var(--cinnabar);
  color: #ffffff;
  border-color: #7d241c;
  box-shadow: 0 3px 0 #591812;
}

/* App Shell */
.shell {
  max-width: 1080px;
  margin: 0 auto;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: 0 12px 12px;
}

.topbar {
  flex: none;
  height: 46px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--line);
  margin-bottom: 10px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 1.05rem;
}

.brand-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--cinnabar);
}

.seed-tag {
  font-size: 0.74rem;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 6px;
  background: var(--surface);
  border: 1px solid var(--line);
  color: var(--muted);
}

/* Paper Card */
.paper-card {
  background: var(--surface);
  border: 1.5px solid var(--line);
  border-radius: 12px;
  padding: 16px;
}

/* Start Screen */
.start-wrap {
  flex: 1;
  display: grid;
  place-items: center;
  overflow-y: auto;
}

.start-card {
  width: min(460px, 100%);
  padding: 24px 22px;
}

.kicker {
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  color: var(--cinnabar);
  margin-bottom: 6px;
}

.display-title {
  font-family: var(--font-display);
  font-size: 1.75rem;
  margin-bottom: 8px;
}

.lead {
  font-size: 0.9rem;
  color: var(--muted);
  margin-bottom: 18px;
}

.partner-banner {
  background: rgba(168, 53, 42, 0.08);
  border: 1.5px dashed var(--cinnabar);
  border-radius: 10px;
  padding: 12px;
  font-size: 0.85rem;
  margin-bottom: 16px;
}

.field-lbl {
  display: block;
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--muted);
  margin-bottom: 6px;
}

.text-input {
  width: 100%;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1.5px solid var(--line);
  background: var(--btn-bg);
  color: var(--ink);
  font: inherit;
  margin-bottom: 14px;
}

.mode-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 16px;
}

.mode-btn {
  flex-direction: column;
  align-items: flex-start;
  text-align: left;
  padding: 10px;
}
.mode-btn b { font-size: 0.84rem; }
.mode-btn span { font-size: 0.72rem; font-weight: 400; color: var(--muted); }
.mode-btn.active span { color: var(--line); }

/* 2-Column Game Grid (Way of Xian Architecture) */
.game-grid {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
}

@media (min-width: 860px) {
  .game-grid {
    grid-template-columns: 380px 1fr;
    gap: 16px;
  }
}

.col-left {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* 8-bit Pixel Canvas Stage */
.stage-frame {
  position: relative;
  border-radius: 12px;
  border: 2px solid var(--ink);
  overflow: hidden;
  background: #141c2b;
  aspect-ratio: 16 / 9;
  box-shadow: 0 4px 0 var(--line);
}

.pixel-canvas {
  width: 100%;
  height: 100%;
  display: block;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

.stage-overlay {
  position: absolute;
  top: 10px;
  left: 10px;
  right: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  pointer-events: none;
}

.stage-pill {
  font-size: 0.74rem;
  font-weight: 700;
  padding: 3px 9px;
  border-radius: 6px;
  background: rgba(29, 39, 36, 0.82);
  color: #f6f9f6;
  backdrop-filter: blur(4px);
  line-height: 1.3;
}

/* Stats Card */
.stats-card { padding: 14px; }

.char-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 12px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--line);
}

.char-title { font-family: var(--font-display); font-size: 1.15rem; }
.char-sub { font-size: 0.8rem; color: var(--muted); }

.wealth-box { text-align: right; }
.wealth-lbl { display: block; font-size: 0.68rem; color: var(--muted); }
.wealth-val { font-size: 1.05rem; color: var(--jade); font-variant-numeric: tabular-nums; }

/* Life Progression Bar */
.life-prog-wrap {
  margin-bottom: 12px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--line);
}
.life-prog-labels {
  display: flex;
  justify-content: space-between;
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 5px;
}
.life-prog-track {
  position: relative;
  height: 8px;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 999px;
  overflow: visible;
}
.life-prog-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--jade), var(--gold));
  border-radius: 999px;
  transition: width 0.25s ease;
}
.life-prog-le-mark {
  position: absolute;
  top: -3px;
  bottom: -3px;
  width: 2.5px;
  background: var(--cinnabar);
  border-radius: 2px;
}

.meters-grid {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.meter-row {
  display: grid;
  grid-template-columns: 58px 1fr 28px;
  align-items: center;
  gap: 8px;
  font-size: 0.78rem;
}

.m-lbl { font-weight: 600; color: var(--muted); }
.m-track { height: 8px; background: var(--paper); border-radius: 999px; border: 1px solid var(--line); overflow: hidden; }
.m-fill { height: 100%; transition: width 0.25s ease; }
.m-fill.health { background: var(--cinnabar); }
.m-fill.happy { background: var(--gold); }
.m-fill.smart { background: var(--tie); }
.m-num { text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; }

/* Right Column */
.col-right {
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1.5px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
}

.tabs {
  flex: none;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  background: var(--paper);
  border-bottom: 1.5px solid var(--line);
}

.tab-btn {
  padding: 10px 6px;
  font-size: 0.82rem;
  font-weight: 700;
  color: var(--muted);
  border-bottom: 3px solid transparent;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
}

.tab-btn.active {
  color: var(--jade);
  background: var(--surface);
  border-bottom-color: var(--jade);
}

.cnt {
  font-size: 0.68rem;
  padding: 0 6px;
  border-radius: 999px;
  background: var(--line);
  color: var(--ink);
}

.chronicle-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 14px;
}

/* Chronicle Entries */
.year-entry {
  padding: 10px 0;
  border-bottom: 1px dashed var(--line);
}
.year-entry:last-child { border-bottom: 0; }
.year-entry.crossroad {
  background: rgba(168, 53, 42, 0.06);
  margin: 0 -14px;
  padding: 10px 14px;
  border-left: 4px solid var(--cinnabar);
}

.year-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
}

.y-age { font-weight: 700; font-size: 0.8rem; color: var(--jade); }
.y-arc { font-size: 0.72rem; color: var(--muted); background: var(--paper); padding: 1px 7px; border-radius: 4px; }
.y-arc.cross { background: var(--cinnabar); color: #ffffff; }

.beat-ul { list-style: none; display: flex; flex-direction: column; gap: 4px; }
.beat-li { font-size: 0.9rem; padding-left: 10px; border-left: 2px solid var(--line); }
.beat-li.major { border-left-color: var(--jade); font-weight: 600; }
.b-delta { font-size: 0.76rem; font-weight: 700; margin-left: 6px; }
.b-delta.pos { color: var(--up); }
.b-delta.neg { color: var(--down); }

/* Decision Card */
.decision-box {
  margin-top: 12px;
  padding: 14px;
  border-radius: 10px;
  background: var(--btn-bg);
  border: 2px solid var(--jade);
}
.decision-box.cross { border-color: var(--cinnabar); }
.decision-box h3 { font-family: var(--font-display); font-size: 1.05rem; margin-bottom: 4px; }
.decision-box p { font-size: 0.88rem; color: var(--muted); }

/* Dock Controls */
.dock {
  flex: none;
  padding: 10px 14px;
  background: var(--paper);
  border-top: 1.5px solid var(--line);
}

.dock-choices {
  display: flex;
  flex-direction: column;
  gap: 7px;
}

.choice-key {
  width: 100%;
  justify-content: space-between;
  text-align: left;
  padding: 10px 12px;
}
.choice-sub { display: block; font-size: 0.74rem; font-weight: 400; color: var(--muted); }

.dock-controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.speed-group { display: flex; gap: 4px; }

/* NPC & Actions Cards */
.sub-list { display: flex; flex-direction: column; gap: 8px; }
.sub-card {
  background: var(--btn-bg);
  border: 1.5px solid var(--line);
  border-radius: 10px;
  padding: 11px 12px;
}
.npc-head { display: flex; justify-content: space-between; margin-bottom: 8px; }
.npc-btns { display: flex; flex-wrap: wrap; gap: 6px; }
.act-row { display: flex; justify-content: space-between; align-items: center; gap: 10px; }

/* Reveal Box */
.reveal-card {
  margin-top: 12px;
  background: var(--btn-bg);
  border: 2px solid var(--ink);
  border-radius: 12px;
  padding: 16px;
}
.polaroid-preview {
  display: block;
  width: 100%;
  max-width: 320px;
  margin: 12px auto 0;
  border-radius: 8px;
  border: 1.5px solid var(--line);
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.22);
}
.reveal-pair {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin: 12px 0;
}
.reveal-col {
  background: var(--paper);
  padding: 10px;
  border-radius: 8px;
  text-align: center;
  font-size: 0.84rem;
}
.cross-hit {
  background: rgba(168, 53, 42, 0.08);
  border-left: 3px solid var(--cinnabar);
  padding: 8px 10px;
  border-radius: 6px;
  font-size: 0.84rem;
  margin-top: 8px;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 6px;
}

/* Wildcard Input Box */
.wildcard-box {
  display: flex;
  gap: 6px;
  margin-top: 2px;
}
.wildcard-box .text-input {
  margin-bottom: 0;
  padding: 8px 10px;
  font-size: 0.85rem;
}

/* Epilogue Narrative */
.epilogue-box {
  background: var(--paper);
  border-left: 3px solid var(--gold);
  border-radius: 8px;
  padding: 12px 14px;
  margin: 10px 0 12px;
  font-size: 0.9rem;
  line-height: 1.65;
}

/* Modals (Album & AI Settings) */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(10, 16, 14, 0.58);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.modal-card {
  width: 100%;
  max-width: 520px;
  max-height: 85vh;
  overflow-y: auto;
  padding: 18px;
}
.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}
.modal-head h2 {
  font-family: var(--font-display);
  font-size: 1.15rem;
}
.album-stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  margin-bottom: 12px;
}
.album-stat {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 6px;
  text-align: center;
}
.album-stat span { display: block; font-size: 0.7rem; color: var(--muted); }
.album-stat strong { font-size: 0.95rem; color: var(--jade); }
.album-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.album-item {
  background: var(--btn-bg);
  border: 1.5px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 0.84rem;
}
.album-item-head {
  display: flex;
  justify-content: space-between;
  font-weight: 700;
  margin-bottom: 3px;
}
