/* Crownlands: a calm, fixed parchment palette (same in light and dark OS modes).
   One accent colour (amber) marks "your action": legal squares, choices, Place. */
:root {
  color-scheme: light;
  --bg: #f3ecdc;
  --card: #fdfaf2;
  --ink: #2b2118;
  --muted: #7d6c57;
  --soft: rgba(43, 33, 24, .06);
  --tray: #e6dac0;
  --empty: #f1e9d6;
  --accent: #a15c05;
  --accent-soft: rgba(161, 92, 5, .16);
  --bad: #c4372b;
  --radius: 14px;
  --vcell: min(52px, calc((100vw - 96px) / 5));
  /* tutorial kit (tutorial.css) */
  --tut-accent: #d9902b;
  --tut-accent-ink: #2b2118;
  --tut-bubble: #fdfaf2;
  --tut-ink: #2b2118;
  --tut-muted: #7d6c57;
  --tut-dim: rgba(43, 33, 24, .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: 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); }
.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,
.domino: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 rgba(43, 33, 24, .18); }
.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: 20px; line-height: 1; }
.btn .lbl { font-size: 14px; }

/* ------------------------------------------------------------- layout */
.app {
  max-width: 1040px; margin: 0 auto;
  padding: 0 max(14px, env(safe-area-inset-right)) calc(12px + env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
  display: grid; gap: 10px; grid-template-columns: minmax(0, 1fr);
}
.side { display: grid; gap: 10px; align-content: start; min-width: 0; }
.board-area { position: relative; min-width: 0; }
@media (min-width: 900px) {
  .app { grid-template-columns: minmax(0, 1fr) 360px; gap: 28px; align-items: start; padding-top: 8px; }
  .board-area { grid-column: 1; grid-row: 1; }
  .side { grid-column: 2; grid-row: 1; gap: 16px; }
}

/* ------------------------------------------------------------- score chips */
.rivals { 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(--card); box-shadow: 0 0 0 2px color-mix(in srgb, var(--pc) 45%, transparent), 0 1px 4px rgba(43, 33, 24, .08); }
.pchip.won .pts::after { content: " \2654"; color: var(--accent); }
.av {
  display: inline-grid; place-items: center; flex: none; width: 28px; height: 28px; 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.bump .av { animation: bump 1.1s ease-out 1 both; }
.pchip .delta {
  position: absolute; right: 2px; top: -6px; font-size: 12px; font-weight: 800; color: var(--pc);
  animation: rise 1.1s 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(-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: 10px; }
}

/* ------------------------------------------------------------- status */
.status {
  display: flex; align-items: center; gap: 9px; min-height: 26px;
  font-size: 16px; 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.thinking .msg::after { content: ""; display: inline-block; width: 1.2em; text-align: left; animation: dots 1.2s steps(4) infinite; }
@keyframes dots { 0% { content: ""; } 25% { content: "."; } 50% { content: ".."; } 75% { content: "..."; } }


/* ------------------------------------------------------------- lines */
.lines { display: grid; gap: 6px; }
.line-row { display: grid; grid-template-columns: 38px minmax(0, 1fr); align-items: center; gap: 4px; padding-top: 7px; }
.line-label { font-size: 11px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .08em; }
.lines.picking #label-next { color: var(--accent); }
.line { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px; }
.domino {
  position: relative; display: flex; gap: 2px; padding: 3px; border-radius: 8px; border: 0;
  background: var(--card); box-shadow: 0 1px 3px rgba(43, 33, 24, .14);
  transition: transform .15s, box-shadow .15s, opacity .25s;
  min-width: 0; font: inherit; text-align: left;
}
.domino .half { flex: 1 1 0; aspect-ratio: 1 / 1; border-radius: 5px; position: relative; overflow: hidden; min-width: 0; }
.domino .dnum {
  position: absolute; top: -8px; left: 50%; transform: translateX(-50%); z-index: 2;
  background: var(--bg); color: var(--muted); font-size: 10.5px; font-weight: 700;
  padding: 0 5px; border-radius: 7px; line-height: 14px;
}
.domino .token {
  position: absolute; right: -5px; bottom: -6px; z-index: 3;
  display: grid; place-items: center; width: 20px; height: 20px; border-radius: 50%;
  background: var(--pc); color: #fff; font-size: 10.5px; font-weight: 800;
  box-shadow: 0 0 0 2px var(--bg);
}
.domino.ph { background: transparent; box-shadow: inset 0 0 0 1.5px rgba(43, 33, 24, .1); }
.domino.active { box-shadow: 0 0 0 3px var(--pc); }
.domino.active.mine { box-shadow: 0 0 0 3px var(--accent); }
.domino.done { opacity: .35; }
.domino.discarded .half { filter: grayscale(1); }
button.domino { cursor: default; }
button.domino.selectable { cursor: pointer; box-shadow: 0 0 0 2px var(--accent-soft), 0 2px 6px rgba(43, 33, 24, .14); }
button.domino.selectable:hover { transform: translateY(-2px); }
button.domino.selected { box-shadow: 0 0 0 3px var(--accent), 0 4px 10px rgba(43, 33, 24, .2); transform: translateY(-3px); }
.domino.flash { animation: flash 1.1s ease-out 1 both; }
@keyframes flash { 0% { transform: translateY(-4px); box-shadow: 0 0 0 5px rgba(161, 92, 5, .55); } 100% { transform: none; box-shadow: 0 1px 3px rgba(43, 33, 24, .14); } }

/* ------------------------------------------------------------- terrain art */
.t-orchard { background: linear-gradient(135deg, #f0d56e 0%, #deb741 100%); }
.t-pinewood { background: linear-gradient(135deg, #4f8f55 0%, #2f6a3a 100%); }
.t-lake { background: linear-gradient(160deg, #5a9bdc 0%, #2f6fb4 100%); }
.t-heath { background: linear-gradient(135deg, #bd8fcb 0%, #93609f 100%); }
.t-bog { background: linear-gradient(160deg, #8c8656 0%, #635e35 100%); }
.t-quarry { background: linear-gradient(135deg, #b1aba2 0%, #858077 100%); }
.t-keep { background: radial-gradient(circle at 50% 40%, #fffaf0 0%, #eadbb6 100%); }
.tile-ico { position: absolute; inset: 14%; width: 72%; height: 72%; display: block; }
.crowns {
  position: absolute; top: 3%; left: 4%; width: 100%; display: flex; gap: 1px; z-index: 1;
  filter: drop-shadow(0 1px 0 rgba(0, 0, 0, .35));
}
.crowns svg { width: 30%; min-width: 7px; max-width: 16px; height: auto; display: block; }

/* ------------------------------------------------------------- realm grid */
.realm-box { width: 100%; display: flex; justify-content: center; }
.realm {
  display: grid; gap: 3px;
  grid-template-columns: repeat(5, 48px); grid-auto-rows: 48px;
  padding: 6px; border-radius: 16px; background: var(--tray);
}
.cell { position: relative; border-radius: 6px; overflow: hidden; }
.cell.empty { background: var(--empty); }
.realm .cell.empty.hint { background: #fbf5e6; box-shadow: inset 0 0 0 2px rgba(161, 92, 5, .5); cursor: pointer; }
.realm .cell.hint::before {
  content: ""; position: absolute; inset: 38%; border-radius: 50%; background: var(--accent); opacity: .45;
}
.realm.previewing .cell.empty.hint { box-shadow: inset 0 0 0 1.5px rgba(161, 92, 5, .28); }
.realm.previewing .cell.hint::before { opacity: .2; }
.realm.interactive .cell { cursor: pointer; }
.cell.ghost { overflow: visible; z-index: 1; animation: ghostin .15s ease-out; }
.cell.ghost.ok { box-shadow: inset 0 0 0 2px rgba(255, 255, 255, .9), 0 0 0 3px var(--accent), 0 6px 14px rgba(43, 33, 24, .28); }
.cell.ghost.bad { box-shadow: 0 0 0 3px var(--bad); }
.cell.ghost.bad::after {
  content: ""; position: absolute; inset: 0; border-radius: inherit;
  background: repeating-linear-gradient(45deg, rgba(196, 55, 43, .45) 0 6px, transparent 6px 12px);
}
.cell.ghost.anchor::before {
  content: ""; position: absolute; right: 4px; bottom: 4px; width: 8px; height: 8px; border-radius: 50%;
  background: #fff; box-shadow: 0 0 0 2px rgba(0, 0, 0, .45); z-index: 2;
}
@keyframes ghostin { from { transform: scale(.9); opacity: .4; } to { transform: none; opacity: 1; } }
.cell.recent { box-shadow: inset 0 0 0 2px rgba(255, 255, 255, .75); }
.cell.fresh { animation: fresh 1.1s ease-out 1 both; }
@keyframes fresh { 0% { filter: brightness(1.45); transform: scale(.9); } 35% { transform: none; } 100% { filter: none; } }

/* ------------------------------------------------------------- action bar */
.actionbar {
  /* In flow under the board, but sticky: on short screens it stays on screen. */
  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(43, 33, 24, .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; } }
@media (max-width: 359px) { .actionbar .lbl { display: none; } }

/* ------------------------------------------------------------- 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, 20, 8, .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; 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(43, 30, 16, .12); place-items: start end; padding: 50px 10px 0; }
.menu {
  min-width: 210px; background: var(--card); border-radius: 16px; padding: 6px;
  box-shadow: 0 10px 30px rgba(30, 20, 8, .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: 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; }

/* 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); }
.regions { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 8px; }
.reg {
  display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px 3px 4px; border-radius: 999px;
  background: var(--soft); font-size: 13px; white-space: nowrap;
}
.reg .sw { width: 20px; height: 20px; border-radius: 50%; position: relative; overflow: hidden; flex: none; }
.reg .sw .tile-ico { inset: 8%; width: 84%; height: 84%; }
.reg.bonus { background: var(--accent-soft); }
.reg.lost { opacity: .5; text-decoration: line-through; }
.reg.muted { color: var(--muted); padding-left: 10px; }
.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.round-line { color: var(--muted); font-style: italic; }
.viewer-realm { display: flex; justify-content: center; }
.viewer-realm .realm { padding: 5px; }
.viewer-realm .cell.recent { box-shadow: inset 0 0 0 2px #fff; }

/* 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; }
.terrain-key { list-style: none; padding: 0 !important; display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 6px; }
.terrain-key li { display: flex; align-items: center; gap: 8px; }
.terrain-key .sw { width: 30px; height: 30px; border-radius: 6px; position: relative; overflow: hidden; 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: 28px; 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): a presence line under the status, in the same grid row. */
.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; }
body.is-online #btn-new { display: none; }
/* The online row sits above How to play and carries the menu's one divider (online, Leave is the
   first row, so it has none). */
.menu #menu-online { border-top: 1px solid rgba(43, 30, 16, .12); border-radius: 0 0 10px 10px; margin-top: 4px; }
#over-leave { display: flex; width: 100%; margin-top: 8px; }
