:root {
  --bg: #1a120e;
  --bg-2: #2e221c;
  --ink: #efe8e0;
  --muted: #b4a498;
  --panel: rgba(32, 24, 20, 0.86);
  --line: rgba(210, 190, 170, 0.16);
  --accent: #c2a08a;
  --accent-2: #5a4336;
  --danger: #e07070;
  --ok: #7dcea0;
  --shadow: 0 24px 60px rgba(0, 0, 0, 0.45);
  --square-light: #cbb59a;
  --square-dark: #6a4530;
  --piece-white: #f4f4f2;
  --piece-black: #1c1a17;
  --radius: 16px;
  --font: "Outfit", "Segoe UI", sans-serif;
  --serif: "Cormorant Garamond", Georgia, serif;
}

.theme-dark {
  --bg: #0c1222;
  --bg-2: #151c30;
  --ink: #f3ead6;
  --muted: #b7ad93;
  --panel: rgba(16, 22, 40, 0.9);
  --line: rgba(212, 175, 55, 0.22);
  --accent: #d4af37;
  --accent-2: #f0d78c;
  --danger: #e07070;
  --ok: #7dcea0;
  --shadow: 0 24px 60px rgba(0, 0, 0, 0.45);
}

.theme-color {
  --bg: #12151c;
  --bg-2: #1c212c;
  --ink: #eef2f7;
  --muted: #8d98ab;
  --panel: rgba(22, 26, 36, 0.72);
  --line: rgba(190, 210, 230, 0.14);
  --accent: #8ec4dc;
  --accent-2: #c9d6e4;
  --danger: #ef7a7a;
  --ok: #6dcea3;
  --shadow: 0 24px 56px rgba(0, 0, 0, 0.42);
}

.board-wood {
  --square-light: #cbb59a;
  --square-dark: #6a4530;
}

.board-marble {
  --square-light: #f6f3ee;
  --square-dark: #8d939e;
}

.board-military {
  --square-light: #d7d3a8;
  --square-dark: #4d5c3a;
}

.board-royal {
  --square-light: #f3ead7;
  --square-dark: #0f1b3d;
}

[hidden] { display: none !important; }

* { box-sizing: border-box; }
html, body { height: 100%; }
html { overflow-x: hidden; }
body {
  margin: 0;
  color: var(--ink);
  font-family: var(--font);
  background-color: var(--bg);
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
  min-height: 100%;
}
input, select, textarea {
  font-size: 16px;
}

