:root {
  --bg: #f4f1ea;
  --surface: #ffffff;
  --surface-2: #f8f6f1;
  --border: #ddd6c7;
  --text: #1e293b;
  --text-muted: #64748b;
  --accent: #1d4ed8;
  --accent-soft: #eff6ff;
  --red: #dc2626;
  --green: #059669;
  --amber: #d97706;
  --topbar-h: 50px;
  --panel-w: 336px;
  --radius: 10px;
  --shadow: 0 2px 10px rgba(15, 23, 42, 0.06);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  width: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  font-size: 14px;
  overscroll-behavior: none;
}

.hidden {
  display: none !important;
}

.flex-1 {
  flex: 1 1 0%;
}

/* App Shell */
.app-shell {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  width: 100vw;
  overflow: hidden;
}

/* Top Bar */
.topbar {
  height: var(--topbar-h);
  min-height: var(--topbar-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 0 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: 30;
}

.topbar-left,
.topbar-center,
.topbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 9px;
}

.brand-icon {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  background: #fdfbf4;
  border: 1.5px solid #92b4db;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 13px;
  letter-spacing: -0.5px;
  box-shadow: inset 0 0 0 1px rgba(197, 216, 236, 0.6);
}

.brand-icon .bx {
  color: #1d4ed8;
}

.brand-icon .bo {
  color: #dc2626;
}

.brand-text {
  display: flex;
  flex-direction: column;
  line-height: 1.15;
}

.brand-text h1 {
  margin: 0;
  font-size: 15px;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.rule-badge {
  font-size: 11px;
  color: var(--text-muted);
  font-weight: 500;
}

/* Server / Local Toggle Pill */
.mode-switch {
  display: inline-flex;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 2px;
  gap: 2px;
}

.mode-btn {
  border: none;
  background: transparent;
  color: var(--text-muted);
  padding: 5px 12px;
  border-radius: 999px;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: all 0.16s ease;
}

.mode-btn.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 3px rgba(15, 23, 42, 0.12);
}

.mode-btn .dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #cbd5e1;
  flex-shrink: 0;
}

.mode-btn .dot.ready {
  background: #10b981;
}

.mode-btn .dot.busy {
  background: #f59e0b;
  animation: pulseDot 1s infinite;
}

.mode-btn .dot.error {
  background: #ef4444;
}

@keyframes pulseDot {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.35; }
}

.icon-btn {
  width: 34px;
  height: 34px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.15s;
}

.icon-btn:hover {
  background: var(--surface-2);
}

/* Loading Banner */
.loading-banner {
  background: #eff6ff;
  border-bottom: 1px solid #bfdbfe;
  padding: 6px 14px;
  z-index: 25;
}

.loading-info {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  font-weight: 600;
  color: #1e40af;
  margin-bottom: 4px;
}

.loading-track {
  height: 5px;
  background: #dbeafe;
  border-radius: 999px;
  overflow: hidden;
}

.loading-fill {
  height: 100%;
  background: #2563eb;
  border-radius: 999px;
  transition: width 0.2s ease;
}

/* Main Workspace */
.workspace {
  flex: 1 1 0%;
  min-height: 0;
  display: flex;
  position: relative;
  overflow: hidden;
}

/* Wide Board Stage */
.board-stage {
  flex: 1 1 0%;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  position: relative;
}

/* Slim Evaluation Strip */
.eval-strip {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 5px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  min-height: 34px;
}

.turn-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-weight: 600;
  white-space: nowrap;
}

.turn-symbol {
  width: 20px;
  height: 20px;
  border-radius: 5px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 12px;
  color: #fff;
}

.turn-symbol.x {
  background: #1d4ed8;
}

.turn-symbol.o {
  background: #dc2626;
}

.winrate-wrap {
  flex: 1 1 0%;
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 80px;
  max-width: 360px;
}

.winrate-bar {
  flex: 1 1 0%;
  height: 8px;
  border-radius: 999px;
  background: #ef4444;
  overflow: hidden;
  border: 1px solid rgba(15, 23, 42, 0.12);
}

.winrate-x {
  height: 100%;
  background: #2563eb;
  transition: width 0.28s ease;
}

.eval-text {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11.5px;
  font-weight: 600;
  color: var(--text-muted);
  white-space: nowrap;
}

.move-counter {
  margin-left: auto;
  font-size: 11.5px;
  font-weight: 600;
  color: var(--text-muted);
  white-space: nowrap;
}

/* Canvas Container */
.board-container {
  flex: 1 1 0%;
  min-height: 0;
  position: relative;
  overflow: hidden;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

#caroCanvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: pointer;
}

