/* Packhorse: a calm alpine-map palette, fixed in light and dark OS modes.
   One accent (deep amber) always means "you can act here". The map is the screen: the page is a
   full-height column and the map takes every pixel the chrome does not need. */
:root {
  color-scheme: light;
  --bg: #f0ead9;
  --paper: #f7f2e4;
  --card: #fdfaf2;
  --ink: #2b2419;
  --muted: #7a6a52;
  --soft: rgba(43, 36, 25, .07);
  --line: rgba(43, 36, 25, .16);
  --accent: #8a5a2b;
  --accent-soft: rgba(138, 90, 43, .16);
  --bad: #b4392b;
  --good: #3f7a46;
  --radius: 14px;

  /* goods: shape tells them apart too, never colour alone */
  --g0: #6f93b8;   /* Salt */
  --g1: #c88a55;   /* Wool */
  --g2: #b5623a;   /* Copper */
  --g3: #d9a327;   /* Honey */
  --g4: #4f8a5b;   /* Tea */

  /* traders */
  --p0: #b5432c;
  --p1: #2f6f8f;
  --p2: #7a5aa8;
  --p3: #4f7a3a;

  /* tutorial kit */
  --tut-accent: #d9a327;
  --tut-accent-ink: #2b2419;
  --tut-bubble: #fdfaf2;
  --tut-ink: #2b2419;
  --tut-muted: #7a6a52;
  --tut-dim: rgba(43, 36, 25, .55);
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  touch-action: manipulation;
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; touch-action: manipulation; }
[hidden] { display: none !important; }

/* ---------------------------------------------------------------- shell */
/* The whole page is one column the height of the screen, so the map fills what is left and
   there is never a dead band under the board. */
.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  max-width: 900px;
  margin: 0 auto;
  padding: 0 max(10px, env(safe-area-inset-right)) calc(6px + env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
}
.topbar { display: flex; align-items: center; gap: 4px; height: 48px; flex: none; }
.back, .icon-btn {
  flex: none; width: 44px; height: 44px; display: grid; place-items: center;
  border: 0; border-radius: 12px; background: transparent; color: var(--ink);
  text-decoration: none; font-size: 22px; line-height: 1; cursor: pointer;
}
.back:hover, .icon-btn:hover { background: var(--soft); }
.icon-btn.small { width: 36px; height: 36px; font-size: 20px; color: var(--muted); }
.brand {
  flex: 1; margin: 0; text-align: center; font-size: 18px; font-weight: 750;
  letter-spacing: .06em; font-variant: small-caps;
}
.back:focus-visible, .icon-btn:focus-visible, .btn:focus-visible, .pchip:focus-visible,
.seg button:focus-visible, .menu button:focus-visible, .shop-row:focus-visible {
  outline: 3px solid var(--accent); outline-offset: 2px;
}