.theme-light {
  background-color: #16100c;
  background-image:
    repeating-linear-gradient(
      90deg,
      rgba(0, 0, 0, 0.22) 0px,
      rgba(0, 0, 0, 0.08) 3px,
      transparent 8px,
      transparent 46px,
      rgba(255, 255, 255, 0.035) 48px,
      rgba(0, 0, 0, 0.16) 52px,
      transparent 78px
    ),
    linear-gradient(180deg, #2c211b 0%, #1a120e 42%, #120c0a 100%);
}

.theme-dark {
  background-color: #0c1222;
  background-image:
    radial-gradient(ellipse 80% 50% at 12% -8%, rgba(212, 175, 55, 0.16), transparent 52%),
    radial-gradient(ellipse 70% 55% at 108% 8%, rgba(70, 90, 170, 0.22), transparent 48%),
    radial-gradient(circle at 50% 120%, rgba(212, 175, 55, 0.06), transparent 42%),
    linear-gradient(180deg, #151c32 0%, #0c1222 48%, #070a14 100%);
}

.theme-color {
  background-color: #12151c;
  background-image:
    linear-gradient(rgba(210, 224, 240, 0.045) 1px, transparent 1px),
    linear-gradient(90deg, rgba(210, 224, 240, 0.045) 1px, transparent 1px),
    radial-gradient(ellipse 90% 55% at 50% -18%, rgba(160, 190, 220, 0.2), transparent 58%),
    radial-gradient(ellipse 50% 36% at 100% 100%, rgba(90, 110, 150, 0.16), transparent 52%),
    linear-gradient(180deg, #1c212c 0%, #141820 46%, #0e1117 100%);
  background-size: 56px 56px, 56px 56px, auto, auto, auto;
}

.grain {
  pointer-events: none;
  position: fixed;
  inset: 0;
  opacity: 0.22;
  mix-blend-mode: overlay;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='180' height='180'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.65' numOctaves='4'/%3E%3C/filter%3E%3Crect width='180' height='180' filter='url(%23n)'/%3E%3C/svg%3E");
  z-index: 0;
}
.theme-dark .grain {
  opacity: 0.09;
  mix-blend-mode: soft-light;
}
.theme-color .grain {
  opacity: 0.11;
  mix-blend-mode: soft-light;
}

.topbar, main { position: relative; z-index: 1; }
main {
  flex: 1;
  min-height: 0;
  overflow: auto;
}

.topbar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: center;
  gap: 8px;
  padding: max(10px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) 10px max(16px, env(safe-area-inset-left));
  position: relative;
  flex-shrink: 0;
}
.brand {
  font-family: var(--serif);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  font-size: 0.92rem;
  font-weight: 600;
  white-space: nowrap;
  pointer-events: none;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: center;
}
.topbar-actions {
  display: flex;
  gap: 8px;
  align-items: center;
  justify-self: end;
}

.icon-btn, .lang-toggle, .chip, .btn, .btn-mode, .btn-text, .studio-back, .avatar-btn {
  font-family: inherit;
  cursor: pointer;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  touch-action: manipulation;
}
input, select, textarea, a {
  touch-action: manipulation;
}
.icon-btn, .lang-toggle {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  font-size: 1.05rem;
}
.lang-toggle { width: auto; padding: 0 12px; font-size: 0.78rem; letter-spacing: 0.08em; }

.screen { display: none; padding: 8px 20px 40px; }
.screen.active { display: block; }

.hero {
  max-width: 760px;
  margin: 6vh auto 0;
  text-align: center;
  position: relative;
}
.hero::before {
  content: "";
  position: absolute;
  inset: -8% -20% auto;
  height: 280px;
  background:
    linear-gradient(var(--line) 1px, transparent 1px) 0 0 / 35px 35px,
    linear-gradient(90deg, var(--line) 1px, transparent 1px) 0 0 / 35px 35px;
  mask-image: radial-gradient(circle, #000 20%, transparent 72%);
  opacity: 0.45;
  pointer-events: none;
}
.eyebrow {
  letter-spacing: 0.28em;
  text-transform: uppercase;
  font-size: 0.72rem;
  color: var(--muted);
}
.hero h1 {
  font-family: var(--serif);
  font-size: clamp(3.2rem, 8vw, 6.4rem);
  margin: 0.1em 0 0.15em;
  font-weight: 600;
  line-height: 0.9;
}
.lede { color: var(--muted); margin-bottom: 2.2rem; font-size: 1.08rem; }
.home-actions {
  display: grid;
  gap: 14px;
  grid-template-columns: repeat(3, 1fr);
}
.btn-mode {
  border-radius: var(--radius);
  padding: 22px 16px;
  box-shadow: var(--shadow);
  transition: transform 0.18s ease, border-color 0.18s ease;
}
.btn-mode:hover { transform: translateY(-3px); border-color: var(--accent); }
.btn-mode span { display: block; font-family: var(--serif); font-size: 1.55rem; }
.btn-mode small { display: block; margin-top: 6px; color: var(--muted); }
.btn-text {
  margin-top: 22px;
  background: transparent;
  border: none;
  text-decoration: underline;
  text-underline-offset: 4px;
}
.topbar-left {
  display: flex;
  align-items: center;
  gap: 8px;
  z-index: 2;
  justify-self: start;
  min-width: 0;
}
.studio-back {
  display: inline-flex;
  align-items: center;
  height: 42px;
  padding: 0 12px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  text-decoration: none;
  font-size: 0.78rem;
  letter-spacing: 0.04em;
  white-space: nowrap;
}
.studio-back-logo {
  display: none;
  height: 22px;
  width: 22px;
  flex-shrink: 0;
  object-fit: contain;
  object-position: center;
  aspect-ratio: 1;
}
.studio-back:hover { border-color: var(--accent); }

.profile-panel {
  position: relative;
  z-index: 2;
  margin: 36px auto 0;
  padding: 22px 20px 18px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 22px;
  box-shadow: var(--shadow);
  text-align: left;
}
.profile-panel h2 {
  font-family: var(--serif);
  font-weight: 600;
  margin: 0 0 14px;
  font-size: 1.45rem;
  text-align: center;
}
.profile-name-row {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  margin-bottom: 16px;
}
.profile-name {
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1;
  margin-bottom: 0;
  color: var(--muted);
  font-size: 0.82rem;
  letter-spacing: 0.04em;
}
#btn-profile-clear {
  flex-shrink: 0;
  margin-bottom: 0;
}
.profile-name input {
  font-family: var(--serif);
  font-size: 1.25rem;
  width: 100%;
  min-width: 0;
  color: var(--ink);
  background: rgba(0, 0, 0, 0.18);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 14px;
}
.profile-name input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
#profile-avatar-section {
  margin-top: 4px;
}
.profile-hint {
  margin: 0 0 10px;
  color: var(--muted);
  font-size: 0.88rem;
}
.avatar-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 8px;
}
.avatar-btn {
  position: relative;
  padding: 0;
  border-radius: 12px;
  overflow: hidden;
  aspect-ratio: 3 / 4;
  border: 2px solid transparent;
  background: #1a1410;
  line-height: 0;
  cursor: pointer;
}
.avatar-btn img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: 50% 12%;
  display: block;
  pointer-events: none;
  user-select: none;
  -webkit-user-drag: none;
}
.avatar-btn-none {
  background:
    linear-gradient(180deg, #3a2e28 0%, #1c1512 100%);
  border: 2px dashed rgba(210, 190, 170, 0.28);
  display: flex;
  align-items: center;
  justify-content: center;
}
.avatar-btn-none::after {
  content: "";
  width: 28%;
  height: 3px;
  border-radius: 2px;
  background: rgba(210, 190, 170, 0.45);
}
.avatar-btn.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent);
  z-index: 1;
}
.avatar-btn:not(.avatar-btn-none).active::after {
  content: "";
  position: absolute;
  inset: 0;
  box-shadow: inset 0 0 0 4px var(--accent);
  border-radius: inherit;
  pointer-events: none;
}
.avatar-btn-none.active {
  border-style: solid;
  border-color: var(--accent);
}
.setup-you {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--line);
}
.setup-you img {
  width: 48px;
  height: 64px;
  object-fit: cover;
  object-position: 50% 12%;
  border-radius: 8px;
}
.setup-you span {
  font-family: var(--serif);
  font-size: 1.2rem;
}