/* Winner Banner */
.winner-banner {
  position: absolute;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 10px;
  background: rgba(15, 23, 42, 0.92);
  color: #fff;
  padding: 8px 14px;
  border-radius: 999px;
  font-weight: 700;
  font-size: 13.5px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.28);
  z-index: 12;
  white-space: nowrap;
}

/* Floating Zoom Controls */
.zoom-controls {
  position: absolute;
  right: 12px;
  bottom: 12px;
  display: inline-flex;
  align-items: center;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(8px);
  border: 1px solid rgba(15, 23, 42, 0.14);
  border-radius: 999px;
  box-shadow: 0 4px 14px rgba(15, 23, 42, 0.14);
  overflow: hidden;
  z-index: 10;
}

.zoom-btn {
  border: none;
  background: transparent;
  color: var(--text);
  height: 34px;
  min-width: 34px;
  padding: 0 8px;
  font-size: 17px;
  font-weight: 700;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.zoom-btn:active {
  background: rgba(15, 23, 42, 0.08);
}

.zoom-pct {
  font-size: 11.5px;
  font-weight: 700;
  font-family: ui-monospace, SFMono-Regular, monospace;
  border-left: 1px solid rgba(15, 23, 42, 0.1);
  border-right: 1px solid rgba(15, 23, 42, 0.1);
  min-width: 48px;
}

/* Side Panel */
.side-panel {
  width: var(--panel-w);
  min-width: var(--panel-w);
  background: var(--surface-2);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
  z-index: 20;
  transition: margin-right 0.2s ease;
}

.app-shell.panel-collapsed .side-panel {
  display: none;
}

.panel-tabs {
  display: flex;
  align-items: center;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 6px;
}

.panel-tab {
  flex: 1 1 0%;
  border: none;
  background: transparent;
  padding: 11px 8px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  transition: color 0.15s, border-color 0.15s;
}

.panel-tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

.panel-close {
  border: none;
  background: transparent;
  font-size: 18px;
  color: var(--text-muted);
  padding: 6px 10px;
  cursor: pointer;
}

.panel-body {
  flex: 1 1 0%;
  min-height: 0;
  overflow-y: auto;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  -webkit-overflow-scrolling: touch;
}

/* Cards */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  box-shadow: var(--shadow);
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 6px;
}

.card-title {
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  margin-bottom: 8px;
}

.card-header .card-title {
  margin-bottom: 0;
}

.hint-text {
  font-size: 11px;
  color: var(--text-muted);
}

/* Segmented Grid */
.segmented-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

.seg-btn {
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  padding: 7px 8px;
  border-radius: 7px;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.14s ease;
}

.seg-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

/* Action Buttons */
.action-row {
  display: flex;
  gap: 7px;
}

