/* Millstone, a cool, dusty mill: stone greys, flour white, one teal accent for "your move"
   and one warm ochre that is only ever used for flour. Fixed palette, the same in light and
   dark OS modes. No serif anywhere: heavy tracked-out sans for the display numbers. */
:root {
  color-scheme: light;
  --bg: #d7dce1;
  --bg2: #c6ced6;
  --surface: #f7f8fa;
  --surface2: #e8ecf0;
  --tray: #b9c3cc;
  --ink: #172029;
  --muted: #5d6b78;
  --line: rgba(23, 32, 41, .14);
  --soft: rgba(23, 32, 41, .06);
  --accent: #14626d;
  --accent-soft: rgba(20, 98, 109, .15);
  --flour: #a8690f;
  --flour-soft: rgba(168, 105, 15, .14);
  --bad: #a8342c;
  --radius: 14px;
  /* the grain: every kind has its own shape as well as its own colour */
  --t-chaff: #b3ac9c;
  --t-rye: #a8553a;
  --t-oats: #5c7f57;
  --t-barley: #8f8a2c;
  --t-millet: #2c7a86;
  --t-wheat: #c2861a;
  /* tutorial kit (tutorial.css) */
  --tut-accent: #14626d;
  --tut-accent-ink: #ffffff;
  --tut-bubble: #f7f8fa;
  --tut-ink: #172029;
  --tut-muted: #5d6b78;
  --tut-dim: rgba(13, 22, 30, .55);
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100vh;
  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; }
.num { font-variant-numeric: tabular-nums; }

/* ------------------------------------------------------------- top bar */
.topbar {
  display: flex; align-items: center; gap: 4px;
  max-width: 1040px; margin: 0 auto; height: 50px;
  padding: 0 max(6px, env(safe-area-inset-right)) 0 max(6px, env(safe-area-inset-left));
}
.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); }
.brand {
  flex: 1; margin: 0; text-align: center;
  font-size: 15px; font-weight: 800; letter-spacing: .18em; text-transform: uppercase;
}
.back:focus-visible, .icon-btn:focus-visible, .btn:focus-visible, .pchip:focus-visible,
.grain:focus-visible, .stone:focus-visible, .stack:focus-visible, .purse:focus-visible,
.sack:focus-visible, .seg button:focus-visible, .menu button:focus-visible {
  outline: 3px solid var(--accent); outline-offset: 2px;
}