.panel {
  max-width: 820px;
  margin: 0 auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 22px;
  padding: 28px;
  box-shadow: var(--shadow);
}
.panel h2, .modal h3 {
  font-family: var(--serif);
  font-weight: 600;
  margin-top: 0;
}
.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 8px;
}
.panel-head h2 { margin: 0; }
.panel-head .icon-btn {
  font-size: 1.6rem;
  line-height: 1;
}
#btn-settings-close {
  width: 100%;
  margin-top: 8px;
}
fieldset {
  border: 1px solid var(--line);
  border-radius: 14px;
  margin: 0 0 16px;
  padding: 14px 16px 16px;
}
legend { padding: 0 8px; color: var(--muted); font-size: 0.82rem; letter-spacing: 0.04em; }
.choice-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.chip, .btn {
  border-radius: 999px;
  padding: 9px 14px;
  font-size: 0.92rem;
}
.chip.active, .btn.primary {
  background: var(--accent);
  color: #1a140c;
  border-color: transparent;
}
.theme-color .chip.active,
.theme-color .btn.primary {
  color: #10141c;
}
.theme-color .panel,
.theme-color .player-card,
.theme-color .notation-box,
.theme-color .coach,
.theme-color .game-buttons,
.theme-color .modal,
.theme-color .btn-mode,
.theme-color .profile-panel {
  background: var(--panel);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
}
.theme-color .overlay {
  background: rgba(8, 10, 16, 0.55);
}
.theme-color .hero::before {
  opacity: 0.28;
}
.toggle, .slider {
  display: flex;
  gap: 10px;
  align-items: center;
  margin-top: 10px;
}
.slider { flex-wrap: wrap; }
.slider input { flex: 1; min-width: 160px; }
.field-label { color: var(--muted); margin-right: 4px; }

.setup-options { display: grid; gap: 10px; }
.setup-options .btn-mode { text-align: left; }