.btn {
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  transition: all 0.14s ease;
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-xs {
  padding: 4px 10px;
  font-size: 12px;
}

.btn-primary {
  background: var(--accent);
  color: #fff;
}

.btn-primary:hover:not(:disabled) {
  background: #1e40af;
}

.btn-accent {
  background: #0f766e;
  color: #fff;
}

.btn-accent:hover:not(:disabled) {
  background: #115e59;
}

.btn-danger {
  background: var(--red);
  color: #fff;
}

.btn-danger-outline {
  background: transparent;
  border-color: #fca5a5;
  color: var(--red);
}

.btn-ghost {
  background: var(--surface);
  border-color: var(--border);
  color: var(--text);
}

.btn-ghost:hover:not(:disabled) {
  background: var(--surface-2);
}

.link-btn {
  border: none;
  background: transparent;
  color: var(--accent);
  font-size: 11.5px;
  font-weight: 600;
  cursor: pointer;
  padding: 0;
}

/* Telemetry */
.status-tag {
  font-size: 11px;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 999px;
  background: #e2e8f0;
  color: #334155;
}

.status-tag.ready {
  background: #dcfce7;
  color: #166534;
}

.status-tag.thinking {
  background: #fef3c7;
  color: #92400e;
}

.status-tag.error {
  background: #fee2e2;
  color: #991b1b;
}

.engine-sub {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 8px;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 6px;
}

.stat-box {
  background: var(--surface-2);
  border: 1px solid #ece7db;
  border-radius: 7px;
  padding: 6px 8px;
}

.stat-lbl {
  font-size: 10.5px;
  color: var(--text-muted);
}

.stat-val {
  font-size: 13px;
  font-weight: 700;
  font-family: ui-monospace, SFMono-Regular, monospace;
}

/* PV List */
.pv-list {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.pv-item {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 6px 8px;
  border-radius: 7px;
  background: var(--surface-2);
  border: 1px solid #ece7db;
  cursor: pointer;
  font-size: 12px;
  transition: border-color 0.12s, background 0.12s;
}

.pv-item:hover,
.pv-item.active {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.pv-rank {
  font-weight: 800;
  color: var(--accent);
  min-width: 18px;
}

.pv-score {
  font-family: ui-monospace, SFMono-Regular, monospace;
  font-weight: 700;
  font-size: 11.5px;
  padding: 1px 5px;
  border-radius: 4px;
  background: #e2e8f0;
  white-space: nowrap;
}

.pv-score.pos {
  background: #dcfce7;
  color: #166534;
}

.pv-score.neg {
  background: #fee2e2;
  color: #991b1b;
}

.pv-moves {
  font-family: ui-monospace, SFMono-Regular, monospace;
  font-size: 11.5px;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.empty-note {
  font-size: 12px;
  color: var(--text-muted);
  padding: 6px 2px;
}

/* Engine Log */
.engine-log {
  font-family: ui-monospace, SFMono-Regular, monospace;
  font-size: 11px;
  color: var(--text-muted);
  background: var(--surface-2);
  border: 1px solid #ece7db;
  border-radius: 7px;
  padding: 7px 8px;
  max-height: 92px;
  overflow-y: auto;
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.4;
}

/* Settings Form Controls */
.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 6px 0;
  border-bottom: 1px solid #f1ede4;
}

.setting-row:last-child {
  border-bottom: none;
}

.setting-row.stack {
  flex-direction: column;
  align-items: stretch;
  gap: 4px;
}

.range-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.mono-val {
  font-family: ui-monospace, SFMono-Regular, monospace;
  font-weight: 700;
  font-size: 12.5px;
  color: var(--accent);
}

.setting-row label {
  font-size: 12.5px;
  font-weight: 500;
  color: var(--text);
}

.setting-row select,
.setting-row input[type='number'] {
  max-width: 170px;
  padding: 5px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface-2);
  color: var(--text);
  font-size: 12.5px;
}

.setting-row input[type='range'] {
  width: 100%;
  accent-color: var(--accent);
}

.setting-note {
  margin: 6px 0 0;
  font-size: 11.5px;
  color: var(--text-muted);
  line-height: 1.4;
}

.custom-box {
  margin-top: 4px;
  padding-top: 4px;
  border-top: 1px dashed var(--border);
}

/* Toggle Checkbox */
.toggle {
  appearance: none;
  -webkit-appearance: none;
  width: 38px;
  height: 22px;
  border-radius: 999px;
  background: #cbd5e1;
  position: relative;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.16s;
}

.toggle::after {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
  transition: transform 0.16s;
}

.toggle:checked {
  background: var(--accent);
}

.toggle:checked::after {
  transform: translateX(16px);
}

/* Mobile Dock */
.mobile-dock {
  display: none;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 5px 6px calc(5px + env(safe-area-inset-bottom, 0px));
  gap: 4px;
}

.dock-btn {
  flex: 1 1 0%;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text);
  border-radius: 8px;
  padding: 5px 2px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
}

.dock-btn .dock-ico {
  font-size: 16px;
  line-height: 1;
}

.dock-btn.dock-primary {
  background: var(--accent);
  color: #fff;
}

.dock-btn.dock-danger {
  background: var(--red);
  color: #fff;
}

.mobile-only {
  display: none;
}

/* Responsive Mobile Layout */
@media (max-width: 860px) {
  .desktop-only {
    display: none !important;
  }

  .mobile-only {
    display: inline-flex;
  }

  .mobile-dock {
    display: flex;
  }

  .topbar {
    padding: 0 8px;
    gap: 6px;
  }

  .brand-text h1 {
    font-size: 14px;
  }

  .rule-badge {
    font-size: 10.5px;
  }

  .mode-btn {
    padding: 5px 9px;
    font-size: 12px;
  }

  .eval-strip {
    padding: 4px 8px;
    gap: 7px;
  }

  .winrate-wrap {
    max-width: none;
  }

  .zoom-controls {
    right: 8px;
    bottom: 8px;
  }

  .zoom-btn {
    height: 36px;
    min-width: 36px;
  }

  /* Full-screen slide-over sheet for Side Panel on Mobile */
  .side-panel {
    position: fixed;
    inset: var(--topbar-h) 0 0 0;
    width: 100%;
    min-width: 0;
    border-left: none;
    transform: translateY(105%);
    transition: transform 0.22s ease;
    box-shadow: 0 -8px 30px rgba(0, 0, 0, 0.2);
  }

  .side-panel.mobile-open {
    transform: translateY(0);
  }
}
