:root {
  --bg: #1c1612;
  --bg-2: #2a211a;
  --line: #4a3b2e;
  --ink-on-dark: #f2e7d3;
  --muted-on-dark: #b9a88f;
  --paper: #f1e4c9;
  --paper-2: #e6d5b2;
  --ink: #2b2118;
  --muted: #6d5a45;
  --gold: #e0b04a;
  --danger: #c8503f;
  --good: #7fb56a;
  --serif: 'Noto Serif Thai', 'Sarabun', serif;
  --sans: 'Sarabun', system-ui, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink-on-dark);
  font: 16px/1.45 var(--sans);
  touch-action: manipulation;
}
main { max-width: 480px; margin: 0 auto; padding: 12px 16px 120px; }
h1, h2, h3 { font-family: var(--serif); margin: 0; line-height: 1.25; }
button, input { font: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; }
:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; }

.city-paris { --c: #5b8bd3; }
.city-london { --c: #d0555a; }
.city-rome { --c: #a273cf; }
.city-vienna { --c: #4bb08d; }
.city-madrid { --c: #e0963c; }

.btn {
  display: block;
  width: 100%;
  min-height: 50px;
  padding: 10px 16px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--bg-2);
  color: var(--ink-on-dark);
  font-weight: 700;
}
.btn.primary { background: var(--gold); border-color: var(--gold); color: #2b1d08; }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn:disabled { opacity: .45; }
.btn-small { min-height: 44px; padding: 6px 14px; border-radius: 8px; width: auto; display: inline-block; }

.label { display: block; font-size: 14px; color: var(--muted-on-dark); margin: 0 0 4px; }
.input {
  width: 100%;
  min-height: 50px;
  padding: 10px 14px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: #120e0b;
  color: var(--ink-on-dark);
  font-size: 18px;
}
.input.code { text-transform: uppercase; letter-spacing: .25em; font-weight: 700; text-align: center; }

.section-title {
  font-family: var(--serif);
  font-size: 15px;
  color: var(--gold);
  margin: 18px 0 8px;
}
.note { font-size: 14px; color: var(--muted-on-dark); margin: 6px 0; }

/* หน้าแรก */
.hero { text-align: center; padding: 34px 0 22px; }
.hero h1 { font-size: 40px; font-weight: 800; color: var(--gold); }
.hero p { margin: 8px 0 0; color: var(--muted-on-dark); }
.stack { display: grid; gap: 12px; }
.divider { text-align: center; color: var(--muted-on-dark); font-size: 14px; margin: 10px 0; }

/* ห้องรอ */
.room-code {
  text-align: center;
  border: 1px dashed var(--line);
  border-radius: 12px;
  padding: 14px;
  margin: 8px 0 12px;
}
.room-code strong { display: block; font-size: 38px; letter-spacing: .2em; color: var(--gold); padding-left: .2em; }
.city-pick {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  min-height: 58px;
  padding: 8px 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  border-left: 6px solid var(--c);
  background: var(--bg-2);
  color: var(--ink-on-dark);
  text-align: left;
  margin-bottom: 8px;
}
.city-pick.mine { background: #3a2d20; border-color: var(--gold); border-left-color: var(--c); }
.city-pick b { font-family: var(--serif); font-size: 17px; }
.city-pick span { margin-left: auto; font-size: 14px; color: var(--muted-on-dark); text-align: right; }

/* ส่วนหัวในเกม */
.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
  font-weight: 700;
}
.topbar .timer { margin-left: auto; font-variant-numeric: tabular-nums; }
.topbar .timer.low { color: var(--danger); }
.topbar .code { color: var(--muted-on-dark); font-weight: 400; font-size: 14px; }
.offline-banner { background: var(--danger); color: #fff; border-radius: 8px; padding: 6px 12px; font-size: 14px; margin-bottom: 8px; }

/* การ์ด */
.central {
  background: linear-gradient(160deg, var(--paper), var(--paper-2));
  color: var(--ink);
  border-radius: 14px;
  padding: 12px 14px;
  border: 2px solid var(--gold);
  display: grid;
  grid-template-columns: 52px 1fr;
  column-gap: 12px;
  align-items: center;
}
.central.fresh { animation: flip .55s ease-out; }
.central .big-icon { font-size: 38px; text-align: center; grid-row: span 3; }
.central small { color: var(--muted); font-weight: 700; font-size: 12px; }
.central h2 { font-size: 18px; }
.central p { margin: 2px 0 0; font-size: 14px; }
.central .fx { font-weight: 700; }

.me-bar {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  margin: 10px 0 0;
}
.stat {
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  text-align: center;
  padding: 6px 2px;
}
.stat b { display: block; font-size: 20px; line-height: 1.15; font-variant-numeric: tabular-nums; }
.stat small { color: var(--muted-on-dark); font-size: 12px; }

.seat {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 2px 8px;
  padding: 7px 10px;
  border-radius: 8px;
  background: var(--bg-2);
  border-left: 5px solid var(--c);
  margin-bottom: 5px;
  font-size: 14px;
}
.seat.me { outline: 1px solid var(--gold); }
.seat.dead { opacity: .5; }
.seat .who { font-weight: 700; min-width: 0; overflow-wrap: anywhere; }
.seat .state { color: var(--muted-on-dark); font-size: 13px; text-align: right; white-space: nowrap; }
.seat .state.done { color: var(--good); }
.seat .res { font-variant-numeric: tabular-nums; color: var(--muted-on-dark); white-space: nowrap; }
.seat .towns { font-size: 13px; color: var(--ink-on-dark); }
.tag-off { color: var(--danger); font-weight: 400; }

.hand { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.card {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 2px;
  text-align: left;
  min-height: 158px;
  padding: 10px;
  border-radius: 12px;
  border: 2px solid var(--paper-2);
  background: linear-gradient(170deg, var(--paper), var(--paper-2));
  color: var(--ink);
}
.card .kind { font-size: 12px; font-weight: 700; color: var(--muted); }
.card .icon { font-size: 28px; line-height: 1.2; }
.card h3 { font-size: 16px; }
.card p { margin: 0; font-size: 13px; line-height: 1.3; color: #4b3b2b; }
.card .fx { margin-top: auto; padding-top: 4px; font-weight: 700; font-size: 13px; }
.card.type-attack { border-color: #b8574a; }
.card.type-develop { border-color: #5a8a55; }
.card.type-trade { border-color: #b78d2e; }
.card.type-recruit { border-color: #4f719c; }
.card:disabled { opacity: .5; filter: grayscale(.6); }
.card.selected { outline: 4px solid var(--gold); outline-offset: 1px; transform: translateY(-2px); }
.card .badge {
  position: absolute;
  top: -8px;
  right: -6px;
  min-width: 28px;
  height: 28px;
  border-radius: 14px;
  background: var(--gold);
  color: #2b1d08;
  font-weight: 800;
  text-align: center;
  line-height: 28px;
}
.card .order { font-weight: 700; font-size: 13px; color: #8a2f22; }

.footer {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 6;
  background: var(--bg);
  border-top: 1px solid var(--line);
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
}
.footer > div { max-width: 448px; margin: 0 auto; display: grid; gap: 6px; }
.waiting { text-align: center; font-size: 14px; color: var(--muted-on-dark); }

/* หน้าต่างเลือกเป้าหมาย */
.sheet-back { position: fixed; inset: 0; background: rgba(0, 0, 0, .6); z-index: 10; display: flex; align-items: flex-end; justify-content: center; }
.sheet {
  width: 100%;
  max-width: 480px;
  max-height: 88vh;
  overflow-y: auto;
  background: var(--bg-2);
  border-radius: 16px 16px 0 0;
  padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
}
.sheet h2 { font-size: 19px; margin-bottom: 4px; }
.target {
  display: flex;
  align-items: center;
  width: 100%;
  min-height: 54px;
  gap: 10px;
  margin-bottom: 6px;
  padding: 6px 12px;
  border-radius: 10px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-left: 6px solid var(--c);
  color: var(--ink-on-dark);
  text-align: left;
}
.target.on { border-color: var(--gold); border-left-color: var(--c); background: #3a2d20; }
.target .def { margin-left: auto; font-size: 14px; color: var(--muted-on-dark); white-space: nowrap; }
.stepper { display: grid; grid-template-columns: 60px 1fr 60px; align-items: center; gap: 8px; margin: 8px 0 14px; }
.stepper button { min-height: 54px; font-size: 26px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); color: var(--ink-on-dark); }
.stepper button:disabled { opacity: .35; }
.stepper output { text-align: center; font-size: 26px; font-weight: 800; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

/* ผลรอบ */
.report-title { font-size: 22px; color: var(--gold); margin: 4px 0; }
.line { padding: 7px 10px; border-radius: 8px; background: var(--bg-2); margin-bottom: 5px; font-size: 14.5px; line-height: 1.4; overflow-wrap: anywhere; border-left: 4px solid var(--line); }
.line.good { border-left-color: var(--good); }
.line.bad { border-left-color: var(--danger); }
.anim .line, .anim .section-title { animation: rise .35s ease-out backwards; }

/* จบเกม */
.winner { text-align: center; padding: 18px 0 8px; }
.winner .crown { font-size: 54px; }
.winner h1 { font-size: 26px; color: var(--gold); }
.rank { display: grid; grid-template-columns: 30px 1fr auto; gap: 8px; align-items: center; padding: 10px 12px; background: var(--bg-2); border-left: 5px solid var(--c); border-radius: 8px; margin-bottom: 6px; }
.rank b { font-size: 18px; }

#toast {
  position: fixed;
  left: 50%;
  top: 14px;
  transform: translate(-50%, calc(-100% - 20px));
  width: max-content;
  max-width: 88vw;
  z-index: 40;
  padding: 9px 16px;
  border-radius: 10px;
  background: linear-gradient(180deg, #3a2c1f, #1f1710);
  border: 1px solid var(--gold);
  color: #fff;
  font-weight: 600;
  font-size: 14.5px;
  box-shadow: 0 6px 18px rgba(0, 0, 0, .55);
  transition: transform .22s cubic-bezier(.2, .9, .3, 1);
}
#toast.bad { background: linear-gradient(180deg, #c8503f, #8f3126); border-color: #e5836f; }
#toast.show { transform: translate(-50%, 0); }

@keyframes flip { from { transform: perspective(700px) rotateX(70deg); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes rise { from { transform: translateY(8px); opacity: 0; } to { transform: none; opacity: 1; } }
@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