#screen-game.active {
  display: flex;
  flex-direction: column;
  height: 100%;
  max-height: 100%;
  overflow: hidden;
  padding: 4px 16px 8px;
}
.studio-mark {
  position: fixed;
  right: 14px;
  bottom: 10px;
  z-index: 4;
  line-height: 0;
  opacity: 0.92;
  text-decoration: none;
}
.studio-mark img {
  display: block;
  height: 28px;
  width: auto;
  filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.45));
}
.studio-mark:hover {
  opacity: 1;
}
.game-layout {
  display: grid;
  grid-template-columns: 1fr minmax(220px, min(640px, calc(100dvh - 14rem))) 1fr;
  gap: 14px;
  align-items: center;
  max-width: 1280px;
  width: 100%;
  margin: 0 auto;
  flex: 1 1 auto;
  min-height: 0;
}
.game-side {
  width: 100%;
  max-width: 240px;
}
.opponent-side { justify-self: end; }
.game-side:not(.opponent-side) { justify-self: start; }
.you-side {
  position: relative;
  max-width: 240px;
  width: 100%;
}
.board-wrap {
  min-width: 0;
  width: min(100%, 640px, calc(100dvh - 14rem));
  justify-self: center;
}
.board-frame {
  padding: 18px 18px 22px;
  background:
    linear-gradient(145deg, #5a4032 0%, #3a281e 32%, #241810 70%, #160e0a 100%);
  border-radius: 10px;
  box-shadow:
    0 28px 48px rgba(0, 0, 0, 0.5),
    inset 0 2px 0 rgba(210, 190, 170, 0.12),
    inset 0 -8px 16px rgba(0, 0, 0, 0.45);
  position: relative;
}
.board-frame::before {
  content: "";
  position: absolute;
  inset: 16px 16px 20px;
  border-radius: 3px;
  pointer-events: none;
  box-shadow:
    inset 0 0 0 2px rgba(180, 150, 130, 0.28),
    inset 0 0 0 4px rgba(20, 12, 8, 0.55);
  z-index: 1;
}
.board-marble .board-frame {
  background:
    linear-gradient(145deg, #d8d4cc 0%, #9a9892 40%, #5e5c58 100%);
  box-shadow:
    0 28px 48px rgba(30, 30, 32, 0.32),
    inset 0 2px 0 rgba(255, 255, 255, 0.4),
    inset 0 -8px 16px rgba(0, 0, 0, 0.25);
}
.board-marble .board-frame::before {
  box-shadow:
    inset 0 0 0 2px rgba(230, 230, 232, 0.7),
    inset 0 0 0 5px rgba(70, 70, 74, 0.45);
}
.board-military .board-frame {
  background:
    linear-gradient(145deg, #6b6a45 0%, #3f4630 45%, #2a3020 100%);
  box-shadow:
    0 28px 48px rgba(20, 24, 12, 0.4),
    inset 0 2px 0 rgba(180, 190, 120, 0.18),
    inset 0 -8px 16px rgba(0, 0, 0, 0.4);
}
.board-military .board-frame::before {
  box-shadow:
    inset 0 0 0 2px rgba(140, 150, 90, 0.35),
    inset 0 0 0 5px rgba(30, 36, 20, 0.55);
}
.board-royal .board-frame {
  background: #0b142c;
  padding: 14px 14px 30px 30px;
  border-radius: 8px;
  box-shadow: none;
}
.board-royal .board-frame::before {
  inset: 13px 13px 29px 29px;
  border-radius: 0;
  box-shadow: 0 0 0 1.5px #c8a24a;
}
.board-royal .board {
  overflow: visible;
  border-radius: 0;
  box-shadow: none;
}
.board-stage {
  position: relative;
  z-index: 2;
}
.board {
  position: relative;
  z-index: 2;
  width: 100%;
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  border-radius: 2px;
  overflow: hidden;
  touch-action: none;
  user-select: none;
  box-shadow: inset 0 0 40px rgba(0, 0, 0, 0.18);
}
.move-arrow {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 6;
  overflow: visible;
  filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.5));
}
.sq {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}
.sq.light { background: var(--square-light); }
.sq.dark { background: var(--square-dark); }
.board-wood .sq.light {
  background-color: #c4b194;
  background-image:
    linear-gradient(92deg, rgba(255, 255, 255, 0.16), transparent 42%, rgba(80, 50, 30, 0.14) 100%),
    repeating-linear-gradient(88deg, rgba(70, 45, 28, 0.1) 0 1px, transparent 1px 4px);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.18), inset 0 -1px 0 rgba(40, 24, 12, 0.16);
}
.board-wood .sq.dark {
  background-color: #5c3b28;
  background-image:
    linear-gradient(98deg, rgba(180, 140, 110, 0.12), transparent 45%, rgba(20, 10, 6, 0.32) 100%),
    repeating-linear-gradient(93deg, rgba(20, 10, 6, 0.22) 0 1px, transparent 1px 5px);
  box-shadow: inset 0 1px 0 rgba(180, 140, 110, 0.1), inset 0 -2px 0 rgba(0, 0, 0, 0.28);
}
.board-wood .sq:nth-child(3n).light {
  background-image:
    linear-gradient(86deg, rgba(255, 255, 255, 0.12), transparent 50%, rgba(80, 50, 30, 0.12) 100%),
    repeating-linear-gradient(95deg, rgba(70, 45, 28, 0.09) 0 1px, transparent 1px 5px);
}
.board-marble .sq.light {
  background-color: #f4f1ea;
  background-image:
    linear-gradient(122deg, rgba(255, 255, 255, 0.65) 0%, transparent 38%, rgba(160, 160, 168, 0.2) 52%, transparent 70%),
    linear-gradient(28deg, transparent 40%, rgba(90, 90, 95, 0.1) 41%, transparent 48%);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.7);
}
.board-marble .sq.dark {
  background-color: #7d838e;
  background-image:
    linear-gradient(158deg, rgba(255, 255, 255, 0.2) 0%, transparent 40%, rgba(20, 22, 28, 0.25) 100%),
    linear-gradient(70deg, transparent 46%, rgba(255, 255, 255, 0.12) 47%, transparent 52%);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.14), inset 0 -2px 0 rgba(0, 0, 0, 0.2);
}
.board-military .sq.light {
  background-color: #cfc89a;
  background-image:
    repeating-linear-gradient(0deg, rgba(90, 95, 50, 0.07) 0 2px, transparent 2px 6px),
    linear-gradient(135deg, rgba(255, 255, 230, 0.15), transparent 50%);
  box-shadow: inset 0 1px 0 rgba(255, 255, 220, 0.2);
}
.board-military .sq.dark {
  background-color: #4a5638;
  background-image:
    repeating-linear-gradient(90deg, rgba(0, 0, 0, 0.08) 0 2px, transparent 2px 7px),
    linear-gradient(160deg, rgba(160, 170, 110, 0.12), transparent 50%, rgba(0, 0, 0, 0.2));
  box-shadow: inset 0 1px 0 rgba(200, 210, 140, 0.1), inset 0 -2px 0 rgba(0, 0, 0, 0.25);
}
.board-royal .sq.light {
  background-color: #f3ead7;
  background-image:
    repeating-linear-gradient(-18deg, transparent 0 3px, rgba(180, 150, 100, 0.04) 3px 4px),
    repeating-linear-gradient(72deg, transparent 0 4px, rgba(90, 70, 40, 0.03) 4px 5px);
  box-shadow: none;
}
.board-royal .sq.dark {
  background-color: #0f1b3d;
  background-image:
    repeating-linear-gradient(-18deg, transparent 0 3px, rgba(255, 255, 255, 0.028) 3px 4px),
    repeating-linear-gradient(72deg, transparent 0 4px, rgba(200, 162, 74, 0.045) 4px 5px);
  box-shadow: none;
}
.coord {
  position: absolute;
  font-size: 0.62rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  opacity: 0.78;
  pointer-events: none;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.25);
}
.coord.file { right: 4px; bottom: 3px; }
.coord.rank { left: 4px; top: 3px; }
.board-royal .coord {
  color: #d9c88a;
  text-shadow: none;
  opacity: 0.95;
  font-size: 0.68rem;
  font-weight: 500;
  letter-spacing: 0.04em;
}
.board-royal .coord.file {
  bottom: -1.45em;
  left: 50%;
  right: auto;
  transform: translateX(-50%);
}
.board-royal .coord.rank {
  left: -1.4em;
  top: 50%;
  transform: translateY(-50%);
}
.piece {
  width: 84%;
  height: 84%;
  z-index: 2;
  filter: drop-shadow(0 3px 2px rgba(0, 0, 0, 0.4));
  cursor: grab;
  transform: none;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}
