/* Layout: one-screen game table — HUD strip on top, pannable map in the middle, hand tray in the thumb zone. */
:root {
  --table: #15202b;      /* indigo cloth the map lies on */
  --table-hi: #1e2b39;
  --paper: #e7e4d0;      /* washi tile */
  --paper-dim: #c9c5ab;
  --ink: #2a2520;        /* sumi roads */
  --shu: #e0472d;        /* vermilion: finished roads */
  --kin: #f2b94b;        /* gold: where you can build, score */
  --fg: #ecead9;
  --muted: #8e9aa7;
  --dead: #9c9584;       /* roads that ran off the map */
  --font-display: "Yuji Syuku", "Hiragino Mincho ProN", "Yu Mincho", serif;
  --font-ui: system-ui, -apple-system, "Hiragino Sans", "Noto Sans JP", "Yu Gothic", sans-serif;
  color-scheme: dark;
}

*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; }
[hidden] { display: none !important; }
body {
  margin: 0;
  background: var(--table);
  color: var(--fg);
  font-family: var(--font-ui);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

#app {
  height: 100%;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto 1fr auto;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
}

/* ---------- HUD ---------- */
.hud {
  display: flex;
  align-items: center;
  gap: 14px;
  padding-block: 8px;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  background: var(--table-hi);
  border-bottom: 1px solid #ffffff12;
}
.brand-box { margin-right: auto; display: grid; gap: 3px; min-width: 0; }
.brand {
  font-family: var(--font-display);
  font-size: 22px;
  letter-spacing: 0.06em;
  line-height: 1;
}
.game-label { font-size: 10px; color: var(--muted); letter-spacing: 0.08em; white-space: nowrap; }
.stat { display: flex; flex-direction: column; align-items: flex-end; line-height: 1.05; }
.stat .label { font-size: 10px; color: var(--muted); letter-spacing: 0.12em; }
.stat .val { font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat.score .val { color: var(--kin); font-size: 26px; }
.stat.finale .val, .stat.finale .label { color: var(--shu); }
.stat .label { white-space: nowrap; }
.icon-btn {
  appearance: none;
  border: 1px solid #ffffff24;
  background: transparent;
  color: var(--fg);
  width: 34px; height: 34px;
  border-radius: 50%;
  font-size: 16px;
  touch-action: manipulation;
}

/* ---------- Board ---------- */
#boardWrap { position: relative; min-height: 0; }
#board { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; display: block; }
.cell-hint { fill: #f2b94b26; stroke: var(--kin); stroke-width: 3.5; stroke-dasharray: 10 8; }
.cell-hint.loose { fill: none; stroke: var(--muted); stroke-width: 2.5; stroke-dasharray: 3 7; }
.map-frame { fill: #0e161f; stroke: #ffffff1a; stroke-width: 2; }
.slot { fill: #ffffff08; stroke: #ffffff10; stroke-width: 1.5; }
.slot-lm { fill: #f2b94b14; stroke: #f2b94b55; stroke-width: 2; }
.lm-icon { stroke: var(--kin); fill: none; stroke-width: 3.5; stroke-linecap: round; }
.lm-icon circle { fill: var(--kin); stroke: none; }
.slot-lm.hi { fill: #f2b94b26; stroke: var(--kin); }
.lm-stars { font: 700 15px var(--font-ui); fill: var(--kin); text-anchor: end; }
.lm-badge-v { font: 800 15px var(--font-ui); fill: var(--kin); paint-order: stroke; stroke: var(--ink); stroke-width: 4px; }
.lm-name { font: 600 15px var(--font-ui); fill: var(--kin); text-anchor: middle; letter-spacing: 0.1em; }
.lm-badge { fill: var(--kin); stroke: var(--ink); stroke-width: 2.5; }
.lm-badge-icon { stroke: var(--ink); fill: none; stroke-width: 4; stroke-linecap: round; }
.lm-badge-icon circle { fill: var(--ink); stroke: none; }
.badge.used .lm-badge { fill: var(--paper-dim); stroke: var(--paper-dim); }
.badge.used .lm-badge-icon { opacity: 0.45; }
.lm-slot.fresh { animation: pop 900ms ease-out; transform-box: fill-box; transform-origin: center; }
@keyframes pop { 0% { transform: scale(0.4); opacity: 0; } 60% { transform: scale(1.12); opacity: 1; } 100% { transform: scale(1); } }
.ghost-frame { fill: none; stroke: var(--kin); stroke-width: 5; }
.ghost-frame.warn { stroke: var(--shu); stroke-dasharray: 12 6; }
.ghost { opacity: 0.92; }
.road-tag rect { fill: #0d151de6; stroke: #ffffff30; stroke-width: 1.5; }
.road-tag text { font: 700 19px var(--font-ui); fill: var(--fg); text-anchor: middle; }
.road-tag.lm rect { stroke: var(--kin); }
.road-tag.lm text { fill: var(--kin); }
.gain-label { font: 700 30px var(--font-ui); fill: var(--kin); paint-order: stroke; stroke: var(--table); stroke-width: 6px; text-anchor: middle; }

.tile-bg { fill: var(--paper); stroke: var(--paper-dim); stroke-width: 1.5; }
.tuft { stroke: var(--paper-dim); stroke-width: 2.4; fill: none; stroke-linecap: round; }
.road { stroke: var(--ink); stroke-width: 15; fill: none; }
.road-dash { stroke: var(--paper); stroke-width: 2; fill: none; stroke-dasharray: 7 7; }
.done .road { stroke: var(--shu); }
.dead .road { stroke: var(--dead); }
.dead .road-dash { stroke-dasharray: none; stroke: var(--paper-dim); }
.town { fill: var(--paper); stroke: var(--ink); stroke-width: 5; }
.town-dot { fill: var(--ink); }
.town.done-town { stroke: var(--shu); }
.town-dot.done-town { fill: var(--shu); }

.flash { animation: flash 900ms ease-out; }
@keyframes flash { 0% { filter: brightness(1.6); } 100% { filter: none; } }
.float { animation: floatUp 1100ms ease-out forwards; }
@keyframes floatUp { 0% { opacity: 0; transform: translateY(10px); } 15% { opacity: 1; } 100% { opacity: 0; transform: translateY(-50px); } }

.stamp {
  position: absolute;
  left: 50%; top: 42%;
  display: grid;
  justify-items: center;
  gap: 6px;
  pointer-events: none;
  transform: translate(-50%, -50%) rotate(-8deg);
}
.stamp-main {
  font-family: var(--font-display);
  font-size: 54px;
  line-height: 1;
  color: var(--shu);
  border: 5px solid var(--shu);
  border-radius: 12px;
  padding: 10px 16px 6px;
  background: #e7e4d0e6;
  letter-spacing: 0.1em;
  box-shadow: 0 0 0 3px #e7e4d0e6;
}
.stamp-sub { white-space: nowrap; font: 800 18px var(--font-ui); color: var(--kin); paint-order: stroke; -webkit-text-stroke: 5px var(--table); }
.stamp.go { animation: stampIn 1900ms cubic-bezier(.2,.9,.3,1.2) forwards; }
@keyframes stampIn {
  0% { opacity: 0; transform: translate(-50%, -50%) rotate(-8deg) scale(1.8); }
  14% { opacity: 1; transform: translate(-50%, -50%) rotate(-8deg) scale(0.96); }
  22% { transform: translate(-50%, -50%) rotate(-8deg) scale(1); }
  80% { opacity: 1; }
  100% { opacity: 0; transform: translate(-50%, -50%) rotate(-8deg) scale(1); }
}
#toast {
  position: absolute;
  left: 50%; top: 12px;
  transform: translateX(-50%);
  background: #0d151dcc;
  border: 1px solid #ffffff1f;
  padding: 6px 14px;
  border-radius: 999px;
  font-size: 14px;
  white-space: nowrap;
  pointer-events: none;
  transition: opacity 200ms;
}
#toast.hide { opacity: 0; }
#fitBtn { position: absolute; right: max(12px, env(safe-area-inset-right, 0px)); bottom: 12px; background: #0d151dcc; }

/* ---------- Tray ---------- */
.tray {
  display: grid;
  grid-template-columns: auto auto 1fr;
  align-items: center;
  gap: 12px;
  padding-block: 10px calc(10px + env(safe-area-inset-bottom, 0px));
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  background: var(--table-hi);
  border-top: 1px solid #ffffff12;
}
.deckbar {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 12px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.deckbar .label { font-size: 11px; letter-spacing: 0.1em; }
.next { display: flex; align-items: center; gap: 6px; padding-right: 12px; border-right: 1px solid #ffffff1a; }
.mini { width: 28px; height: 28px; display: inline-block; }
.mini svg { width: 100%; height: 100%; display: block; }
.counts { display: flex; gap: 10px; flex-wrap: wrap; min-width: 0; }
.count { display: flex; align-items: center; gap: 3px; }
.count .mini { width: 20px; height: 20px; }
.count.empty { opacity: 0.3; }
.hand { width: 76px; height: 76px; }
.hand svg { width: 100%; height: 100%; display: block; }
.actions { display: flex; gap: 8px; justify-self: end; }
.store {
  appearance: none;
  border: 1px dashed #ffffff33;
  background: transparent;
  color: var(--fg);
  border-radius: 12px;
  width: 60px; height: 76px;
  padding: 6px 0 4px;
  display: grid;
  justify-items: center;
  align-content: space-between;
  touch-action: manipulation;
}
.store:disabled { opacity: 0.4; }
.store.armed { border-style: solid; border-color: var(--kin); }
.store-tile { width: 38px; height: 38px; display: block; }
.store-tile svg { width: 100%; height: 100%; display: block; }
.store-label { font-size: 12px; letter-spacing: 0.05em; display: flex; gap: 4px; align-items: center; }
.pips { color: var(--kin); letter-spacing: 1px; font-size: 9px; }
.hint {
  position: absolute;
  left: 50%; top: 10px;
  transform: translateX(-50%);
  font-size: 13px;
  color: var(--muted);
  white-space: nowrap;
  pointer-events: none;
  transition: opacity 150ms;
}
.hint strong { color: var(--fg); font-weight: 600; }
.hint.gain strong { color: var(--kin); }
.hint.hide { opacity: 0; }
.btn {
  appearance: none;
  border: 0;
  border-radius: 14px;
  padding: 0 16px;
  height: 52px;
  min-width: 64px;
  font: 700 16px var(--font-ui);
  white-space: nowrap;
  touch-action: manipulation;
  background: #ffffff14;
  color: var(--fg);
}
.btn.primary { background: var(--kin); color: #1b1408; }
.btn:disabled { opacity: 0.35; }
.btn:focus-visible, .icon-btn:focus-visible { outline: 3px solid var(--kin); outline-offset: 2px; }

/* ---------- Overlays ---------- */
/* Flex + margin:auto centres the card but lets a tall card scroll from its top. */
.overlay {
  position: fixed; inset: 0;
  background: #0b1118d9;
  display: flex; flex-direction: column;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-block: calc(12px + env(safe-area-inset-top, 0px)) calc(12px + env(safe-area-inset-bottom, 0px));
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  z-index: 20;
}
.overlay > .card { margin: auto; flex: none; }
.card {
  background: var(--table-hi);
  border: 1px solid #ffffff1c;
  border-radius: 18px;
  padding: 22px 20px;
  width: min(360px, 100%);
  display: grid;
  gap: 14px;
}
.card h2 { margin: 0; font-family: var(--font-display); font-weight: 400; font-size: 28px; letter-spacing: 0.06em; text-wrap: balance; }
.card p, .card li { margin: 0; font-size: 14px; line-height: 1.6; color: var(--fg); }
.card ul { margin: 0; padding-left: 1.2em; display: grid; gap: 6px; }
.card .sub { color: var(--muted); font-size: 12px; }
.help-card { gap: 12px; }
.steps { list-style: none; margin: 0; padding: 0; display: grid; gap: 12px; }
.steps li { display: grid; grid-template-columns: 112px 1fr; gap: 12px; align-items: center; }
.steps p { font-size: 13px; line-height: 1.5; }
.steps strong { font-size: 15px; color: var(--kin); }
.demo { display: block; }
@media (max-width: 380px), (max-height: 700px) {
  .card { padding: 18px 16px; gap: 10px; }
  .card h2 { font-size: 24px; }
  .steps { gap: 8px; }
  .steps li { grid-template-columns: 84px 1fr; gap: 10px; }
  .steps p { font-size: 12px; line-height: 1.45; }
  .steps strong { font-size: 14px; }
}
.demo svg { width: 100%; height: auto; display: block; }
.more summary { font-size: 13px; color: var(--muted); cursor: pointer; padding: 4px 0; }
.more ul { margin-top: 6px; }
.more li { font-size: 13px; }
.overlay.sheet {
  background: linear-gradient(to bottom, transparent 35%, #0b1118e6 70%);
  pointer-events: none;
}
.overlay.sheet > .card { pointer-events: auto; margin: auto auto 0; max-height: 100%; overflow-y: auto; }
.result-card { gap: 12px; }
.result-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.link-btn { appearance: none; background: none; border: 0; color: var(--muted); font: 13px var(--font-ui); text-decoration: underline; text-underline-offset: 3px; padding: 4px 0; white-space: nowrap; flex: none; }
.result-score { display: flex; align-items: center; gap: 16px; }
.rank { font-family: var(--font-display); font-size: 24px; color: var(--fg); letter-spacing: 0.08em; }
.result-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.result-actions .btn { padding: 0 8px; font-size: 15px; }
.btn.daily { height: 44px; background: transparent; border: 1px solid #ffffff2a; font-size: 14px; }
.share { display: flex; align-items: center; gap: 10px; justify-content: space-between; }
.share-text { font-size: 12px; color: var(--muted); line-height: 1.5; user-select: text; -webkit-user-select: text; min-width: 0; }
.result-pill {
  position: fixed;
  left: 50%; transform: translateX(-50%);
  bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  z-index: 21;
  background: var(--kin); color: #1b1408;
  box-shadow: 0 6px 20px #0008;
}
.big-score { font-size: 56px; font-weight: 800; color: var(--kin); font-variant-numeric: tabular-nums; line-height: 1; }
.summary { display: grid; grid-template-columns: 1fr auto 1fr auto; gap: 4px 12px; font-size: 14px; font-variant-numeric: tabular-nums; }
.summary dt { color: var(--muted); }
.summary dd { margin: 0; text-align: right; }

@media (prefers-reduced-motion: reduce) {
  .flash, .float, .lm-slot.fresh, .stamp.go { animation: none; }
}

/* Small phones (iPhone SE and friends): keep the tray inside 320px. */
@media (max-width: 360px) {
  .tray { gap: 8px; }
  .hand { width: 64px; height: 64px; }
  .store { width: 52px; height: 64px; }
  .store-tile { width: 32px; height: 32px; }
  .btn { min-width: 56px; padding: 0 12px; height: 48px; }
  .counts { gap: 6px; }
  .next { padding-right: 8px; }
}

/* Phones held sideways: map on the left, hand and buttons in a column on the right. */
@media (orientation: landscape) and (max-height: 520px) {
  #app { grid-template-columns: minmax(0, 1fr) auto; grid-template-rows: auto minmax(0, 1fr); }
  .hud { grid-column: 1 / -1; padding-block: 4px; }
  .brand { font-size: 18px; }
  .game-label { display: none; }
  #boardWrap { grid-column: 1; grid-row: 2; }
  .tray {
    grid-column: 2; grid-row: 2;
    width: 280px;
    grid-template-columns: auto auto;
    align-content: center;
    border-top: 0;
    border-left: 1px solid #ffffff12;
    padding-left: 14px;
  }
  .deckbar { flex-wrap: wrap; row-gap: 6px; }
  .actions { grid-column: 1 / -1; justify-self: stretch; }
  .actions .btn { flex: 1; }
}