/* ---------------------------------------------------------------- buttons */
.btn {
  min-height: 44px; padding: 0 14px; border-radius: 12px; border: 0;
  background: var(--soft); cursor: pointer; font-weight: 650; line-height: 1.1;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  transition: transform .08s, background .15s, opacity .15s;
}
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); color: #fff; }
.btn.ghost { background: transparent; box-shadow: inset 0 0 0 1.5px var(--line); }
.btn.warn { background: var(--bad); color: #fff; }
.btn[disabled] { opacity: .4; cursor: not-allowed; transform: none; }
.btn .sub { font-weight: 500; opacity: .85; font-size: 13px; }

/* ---------------------------------------------------------------- trader chips */
.chips { display: flex; gap: 5px; flex: none; min-width: 0; }
.pchip {
  position: relative; display: inline-flex; align-items: center; gap: 6px;
  min-height: 42px; padding: 0 11px 0 5px; border: 0; border-radius: 21px;
  background: transparent; cursor: pointer; min-width: 0; flex: 1 1 auto;
  transition: background .2s, box-shadow .2s;
}
.pchip:hover { background: var(--soft); }
.pchip.turn { background: var(--card); box-shadow: 0 0 0 2px color-mix(in srgb, var(--pc) 45%, transparent), 0 1px 4px rgba(43, 36, 25, .08); }
.pchip .av {
  display: inline-grid; place-items: center; flex: none; width: 26px; height: 26px; border-radius: 50%;
  background: var(--pc, #888); color: #fff; font-size: 12px; font-weight: 800;
}
.pchip .nm { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.pchip .coin { margin-left: auto; font-size: 16px; font-weight: 800; font-variant-numeric: tabular-nums; }
.pchip.won .coin::after { content: " \2605"; color: var(--accent); }
.pchip .delta {
  position: absolute; right: 4px; top: -4px; font-size: 12px; font-weight: 800; color: var(--good);
  animation: rise 1.4s ease-out 1 both; pointer-events: none;
}
@keyframes rise { 0% { opacity: 0; transform: translateY(6px); } 20% { opacity: 1; } 75% { opacity: 1; } 100% { opacity: 0; transform: translateY(-9px); } }
@media (max-width: 460px) { .chips[data-n="4"] .nm, .chips[data-n="3"] .nm { display: none; } }

/* ---------------------------------------------------------------- status + what happened */
.status-row { display: flex; align-items: baseline; gap: 8px; flex: none; padding: 3px 2px 0; min-width: 0; }
.status { display: flex; align-items: center; gap: 8px; font-size: 15px; font-weight: 650; min-width: 0; }
.status .dot { flex: none; width: 9px; height: 9px; border-radius: 50%; background: var(--pc, var(--accent)); }
.status.mine .dot { background: var(--accent); box-shadow: 0 0 0 4px var(--accent-soft); }
.status .msg { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.status.thinking .msg { color: var(--muted); }
.net { margin-left: auto; font-size: 12.5px; color: var(--muted); white-space: nowrap; }

/* the plain-English line: what the computer just did */
/* Two lines, always the same height, so the map below never shifts between turns. */
.happened {
  flex: none; margin: 2px 0 4px; padding: 0 2px; height: 36px;
  font-size: 13px; line-height: 1.38; color: var(--muted);
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.happened.fresh { color: var(--ink); animation: saidit .5s ease-out; }
@keyframes saidit { from { opacity: .25; } to { opacity: 1; } }

/* ---------------------------------------------------------------- the map */
.mapwrap {
  flex: 1 1 auto; min-height: 0; position: relative;
  border-radius: 16px; background: var(--paper);
  box-shadow: inset 0 0 0 1px var(--line);
  overflow: hidden;
}
#map { display: block; width: 100%; height: 100%; }

.pass { stroke: #c3b394; stroke-width: 1.7; stroke-linecap: round; fill: none; }
.pass.long { stroke-dasharray: 4 2.4; }
.pass.warn { stroke: #c98b2e; stroke-width: 2.1; }
.pass.shut { stroke: #9aa7b4; stroke-width: 1.6; stroke-dasharray: 1.6 3.2; }
.pass.route { stroke: var(--accent); stroke-width: 2.6; opacity: .95; }
.passmark { pointer-events: none; }
.passmark text { font-size: 4.4px; font-weight: 700; fill: var(--muted); text-anchor: middle; }
.passmark.warn text { fill: #9a6a17; }
.passcost { font-size: 4.2px; fill: #9a8a6c; text-anchor: middle; font-weight: 600; pointer-events: none; }

.vil { cursor: pointer; }
.vil .card { fill: var(--card); stroke: var(--line); stroke-width: .7; }
.vil.reach .card { stroke: var(--accent); stroke-width: 1.5; }
.vil.here .card { fill: #fff6e4; stroke: var(--accent); stroke-width: 1.9; }
.vil.sel .card { stroke: var(--ink); stroke-width: 1.9; }
.vil .vname { font-size: 4.6px; font-weight: 750; fill: var(--ink); text-anchor: middle; }
.vil .makes { font-size: 3.9px; fill: var(--muted); text-anchor: start; }
.vil .wchip rect { fill: var(--soft); }
.vil .wprice { font-size: 5px; font-weight: 800; fill: var(--ink); text-anchor: middle; }
.vil .wnext { font-size: 3.9px; font-weight: 600; fill: var(--muted); text-anchor: middle; }
.vil .nowant { font-size: 3.9px; fill: #a5977e; text-anchor: middle; }
.vil .wmore { font-size: 3.5px; font-weight: 700; fill: var(--muted); text-anchor: end; }
.vil.reach .card, .vil.here .card { filter: drop-shadow(0 .6px .8px rgba(43, 36, 25, .18)); }
.vil.paid .card { fill: #f2fbea; stroke: var(--good); stroke-width: 1.7; }

.pony { cursor: pointer; }
.pony .disc { fill: var(--pc); stroke: #fff; stroke-width: .9; }
.pony.mine.on .disc { stroke: var(--ink); stroke-width: 1.4; }
.pony .crate { stroke: #2b2419; stroke-width: .5; }
.pony .empty { font-size: 3.6px; fill: #fff; text-anchor: middle; font-weight: 700; }

/* ---------------------------------------------------------------- action bar */
.actionbar {
  flex: none; display: flex; gap: 6px; align-items: center; flex-wrap: wrap;
  padding: 7px 2px 2px; min-height: 54px;
}
.actionbar:empty { min-height: 0; padding: 0; }
.actionbar .btn { flex: 1 1 auto; }
.actionbar .btn.slim { flex: 0 0 auto; }
.ponypick { display: inline-flex; gap: 4px; padding: 3px; border-radius: 13px; background: var(--soft); flex: 0 0 auto; }
.ponypick button {
  min-height: 38px; padding: 0 9px; border: 0; border-radius: 10px; background: transparent;
  cursor: pointer; font-weight: 700; font-size: 13px; display: inline-flex; align-items: center; gap: 5px;
}
.ponypick button[aria-pressed="true"] { background: var(--card); box-shadow: 0 1px 3px rgba(43, 36, 25, .16); }
.ponypick .st { color: var(--muted); font-weight: 600; }
.ponypick svg { width: 16px; height: 16px; color: var(--pc); }

/* ---------------------------------------------------------------- village sheet */
.sheet-wrap { position: fixed; inset: 0; z-index: 30; display: flex; align-items: flex-end; justify-content: center; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(43, 36, 25, .28); }
.sheet {
  position: relative; width: 100%; max-width: 520px;
  background: var(--card); border-radius: 18px 18px 0 0;
  padding: 12px 14px calc(14px + env(safe-area-inset-bottom));
  box-shadow: 0 -8px 30px rgba(30, 24, 12, .22);
  animation: slideup .18s ease-out; max-height: 72vh; overflow-y: auto;
}
@keyframes slideup { from { transform: translateY(14px); opacity: .4; } to { transform: none; opacity: 1; } }
.sheet-head { display: flex; align-items: center; gap: 8px; }
.sheet-head h2 { flex: 1; margin: 0; font-size: 17px; display: flex; align-items: center; gap: 7px; }
.sheet-actions { display: flex; flex-wrap: wrap; gap: 7px; margin-top: 12px; }
.sheet-actions .btn { flex: 1 1 auto; }
.vrow { display: flex; align-items: center; gap: 8px; margin: 9px 0; flex-wrap: wrap; }
.vrow .lbl { font-size: 12px; text-transform: uppercase; letter-spacing: .07em; color: var(--muted); font-weight: 700; width: 100%; }
.gtag {
  display: inline-flex; align-items: center; gap: 5px; padding: 4px 10px 4px 6px;
  border-radius: 999px; background: var(--soft); font-size: 13.5px; font-weight: 650;
}
.gtag svg { width: 17px; height: 17px; color: var(--gc); flex: none; }
.gtag .ladder { display: inline-flex; gap: 3px; margin-left: 2px; }
.gtag .rung { font-variant-numeric: tabular-nums; color: var(--muted); font-weight: 700; }
.gtag .rung.now { color: var(--ink); background: var(--accent-soft); border-radius: 6px; padding: 0 5px; }
.muted { color: var(--muted); }
.sheet p { margin: 6px 0; font-size: 13.5px; }

/* ---------------------------------------------------------------- overlays */
.overlay {
  position: fixed; inset: 0; z-index: 50; display: grid; place-items: center;
  background: rgba(43, 30, 16, .42); padding: 14px; overflow-y: auto;
}
.modal {
  width: 100%; max-width: 420px; max-height: calc(100vh - 28px); max-height: calc(100dvh - 28px); overflow-y: auto;
  background: var(--card); border-radius: 20px; padding: 18px;
  box-shadow: 0 12px 40px rgba(30, 24, 12, .28);
  animation: pop .18s ease-out;
}
.modal.wide { max-width: 600px; }
@keyframes pop { from { transform: translateY(8px); opacity: 0; } to { transform: none; opacity: 1; } }
.modal h2 { margin: 0 0 12px; font-size: 20px; text-align: center; }
.modal-head { display: flex; align-items: center; gap: 8px; margin: -6px -6px 8px 0; }
.modal-head h2 { flex: 1; margin: 0; text-align: left; }
.modal .lede { margin: -4px 0 14px; text-align: center; color: var(--muted); font-size: 13.5px; }
.modal-actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-end; margin-top: 18px; }
.modal-actions .btn { flex: 1 1 auto; }

.menu-overlay { background: rgba(43, 30, 16, .12); place-items: start end; padding: 48px 10px 0; }
.menu {
  min-width: 212px; background: var(--card); border-radius: 16px; padding: 6px;
  box-shadow: 0 10px 30px rgba(30, 24, 12, .22); animation: pop .15s ease-out; display: grid;
}
.menu button {
  min-height: 48px; padding: 0 14px; border: 0; background: transparent; text-align: left;
  border-radius: 10px; cursor: pointer; font-size: 15px; font-weight: 550;
}
.menu button:hover { background: var(--soft); }
@media (min-width: 920px) { .menu-overlay { padding-right: calc((100vw - 900px) / 2 + 8px); } }

.field { margin: 14px 0; }
.field-label { font-weight: 650; margin-bottom: 6px; font-size: 14px; }
.field-hint { font-size: 12.5px; color: var(--muted); margin-top: 6px; }
.seg { display: flex; gap: 4px; padding: 4px; border-radius: 14px; background: var(--soft); }
.seg button { flex: 1; min-height: 44px; border: 0; border-radius: 10px; background: transparent; cursor: pointer; font-weight: 650; }
.seg button[aria-checked="true"] { background: var(--accent); color: #fff; }

/* ---------------------------------------------------------------- shop */
.shop-list { display: grid; gap: 8px; }
.shop-row {
  display: flex; align-items: center; gap: 11px; width: 100%; text-align: left;
  padding: 11px 12px; border: 0; border-radius: 14px; background: var(--soft); cursor: pointer;
}
.shop-row:disabled { opacity: .45; cursor: not-allowed; }
.shop-row .what { flex: 1; min-width: 0; }
.shop-row .what b { display: block; font-size: 15px; }
.shop-row .what small { color: var(--muted); font-size: 12.5px; }
.shop-row .price { font-size: 17px; font-weight: 800; font-variant-numeric: tabular-nums; display: inline-flex; align-items: center; gap: 4px; }
.shop-row .price svg { width: 15px; height: 15px; color: var(--g3); }
.shop-key { list-style: none; padding: 0 !important; display: grid; gap: 6px; }
.shop-key li { display: flex; gap: 8px; align-items: baseline; font-size: 14px; }

/* ---------------------------------------------------------------- info + help */
.info-round { margin: 0 0 12px; color: var(--muted); font-size: 13.5px; }
.info-scores { display: grid; gap: 12px; }
.info-player { border-radius: 12px; background: var(--soft); padding: 10px 12px; }
.info-head { display: flex; align-items: center; gap: 8px; font-weight: 700; }
.info-head .av { display: inline-grid; place-items: center; width: 24px; height: 24px; border-radius: 50%; background: var(--pc); color: #fff; font-size: 11px; font-weight: 800; }
.info-head .total { margin-left: auto; font-size: 18px; }
.info-sub { font-weight: 400; font-size: 12.5px; color: var(--muted); margin-top: 3px; }
.sub-title { margin: 18px 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }
.log { list-style: none; margin: 0; padding: 0; display: grid; gap: 5px; font-size: 13px; max-height: 40vh; overflow-y: auto; }
.log li { display: flex; gap: 7px; align-items: flex-start; }
.log li .pip { flex: none; width: 8px; height: 8px; border-radius: 50%; background: var(--pc, var(--muted)); margin-top: 5px; }
.log li.round-line { color: var(--muted); font-style: italic; }

.help-body h3 { margin: 16px 0 4px; font-size: 15px; }
.help-body h3:first-child { margin-top: 0; }
.help-body p, .help-body li { font-size: 14px; }
.help-body ul, .help-body ol { padding-left: 20px; margin: 4px 0; }
.goods-key { list-style: none; padding: 0 !important; display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 7px; }
.goods-key li { display: flex; align-items: center; gap: 8px; }
.goods-key svg { width: 22px; height: 22px; color: var(--gc); flex: none; }

/* ---------------------------------------------------------------- game over */
.go-table { display: grid; gap: 4px; }
.go-row { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 12px; }
.go-row.win { background: var(--accent-soft); }
.go-row .place { width: 26px; color: var(--muted); font-size: 13px; font-weight: 700; }
.go-row .av { display: inline-grid; place-items: center; width: 24px; height: 24px; border-radius: 50%; background: var(--pc); color: #fff; font-size: 11px; font-weight: 800; }
.go-name { display: flex; flex-direction: column; font-weight: 700; min-width: 0; }
.go-name small { font-weight: 400; color: var(--muted); font-size: 12px; }
.go-row .total { margin-left: auto; font-size: 21px; font-variant-numeric: tabular-nums; }

/* ---------------------------------------------------------------- online */
body.is-online #btn-new { display: none; }

/* The tutorial kit's first-visit chip is fixed 14px from the bottom, which is where the action
   bar lives: lift it clear so it never covers Spend or End turn (the kit's KIT.md asks for this). */
.tut-chip { bottom: calc(78px + env(safe-area-inset-bottom, 0px)) !important; }
/* ...and while the chip is on screen, hold the map clear of it, so on a short screen it never
   sits over the bottom row of villages. The margin goes when the chip is dismissed. */
body:has(.tut-chip) .mapwrap { margin-bottom: 62px; }
.menu #menu-online { border-top: 1px solid var(--line); border-radius: 0 0 10px 10px; margin-top: 4px; }
#over-leave { display: flex; width: 100%; margin-top: 8px; }

/* ---------------------------------------------------------------- wide screens */
@media (min-width: 760px) and (min-height: 560px) {
  .brand { font-size: 20px; }
  .status { font-size: 16px; }
  .happened { font-size: 14px; }
}
/* Short screens (360x640 and the like): give the map back the height the chrome was using. */
@media (max-height: 700px) {
  .topbar { height: 42px; }
  .pchip { min-height: 38px; }
  .happened { height: 19px; -webkit-line-clamp: 1; margin: 1px 0 3px; }
  .status { font-size: 14px; }
  .actionbar { min-height: 48px; padding-top: 5px; }
}

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