.piece svg {
  width: 100%;
  height: 100%;
  display: block;
  overflow: visible;
}
.sq.selected { outline: 3px solid var(--accent); outline-offset: -3px; }
.sq.last-from, .sq.last-to { box-shadow: inset 0 0 0 100px rgba(180, 150, 130, 0.32); }
.board-royal .sq.selected { outline-color: #c8a24a; }
.board-royal .sq.last-from,
.board-royal .sq.last-to { box-shadow: inset 0 0 0 100px rgba(200, 162, 74, 0.22); }
.board-royal .sq.check { box-shadow: inset 0 0 0 100px rgba(180, 40, 40, 0.32); }
.board-royal .hint-dot { background: rgba(200, 162, 74, 0.45); }
.board-royal .hint-cap { border-color: rgba(200, 162, 74, 0.5); }
.sq.check { box-shadow: inset 0 0 0 100px rgba(180, 40, 40, 0.32); }
.hint-dot, .hint-cap {
  position: absolute;
  z-index: 3;
  pointer-events: none;
}
.hint-dot {
  width: 18%;
  height: 18%;
  border-radius: 50%;
  background: rgba(20, 20, 20, 0.28);
}
.hint-cap {
  inset: 6%;
  border-radius: 50%;
  border: 4px solid rgba(20, 20, 20, 0.3);
}
.sq.hint-from {
  outline: 3px solid var(--accent);
  outline-offset: -3px;
}
.sq.hint-to {
  outline: 3px solid rgba(125, 206, 160, 0.95);
  outline-offset: -3px;
}
.piece.hint-piece {
  z-index: 5;
  filter: drop-shadow(0 0 10px rgba(125, 206, 160, 0.95)) drop-shadow(0 3px 2px rgba(0, 0, 0, 0.4));
  animation: hint-pulse 0.9s ease-in-out infinite;
}
@keyframes hint-pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.14); }
}
.flying {
  position: absolute;
  z-index: 20;
  pointer-events: none;
  transition: left 0.22s ease, top 0.22s ease;
}

