/* Sapling web UI styles. */

:root {
  --canvas: #0b0b0f;
  --surface: #1c1c1e;
  --surface-2: #2c2c2e;
  --primaryText: #f2f2f7;
  --secondaryText: #98989f;
  --tertiaryText: #6c6c74;
  --accent: #0a84ff;
  --accentText: #409cff;
  --accentSoft: rgba(10, 132, 255, 0.16);
  --separator: #38383a;
  --strongSeparator: #48484a;
  --success: #30d158;
  --danger: #ff453a;
  --warningSurface: rgba(255, 69, 58, 0.16);
  --infoSurface: rgba(10, 132, 255, 0.16);

  --sq-light: #f0d9b5;
  --sq-dark: #b58863;
  --board-border: #333333;
  --sel: #ffff00;
  --move-highlight: #30d158;
  --move-highlight-wash: rgba(48, 209, 88, 0.2);
  --last-from: rgba(10, 132, 255, 0.24);
  --last-to: rgba(48, 209, 88, 0.3);
  --check: rgba(255, 69, 58, 0.5);

  --radius-panel: 20px;
  --radius-control: 12px;
  --radius-capsule: 999px;
  --space-screen: 20px;
  --space-panel: 16px;
  --space-section: 24px;
  --space-related: 10px;
  --touch: 44px;
}

html:not(.dark) {
  --canvas: #f2f2f7;
  --surface: #ffffff;
  --surface-2: #f2f2f7;
  --primaryText: #1c1c1e;
  --secondaryText: #63636b;
  --tertiaryText: #74747c;
  --accent: #0066d6;
  --accentText: #005bc4;
  --accentSoft: #e8f1ff;
  --separator: #e5e5ea;
  --strongSeparator: #c7c7cc;
  --success: #248a3d;
  --move-highlight: #248a3d;
  --move-highlight-wash: rgba(36, 138, 61, 0.2);
  --danger: #c9342d;
  --warningSurface: #ffebe9;
  --infoSurface: #e8f1ff;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--canvas);
  color: var(--primaryText);
  font-family: system-ui, -apple-system, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}

body {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding:
    max(0px, env(safe-area-inset-top))
    max(var(--space-screen), env(safe-area-inset-right))
    max(0px, env(safe-area-inset-bottom))
    max(var(--space-screen), env(safe-area-inset-left));
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  padding: 8px 0 var(--space-section);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand h1 { font-size: 22px; font-weight: 700; margin: 0; letter-spacing: -0.01em; }
.logo { width: 28px; height: 28px; border-radius: 6px; display: block; }
.topbar-right { display: flex; align-items: center; gap: 14px; }
.github {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 36px;
  padding: 0 14px;
  border-radius: var(--radius-control);
  background: var(--surface-2);
  color: var(--primaryText);
  font-weight: 600;
  font-size: 15px;
  text-decoration: none;
}
.github:hover { background: var(--strongSeparator); }
.github:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

.status {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--secondaryText);
  box-sizing: border-box;
  width: min(100%, 560px);
  height: 28px;
  margin: 8px auto 0;
  overflow: hidden;
  white-space: nowrap;
}
.status #status-text {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}
.status .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--tertiaryText); flex: none; }
.status[data-state="thinking"] .dot,
.status[data-state="yourmove"] .dot { background: var(--accent); }
.status[data-state="check"] .dot,
.status[data-state="error"] .dot { background: var(--danger); }
.status[data-state="over"] .dot,
.status[data-state="copied"] .dot { background: var(--success); }
.status[data-state="thinking"] .dot { animation: pulse 1.1s ease-in-out infinite; }
.status[data-state="thinking"] #status-text::after {
  content: '';
  display: inline-block;
  width: 1.25em;
  animation: thinking-dots 1.2s steps(1, end) infinite;
}
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }
@keyframes thinking-dots {
  0%, 100% { content: ''; }
  25% { content: '.'; }
  50% { content: '..'; }
  75% { content: '...'; }
}
@media (prefers-reduced-motion: reduce) {
  .status[data-state="thinking"] .dot { animation: none; }
  .status[data-state="thinking"] #status-text::after { content: '...'; animation: none; }
}

