/* Kata, ink, rice paper and a wooden practice mat. Fixed light palette. */
:root {
  color-scheme: light;
  --paper: #efe4cf;
  --paper-2: #e5d6ba;
  --card: #fbf6ea;
  --ink: #2a211b;
  --muted: #6f5f50;
  --line: #cbb68f;
  --wood: #e3c897;
  --wood-2: #dabd89;
  --frame: #5b3f2a;
  --frame-2: #3f2b1c;
  --sun: #c2412d;
  --sun-dark: #6e2014;
  --sun-light: #f6d27a;
  --moon: #2e4a7d;
  --moon-dark: #15254a;
  --moon-light: #e4ebf7;
  --jade: #2f7a57;
  --gold: #e0a93b;
  --danger: #b3261e;
  /* tutorial kit (tutorial.css) */
  --tut-accent: #e0a93b;
  --tut-accent-ink: #2a211b;
  --tut-bubble: #fbf6ea;
  --tut-ink: #2a211b;
  --tut-muted: #6f5f50;
  --tut-dim: rgba(42, 33, 27, .58);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--paper);
  background-image: radial-gradient(ellipse at 50% 0%, #f6eedd 0%, var(--paper) 60%, #e7d9bf 100%);
  color: var(--ink);
  font: 15px/1.4 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 ---------- */
.app {
  position: relative;
  display: flex; flex-direction: column;
  height: 100vh; height: 100dvh;
  max-width: 1100px; margin: 0 auto;
  padding: env(safe-area-inset-top) max(12px, env(safe-area-inset-right)) env(safe-area-inset-bottom) max(12px, env(safe-area-inset-left));
}
.topbar { flex: none; display: grid; grid-template-columns: 48px 1fr 48px; align-items: center; min-height: 52px; }
.back {
  display: inline-flex; align-items: center; justify-content: center; width: 44px; height: 44px;
  color: var(--muted); text-decoration: none; font-size: 24px; border-radius: 22px;
}
.back:hover, .back:focus-visible { color: var(--ink); background: rgba(255, 255, 255, .5); }
.title { margin: 0; text-align: center; font-size: 19px; font-weight: 750; letter-spacing: .06em; }
.iconbtn {
  width: 44px; height: 44px; border: 0; border-radius: 22px; background: transparent;
  font-size: 22px; line-height: 1; color: var(--ink); cursor: pointer; justify-self: end;
}
.iconbtn:hover, .iconbtn[aria-expanded="true"] { background: rgba(255, 255, 255, .55); }
.menu {
  position: absolute; z-index: 15; top: calc(env(safe-area-inset-top) + 50px); right: max(12px, env(safe-area-inset-right));
  min-width: 210px; padding: 6px; border-radius: 16px; background: var(--card);
  box-shadow: 0 12px 32px rgba(63, 43, 28, .22), 0 0 0 1px var(--line);
  display: flex; flex-direction: column; animation: rise .16s ease-out;
}
.menu button {
  min-height: 48px; padding: 0 14px; border: 0; border-radius: 10px; background: transparent;
  text-align: left; font-weight: 600; cursor: pointer;
}
.menu button:hover, .menu button:focus-visible { background: var(--paper); outline: none; }
.menu button:disabled { opacity: .4; cursor: default; background: transparent; }

.btn {
  min-height: 48px; padding: 0 20px; border-radius: 24px;
  border: 0; background: var(--card); color: var(--ink); box-shadow: 0 0 0 1px var(--line);
  font-weight: 700; cursor: pointer; transition: transform .12s, background .15s, opacity .15s;
}
.btn:active { transform: scale(.97); }
.btn.primary { background: var(--ink); color: var(--paper); box-shadow: none; }
.btn.wide { width: 100%; min-height: 52px; font-size: 17px; }
.btn:disabled { opacity: .35; cursor: default; transform: none; }
.btn:focus-visible, .card:focus-visible, .cell:focus-visible, .seg button:focus-visible, .iconbtn:focus-visible, .back:focus-visible, .link:focus-visible {
  outline: 3px solid var(--gold); outline-offset: 2px;
}
.link { display: block; margin: 6px auto 0; min-height: 44px; padding: 0 16px; border: 0; background: transparent; color: var(--muted); cursor: pointer; }

/* ---------- game layout (portrait first) ---------- */
.game {
  flex: 1; min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  grid-template-areas: "top" "status" "board" "bottom";
  gap: 8px; padding: 0 0 12px;
  width: 100%; max-width: 560px; margin: 0 auto;
}
.side-panel.top { grid-area: top; display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.side-panel.bottom { grid-area: bottom; }
.status { grid-area: status; }
.board-wrap { grid-area: board; }

.badge {
  display: inline-flex; align-items: center; gap: 7px; min-height: 40px; cursor: pointer;
  font-size: 14px; font-weight: 650; padding: 0 14px 0 10px; border-radius: 20px;
  background: rgba(255, 255, 255, .45); color: var(--muted); transition: background .2s, color .2s, box-shadow .2s;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0;
}
.badge .dot { flex: none; }
.badge.active { background: var(--card); color: var(--ink); box-shadow: 0 0 0 2px var(--ink); }
.dot { display: inline-block; width: 12px; height: 12px; border-radius: 50%; vertical-align: -1px; }
.dot.sun { background: var(--sun); box-shadow: 0 0 0 2px var(--sun-light) inset; }
.dot.moon { background: var(--moon); box-shadow: 0 0 0 2px var(--moon-light) inset; }

.hand { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.hand.small { display: flex; gap: 6px; flex: none; }
.hand.small .card { width: 58px; padding: 4px; }
.hand.small .card-name { display: none; }
.hand.small .card-grid { max-width: 46px; }
.hand.small .stamp { display: none; }

/* ---------- cards ---------- */
.card {
  position: relative; display: flex; flex-direction: column; align-items: center; gap: 3px;
  padding: 6px 4px 7px; min-height: 44px; min-width: 0;
  background: var(--card); border: 0; border-radius: 12px;
  box-shadow: 0 0 0 1px var(--line), 0 2px 4px rgba(63, 43, 28, .08);
  cursor: default; transition: transform .16s, box-shadow .16s, opacity .16s;
}
.card.usable { cursor: pointer; }
.card.selected { transform: translateY(-3px); box-shadow: 0 0 0 3px var(--gold), 0 6px 12px rgba(63, 43, 28, .15); }
.card.dim { opacity: .5; }
.card.side { background: rgba(255, 255, 255, .35); box-shadow: none; outline: 2px dashed rgba(111, 95, 80, .4); outline-offset: -2px; }
.card.spent { animation: spent 1.4s ease-out; }
@keyframes spent { 0%, 40% { background: #fff3cf; box-shadow: 0 0 0 3px var(--gold); } 100% { background: rgba(255, 255, 255, .35); box-shadow: none; } }
.card-name {
  font-size: 12px; font-weight: 700; color: var(--muted); line-height: 1.2;
  max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding: 0 15px;
}
.card.usable .card-name { color: var(--ink); }
.card-grid { display: block; width: 100%; max-width: 72px; aspect-ratio: 1; height: auto; }
.card .stamp { position: absolute; top: 6px; right: 6px; width: 10px; height: 10px; opacity: .8; }

/* ---------- status (one line) ---------- */
.status {
  display: flex; align-items: center; justify-content: center; gap: 8px; min-height: 26px;
  font-weight: 700; font-size: 16px; white-space: nowrap; overflow: hidden;
}
.status::before { content: ''; width: 10px; height: 10px; border-radius: 50%; flex: none; background: transparent; }
.status.p-sun::before { background: var(--sun); }
.status.p-moon::before { background: var(--moon); }
#status-main { overflow: hidden; text-overflow: ellipsis; }
#status-main.warn { color: var(--danger); }
.thinking::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: '...'; } }
.badge.thinking::after { content: none; }

/* ---------- board ---------- */
.board-wrap { min-height: 0; min-width: 0; display: flex; align-items: center; justify-content: center; }
.board {
  width: min(94vw, 56vh, 600px);
  aspect-ratio: 1;
  display: grid; grid-template-columns: repeat(5, 1fr); grid-template-rows: repeat(5, 1fr);
  gap: 3px; padding: 6px;
  background: linear-gradient(145deg, var(--frame), var(--frame-2));
  border-radius: 14px;
  box-shadow: 0 8px 20px rgba(63, 43, 28, .25);
}
@supports (container-type: size) {
  .board-wrap { container-type: size; }
  .board { width: min(100cqw, 100cqh, 600px); }
}
.cell {
  position: relative; border: 0; padding: 0; margin: 0; min-width: 0; min-height: 0;
  background: var(--wood); border-radius: 4px;
  display: grid; place-items: center; cursor: pointer;
  box-shadow: inset 0 0 0 1px rgba(91, 63, 42, .18);
  transition: background .15s, box-shadow .15s;
}
.cell.alt { background: var(--wood-2); }
.cell > * { grid-area: 1 / 1; }
.cell .piece { width: 90%; height: 90%; pointer-events: none; transition: transform .18s; z-index: 1; }
.cell .shrine-mark { width: 70%; height: 70%; pointer-events: none; opacity: .5; }

.cell.last-from { box-shadow: inset 0 0 0 3px rgba(224, 169, 59, .6); }
.cell.last-to { background: #ecd49e; box-shadow: inset 0 0 0 3px var(--gold); }
.cell.movable { box-shadow: inset 0 0 0 3px rgba(47, 122, 87, .45); }
.cell.sel { background: #f3df9f; box-shadow: inset 0 0 0 3px var(--jade); }
.cell.sel .piece { transform: scale(1.08) translateY(-2px); }
.cell.target::after {
  content: ''; grid-area: 1 / 1; width: 28%; height: 28%; border-radius: 50%;
  background: rgba(47, 122, 87, .8); z-index: 3; pointer-events: none;
}
.cell.target.capture::after {
  width: 90%; height: 90%; background: transparent;
  border: 4px solid rgba(47, 122, 87, .9); box-sizing: border-box;
}
.cell.threat .piece { animation: threat 1.1s ease-in-out infinite; }
@keyframes threat {
  0%, 100% { filter: drop-shadow(0 0 0 rgba(179, 38, 30, 0)); }
  50% { filter: drop-shadow(0 0 6px rgba(179, 38, 30, .95)); }
}
.cell.arrive .piece { animation: arrive .28s ease-out; }
@keyframes arrive { from { transform: scale(.75); opacity: .5; } to { transform: scale(1); opacity: 1; } }

/* ---------- overlays ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 20;
  background: rgba(42, 33, 27, .4);
  display: flex; align-items: flex-end; justify-content: center;
  padding: 12px;
  animation: fade .18s ease-out;
}
.overlay.soft { background: rgba(239, 228, 207, .55); align-items: center; }
@media (min-width: 600px) { .overlay { align-items: center; } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes rise { from { transform: translateY(14px); opacity: .6; } to { transform: none; opacity: 1; } }
.sheet {
  background: var(--card); color: var(--ink);
  width: 100%; max-width: 440px; max-height: calc(100vh - 24px); max-height: calc(100dvh - 24px);
  overflow: auto; overscroll-behavior: contain;
  border-radius: 24px; padding: 18px 20px 20px;
  box-shadow: 0 20px 50px rgba(42, 33, 27, .25);
  animation: rise .22s ease-out;
}
.sheet h2 { margin: 0 0 6px; font-size: 21px; }
.sheet-actions { display: flex; gap: 10px; margin-top: 18px; }
.sheet-actions .btn { flex: 1; }
.fine { font-size: 13px; color: var(--muted); margin: 6px 0; }

.choice { border: 0; margin: 14px 0 0; padding: 0; min-width: 0; }
.choice legend { font-size: 13px; color: var(--muted); margin-bottom: 6px; padding: 0; }
.seg { display: flex; gap: 4px; padding: 4px; border-radius: 16px; background: var(--paper); }
.seg button {
  flex: 1 1 0; min-height: 44px; padding: 0 8px; border-radius: 12px;
  border: 0; background: transparent; font-weight: 600; color: var(--muted); cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; white-space: nowrap;
}
.seg button[aria-pressed="true"] { background: var(--card); color: var(--ink); box-shadow: 0 1px 3px rgba(42, 33, 27, .2); }
.choice:disabled { opacity: .4; }

.chooser-cards { display: flex; gap: 12px; justify-content: center; margin-top: 10px; }
.chooser-cards .card { width: 120px; padding: 8px 6px 10px; }
.chooser-cards .card-grid { max-width: 84px; }
.chooser-cards .card-name { font-size: 14px; }

.over-sheet { text-align: center; max-width: 360px; padding-bottom: 10px; }
.over-mark { display: flex; justify-content: center; gap: 6px; margin-bottom: 4px; }
.over-mark svg { width: 70px; height: 70px; }
.over-sheet h2 { font-size: 28px; margin: 2px 0; }
.over-way { margin: 0; font-weight: 700; color: var(--muted); font-size: 15px; }
.over-sheet .btn.wide { margin-top: 16px; }

.help-sheet, .info-sheet { max-width: 620px; padding: 0; display: flex; flex-direction: column; }
.help-head {
  position: sticky; top: 0; display: flex; align-items: center; justify-content: space-between;
  padding: 8px 10px 8px 20px; background: var(--card); border-bottom: 1px solid var(--paper-2); z-index: 1;
}
.help-head h2 { margin: 0; font-size: 20px; }
.help-body { padding: 4px 20px 20px; }
.help-body h3 { font-size: 16px; margin: 18px 0 4px; }
.help-body p, .help-body li { font-size: 15px; }
.help-body ul, .help-body ol { padding-left: 20px; margin: 6px 0; }
.t-sun { color: var(--sun); }
.t-moon { color: var(--moon); }
.card-gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 8px; margin-top: 8px; }
.info-players { list-style: none; padding: 0 !important; margin: 10px 0 !important; }
.info-players li { display: flex; align-items: center; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--paper-2); }
.info-players .caps { margin-left: auto; display: flex; gap: 2px; align-items: center; }
.info-players .caps svg { width: 22px; height: 22px; }
.info-players small { color: var(--muted); }
.movelist { font-size: 14px; color: var(--ink); }
.movelist li { margin: 3px 0; }

/* tall phones: roomier hand cards */
@media (max-aspect-ratio: 1/1) and (min-height: 780px) {
  .side-panel.bottom .card-grid { max-width: 88px; }
  .side-panel.bottom .card-name { font-size: 13px; }
  .side-panel.bottom .card { padding: 8px 4px 9px; }
}

/* ---------- small phones ---------- */
@media (max-width: 380px) {
  .app { padding-left: max(8px, env(safe-area-inset-left)); padding-right: max(8px, env(safe-area-inset-right)); }
  .hand { gap: 6px; }
  .hand.small .card { width: 52px; }
  .badge { font-size: 13px; padding: 0 10px 0 8px; }
}

/* ---------- short portrait screens: smaller card grids so the board keeps 40px+ cells ---------- */
@media (max-aspect-ratio: 1/1) and (max-height: 700px) {
  .card-grid { max-width: 52px; }
  .hand.small .card-grid { max-width: 38px; }
  .hand.small .card { width: 48px; }
  .card-name { font-size: 11px; }
  .game { gap: 6px; }
}
@media (max-aspect-ratio: 1/1) and (max-height: 580px) {
  .card-grid { max-width: 40px; }
}

/* ---------- wide / landscape: panels left, board right ---------- */
@media (min-aspect-ratio: 1/1) and (min-width: 560px) {
  .game {
    max-width: 1100px;
    grid-template-columns: minmax(260px, 360px) minmax(0, 1fr);
    grid-template-rows: 1fr auto auto auto 1fr;
    grid-template-areas: ". board" "top board" "status board" "bottom board" ". board";
    column-gap: 28px; row-gap: 16px;
  }
  .side-panel.top { flex-direction: column; align-items: flex-start; }
  .hand.small { justify-content: flex-start; }
  .board { width: min(60vw, 80vh, 700px); }
  @supports (container-type: size) { .board { width: min(100cqw, 100cqh, 700px); } }
  .status { justify-content: flex-start; }
}
@media (min-aspect-ratio: 1/1) and (max-height: 460px) {
  .topbar { min-height: 42px; }
  .card-grid { max-width: 44px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

/* Online play (online.js): a presence line under the turn line, in the same grid row. */
.status-row { grid-area: status; display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.net { color: var(--muted); }
.over-sheet #over-leave { margin-top: 8px; }