.status-bar {
  margin-top: 10px;
  text-align: center;
  min-height: 1.4em;
  color: var(--muted);
}
.challenge-banner {
  margin: 0 0 10px;
  text-align: center;
  font-family: var(--serif);
  font-size: 1.45rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  color: var(--accent-2);
}
.player-card, .notation-box, .coach, .game-buttons {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 14px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
}
.theme-light .player-card,
.theme-light .notation-box,
.theme-light .coach,
.theme-light .game-buttons {
  background:
    linear-gradient(180deg, rgba(48, 36, 30, 0.94), rgba(28, 20, 16, 0.9));
}
.player-card { overflow: visible; }
.you-wrap {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  width: 100%;
  margin: 0 0 12px;
}
.you-portrait {
  width: 100%;
  aspect-ratio: 3 / 4;
  height: auto;
  max-height: min(280px, 38vh);
  border-radius: 16px;
  position: relative;
  overflow: hidden;
  box-shadow:
    0 12px 28px rgba(0, 0, 0, 0.45),
    inset 0 0 0 1px rgba(255, 255, 255, 0.16);
  background:
    radial-gradient(ellipse 70% 55% at 50% 38%, rgba(210, 190, 170, 0.18), transparent 62%),
    linear-gradient(180deg, #3a2e28 0%, #1c1512 100%);
}
.you-portrait::before {
  content: "";
  position: absolute;
  left: 50%;
  top: 28px;
  width: 58px;
  height: 58px;
  margin-left: -29px;
  border-radius: 50%;
  background: #c4b09a;
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.2);
}
.you-portrait::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 92px;
  width: 96px;
  height: 110px;
  margin-left: -48px;
  border-radius: 48px 48px 8px 8px;
  background: #8a7464;
}
.you-portrait-self {
  background:
    radial-gradient(ellipse 70% 55% at 50% 38%, rgba(194, 160, 138, 0.22), transparent 62%),
    linear-gradient(180deg, #4a382e 0%, #241810 100%);
}
.you-portrait-self::before { background: #e0c8b0; }
.you-portrait-self::after { background: #6a5344; }
.you-portrait.has-face::before,
.you-portrait.has-face::after { content: none; }
.you-portrait img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: left 12%;
  display: block;
  transform: none;
}
.max-wrap {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  width: 100%;
  margin: 0 0 12px;
  z-index: 6;
}
.max-portrait {
  position: relative;
  width: 100%;
  aspect-ratio: 3 / 4;
  height: auto;
  max-height: min(280px, 38vh);
  border-radius: 16px;
  overflow: hidden;
  transform-origin: 50% 80%;
  box-shadow:
    0 12px 28px rgba(0, 0, 0, 0.45),
    inset 0 0 0 1px rgba(255, 255, 255, 0.16);
}
.max-portrait::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: linear-gradient(180deg, transparent 55%, rgba(0, 0, 0, 0.28) 100%);
}
.max-portrait img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: left 12%;
  opacity: 0;
  transition: opacity 0.32s ease;
}
.max-portrait[data-mood="idle"] .m-idle,
.max-portrait[data-mood="think"] .m-think,
.max-portrait[data-mood="happy"] .m-happy,
.max-portrait[data-mood="mad"] .m-mad { opacity: 1; }
.max-portrait[data-mood="think"] {
  animation: max-ponder 2.4s ease-in-out infinite;
}
.max-portrait[data-mood="happy"] {
  animation: max-bounce 0.55s ease;
}
.max-portrait[data-mood="mad"] {
  animation: max-shake 0.4s ease;
}
@keyframes max-ponder {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-3px); }
}
@keyframes max-bounce {
  0%, 100% { transform: translateY(0) scale(1); }
  40% { transform: translateY(-5px) scale(1.03); }
}
@keyframes max-shake {
  0%, 100% { transform: translateX(0); }
  30% { transform: translateX(-3px); }
  70% { transform: translateX(3px); }
}
.comic-bubble {
  position: absolute;
  left: 50%;
  bottom: calc(100% - 12px);
  z-index: 8;
  min-width: 88px;
  max-width: 168px;
  padding: 8px 12px 10px;
  background: #fffdf4;
  color: #1a120c;
  border: 3px solid #1a120c;
  border-radius: 18px 22px 16px 20px;
  font-family: "Bangers", "Comic Sans MS", "Outfit", sans-serif;
  font-size: 1.12rem;
  letter-spacing: 0.04em;
  line-height: 1.15;
  text-align: center;
  transform: translate(-50%, 0) rotate(-2deg);
  box-shadow: 3px 3px 0 #1a120c;
  pointer-events: none;
}
.comic-bubble::after {
  content: "";
  position: absolute;
  left: 22%;
  bottom: -9px;
  width: 14px;
  height: 14px;
  background: #fffdf4;
  border-right: 3px solid #1a120c;
  border-bottom: 3px solid #1a120c;
  transform: rotate(45deg);
}
.comic-bubble.pop {
  animation: bubble-pop 0.28s cubic-bezier(0.2, 1.4, 0.4, 1);
}
@keyframes bubble-pop {
  from { transform: translate(-50%, 0) rotate(-2deg) scale(0.4); opacity: 0; }
  to { transform: translate(-50%, 0) rotate(-2deg) scale(1); opacity: 1; }
}
.player-name { font-family: var(--serif); font-size: 1.25rem; }
.player-stats {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin: 4px 0;
}
.clock {
  font-variant-numeric: tabular-nums;
  font-size: 1.4rem;
  margin: 0;
}
.ply-count {
  font-variant-numeric: tabular-nums;
  font-size: 0.82rem;
  color: var(--muted);
  letter-spacing: 0.03em;
  white-space: nowrap;
}
.ply-count strong {
  color: var(--ink);
  font-size: 1.15rem;
  font-weight: 600;
  margin-left: 4px;
}
.clock.low { color: var(--danger); }
.captures { min-height: 28px; display: flex; flex-wrap: wrap; gap: 2px; }
.captures svg { width: 22px; height: 22px; }
.notation-box ol {
  max-height: 180px;
  overflow: auto;
  padding-left: 22px;
  margin: 0;
  font-size: 0.92rem;
}
.coach h3, .notation-box h3 { margin: 0 0 8px; font-size: 1rem; }
.coach {
  position: absolute;
  left: calc(100% + 12px);
  top: 0;
  width: 260px;
  z-index: 8;
  margin-bottom: 0;
  padding: 10px 12px;
}
.coach-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  width: 100%;
  margin: 0;
  padding: 2px 0;
  border: 0;
  background: transparent;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.coach-head:hover .coach-chevron {
  border-color: var(--accent);
}
.coach-chevron {
  width: 8px;
  height: 8px;
  flex-shrink: 0;
  margin: 0 4px 3px 0;
  border-right: 2px solid var(--muted);
  border-bottom: 2px solid var(--muted);
  transform: rotate(45deg);
  transition: transform 0.16s ease;
}
.coach.is-collapsed .coach-chevron {
  margin-bottom: 0;
  transform: rotate(-45deg);
}
.coach.is-collapsed .coach-body {
  display: none;
}
.coach:not(.is-collapsed) .coach-body {
  margin-top: 8px;
}
.coach-item {
  margin: 0 0 4px;
  padding: 8px;
  border-bottom: 1px solid var(--line);
  border-radius: 8px;
  cursor: default;
}
.coach-item:last-child { border-bottom: none; margin-bottom: 0; }
.coach-item:hover { background: rgba(255, 255, 255, 0.07); }
.coach-item p { margin: 4px 0 0; }
.coach-move {
  font-size: 1rem;
  font-weight: 600;
  color: var(--ink);
}
.coach-why {
  font-size: 0.82rem;
  line-height: 1.35;
  color: var(--muted);
}
.tag {
  display: inline-block;
  font-size: 0.72rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  margin-bottom: 4px;
  color: var(--accent);
}
.tag.warn { color: var(--danger); }
.game-buttons {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  max-width: 1280px;
  width: 100%;
  margin: 6px auto 0;
  padding: 8px 14px 10px;
  flex-shrink: 0;
}
.game-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}
.game-buttons .btn { min-width: 108px; padding: 8px 12px; }
.challenge-switch {
  width: 100%;
  display: flex;
  justify-content: center;
}
.challenge-switch .choice-row {
  justify-content: center;
  gap: 6px;
}
.challenge-switch .chip {
  padding: 6px 11px;
  font-size: 0.82rem;
}
.solution-offer {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 2px 0 4px;
  text-align: center;
}
.solution-offer p {
  margin: 0;
  font-size: 1.05rem;
  color: var(--ink);
}
.solution-offer-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}
.btn-ghost { background: transparent; }

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(20, 14, 8, 0.45);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 20px;
}
.overlay:not([hidden]) {
  display: flex;
}
.modal {
  width: min(520px, 100%);
  background: var(--panel);
  color: var(--ink);
  border-radius: 18px;
  padding: 24px;
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
}
.modal-actions, .promo-grid { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 16px; }
.promo-grid .btn { min-width: 72px; }
.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  background: var(--ink);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 999px;
  z-index: 60;
  max-width: 90vw;
}

.thinking::after { content: " …"; }

