:root {
  color-scheme: dark;
  --bg: #0a0a0b;
  --panel-bg: #131315;
  --panel-bg-2: #1a1a1d;
  --board-bg: #050505;
  --cell-bg: #131315;
  --cell-bg-alt: #17171a;
  --line: #2a2a2e;
  --line-soft: #202023;
  --text: #ece9e2;
  --text-dim: #96938c;
  --text-faint: #5f5c56;
  --accent: #cfa64b;
  --accent-dim: #7a6431;
  --p1-a: #ff6b5d;
  --p1-b: #8a1f19;
  --p2-a: #5ea1ff;
  --p2-b: #16336e;
  --danger: #e05a4e;
  --radius: 10px;
}

@media (prefers-color-scheme: light) {
  :root {
    color-scheme: dark;
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, Helvetica, Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
}

.app {
  max-width: 1080px;
  margin: 0 auto;
  padding: 20px 16px 48px;
  padding-left: max(16px, env(safe-area-inset-left));
  padding-right: max(16px, env(safe-area-inset-right));
  padding-bottom: max(48px, env(safe-area-inset-bottom));
}

.topbar {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}

.topbar h1 {
  font-size: 1.4rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  margin: 0;
  color: var(--text);
}

.status {
  font-size: 0.95rem;
  color: var(--text-dim);
  min-height: 1.2em;
}

.status.win {
  color: var(--accent);
  font-weight: 600;
}

.layout {
  display: grid;
  grid-template-columns: 28px minmax(300px, 560px) 300px;
  gap: 22px;
  align-items: start;
  justify-content: center;
}

/* --- Eval bar --- */

.eval-col {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  position: sticky;
  top: 20px;
  transition: opacity 0.15s ease, filter 0.15s ease;
}

.eval-col.disabled {
  opacity: 0.35;
  filter: grayscale(1);
  pointer-events: none;
}

.eval-bar {
  width: 22px;
  height: min(70vh, 520px);
  border-radius: 6px;
  overflow: hidden;
  background: var(--p2-b);
  border: 1px solid var(--line);
  position: relative;
  display: flex;
  align-items: flex-end;
}

.eval-fill {
  width: 100%;
  height: var(--pct, 50%);
  background: var(--p1-a);
  transition: height 0.4s ease;
}

.eval-label {
  font-variant-numeric: tabular-nums;
  font-size: 0.75rem;
  color: var(--text-dim);
  writing-mode: horizontal-tb;
}

/* --- Board --- */

.board-col {
  display: flex;
  justify-content: center;
}

.board-grid {
  display: grid;
  grid-template-columns: minmax(32px, 44px) repeat(7, 1fr) minmax(32px, 44px);
  grid-template-rows: minmax(32px, 44px) repeat(7, 1fr) minmax(32px, 44px);
  gap: 3px;
  touch-action: manipulation;
  width: min(100%, 560px);
  aspect-ratio: 1 / 1;
  background: var(--board-bg);
  padding: 8px;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  box-shadow: 0 20px 60px -30px rgba(0, 0, 0, 0.9), inset 0 0 0 1px rgba(255, 255, 255, 0.02);
}

.cell {
  background: var(--cell-bg);
  border-radius: 5px;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

.cell::before {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 5px;
  box-shadow: inset 0 0 0 1px var(--line-soft);
}

.cell.landing {
  box-shadow: inset 0 0 0 2px var(--accent);
}

.marble {
  width: 78%;
  height: 78%;
  border-radius: 50%;
  transform: scale(0.001);
  transition: transform 0.18s cubic-bezier(0.22, 0.61, 0.36, 1);
}

.marble.placed {
  transform: scale(1);
}

.marble.p1 {
  background: radial-gradient(circle at 32% 28%, var(--p1-a), var(--p1-b) 75%);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.5), inset 0 0 0 1px rgba(0, 0, 0, 0.25);
}

.marble.p2 {
  background: radial-gradient(circle at 32% 28%, var(--p2-a), var(--p2-b) 75%);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.5), inset 0 0 0 1px rgba(0, 0, 0, 0.25);
}

.marble.last-move::after {
  content: "";
  position: absolute;
  inset: 14%;
  border-radius: 50%;
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.85);
}

/* --- Edge push arrows --- */

