:root {
  --gold: #d9b45a;
  --gold-hi: #f3d98b;
  --ink: #ece2cc;
  --muted: #a99e8a;
  --glass: rgba(14, 12, 16, 0.72);
  --glass-hi: rgba(28, 24, 30, 0.86);
  --line: rgba(217, 180, 90, 0.28);
  --serif: "Cinzel", "Trajan Pro", "Palatino Linotype", "Book Antiqua", Palatino, Georgia, serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #050407; color: var(--ink); font-family: var(--sans); overscroll-behavior: none; }
button { font: inherit; color: inherit; cursor: pointer; }

#stage { position: fixed; inset: 0; touch-action: none; }
#stage canvas { display: block; width: 100%; height: 100%; outline: none; }

/* ---------- loading ---------- */
#loading {
  position: fixed; inset: 0; z-index: 50; display: grid; place-items: center;
  background: radial-gradient(ellipse at center, #1a1510 0%, #050407 70%);
  transition: opacity 0.8s ease;
}
#loading.done { opacity: 0; pointer-events: none; }
.loading-inner { text-align: center; width: min(320px, 80vw); }
.loading-inner h1 { font-family: var(--serif); letter-spacing: 0.12em; color: var(--gold-hi); font-weight: 600; margin: 0.2em 0 1em; }
.bar { height: 3px; background: rgba(255,255,255,0.08); border-radius: 2px; overflow: hidden; }
.bar span { display: block; height: 100%; width: 0; background: linear-gradient(90deg, #8a6a1f, var(--gold-hi)); transition: width 0.3s ease; }
#load-text { color: var(--muted); font-size: 0.85rem; margin-top: 0.9em; font-style: italic; }
.crest { font-size: 3rem; line-height: 1; color: var(--gold); text-shadow: 0 0 24px rgba(217, 180, 90, 0.5); }
#over-crest[data-kind="lose"] { color: #c0503a; }
#over-crest[data-kind="draw"] { color: #a99e8a; }
.crest-svg { width: 64px; height: 64px; filter: drop-shadow(0 0 14px rgba(217, 180, 90, 0.45)); }

/* ---------- HUD ---------- */
.hud { position: fixed; z-index: 10; transition: opacity 0.4s ease; }
body.menu-open .hud { opacity: 0 !important; pointer-events: none !important; }

#topbar {
  top: 0; left: 0; right: 0; padding: calc(10px + var(--safe-t)) 14px 10px;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  background: linear-gradient(180deg, rgba(5,4,7,0.85), rgba(5,4,7,0));
  pointer-events: none;
}
#topbar > * { pointer-events: auto; }
.brand { font-family: var(--serif); color: var(--gold-hi); letter-spacing: 0.14em; font-size: 1.05rem; text-shadow: 0 2px 10px #000; white-space: nowrap; }
.status {
  display: flex; align-items: center; gap: 8px; padding: 7px 16px; border-radius: 999px;
  background: var(--glass); border: 1px solid var(--line); backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
  font-size: 0.92rem; max-width: 60vw; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.status .dot { width: 9px; height: 9px; border-radius: 50%; background: #eee; box-shadow: 0 0 0 2px rgba(255,255,255,0.15); flex: none; }
.status.black .dot { background: #222; box-shadow: 0 0 0 2px rgba(255,255,255,0.35); }
.status.thinking .dot { animation: pulse 1s ease-in-out infinite; }
.status.check { border-color: rgba(255, 80, 60, 0.7); box-shadow: 0 0 18px rgba(255, 60, 40, 0.35); }

.icon-btn {
  width: 46px; height: 46px; border-radius: 50%; display: grid; place-items: center;
  background: var(--glass); border: 1px solid var(--line); backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
  transition: transform 0.15s ease, background 0.2s ease, border-color 0.2s;
}
.icon-btn:hover { background: var(--glass-hi); border-color: var(--gold); }
.icon-btn:active { transform: scale(0.92); }
.icon-btn:disabled { opacity: 0.35; pointer-events: none; }
.icon-btn svg, .mic svg { width: 22px; height: 22px; fill: none; stroke: var(--ink); stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
#btn-menu { width: 42px; height: 42px; }

/* ---------- side panel ---------- */
#panel {
  top: calc(70px + var(--safe-t)); right: 14px; bottom: 120px; width: 250px;
  display: flex; flex-direction: column; gap: 10px; padding: 12px;
  background: var(--glass); border: 1px solid var(--line); border-radius: 16px;
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
}
.player .who { display: flex; align-items: baseline; gap: 8px; }
.player .swatch { width: 11px; height: 11px; border-radius: 50%; background: #eee; flex: none; transform: translateY(1px); }
.player.black .swatch { background: #1a1a1a; box-shadow: 0 0 0 1px rgba(255,255,255,0.4); }
.player .name { font-family: var(--serif); color: var(--gold-hi); font-size: 0.95rem; }
.player .sub { color: var(--muted); font-size: 0.75rem; margin-left: auto; }
.player .captured { min-height: 22px; font-size: 1.15rem; letter-spacing: -0.12em; color: var(--ink); margin-top: 3px; }
.player .captured .adv { font-size: 0.75rem; letter-spacing: 0; color: var(--muted); margin-left: 8px; vertical-align: middle; }
.player.turn .name::after { content: " •"; color: var(--gold); }
#movelist {
  flex: 1; overflow-y: auto; margin: 0; padding: 8px 4px; list-style: none; counter-reset: mv;
  border-top: 1px solid var(--line); border-bottom: 1px solid var(--line);
  display: grid; grid-template-columns: 2.2em 1fr 1fr; align-content: start; row-gap: 2px; font-size: 0.9rem;
  scrollbar-width: thin; scrollbar-color: var(--line) transparent;
}
#movelist li { display: contents; }
#movelist .n { color: var(--muted); font-size: 0.8rem; padding: 3px 0; }
#movelist .m { padding: 3px 6px; border-radius: 6px; font-variant-numeric: tabular-nums; }
#movelist .m.last { background: rgba(217, 180, 90, 0.18); color: var(--gold-hi); }

/* ---------- dock ---------- */
#dock {
  left: 50%; transform: translateX(-50%); bottom: calc(14px + var(--safe-b));
  display: flex; flex-direction: column; align-items: center; gap: 10px; width: min(560px, 96vw);
}
#dock .controls { display: flex; align-items: center; gap: 14px; }
.mic {
  position: relative; width: 74px; height: 74px; border-radius: 50%; display: grid; place-items: center;
  background: radial-gradient(circle at 35% 30%, #3a2f22, #15110d 70%); border: 1.5px solid var(--gold);
  box-shadow: 0 6px 24px rgba(0,0,0,0.6), inset 0 1px 0 rgba(255,255,255,0.08);
  transition: transform 0.15s ease, box-shadow 0.3s ease;
}
.mic svg { width: 30px; height: 30px; stroke: var(--gold-hi); }
.mic:active { transform: scale(0.94); }
.mic .ring { position: absolute; inset: -6px; border-radius: 50%; border: 2px solid var(--gold); opacity: 0; pointer-events: none; }
.mic.listening { background: radial-gradient(circle at 35% 30%, #7a2a1a, #2a0d08 70%); box-shadow: 0 0 34px rgba(255, 120, 60, 0.55); border-color: #ffb070; }
.mic.listening .ring { animation: ripple 1.4s ease-out infinite; border-color: #ffb070; }
.mic.unsupported { opacity: 0.45; }

.bubble {
  min-height: 0; max-width: 100%; padding: 0 16px; border-radius: 14px; text-align: center;
  background: var(--glass-hi); border: 1px solid var(--line); font-size: 0.98rem;
  opacity: 0; transform: translateY(6px); transition: opacity 0.25s ease, transform 0.25s ease, padding 0.2s;
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
.bubble.show { opacity: 1; transform: none; padding: 9px 16px; }
.bubble .heard { font-style: italic; color: var(--muted); }
.bubble .ok { color: #9fe2a0; font-weight: 600; }
.bubble .bad { color: #ff9b85; font-weight: 600; }

#cmdform { width: 100%; display: flex; justify-content: center; }
#cmd {
  width: min(380px, 92vw); padding: 10px 14px; border-radius: 12px; font-size: 16px;
  background: var(--glass); color: var(--ink); border: 1px solid var(--line); outline: none;
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
#cmd:focus { border-color: var(--gold); }
#cmd::placeholder { color: #7c7366; }
#btn-type, #btn-moves { display: none; }

/* ---------- toasts ---------- */
#toasts { position: fixed; z-index: 40; top: calc(64px + var(--safe-t)); left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; align-items: center; gap: 8px; pointer-events: none; }
.toast {
  padding: 9px 18px; border-radius: 12px; background: var(--glass-hi); border: 1px solid var(--line);
  font-size: 0.95rem; animation: toast-in 0.3s ease both; backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
  text-align: center; max-width: 88vw;
}
.toast.big { font-family: var(--serif); font-size: 1.6rem; letter-spacing: 0.12em; color: var(--gold-hi); padding: 12px 28px; border-color: var(--gold); }
.toast.danger { border-color: rgba(255, 90, 60, 0.7); color: #ffc2b0; }
.toast.out { animation: toast-out 0.4s ease both; }

/* ---------- modals ---------- */
.modal {
  position: fixed; inset: 0; z-index: 30; display: grid; place-items: center; padding: 16px;
  background: radial-gradient(ellipse at center, rgba(5,4,7,0.35), rgba(5,4,7,0.8));
  opacity: 0; pointer-events: none; transition: opacity 0.35s ease;
}
.modal.open { opacity: 1; pointer-events: auto; }
#menu { background: linear-gradient(180deg, rgba(5,4,7,0.2), rgba(5,4,7,0.65) 60%); }
.sheet {
  width: min(520px, 100%); max-height: calc(100vh - 32px); max-height: calc(100dvh - 32px); overflow-y: auto;
  padding: 26px 24px 20px; border-radius: 20px; text-align: center;
  background: linear-gradient(180deg, rgba(26, 22, 26, 0.92), rgba(12, 10, 13, 0.94));
  border: 1px solid var(--line); box-shadow: 0 30px 80px rgba(0,0,0,0.7), inset 0 1px 0 rgba(255,255,255,0.05);
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  transform: translateY(14px) scale(0.98); transition: transform 0.35s ease;
}
.modal.open .sheet { transform: none; }
.sheet.compact { width: min(400px, 100%); }
.sheet h1 { font-family: var(--serif); font-weight: 600; letter-spacing: 0.12em; color: var(--gold-hi); margin: 6px 0 4px; font-size: clamp(1.6rem, 6vw, 2.2rem); }
.sheet h2 { font-family: var(--serif); font-weight: 600; letter-spacing: 0.08em; color: var(--gold-hi); margin: 4px 0 12px; }
.sheet h3 { font-family: var(--serif); color: var(--gold); font-weight: 600; font-size: 0.95rem; margin: 6px 0; }
.tag { color: var(--muted); margin: 0 0 18px; font-style: italic; }
.field { margin: 14px 0; text-align: left; }
.field > label, .field summary { display: block; font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.14em; color: var(--muted); margin-bottom: 7px; }
.field summary { cursor: pointer; }
.seg { display: flex; gap: 6px; background: rgba(0,0,0,0.3); padding: 4px; border-radius: 12px; border: 1px solid rgba(255,255,255,0.06); }
.seg button { flex: 1; padding: 9px 6px; border-radius: 9px; border: 1px solid transparent; background: transparent; color: var(--muted); transition: all 0.2s; }
.seg button:hover { color: var(--ink); }
.seg button.on { background: linear-gradient(180deg, rgba(217,180,90,0.25), rgba(217,180,90,0.12)); border-color: rgba(217,180,90,0.6); color: var(--gold-hi); }
.seg-wrap { display: grid; grid-template-columns: 1fr 1fr; }
.seg-wrap button { display: flex; flex-direction: column; gap: 2px; padding: 9px 8px; }
.seg-wrap b { font-family: var(--serif); font-weight: 600; font-size: 0.95rem; }
.seg-wrap small { font-size: 0.72rem; opacity: 0.8; }
.seg.small button { font-size: 0.85rem; padding: 7px; }
.more .check { display: flex; align-items: center; gap: 10px; margin: 8px 0; font-size: 0.92rem; text-transform: none; letter-spacing: 0; color: var(--ink); }
.more .check input { accent-color: var(--gold); width: 18px; height: 18px; }
.more .seg { margin-top: 10px; }
.actions { display: flex; gap: 10px; justify-content: center; margin-top: 18px; flex-wrap: wrap; }
.btn { padding: 12px 26px; border-radius: 12px; border: 1px solid var(--line); background: rgba(255,255,255,0.04); font-family: var(--serif); letter-spacing: 0.1em; font-size: 1rem; transition: all 0.2s; }
.btn.primary { background: linear-gradient(180deg, #e3c06a, #a77d26); color: #1b1408; border-color: #f3d98b; font-weight: 700; box-shadow: 0 6px 20px rgba(217,180,90,0.25); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost:hover { border-color: var(--gold); }
.link { background: none; border: none; color: var(--gold); margin-top: 14px; font-size: 0.88rem; text-decoration: underline; text-underline-offset: 3px; opacity: 0.85; }
.cols { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; text-align: left; }
.cols ul { margin: 0; padding-left: 18px; color: var(--ink); font-size: 0.9rem; line-height: 1.7; }
.note { color: var(--muted); font-size: 0.82rem; margin-top: 14px; }
kbd { font-family: ui-monospace, monospace; font-size: 0.8em; padding: 1px 6px; border-radius: 5px; border: 1px solid var(--line); background: rgba(255,255,255,0.05); }
.promo-choices { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.promo-choices button { display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 12px 4px; border-radius: 12px; background: rgba(255,255,255,0.04); border: 1px solid var(--line); font-size: 0.8rem; color: var(--muted); }
.promo-choices button span { font-size: 2.1rem; color: var(--ink); }
.promo-choices button:hover { border-color: var(--gold); color: var(--gold-hi); }
.noscript { position: fixed; inset: 0; display: grid; place-items: center; }

@keyframes pulse { 50% { opacity: 0.35; } }
@keyframes ripple { from { transform: scale(0.9); opacity: 0.9; } to { transform: scale(1.5); opacity: 0; } }
@keyframes toast-in { from { opacity: 0; transform: translateY(-8px) scale(0.96); } to { opacity: 1; transform: none; } }
@keyframes toast-out { to { opacity: 0; transform: translateY(-8px); } }

/* ---------- small screens ---------- */
@media (max-width: 900px) {
  #panel {
    top: auto; right: 10px; left: 10px; width: auto; bottom: calc(150px + var(--safe-b)); max-height: 46vh;
    transform: translateY(16px); opacity: 0; pointer-events: none; transition: transform 0.3s ease, opacity 0.3s ease;
  }
  body.panel-open #panel { transform: none; opacity: 1; pointer-events: auto; }
  .brand { display: none; }
  #topbar { justify-content: center; }
  #btn-menu { position: absolute; right: 12px; top: calc(10px + var(--safe-t)); }
  #cmdform { display: none; }
  body.typing #cmdform { display: flex; }
  #btn-type, #btn-moves { display: grid; }
  .status { max-width: calc(100vw - 120px); }
}
@media (max-width: 420px) {
  #dock .controls { gap: 10px; }
  .icon-btn { width: 44px; height: 44px; }
  .mic { width: 68px; height: 68px; }
  .cols { grid-template-columns: 1fr; }
  .sheet { padding: 20px 16px 16px; }
}
@media (max-height: 520px) and (orientation: landscape) {
  #panel { top: calc(56px + var(--safe-t)); bottom: 12px; width: 210px; transform: none; opacity: 1; pointer-events: auto; left: auto; max-height: none; }
  body.panel-open #panel { transform: none; }
  #dock { left: calc(10px + env(safe-area-inset-left, 0px)); top: calc(54px + var(--safe-t)); bottom: 10px; transform: none; width: auto; justify-content: center; }
  #dock .controls { flex-direction: column; gap: 8px; }
  #dock .icon-btn { width: 40px; height: 40px; }
  #dock .mic { width: 58px; height: 58px; }
  #dock .bubble { position: fixed; left: 50%; bottom: 12px; transform: translateX(-50%); max-width: 60vw; }
  #dock .bubble.show { transform: translateX(-50%); }
  #cmdform { display: none; }
  body.typing #cmdform { display: flex; position: fixed; left: 50%; transform: translateX(-50%); top: calc(56px + var(--safe-t)); width: auto; }
  #btn-moves { display: none; }
}