@media (max-width: 980px) {
  .home-actions { grid-template-columns: 1fr; }
  .avatar-grid { grid-template-columns: repeat(4, 1fr); }
  .game-layout { grid-template-columns: 1fr; align-items: start; }
  #screen-game.active { overflow: auto; height: auto; max-height: none; }
  .opponent-side { order: -1; }
  .game-side {
    max-width: none;
    justify-self: stretch;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
  }
  .game-side.you-side {
    display: grid;
    position: relative;
  }
  .coach {
    position: static;
    width: auto;
  }
  .game-buttons { grid-template-columns: none; }
  .studio-mark {
    display: none;
  }
}
@media (max-width: 700px) {
  .topbar {
    gap: 6px;
    padding: max(8px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right)) 8px max(10px, env(safe-area-inset-left));
  }
  .icon-btn, .lang-toggle {
    width: 36px;
    height: 36px;
  }
  .lang-toggle {
    width: auto;
    min-width: 36px;
    padding: 0 8px;
  }
  .studio-back {
    height: 36px;
    padding: 0 8px;
  }
  .studio-back span { display: none; }
  .studio-back-logo { display: block; }
  .brand {
    font-size: 0.72rem;
    letter-spacing: 0.08em;
  }
}
@media (orientation: portrait) and (max-width: 700px) {
  #screen-game .opponent-side .player-card {
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    align-items: center;
    gap: 8px;
    padding: 6px 8px;
    margin-bottom: 6px;
    min-height: 0;
  }
  #screen-game .opponent-side .max-wrap,
  #screen-game .opponent-side .you-wrap {
    width: 48px;
    height: 48px;
    margin: 0;
    flex-shrink: 0;
  }
  #screen-game .opponent-side .max-portrait,
  #screen-game .opponent-side .you-portrait {
    width: 48px;
    height: 48px;
    max-height: 48px;
    aspect-ratio: 1;
    border-radius: 8px;
  }
  #screen-game .opponent-side .you-portrait::before,
  #screen-game .opponent-side .you-portrait::after,
  #screen-game .opponent-side .max-portrait::after,
  #screen-game .opponent-side .comic-bubble {
    display: none;
  }
  #screen-game .opponent-side .player-name {
    font-size: 0.95rem;
    flex: 1 1 auto;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  #screen-game .opponent-side .player-stats {
    margin: 0;
    gap: 6px;
    flex-shrink: 0;
  }
  #screen-game .opponent-side .clock,
  #screen-game .opponent-side .captures {
    display: none;
  }
  #screen-game .opponent-side .ply-count {
    font-size: 0.75rem;
  }
  #screen-game .opponent-side .ply-count strong {
    font-size: 0.9rem;
  }
}
@media (max-width: 560px) {
  .screen { padding: 4px 12px 28px; }
  .game-side { grid-template-columns: 1fr; }
  .hero { margin-top: 3vh; }
  .avatar-grid { grid-template-columns: repeat(3, 1fr); }
  .game-buttons .btn { min-width: 96px; flex: 0 1 auto; }
}
@media (max-width: 430px) {
  .topbar { gap: 4px; }
  .topbar-actions { gap: 4px; }
  .brand {
    font-size: 0.64rem;
    letter-spacing: 0.05em;
  }
  .profile-name-row { flex-wrap: wrap; align-items: stretch; }
  .profile-name { min-width: 0; }
  #btn-profile-clear { width: 100%; }
}
@media (max-width: 360px) and (orientation: portrait) {
  .brand { display: none; }
  .topbar { grid-template-columns: 1fr auto; }
}