/* ------------------------------------------------------------- buttons */
.btn {
  min-height: 44px; padding: 0 16px; 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.icon { min-width: 44px; padding: 0 10px; }
.btn .ico { font-size: 19px; line-height: 1; }
.btn small { font-weight: 500; opacity: .8; font-size: 12px; }

/* ------------------------------------------------------------- layout */
.app {
  max-width: 1040px; margin: 0 auto;
  min-height: calc(100vh - 50px); min-height: calc(100dvh - 50px);
  padding: 0 max(12px, env(safe-area-inset-right)) calc(8px + env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  display: flex; flex-direction: column; gap: 10px;
}
.top { display: grid; gap: 4px; }
/* On a phone the mill is one column, the stone above, the granary full width below, so a tall
   screen is filled by the board itself instead of leaving a dead band under it. Everything inside
   is sized in vh as well as px, so a 700px screen and an 900px screen both come out full. */
.mill {
  flex: 1 1 auto; min-height: 0;
  display: grid; grid-template-columns: minmax(0, 1fr); gap: clamp(8px, 1.8vh, 18px);
  align-content: center; justify-items: center;
  padding: clamp(8px, 1.8vh, 18px); border-radius: 18px; background: var(--bg2);
}
.mill > * { width: 100%; }
.mine { display: grid; gap: 8px; }
@media (min-width: 560px) and (max-width: 899px) {
  .mill { grid-template-columns: minmax(0, 1fr) minmax(0, 1.05fr); align-items: center; }
}
@media (min-width: 900px) {
  .app {
    display: grid; min-height: auto;
    grid-template-columns: minmax(0, 1fr) 400px; grid-template-rows: auto auto 1fr;
    column-gap: 26px; align-items: start; padding-top: 6px;
  }
  .top { grid-column: 2; }
  .mill { grid-column: 1; grid-row: 1 / span 3; grid-template-columns: minmax(0, 1fr); padding: 18px; align-self: start; }
  .mine { grid-column: 2; align-self: start; }
  .actionbar { grid-column: 2; position: static; margin: 4px auto 0; align-self: start; }
}

/* ------------------------------------------------------------- flour chips */
.millers { display: flex; gap: 6px; flex-wrap: nowrap; min-width: 0; }
.pchip {
  position: relative; display: inline-flex; align-items: center; gap: 7px;
  min-height: 44px; padding: 0 12px 0 6px; border: 0; border-radius: 22px;
  background: transparent; cursor: pointer; min-width: 0; flex: 0 1 auto;
  transition: background .2s, box-shadow .2s;
}
.pchip:hover { background: var(--soft); }
.pchip.turn { background: var(--surface); box-shadow: 0 0 0 2px var(--pc), 0 1px 4px rgba(23, 32, 41, .1); }
.pchip.won .pts::after { content: " \2605"; color: var(--flour); }
.av {
  display: inline-grid; place-items: center; flex: none; width: 28px; height: 28px; border-radius: 9px;
  background: var(--pc, #888); color: #fff; font-size: 13px; font-weight: 800;
}
.pchip .nm { font-size: 14px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pchip .pts { font-size: 18px; font-weight: 800; font-variant-numeric: tabular-nums; color: var(--flour); }
.pchip .first { font-size: 11px; color: var(--muted); }
.pchip.bump .av { animation: bump 1.1s ease-out 1 both; }
.pchip .delta {
  position: absolute; right: 2px; top: -4px; font-size: 12px; font-weight: 800; color: var(--flour);
  animation: rise 1.2s ease-out 1 both; pointer-events: none;
}
@keyframes bump { 0% { box-shadow: 0 0 0 0 var(--pc); } 30% { box-shadow: 0 0 0 7px transparent; transform: scale(1.12); } 100% { box-shadow: none; transform: none; } }
@keyframes rise { 0% { opacity: 0; transform: translateY(6px); } 20% { opacity: 1; } 80% { opacity: 1; } 100% { opacity: 0; transform: translateY(-10px); } }
@media (max-width: 599px) {
  .millers[data-n="3"] .rival .nm, .millers[data-n="4"] .nm { display: none; }
  .millers[data-n="4"] .pchip { padding-right: 10px; }
}

/* ------------------------------------------------------------- status */
.status { display: flex; align-items: center; gap: 9px; min-height: 24px; font-size: 15.5px; font-weight: 650; padding: 0 2px; }
.status .dot { flex: none; width: 10px; height: 10px; border-radius: 50%; background: var(--pc); }
.status.mine .dot { background: var(--accent); box-shadow: 0 0 0 4px var(--accent-soft); }
.status.err .msg { color: var(--bad); }
.status.thinking .msg { color: var(--muted); }
.status-row { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.status-row .net { color: var(--muted); justify-content: flex-start; padding: 0 3px; }

/* ------------------------------------------------------------- the grain */
.grain {
  position: relative; display: inline-grid; place-items: center; border: 0; padding: 0;
  width: clamp(52px, 8vh, 70px); height: clamp(52px, 8vh, 70px); border-radius: 14px; background: var(--surface);
  box-shadow: 0 1px 0 var(--line), 0 2px 6px rgba(23, 32, 41, .12);
  cursor: default; transition: transform .12s, box-shadow .12s;
}
.grain .sh { position: absolute; inset: 8%; width: 84%; height: 84%; display: block; }
.grain .gv {
  position: relative; z-index: 1; font-size: 19px; font-weight: 800; line-height: 1;
  font-variant-numeric: tabular-nums; color: #fff; text-shadow: 0 1px 2px rgba(0, 0, 0, .35);
}
.grain.pale .gv { color: #2c2a24; text-shadow: none; }
.grain .gc { position: absolute; z-index: 1; bottom: 4px; left: 0; right: 0; display: flex; justify-content: center; gap: 2px; }
.grain .gc i { width: 5px; height: 5px; border-radius: 50%; background: #fff; box-shadow: 0 0 0 1px rgba(0, 0, 0, .25); }
.grain.pale .gc i { background: #2c2a24; box-shadow: none; }
.grain .quick {
  position: absolute; z-index: 2; top: 2px; right: 3px; font-size: 10px; font-weight: 800; color: #fff;
  text-shadow: 0 1px 2px rgba(0, 0, 0, .4);
}
button.grain.pick { cursor: pointer; }
button.grain.pick:hover { transform: translateY(-2px); }
.grain.on { box-shadow: 0 0 0 3px var(--accent), 0 6px 14px rgba(23, 32, 41, .26); transform: translateY(-4px); }
.grain.sm { width: 34px; height: 34px; border-radius: 9px; box-shadow: 0 1px 3px rgba(23, 32, 41, .18); }
.grain.sm .gv { font-size: 13px; }
.grain.sm .gc, .grain.sm .quick { display: none; }
.grain.xs { width: 19px; height: 19px; border-radius: 6px; box-shadow: none; }
.grain.xs .gv, .grain.xs .gc, .grain.xs .quick { display: none; }
.grain.fresh { animation: land .35s ease-out 1 both; }
@keyframes land { from { transform: translateY(-10px) scale(.86); opacity: .3; } to { transform: none; opacity: 1; } }

/* ------------------------------------------------------------- the millstone */
.stone-wrap { display: grid; justify-items: center; gap: 6px; min-width: 0; }
.stone {
  position: relative; display: grid; place-items: center; border: 0; cursor: default;
  width: clamp(120px, 23vh, 200px); height: clamp(120px, 23vh, 200px); border-radius: 50%; padding: 0;
  background: radial-gradient(circle at 50% 38%, #e4e8ec 0%, #b6bfc8 62%, #97a3ad 100%);
  box-shadow: inset 0 0 0 5px rgba(255, 255, 255, .35), 0 3px 10px rgba(23, 32, 41, .2);
  transition: box-shadow .2s;
}
.stone.target { cursor: pointer; box-shadow: inset 0 0 0 5px rgba(255, 255, 255, .35), 0 0 0 4px var(--accent), 0 4px 14px rgba(23, 32, 41, .28); }
.stone.dull { background: radial-gradient(circle at 50% 38%, #cfd0cc 0%, #a9a9a4 62%, #8d8d88 100%); }
.stone .grooves { position: absolute; inset: 0; display: block; }
.stone .eye {
  position: relative; z-index: 1; display: grid; place-items: center; gap: 1px;
  width: 56%; height: 56%; border-radius: 50%; background: var(--surface);
  box-shadow: inset 0 0 0 2px rgba(23, 32, 41, .1);
}
.stone .big { font-size: 26px; font-weight: 800; line-height: 1; font-variant-numeric: tabular-nums; color: var(--flour); }
.stone.dressing .big { color: var(--accent); }
.stone .cap { font-size: 10.5px; font-weight: 700; letter-spacing: .07em; text-transform: uppercase; color: var(--muted); }
.stone.dull .cap { color: var(--bad); }
.stone-load { display: flex; gap: 4px; flex-wrap: wrap; justify-content: center; min-height: 34px; }
.stone-note { font-size: 12px; color: var(--muted); text-align: center; }

/* ------------------------------------------------------------- the granary */
.granary { display: grid; gap: clamp(5px, 1vh, 10px); align-content: center; min-width: 0; }
.stack {
  display: grid; grid-template-columns: 36px minmax(0, 1fr) auto; align-items: center; gap: 10px;
  min-height: clamp(44px, 6.6vh, 62px); padding: 3px 12px 3px 5px; border: 0; border-radius: 12px;
  background: var(--surface); box-shadow: 0 1px 2px rgba(23, 32, 41, .1); cursor: default; text-align: left;
  transition: box-shadow .15s, transform .1s, opacity .2s;
}
.stack .nm { font-size: 14.5px; font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.stack .nm small { display: block; font-weight: 400; font-size: 11.5px; color: var(--muted); }
.stack .price { display: inline-flex; align-items: center; gap: 4px; font-size: 15px; font-weight: 800; font-variant-numeric: tabular-nums; }
.stack.buyable { cursor: pointer; box-shadow: 0 0 0 2px var(--accent-soft), 0 2px 6px rgba(23, 32, 41, .14); }
.stack.buyable:hover { transform: translateY(-1px); }
.stack.empty { opacity: .38; }
.stack.flash { animation: flash 1.2s ease-out 1 both; }
@keyframes flash { 0% { box-shadow: 0 0 0 4px var(--accent); } 100% { box-shadow: 0 1px 2px rgba(23, 32, 41, .1); } }
.coin {
  display: inline-grid; place-items: center; width: 17px; height: 17px; border-radius: 50%;
  background: linear-gradient(160deg, #e9d9a8, #c9a951); box-shadow: inset 0 0 0 1px rgba(90, 65, 10, .35);
  font-size: 9.5px; font-weight: 800; color: #4a3607;
}

/* ------------------------------------------------------------- your row */
.mine-row { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 8px; align-items: stretch; }
.purse {
  display: grid; grid-template-columns: auto auto; align-items: center; gap: 6px;
  min-height: 46px; padding: 0 12px; border: 0; border-radius: 13px; cursor: default;
  background: var(--surface); box-shadow: 0 1px 2px rgba(23, 32, 41, .12);
  transition: box-shadow .15s;
}
.purse.target { cursor: pointer; box-shadow: 0 0 0 3px var(--accent), 0 3px 10px rgba(23, 32, 41, .2); }
.purse .n { font-size: 20px; font-weight: 800; font-variant-numeric: tabular-nums; }
.sack {
  display: flex; align-items: center; gap: 4px; flex-wrap: wrap; min-width: 0;
  min-height: 46px; padding: 5px 8px; border: 0; border-radius: 13px; text-align: left;
  background: var(--surface); box-shadow: 0 1px 2px rgba(23, 32, 41, .12); cursor: pointer;
}
.sack .grain.used { opacity: .3; }
.sack .sep { width: 1px; align-self: stretch; margin: 2px 3px; background: var(--line); }
.sack .lbl { font-size: 11px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); }
.handful {
  display: flex; gap: 8px; flex-wrap: wrap; align-items: center; justify-content: center;
  min-height: clamp(58px, 8.6vh, 76px); padding: 3px; border-radius: 14px;
}
.handful.empty { min-height: 0; padding: 0; }

/* ------------------------------------------------------------- action bar */
.actionbar {
  /* above the tutorial kit's first-visit chip (z 12), which would otherwise cover it */
  position: sticky; bottom: calc(6px + env(safe-area-inset-bottom)); z-index: 14;
  width: fit-content; max-width: 100%; margin: 0 auto; min-height: 54px;
  display: flex; gap: 6px; align-items: center; padding: 5px; flex-wrap: wrap; justify-content: center;
  background: var(--surface); border-radius: 18px;
  box-shadow: 0 4px 18px rgba(23, 32, 41, .18);
  animation: barin .18s ease-out;
}
.actionbar:empty { visibility: hidden; animation: none; min-height: 0; padding: 0; }
@keyframes barin { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

/* ------------------------------------------------------------- overlays */
.overlay {
  position: fixed; inset: 0; z-index: 50; display: grid; place-items: center;
  background: rgba(15, 24, 32, .45); 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(--surface); border-radius: 20px; padding: 18px;
  box-shadow: 0 12px 40px rgba(10, 18, 26, .3);
  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: 19px; text-align: center; letter-spacing: .01em; }
.modal-head { display: flex; align-items: center; gap: 8px; margin: -6px -6px 8px 0; }
.modal-head h2 { flex: 1; margin: 0; text-align: left; display: flex; align-items: center; gap: 8px; }
.modal .lede { margin: -4px 0 14px; text-align: center; color: var(--muted); font-size: 14px; }
.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(15, 24, 32, .12); place-items: start end; padding: 50px 10px 0; }
.menu {
  min-width: 212px; background: var(--surface); border-radius: 16px; padding: 6px;
  box-shadow: 0 10px 30px rgba(10, 18, 26, .25); 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); }
.menu #menu-online { border-top: 1px solid var(--line); border-radius: 0 0 10px 10px; margin-top: 4px; }
@media (min-width: 1060px) { .menu-overlay { padding-right: calc((100vw - 1040px) / 2 + 8px); } }

/* setup */
.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; }

/* sift picker */
.sift-list { display: grid; gap: 6px; }
.sift-row {
  display: grid; grid-template-columns: 34px minmax(0, 1fr) auto; align-items: center; gap: 10px;
  min-height: 52px; padding: 4px 12px 4px 6px; border: 0; border-radius: 12px; text-align: left;
  background: var(--soft); cursor: pointer;
}
.sift-row:hover { background: var(--accent-soft); }
.sift-row b { font-size: 14px; }
.sift-row small { display: block; font-weight: 400; font-size: 12px; color: var(--muted); }
.sift-row .have { font-size: 13px; font-weight: 700; color: var(--muted); font-variant-numeric: tabular-nums; }

/* info + viewer */
.info-round { margin: 0 0 12px; color: var(--muted); font-size: 13.5px; }
.info-scores { display: grid; gap: 14px; }
.info-head { display: flex; align-items: center; gap: 8px; font-weight: 700; }
.info-head .info-sub { font-weight: 400; font-size: 12.5px; color: var(--muted); }
.info-head .total { margin-left: auto; font-size: 18px; color: var(--flour); font-variant-numeric: tabular-nums; }
.sub-title { margin: 20px 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }
.viewer-body, .info-player .sackline { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; margin-top: 6px; }
.viewer-note { margin: 10px 0 0; font-size: 13px; color: var(--muted); }
.log { list-style: none; margin: 0; padding: 0; display: grid; gap: 5px; font-size: 13px; max-height: 38vh; overflow-y: auto; }
.log li { display: flex; gap: 7px; align-items: flex-start; }
.log li .av { width: 18px; height: 18px; border-radius: 6px; font-size: 10px; margin-top: 1px; }
.log li.round-line { color: var(--muted); font-style: italic; }

/* help */
.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; }
.grain-key { list-style: none; padding: 0 !important; display: grid; gap: 7px; }
.grain-key li { display: flex; align-items: center; gap: 9px; }
.grain-key b { font-size: 14px; }
.grain-key small { color: var(--muted); }

/* 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(--flour-soft); }
.go-row .place { width: 26px; color: var(--muted); font-size: 13px; font-weight: 700; }
.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: 22px; font-weight: 800; color: var(--flour); font-variant-numeric: tabular-nums; }

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

/* online kit */
body.is-online #btn-new { display: none; }
#over-leave { display: flex; width: 100%; margin-top: 8px; }

/* very narrow screens: the stone must not crowd the granary out */
@media (max-width: 359px) {
  .stone { width: min(46vw, 150px); height: min(46vw, 150px); }
}
/* short screens (360x640 and the like): everything tightens so the handful and the action bar
   both stay on screen without scrolling */
@media (max-height: 720px) and (max-width: 899px) {
  .app { gap: 6px; }
  .stone { width: clamp(100px, 20vh, 140px); height: clamp(100px, 20vh, 140px); }
  .stone .big { font-size: 21px; }
  .stone .cap { font-size: 9px; }
  .stone-note { display: none; }
  .stack { min-height: 42px; }
  .stack .nm { font-size: 13.5px; }
  .grain { width: 46px; height: 46px; }
  .grain .gv { font-size: 17px; }
  .handful { min-height: 50px; }
  .actionbar { min-height: 48px; }
  .actionbar .btn { min-height: 40px; }
}
