/* Tilewright: a stone-yard palette, fixed in light and dark OS modes.
   One accent (brick) always means "your action": layable cells, the chosen take, Lay.
   The five stones differ by colour AND by shape, so colour is never the only signal. */
:root {
  color-scheme: light;
  --bg: #ecedE8;
  --bg: #eceee9;
  --card: #fbfcf9;
  --ink: #232a2c;
  --muted: #6d7a7d;
  --soft: rgba(35, 42, 44, .07);
  --line: rgba(35, 42, 44, .14);
  --tray: #dcdfd7;
  --empty: #f2f3ef;
  --accent: #9a3f2b;
  --accent-soft: rgba(154, 63, 43, .15);
  --bad: #b3352a;
  --good: #2f6b4f;
  --radius: 14px;
  --vcell: min(46px, calc((100vw - 104px) / 5));
  /* tutorial kit (tutorial.css) */
  --tut-accent: #c2703f;
  --tut-accent-ink: #231a12;
  --tut-bubble: #fbfcf9;
  --tut-ink: #232a2c;
  --tut-muted: #6d7a7d;
  --tut-dim: rgba(28, 34, 36, .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; }

/* ------------------------------------------------------------- top bar */
.topbar {
  display: flex; align-items: center; gap: 4px;
  max-width: 1060px; margin: 0 auto; height: 48px;
  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: 18px; font-weight: 750;
  letter-spacing: .07em; font-variant: small-caps;
}
.back:focus-visible, .icon-btn:focus-visible, .btn:focus-visible, .pchip:focus-visible,
.stack:focus-visible, .seg button:focus-visible, .menu button:focus-visible, .cell: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 .lbl { font-size: 14px; }

/* ------------------------------------------------------------- layout */
.app {
  max-width: 1060px; margin: 0 auto;
  padding: 0 max(12px, env(safe-area-inset-right)) calc(10px + env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  display: grid; gap: 8px; grid-template-columns: minmax(0, 1fr);
}
.side { display: grid; gap: 8px; align-content: start; min-width: 0; }
.board-area { position: relative; min-width: 0; }
/* On a phone the floor is limited by width, not height, so the space left over
   would otherwise pile up as a dead band under the board. Fill the screen and
   centre the floor in what remains. */
@media (max-width: 899px) {
  .app { min-height: calc(100vh - 48px); min-height: calc(100dvh - 48px); grid-template-rows: auto minmax(0, 1fr); }
  .board-area { display: flex; flex-direction: column; justify-content: center; }
}
@media (min-width: 900px) {
  .app { grid-template-columns: minmax(0, 1fr) 380px; gap: 28px; align-items: start; padding-top: 6px; }
  .board-area { grid-column: 1; grid-row: 1; }
  .side { grid-column: 2; grid-row: 1; gap: 14px; }
}

/* ------------------------------------------------------------- score chips */
.rivals { display: flex; gap: 5px; flex-wrap: nowrap; min-width: 0; }
.pchip {
  position: relative; display: inline-flex; align-items: center; gap: 6px;
  min-height: 44px; padding: 0 11px 0 5px; 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(--card); box-shadow: 0 0 0 2px color-mix(in srgb, var(--pc) 45%, transparent), 0 1px 4px rgba(35, 42, 44, .08); }
.pchip.won .pts::after { content: " \2605"; color: var(--accent); }
.av {
  display: inline-grid; place-items: center; flex: none; width: 27px; height: 27px; border-radius: 50%;
  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: 17px; font-weight: 800; font-variant-numeric: tabular-nums; }
.pchip .chipmark { font-size: 12px; font-weight: 700; color: var(--bad); white-space: nowrap; }
.pchip.bump .av { animation: bump 1.1s ease-out 1 both; }
.pchip .delta {
  position: absolute; right: 2px; top: -5px; font-size: 12px; font-weight: 800; color: var(--pc);
  animation: rise 1.1s ease-out 1 both; pointer-events: none;
}
.pchip .delta.minus { color: var(--bad); }
@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(-8px); } }
@media (max-width: 599px) {
  .rivals[data-n="3"] .rival .nm, .rivals[data-n="4"] .nm { display: none; }
  .rivals[data-n="4"] .pchip { padding-right: 9px; }
}