@media (orientation: landscape) and (max-height: 900px) and (max-width: 1200px) {
  .topbar {
    gap: 4px;
    min-height: 0;
    padding: max(2px, env(safe-area-inset-top, 0px)) max(6px, env(safe-area-inset-right, 0px)) 2px max(6px, env(safe-area-inset-left, 0px));
  }
  .icon-btn, .lang-toggle {
    width: 26px;
    height: 26px;
    border-radius: 8px;
    font-size: 0.88rem;
  }
  .lang-toggle {
    width: auto;
    min-width: 26px;
    padding: 0 5px;
    font-size: 0.68rem;
  }
  .studio-back {
    height: 26px;
    padding: 0 4px;
    border-radius: 8px;
  }
  .studio-back span { display: none; }
  .studio-back-logo {
    display: block;
    width: 20px;
    height: 20px;
    object-fit: contain;
  }
  .brand {
    display: block;
    font-size: 0.62rem;
    letter-spacing: 0.06em;
  }
  body:has(#screen-game.active) .brand { display: none; }
  body:has(#screen-game.active) {
    height: 100dvh;
    max-height: 100dvh;
    overflow: hidden;
  }
  body:has(#screen-game.active) main {
    overflow: hidden;
  }
  .grain { opacity: 0.06; }
  .studio-mark { display: none; }

  #screen-game.active {
    --land-act: 5.15rem;
    --land-card: clamp(6.4rem, 14vw, 8.75rem);
    --land-gap: 8px;
    --land-pad-l: max(8px, env(safe-area-inset-left, 0px));
    --land-pad-r: max(8px, env(safe-area-inset-right, 0px));
    --land-avail-h: calc(100dvh - 1.7rem - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px) - 1.2rem);
    --land-avail-w: calc(100dvw - var(--land-pad-l) - var(--land-pad-r) - (2 * var(--land-card)) - var(--land-act) - 28px);
    --land-board: min(var(--land-avail-h), var(--land-avail-w));
    display: grid;
    grid-template-columns: minmax(0, 1fr) var(--land-board) minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr) auto;
    column-gap: var(--land-gap);
    row-gap: 2px;
    align-items: stretch;
    justify-content: stretch;
    container-type: size;
    height: 100%;
    max-height: 100%;
    min-height: 0;
    overflow: hidden;
    padding: 2px var(--land-pad-r) max(8px, env(safe-area-inset-bottom, 0px)) var(--land-pad-l);
  }
  #screen-game .game-layout,
  #screen-game .board-wrap,
  #screen-game .game-buttons {
    display: contents;
  }
  #screen-game .board-frame {
    grid-column: 2;
    grid-row: 1;
    width: var(--land-board);
    max-width: 100%;
    padding: 2px;
    border-radius: 4px;
    align-self: center;
    justify-self: center;
    box-shadow:
      0 8px 18px rgba(0, 0, 0, 0.4),
      inset 0 1px 0 rgba(210, 190, 170, 0.1);
  }
  #screen-game .board-frame::before {
    inset: 2px;
  }
  body.board-royal #screen-game .board-frame {
    padding: 2px;
  }
  body.board-royal #screen-game .board-frame::before {
    inset: 2px;
  }
  #screen-game .board-stage,
  #screen-game .board {
    width: 100%;
    aspect-ratio: 1;
    height: auto;
    max-width: 100%;
  }
  @supports (width: 1cqh) {
    #screen-game.active {
      --land-board: min(
        calc(100cqh - 1.2rem),
        var(--land-avail-h),
        var(--land-avail-w)
      );
    }
  }
  #screen-game .challenge-banner {
    grid-column: 2;
    grid-row: 1;
    align-self: start;
    justify-self: center;
    margin: 0;
    font-size: 0.78rem;
    text-align: center;
    z-index: 2;
    pointer-events: none;
  }
  #screen-game .opponent-side,
  #screen-game .you-side {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    max-width: var(--land-card);
    width: var(--land-card);
    min-width: 0;
    order: 0;
  }
  #screen-game .opponent-side {
    grid-column: 1;
    grid-row: 1;
    justify-self: end;
    align-self: center;
  }
  #screen-game .you-side {
    grid-column: 3;
    grid-row: 1;
    justify-self: start;
    align-self: center;
    max-height: 100%;
    overflow-x: hidden;
    overflow-y: auto;
    margin-right: calc(var(--land-act) + 6px);
  }
  #screen-game .status-bar {
    grid-column: 2;
    grid-row: 2;
    margin: 0;
    min-height: 0;
    font-size: 0.72rem;
    text-align: center;
    line-height: 1.2;
    justify-self: center;
  }
  #screen-game .game-actions {
    grid-column: 3;
    grid-row: 1 / -1;
    justify-self: end;
    align-self: center;
    display: flex;
    flex-direction: column;
    flex-wrap: nowrap;
    justify-content: center;
    gap: 6px;
    width: var(--land-act);
    margin: 0;
    padding: 6px;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 10px;
    z-index: 3;
  }
  #screen-game .solution-offer {
    grid-column: 3;
    grid-row: 1;
    justify-self: start;
    align-self: end;
    max-width: calc(100% - var(--land-act) - 8px);
    margin: 0 0 4px;
    padding: 6px 8px;
    font-size: 0.72rem;
    z-index: 4;
  }
  #screen-game .challenge-switch {
    grid-column: 3;
    grid-row: 1;
    justify-self: start;
    align-self: end;
    width: auto;
    max-width: var(--land-card);
    margin: 0 calc(var(--land-act) + 6px) 0 0;
  }
  #screen-game .player-card {
    display: flex;
    flex-direction: column;
    flex-wrap: nowrap;
    align-items: center;
    gap: 4px;
    width: 100%;
    max-width: 100%;
    padding: 6px 8px;
    margin: 0;
    border-radius: 10px;
    text-align: center;
  }
  #screen-game .you-wrap,
  #screen-game .max-wrap {
    width: 44px;
    height: 44px;
    margin: 0;
    flex-shrink: 0;
  }
  #screen-game .you-portrait,
  #screen-game .max-portrait {
    width: 44px;
    height: 44px;
    max-height: 44px;
    aspect-ratio: 1;
    border-radius: 8px;
  }
  #screen-game .you-portrait::before,
  #screen-game .you-portrait::after,
  #screen-game .max-portrait::after,
  #screen-game .comic-bubble {
    display: none;
  }
  #screen-game .player-name {
    font-size: 0.72rem;
    flex: 0 1 auto;
    min-width: 0;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  #screen-game .player-stats {
    margin: 0;
    gap: 0;
    flex-shrink: 0;
    justify-content: center;
  }
  #screen-game .clock,
  #screen-game .captures {
    display: none;
  }
  #screen-game .ply-count {
    font-size: 0.65rem;
  }
  #screen-game .ply-count strong {
    font-size: 0.75rem;
    margin-left: 2px;
  }
  #screen-game .coach,
  #screen-game .notation-box {
    position: static;
    width: auto;
    max-height: min(28vh, 9rem);
    min-height: 0;
    overflow: auto;
    padding: 6px 8px;
    margin: 6px 0 0;
    border-radius: 8px;
    font-size: 0.72rem;
  }
  #screen-game .game-actions .btn {
    min-width: 0;
    min-height: 40px;
    width: 100%;
    padding: 7px 6px;
    font-size: 0.68rem;
    white-space: normal;
    line-height: 1.15;
  }
  #screen-game .challenge-switch .chip {
    padding: 3px 7px;
    font-size: 0.68rem;
  }
  #screen-game .challenge-switch .choice-row {
    justify-content: flex-start;
  }
}