.arrow {
  background: transparent;
  border: none;
  border-radius: 5px;
  color: var(--text-faint);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  position: relative;
  transition: background 0.15s ease, color 0.15s ease;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.arrow:disabled {
  cursor: default;
  opacity: 0.25;
}

.arrow .chevron {
  width: 45%;
  height: 45%;
  fill: currentColor;
  transition: transform 0.15s ease;
}

/* Base chevron points down; rotate per edge so it always points into the board. */
.arrow.bottom .chevron {
  transform: rotate(180deg);
}

.arrow.left .chevron {
  transform: rotate(-90deg);
}

.arrow.right .chevron {
  transform: rotate(90deg);
}

.arrow:not(:disabled):hover {
  background: var(--panel-bg-2);
  color: var(--text);
}

.arrow:not(:disabled):active {
  background: var(--line);
}

.arrow.best-1,
.arrow.best-2,
.arrow.best-3 {
  color: var(--bg);
}

.arrow.best-1 {
  background: var(--accent);
}

.arrow.best-2 {
  background: color-mix(in srgb, var(--accent) 65%, transparent);
}

.arrow.best-3 {
  background: color-mix(in srgb, var(--accent) 35%, transparent);
}

.arrow .rank-eval {
  position: absolute;
  font-size: 0.55rem;
  bottom: 1px;
  right: 2px;
  font-variant-numeric: tabular-nums;
  opacity: 0.85;
}

/* --- Panel --- */

.panel {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.panel-section {
  background: var(--panel-bg);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px 14px;
}

.panel-section h2 {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-faint);
  margin: 0 0 10px;
  font-weight: 600;
}

.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-top: 10px;
}

.row:first-child {
  margin-top: 0;
}

.row label {
  font-size: 0.85rem;
  color: var(--text-dim);
}

.row select {
  background: var(--panel-bg-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px;
  font-size: 0.85rem;
  min-height: 38px;
}

.row input[type="range"] {
  flex: 1;
  margin-left: 12px;
  accent-color: var(--accent);
  height: 28px;
  touch-action: manipulation;
}

.row input[type="range"]::-webkit-slider-thumb {
  width: 20px;
  height: 20px;
}

.row input[type="range"]::-moz-range-thumb {
  width: 20px;
  height: 20px;
}

.segmented {
  display: flex;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
}

.segmented button {
  flex: 1;
  background: var(--panel-bg-2);
  color: var(--text-dim);
  border: none;
  padding: 10px 8px;
  font-size: 0.8rem;
  cursor: pointer;
  border-right: 1px solid var(--line);
  transition: background 0.15s ease, color 0.15s ease;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.segmented button:last-child {
  border-right: none;
}

.segmented button:hover {
  color: var(--text);
}

.segmented button.active {
  background: var(--accent-dim);
  color: var(--text);
}

.toggle {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.85rem;
  color: var(--text-dim);
  cursor: pointer;
  padding: 4px 0;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.toggle input {
  accent-color: var(--accent);
  width: 20px;
  height: 20px;
  flex-shrink: 0;
}

.hud-row {
  display: flex;
  justify-content: space-between;
  font-size: 0.75rem;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
  gap: 6px;
}

.hud-progress {
  margin-top: 8px;
  height: 4px;
  border-radius: 2px;
  background: var(--line-soft);
  overflow: hidden;
}

.hud-progress-fill {
  height: 100%;
  width: 0%;
  background: var(--accent);
  transition: width 0.25s ease;
}

.actions {
  display: flex;
  gap: 10px;
}

.actions button {
  flex: 1;
  background: var(--panel-bg-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px 10px;
  font-size: 0.85rem;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.actions button:hover {
  background: var(--line);
  border-color: var(--accent-dim);
}

.actions button:disabled {
  opacity: 0.35;
  cursor: default;
}

/* --- Responsive --- */

@media (max-width: 860px) {
  .layout {
    grid-template-columns: 1fr;
    gap: 14px;
  }

  .eval-col {
    order: 1;
    flex-direction: row-reverse;
    width: 100%;
    position: static;
    gap: 10px;
  }

  .eval-bar {
    width: 100%;
    height: 18px;
    display: flex;
    flex-direction: row;
    align-items: stretch;
  }

  .eval-fill {
    height: 100%;
    width: var(--pct, 50%);
    background: linear-gradient(90deg, var(--p1-a), var(--p1-b));
    transition: width 0.4s ease;
  }

  .board-col {
    order: 2;
  }

  .panel {
    order: 3;
  }
}

@media (max-width: 420px) {
  .app {
    padding-top: 14px;
  }

  .topbar h1 {
    font-size: 1.2rem;
  }

  .panel-section {
    padding: 10px 12px;
  }
}