.layout {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-areas: "controls" "board";
  gap: var(--space-section);
  max-width: 1080px;
  margin: 0 auto;
  width: 100%;
  flex: 1 0 auto;
}
.board-area { grid-area: board; width: 100%; display: flex; flex-direction: column; gap: 8px; }
.controls-column { grid-area: controls; display: flex; flex-direction: column; gap: var(--space-section); min-width: 0; }
.board-wrap { position: relative; width: min(100%, 560px); margin: 0 auto; }
.play-toolbar {
  width: min(100%, 560px);
  margin: 0 auto;
  padding: 6px 8px;
  display: grid;
  gap: 5px;
  background: var(--surface);
  border-radius: var(--radius-panel);
}
.toolbar-actions { display: flex; align-items: center; gap: 3px; }
.compact-button { min-height: 32px; padding: 0 7px; font-size: 12px; }
.toolbar-settings { display: grid; grid-template-columns: minmax(0, 1fr); gap: 4px; }
.toolbar-side { display: flex; align-items: center; gap: 4px; }
.toolbar-caption { color: var(--secondaryText); font-size: 11px; font-weight: 600; }
.compact-segmented { gap: 2px; padding: 2px; }
.compact-segmented .pill { min-height: 28px; padding: 0 7px; font-size: 12px; }
.toolbar-level { width: 100%; min-width: 0; display: grid; grid-template-columns: auto minmax(60px, 1fr); align-items: center; gap: 6px; }
.toolbar-level > span { white-space: nowrap; font-size: 10px; }
.toolbar-level > span em { font-size: 10px; }
.toolbar-level .slider { display: block; height: 18px; }
.hint-toggle { gap: 5px; }
.hint-toggle .hint-state { min-width: 30px; padding: 1px 4px; font-size: 10px; }