/* ------------------------------------------------------------- status */
.status-row { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.status {
  display: flex; align-items: center; gap: 8px; 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 .net { color: var(--muted); justify-content: flex-start; padding: 0 3px; }
/* the plain-English "what just happened" line, always on screen */
.lastline {
  font-size: 12.5px; line-height: 1.35; color: var(--muted); padding: 0 3px;
  min-height: 17px; overflow: hidden; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2;
}

/* ------------------------------------------------------------- the yard */
.yard { display: grid; gap: 6px; }
.yard-head { display: flex; align-items: baseline; gap: 8px; padding: 0 2px; }
.yard-label { font-size: 11px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .08em; }
.yard-note { font-size: 11.5px; color: var(--muted); margin-left: auto; }
.stacks { display: grid; grid-template-columns: repeat(auto-fit, minmax(64px, 1fr)); gap: 6px; }
.stack {
  position: relative; display: grid; grid-template-columns: 1fr 1fr; gap: 3px;
  padding: 4px; border: 0; border-radius: 12px; background: var(--card);
  box-shadow: 0 1px 3px rgba(35, 42, 44, .14); cursor: default; min-width: 0;
  transition: transform .15s, box-shadow .15s, opacity .2s;
}
.stack.empty { background: transparent; box-shadow: inset 0 0 0 1.5px var(--line); min-height: 44px; }
.stack.live { cursor: pointer; }
.stack.live:hover { transform: translateY(-2px); }
.stack.picked { box-shadow: 0 0 0 3px var(--accent), 0 4px 10px rgba(35, 42, 44, .2); }

/* the spill: one row of loose tiles, grouped by colour */
.spill {
  display: flex; flex-wrap: wrap; gap: 4px; align-items: center; min-height: 46px;
  padding: 5px 7px; border-radius: 12px; background: var(--tray);
}
.spill.bare { color: var(--muted); font-size: 12.5px; }
.spill-group {
  display: flex; gap: 2px; padding: 6px 4px; border: 0; border-radius: 9px; background: transparent;
  align-items: center; min-height: 44px;            /* a full-size tap target */
  cursor: default; transition: box-shadow .15s, transform .15s;
}
/* The spill is a flex row, so its tiles need a real size: aspect-ratio alone
   collapses them to zero width (a grid cell gives the stacks theirs). */
.spill-group .tile { width: 32px; height: 32px; flex: none; }
.spill-group.live { cursor: pointer; }
.spill-group.live:hover { transform: translateY(-2px); }
.spill-group.picked { box-shadow: 0 0 0 3px var(--accent); background: var(--card); }
.chisel {
  display: inline-flex; align-items: center; gap: 4px; margin-left: auto;
  font-size: 11.5px; font-weight: 700; color: var(--accent);
  padding: 3px 8px; border-radius: 999px; background: var(--accent-soft); white-space: nowrap;
}

/* ------------------------------------------------------------- tiles */
.tile {
  position: relative; aspect-ratio: 1 / 1; border-radius: 6px; overflow: hidden; min-width: 0;
  display: block;
}
.tile svg { position: absolute; inset: 16%; width: 68%; height: 68%; display: block; }
.tile.gone { opacity: .16; }
.tile.taking { animation: lift .9s ease-out 1 both; }
.tile.spilling { animation: sink .9s ease-out 1 both; }
@keyframes lift { 0% { transform: none; } 40% { transform: translateY(-5px) scale(1.06); } 100% { transform: none; } }
@keyframes sink { 0% { transform: none; } 40% { transform: translateY(4px); opacity: .55; } 100% { transform: none; } }

/* Five stones: distinct hue AND distinct glyph (never colour alone). */
.t-slate  { background: linear-gradient(150deg, #8095ac 0%, #5b7086 100%); }
.t-ochre  { background: linear-gradient(150deg, #e8bb52 0%, #c9942a 100%); }
.t-jade   { background: linear-gradient(150deg, #5da87c 0%, #357a56 100%); }
.t-rose   { background: linear-gradient(150deg, #dd8ba0 0%, #bd5f79 100%); }
.t-basalt { background: linear-gradient(150deg, #58595e 0%, #33343a 100%); }

/* ------------------------------------------------------------- your floor */
.floor-box { width: 100%; display: flex; justify-content: center; }
.floor {
  display: grid; gap: 3px;
  grid-template-columns: repeat(5, 52px); grid-auto-rows: 52px;
  padding: 6px; border-radius: 16px; background: var(--tray);
  position: relative;
}
.cell { position: relative; border-radius: 6px; overflow: hidden; border: 0; padding: 0; background: var(--empty); }
.floor .cell.free {
  background: #fdf6f1; box-shadow: inset 0 0 0 2px rgba(154, 63, 43, .45); cursor: pointer;
}
.floor .cell.free::before {
  content: ""; position: absolute; inset: 36%; border-radius: 50%; background: var(--accent); opacity: .38;
}
.floor .cell.best::before { opacity: .7; }
.cell.fresh { animation: settle 1s ease-out 1 both; }
@keyframes settle { 0% { transform: scale(.82); filter: brightness(1.4); } 45% { transform: none; } 100% { filter: none; } }
.cell .pop {
  position: absolute; inset: 0; display: grid; place-items: center; z-index: 2;
  font-size: 15px; font-weight: 800; color: #fff; text-shadow: 0 1px 3px rgba(0, 0, 0, .6);
  animation: rise 1s ease-out 1 both; pointer-events: none;
}
/* a finished row / column glows */
.floor .cell.in-done { box-shadow: inset 0 0 0 2px rgba(255, 255, 255, .85); }
.floor.laying .cell.free { animation: breathe 1.6s ease-in-out infinite; }
@keyframes breathe { 0%, 100% { box-shadow: inset 0 0 0 2px rgba(154, 63, 43, .45); } 50% { box-shadow: inset 0 0 0 2px rgba(154, 63, 43, .8); } }

/* ------------------------------------------------------------- action bar */
.actionbar {
  position: sticky; bottom: calc(8px + env(safe-area-inset-bottom)); z-index: 5;
  width: fit-content; max-width: 100%; margin: 8px auto 0; min-height: 54px;
  display: flex; gap: 6px; align-items: center; padding: 5px;
  background: var(--card); border-radius: 18px; white-space: nowrap;
  box-shadow: 0 4px 18px rgba(35, 42, 44, .16);
  animation: barin .18s ease-out;
}
.actionbar:empty { visibility: hidden; animation: none; }
@keyframes barin { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
.held { display: inline-flex; align-items: center; gap: 5px; padding: 0 6px 0 4px; font-weight: 700; font-size: 14px; }
.held .tile { width: 26px; height: 26px; flex: none; }
.held .count { font-variant-numeric: tabular-nums; }
.takebox { display: inline-flex; align-items: center; gap: 4px; padding: 0 4px; font-size: 13px; }
.takebox .tile { width: 22px; height: 22px; flex: none; }
.takebox .sep { color: var(--muted); font-size: 12px; padding: 0 2px; }
@media (max-width: 359px) { .actionbar .lbl { display: none; } }

/* ------------------------------------------------------------- overlays */
.overlay {
  position: fixed; inset: 0; z-index: 50; display: grid; place-items: center;
  background: rgba(28, 34, 36, .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(20, 26, 28, .28);
  animation: pop .18s ease-out;
}
.modal.wide { max-width: 620px; }
@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; display: flex; align-items: center; gap: 8px; }
.modal-head h2 .pts { margin-left: auto; font-size: 22px; }
.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 */
.menu-overlay { background: rgba(28, 34, 36, .12); place-items: start end; padding: 48px 10px 0; }
.menu {
  min-width: 214px; background: var(--card); border-radius: 16px; padding: 6px;
  box-shadow: 0 10px 30px rgba(20, 26, 28, .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); }
.menu #menu-online { border-top: 1px solid var(--line); border-radius: 0 0 10px 10px; margin-top: 4px; }
@media (min-width: 1080px) { .menu-overlay { padding-right: calc((100vw - 1060px) / 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; }

/* 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; }
.sub-title { margin: 20px 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }
.parts { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 8px; }
.part {
  display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: 999px;
  background: var(--soft); font-size: 13px; white-space: nowrap;
}
.part .tile { width: 18px; height: 18px; flex: none; border-radius: 4px; }
.part.bonus { background: var(--accent-soft); }
.part.cost { background: rgba(179, 53, 42, .14); color: var(--bad); }
.part.muted { 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; font-size: 10px; margin-top: 1px; }
.log li.course-line { color: var(--muted); font-style: italic; }
.viewer-floor { display: flex; justify-content: center; }
.viewer-floor .floor { padding: 5px; grid-template-columns: repeat(5, var(--vcell)); grid-auto-rows: var(--vcell); }

/* 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; }
.stone-key { list-style: none; padding: 0 !important; display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 6px; }
.stone-key li { display: flex; align-items: center; gap: 8px; }
.stone-key .tile { width: 30px; height: 30px; 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-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; }

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

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