.material {
  width: min(100%, 560px);
  min-height: 92px;
  margin: 0 auto;
  padding: var(--space-panel);
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 2px 10px;
  background: var(--surface);
  border-radius: var(--radius-panel);
}
.game-info-topline { grid-column: 1 / -1; display: flex; align-items: center; justify-content: space-between; gap: 10px; min-width: 0; }
.game-info-topline .status { width: auto; height: auto; min-height: 24px; margin: 0; }
.move-count { flex: none; padding: 3px 8px; border-radius: var(--radius-capsule); background: var(--surface-2); color: var(--secondaryText); font-size: 11px; font-weight: 700; font-variant-numeric: tabular-nums; }
.material-row { min-width: 0; display: flex; align-items: center; gap: 10px; }
.material-label { width: 54px; flex: none; color: var(--secondaryText); font-size: 12px; font-weight: 600; }
.material-pieces { display: flex; align-items: center; gap: 1px; min-width: 0; min-height: 27px; overflow-x: auto; scrollbar-width: thin; }
.material-piece { width: 25px; height: 25px; object-fit: contain; }
.material-empty { color: var(--tertiaryText); padding-left: 3px; }
.material-balance {
  grid-column: 2;
  grid-row: 2 / span 2;
  padding: 4px 9px;
  border-radius: var(--radius-capsule);
  background: var(--surface-2);
  color: var(--secondaryText);
  font-size: 11px;
  font-weight: 700;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
.material-balance[data-edge="you"] { color: var(--accentText); background: var(--accentSoft); }
.material-balance[data-edge="opponent"] { color: var(--danger); background: var(--warningSurface); }

.board {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  margin: 0;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  border: 0.2rem solid var(--board-border);
  border-radius: var(--radius-panel);
  overflow: hidden;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}
.sq {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  border: 0;
  background: var(--sq-dark);
  cursor: pointer;
}
.sq.light { background: var(--sq-light); }
.sq:focus-visible { outline: 3px solid var(--accent); outline-offset: -3px; }
.hint-score {
  position: absolute; top: 2px; right: 2px; z-index: 3;
  max-width: 92%; overflow: hidden; white-space: nowrap; text-overflow: clip;
  padding: 1px 3px; border-radius: 4px;
  background: rgba(0, 0, 0, 0.78); color: #fff;
  font-size: clamp(7px, 1.7vw, 10px); font-weight: 700; line-height: 1.2;
  pointer-events: none;
}
.sq.last::after {
  content: ""; position: absolute; inset: 0; pointer-events: none; z-index: 0;
}
.sq.last:not(.valid):not(.selected)::after {
  box-shadow: inset 0 0 0 3px rgba(255, 255, 255, 0.85);
}
.sq.valid::after {
  content: ""; position: absolute; inset: 0; pointer-events: none; z-index: 0;
  outline: 3px solid var(--move-highlight); outline-offset: -3px; background: var(--move-highlight-wash);
}
.sq.selected::after {
  content: ""; position: absolute; inset: 0; pointer-events: none; z-index: 0;
  outline: 3px solid var(--sel); outline-offset: -3px;
}
.sq.last-from::after { background: var(--last-from); }
.sq.last-to::after { background: var(--last-to); }
.move-path { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 1; }
.move-path line {
  stroke: rgba(70, 70, 70, 0.72);
  stroke-width: 4;
  stroke-dasharray: 12 9;
  stroke-linecap: butt;
}
.move-path polyline { fill: none; stroke: rgba(70, 70, 70, 0.72); stroke-width: 4; stroke-dasharray: 12 9; stroke-linecap: butt; }
.sq.check::before {
  content: ""; position: absolute; inset: 0;
  background: radial-gradient(circle at center, var(--check), transparent 70%);
  pointer-events: none;
}
.piece {
  position: absolute;
  width: 80%; height: 80%; top: 10%; left: 10%;
  z-index: 2;
  pointer-events: none;
}
.sq.animating .piece { visibility: hidden; }
.move-animation {
  position: absolute;
  z-index: 4;
  object-fit: contain;
  pointer-events: none;
  will-change: transform;
}
.coord { position: absolute; font-size: 10px; color: var(--tertiaryText); pointer-events: none; }
.coord.file { right: 3px; bottom: 2px; }
.coord.rank { left: 3px; top: 2px; }

.promotion {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  display: flex; gap: 8px; padding: 12px;
  background: var(--surface);
  border-radius: var(--radius-panel);
  z-index: 5;
}
.promotion[hidden] { display: none; }
.promotion button {
  width: var(--touch); height: var(--touch);
  background: var(--surface-2); border: 0; border-radius: var(--radius-control);
  cursor: pointer; display: flex; align-items: center; justify-content: center;
}
.promotion .piece { position: static; width: 30px; height: 30px; top: auto; left: auto; }

.panel { display: flex; flex-direction: column; gap: var(--space-section); }
.group { background: var(--surface); border-radius: var(--radius-panel); padding: var(--space-panel); }
.section-label { font-size: 12px; font-weight: 600; color: var(--tertiaryText); margin: 0 0 var(--space-related); }

.row { display: flex; flex-wrap: wrap; gap: var(--space-related); align-items: center; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field > span { font-size: 13px; color: var(--secondaryText); }
.field > span em { font-style: normal; color: var(--accentText); font-weight: 600; }

.slider { width: 100%; height: 22px; accent-color: var(--accent); cursor: pointer; }
.slider-scale { display: flex; justify-content: space-between; font-size: 11px; color: var(--secondaryText); }
.policy-status { min-height: 16px; margin-top: 4px; font-size: 11px; color: var(--secondaryText); }

.btn {
  min-height: var(--touch);
  padding: 0 16px;
  border: 0;
  border-radius: var(--radius-control);
  background: var(--surface-2);
  color: var(--primaryText);
  font: inherit; font-weight: 600; font-size: 15px;
  cursor: pointer;
  transition: transform 0.12s ease, opacity 0.12s ease;
}
.btn.primary { background: var(--accent); color: #ffffff; }
.btn.secondary { background: var(--accentSoft); color: var(--accentText); }
.btn[aria-pressed="true"] { background: var(--accentSoft); color: var(--accentText); }
.btn.hint-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  border: 1px solid var(--strongSeparator);
}
.hint-state {
  min-width: 38px;
  padding: 2px 7px;
  border-radius: var(--radius-capsule);
  background: var(--strongSeparator);
  color: var(--primaryText);
  text-align: center;
  font-size: 12px;
  font-weight: 700;
  line-height: 1.5;
}
.btn.hint-toggle[aria-pressed="true"] { border-color: var(--accent); }
.btn.hint-toggle[aria-pressed="true"] .hint-state { background: var(--accent); color: #fff; }
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.play-toolbar .btn { min-height: 30px; height: 30px; padding: 0 7px; font-size: 12px; }

.segmented {
  display: inline-flex;
  gap: 3px;
  padding: 3px;
  background: var(--surface-2);
  border-radius: var(--radius-capsule);
  flex-wrap: wrap;
}
.pill {
  min-height: var(--touch);
  padding: 0 16px;
  border: 0;
  border-radius: var(--radius-capsule);
  background: transparent;
  color: var(--secondaryText);
  font: inherit; font-weight: 600; font-size: 15px;
  cursor: pointer;
  transition: background 0.12s ease, color 0.12s ease;
}
.pill.selected { background: var(--accentSoft); color: var(--accentText); }
.pill:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

.moves { list-style: none; margin: 0; padding: 0; max-height: 240px; overflow: auto; font-variant-numeric: tabular-nums; }
.moves li { display: flex; gap: 8px; padding: 7px 0; border-bottom: 1px solid var(--separator); font-size: 15px; }
.moves li:last-child { border-bottom: 0; }
.moves li .no { color: var(--tertiaryText); width: 2.4em; }
.move-entry { flex: 1; display: flex; align-items: baseline; gap: 4px; min-width: 0; }
.move-entry .san { flex: 1; min-width: 0; }
.move-eval { min-width: 2.8em; color: var(--secondaryText); text-align: right; font-size: 11px; font-variant-numeric: tabular-nums; }
.move-chart-wrap { margin: 2px 0 8px; }
.move-chart-label { color: var(--tertiaryText); font-size: 10px; line-height: 1.2; text-align: right; }
.move-chart-label-white { margin-bottom: 2px; }
.move-chart-label-black { margin-top: 2px; }
.move-eval-chart { display: block; width: 100%; height: 54px; margin: 3px 0 1px; overflow: visible; }
.move-eval-zero { stroke: var(--separator); stroke-width: 1; stroke-dasharray: 3 3; }
.move-eval-path { fill: none; stroke: var(--accent); stroke-width: 2.5; stroke-linecap: round; stroke-linejoin: round; }
.move-eval-current { fill: var(--accent); stroke: var(--surface); stroke-width: 1.5; }

.stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px 16px; margin: 0; }
.stats > div { display: flex; justify-content: space-between; gap: 8px; }
.stats dt { color: var(--secondaryText); font-size: 13px; }
.stats dd { margin: 0; font-variant-numeric: tabular-nums; }
.pv { margin-top: 12px; }
.pv summary { cursor: pointer; color: var(--secondaryText); font-size: 13px; }
.pv-text { display: block; margin-top: 6px; font-size: 14px; word-break: break-word; color: var(--primaryText); }

.footer {
  padding: 16px 0 24px;
  color: var(--tertiaryText);
  font-size: 13px;
  text-align: center;
  display: flex;
  gap: 6px;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
  flex-shrink: 0;
}
.foot-link { color: var(--accentText); text-decoration: none; font-weight: 600; }
.foot-link:hover { text-decoration: underline; }
.foot-link:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; border-radius: 4px; }

.mobile-menu-backdrop,
.mobile-menu-toggle { display: none; }

@media (min-width: 900px) {
  .layout {
    grid-template-columns: minmax(0, 1fr) 340px;
    grid-template-areas: "board controls";
    align-items: start;
  }
  .play-toolbar {
    width: 100%;
    max-width: none;
    margin: 0;
  }
}

@media (max-width: 899px) {
  .layout { gap: 8px; }
  .panel {
    display: flex;
    position: fixed;
    z-index: 31;
    left: 12px;
    right: 12px;
    bottom: calc(82px + env(safe-area-inset-bottom));
    max-height: min(78dvh, 720px);
    overflow-y: auto;
    padding: 12px;
    border: 1px solid var(--separator);
    border-radius: var(--radius-panel);
    background: var(--canvas);
    box-shadow: 0 16px 48px rgba(0, 0, 0, 0.35);
    transform: translateY(calc(100% + 100px));
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition: transform 0.2s ease, opacity 0.2s ease, visibility 0.2s ease;
  }
  .panel.mobile-menu-open {
    transform: translateY(0);
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
  }
  .mobile-menu-backdrop:not([hidden]) {
    display: block;
    position: fixed;
    z-index: 30;
    inset: 0;
    width: 100%;
    height: 100%;
    border: 0;
    padding: 0;
    background: rgba(0, 0, 0, 0.46);
  }
  .mobile-menu-toggle {
    display: flex;
    position: fixed;
    z-index: 32;
    right: max(16px, env(safe-area-inset-right));
    bottom: max(16px, env(safe-area-inset-bottom));
    width: 56px;
    height: 56px;
    align-items: center;
    justify-content: center;
    border: 0;
    border-radius: 50%;
    background: var(--accent);
    color: #fff;
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
  }
  .mobile-menu-toggle:focus-visible { outline: 3px solid var(--primaryText); outline-offset: 3px; }
  .menu-icon { width: 22px; height: 18px; display: flex; flex-direction: column; justify-content: space-between; }
  .menu-icon span { height: 2px; width: 100%; border-radius: 2px; background: currentColor; transition: transform 0.18s ease, opacity 0.18s ease; }
  .mobile-menu-toggle[aria-expanded="true"] .menu-icon span:first-child { transform: translateY(8px) rotate(45deg); }
  .mobile-menu-toggle[aria-expanded="true"] .menu-icon span:nth-child(2) { opacity: 0; }
  .mobile-menu-toggle[aria-expanded="true"] .menu-icon span:last-child { transform: translateY(-8px) rotate(-45deg); }
}

@media (max-width: 420px) {
  .material { gap: 2px 4px; }
  .material-label { width: 50px; font-size: 11px; }
  .material-piece { width: 22px; height: 22px; }
  .material-balance { padding: 3px 6px; font-size: 10px; }
}

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