:root {
  color-scheme: dark;
  --ink: #e9ece5;
  --muted: #9ca49a;
  --panel: rgba(23, 29, 25, 0.88);
  --panel-border: rgba(255, 255, 255, 0.09);
  --accent: #a4cf71;
  --accent-dark: #365b2c;
  --danger: #e18477;
  --board: #d3a65d;
  --board-dark: #a97839;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  height: 100vh;
  height: 100dvh;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  color: var(--ink);
  background:
    radial-gradient(circle at 20% 0%, rgba(73, 103, 68, .35), transparent 42rem),
    #0d120f;
}

button, select { font: inherit; }

button, .button-link {
  border: 1px solid transparent;
  border-radius: .75rem;
  padding: .78rem 1rem;
  color: var(--ink);
  background: #29342c;
  cursor: pointer;
  text-align: center;
  text-decoration: none;
  transition: transform .12s ease, background .12s ease, border-color .12s ease;
}

button:hover:not(:disabled), .button-link:hover { transform: translateY(-1px); background: #344239; }
button:disabled { cursor: not-allowed; opacity: .45; }
button.primary { background: var(--accent-dark); border-color: #56864a; }
button.secondary, .button-link { background: transparent; border-color: var(--panel-border); }
button.danger { background: transparent; border-color: rgba(225, 132, 119, .35); color: #f0a69c; }

.topbar {
  flex: 0 0 auto;
  width: min(1420px, 100%);
  margin: 0 auto;
  padding: .75rem clamp(1rem, 3vw, 2.5rem) .4rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

h1, h2, p { margin-top: 0; }
h1 { margin-bottom: 0; font-size: clamp(1.65rem, 3vw, 2.25rem); letter-spacing: -.04em; }
h2 { font-size: 1.15rem; }
.eyebrow { margin-bottom: .25rem; color: var(--accent); font-size: .68rem; font-weight: 800; letter-spacing: .18em; }
.muted { color: var(--muted); }
.hidden { display: none !important; }
.title-with-help { display: flex; align-items: center; gap: .4rem; }
.title-with-help > h2, .title-with-help > p { margin-bottom: 0; }
.helper { position: relative; display: inline-block; flex: 0 0 auto; }
.helper summary {
  display: grid;
  width: 1.35rem;
  height: 1.35rem;
  place-items: center;
  border: 1px solid var(--panel-border);
  border-radius: 50%;
  color: var(--muted);
  background: rgba(0, 0, 0, .18);
  cursor: pointer;
  font-size: .72rem;
  font-weight: 800;
  list-style: none;
}
.helper summary::-webkit-details-marker { display: none; }
.helper[open] summary { color: var(--accent); border-color: rgba(164, 207, 113, .4); }
.helper-text {
  position: absolute;
  z-index: 5;
  top: calc(100% + .4rem);
  left: 0;
  width: min(18rem, 70vw);
  margin: 0;
  padding: .65rem;
  border: 1px solid var(--panel-border);
  border-radius: .65rem;
  color: var(--muted);
  background: #121a14;
  box-shadow: 0 12px 30px rgba(0, 0, 0, .35);
  font-size: .78rem;
  line-height: 1.45;
}
.helper-right .helper-text { right: 0; left: auto; }

.chip {
  padding: .55rem .8rem;
  border: 1px solid var(--panel-border);
  border-radius: 999px;
  color: var(--muted);
  font-size: .82rem;
  background: rgba(0,0,0,.2);
}
.chip.active { color: #dff5c6; border-color: rgba(164, 207, 113, .4); }
.chip.hot::before { content: "●"; margin-right: .45rem; color: #ed9b63; }

.layout {
  flex: 1 1 auto;
  min-height: 0;
  width: min(1420px, 100%);
  margin: 0 auto;
  padding: .4rem clamp(1rem, 3vw, 2.5rem) .8rem;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(180px, 200px) 250px;
  gap: 1.2rem;
  align-items: start;
}

.board-card, .panel {
  border: 1px solid var(--panel-border);
  border-radius: 1.25rem;
  background: var(--panel);
  box-shadow: 0 24px 70px rgba(0, 0, 0, .22);
}

.board-card { min-height: 0; align-self: start; padding: clamp(.65rem, 1.3vw, .9rem); }
.board-heading { min-height: 3rem; padding: .1rem .2rem .55rem; display: flex; justify-content: space-between; gap: 1rem; }
.turn-label { margin-bottom: .25rem; font-weight: 750; }
.analysis-status { color: #edb67e; font-size: .78rem; text-align: right; }

.board-wrap {
  aspect-ratio: 1;
  width: min(100%, calc(100vh - 12rem));
  width: min(100%, calc(100dvh - 12rem));
  margin: 0 auto;
  border-radius: .75rem;
  overflow: hidden;
  background: linear-gradient(135deg, #dbb66e, var(--board));
  box-shadow: inset 0 0 50px rgba(88, 47, 16, .2);
}

#board { display: block; width: 100%; height: 100%; touch-action: manipulation; }
.grid-line { stroke: rgba(45, 28, 12, .78); stroke-width: 2; }
.coord { fill: rgba(50, 31, 13, .75); font-size: 20px; text-anchor: middle; dominant-baseline: middle; font-weight: 650; }
.hoshi { fill: rgba(43, 27, 11, .85); }
.stone-black { fill: #101512; stroke: #313933; stroke-width: 2; }
.stone-white { fill: #f3f0e7; stroke: #b9b5aa; stroke-width: 2; }
.stone-preview { opacity: .58; stroke: var(--accent); stroke-width: 5; }
.stone-pending { opacity: .9; stroke: var(--accent); stroke-width: 4; }
.last-move-marker { fill: none; stroke-width: 3.5; pointer-events: none; }
.last-move-marker.on-black { stroke: #f4f1e8; }
.last-move-marker.on-white { stroke: #222922; }
.loss-badge { pointer-events: none; }
.loss-background { fill: hsl(136 53% 39%); stroke: rgba(255, 255, 255, .28); stroke-width: 2; }
.loss-label {
  fill: #fff;
  stroke: rgba(0, 0, 0, .42);
  stroke-width: 2;
  paint-order: stroke;
  stroke-linejoin: round;
  font-weight: 900;
  text-anchor: middle;
  dominant-baseline: middle;
  pointer-events: none;
}
.hit { fill: transparent; cursor: pointer; }

.sidebar { min-height: 0; max-height: 100%; align-self: start; display: flex; flex-direction: column; gap: .7rem; overflow-y: auto; scrollbar-gutter: stable; }
.panel { padding: 1rem; }
.score-row { display: flex; justify-content: space-between; gap: 1rem; padding: .42rem 0; color: var(--muted); }
.score-row strong { color: var(--ink); font-weight: 650; }
.best-score-row { margin-top: .35rem; padding-top: .75rem; border-top: 1px solid var(--panel-border); }
.best-score-row strong { color: var(--accent); font-variant-numeric: tabular-nums; }
.result { margin-top: .8rem; padding: .8rem; border-radius: .7rem; color: #e7f8d2; background: rgba(83, 127, 67, .25); text-align: center; font-weight: 750; }
.controls { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .45rem; }
.controls button, .controls .button-link { min-width: 0; padding: .68rem .45rem; font-size: .82rem; }
.legend-scale {
  position: relative;
  display: flex;
  justify-content: space-between;
  padding-top: .7rem;
  color: var(--muted);
  font-size: .72rem;
}
.legend-scale::before {
  position: absolute;
  inset: 0 0 auto;
  height: .3rem;
  border-radius: 999px;
  background: linear-gradient(90deg, hsl(136 53% 39%), hsl(40 85% 46%) 20%, hsl(0 57% 53%));
  content: "";
}

.simulations-widget {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-self: start;
  width: auto;
  max-height: calc(100vh - 5.5rem);
  max-height: calc(100dvh - 5.5rem);
  margin: 0;
  padding: .7rem;
  border: 1px solid var(--panel-border);
  border-radius: 1.25rem;
  background: var(--panel);
  box-shadow: 0 24px 70px rgba(0, 0, 0, .22);
}
.simulations-drawer-handle { display: none; }
.simulations-heading {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .35rem;
  margin-bottom: .55rem;
}
.simulations-heading h2 { margin-bottom: 0; font-size: 1rem; }
.simulations-heading button { flex: 0 0 auto; padding: .4rem .45rem; font-size: .65rem; }
.simulations-scroll {
  flex: 1 1 auto;
  min-height: 0;
  height: auto;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: .15rem .35rem .35rem .15rem;
  scrollbar-gutter: stable;
}
.simulations-empty {
  display: grid;
  min-height: 100%;
  place-items: center;
  margin: 0;
  color: var(--muted);
  text-align: center;
}
.simulations-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: .55rem;
}
.simulation-card {
  min-width: 0;
  position: relative;
  padding: .35rem;
  border: 1px solid var(--panel-border);
  border-radius: .9rem;
  background: rgba(8, 13, 9, .48);
}
.simulation-board {
  display: block;
  width: 100%;
  aspect-ratio: 1;
  border-radius: .55rem;
  background: linear-gradient(135deg, #dbb66e, var(--board));
  box-shadow: inset 0 0 30px rgba(88, 47, 16, .2);
  pointer-events: none;
  user-select: none;
}
.simulation-card-controls {
  position: absolute;
  z-index: 2;
  inset: .7rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity .12s ease;
}
.simulation-card:hover .simulation-card-controls,
.simulation-card:focus-within .simulation-card-controls {
  opacity: 1;
  pointer-events: auto;
}
.simulation-result {
  position: absolute;
  top: 0;
  left: 0;
  overflow: hidden;
  padding: .28rem .42rem;
  border-radius: .4rem;
  color: #dfe8d9;
  background: rgba(12, 18, 13, .65);
  font-size: .68rem;
  font-weight: 800;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.simulation-card.error .simulation-result { color: #ffd8d2; background: rgba(58, 33, 31, .9); }
.simulation-delete {
  position: absolute;
  top: 0;
  right: 0;
  padding: .28rem .42rem;
  border-radius: .4rem;
  color: #ffd8d2;
  background: rgba(58, 33, 31, .88);
  font-size: .65rem;
  pointer-events: auto;
}
.simulation-navigation {
  position: absolute;
  bottom: 0;
  left: 50%;
  display: flex;
  gap: .2rem;
  padding: .25rem;
  border-radius: .55rem;
  background: rgba(12, 18, 13, .78);
  transform: translateX(-50%);
  pointer-events: auto;
}
.simulation-navigation button {
  display: grid;
  width: 1.65rem;
  height: 1.65rem;
  place-items: center;
  padding: 0;
  border-radius: .35rem;
  color: #e7eee4;
  background: rgba(55, 70, 59, .92);
  font-size: .65rem;
  line-height: 1;
}
.simulation-card-controls button:hover:not(:disabled) { transform: none; }
.legend .title-with-help { justify-content: space-between; }

.setup {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  padding: 1rem;
  background: rgba(7, 10, 8, .86);
  backdrop-filter: blur(12px);
}
.setup-card { width: min(620px, 100%); padding: clamp(1.4rem, 5vw, 2.4rem); border: 1px solid var(--panel-border); border-radius: 1.25rem; background: #172019; }
.engine-log { max-height: 17rem; overflow: auto; margin: 1rem 0 0; padding: .8rem; border-radius: .6rem; color: #b7c6b8; background: #080c09; font: .7rem/1.45 ui-monospace, SFMono-Regular, Menlo, monospace; white-space: pre-wrap; }

dialog { width: min(440px, calc(100% - 2rem)); color: var(--ink); border: 1px solid var(--panel-border); border-radius: 1rem; background: #172019; }
dialog::backdrop { background: rgba(7, 10, 8, .75); backdrop-filter: blur(8px); }
dialog form { display: grid; gap: 1rem; }
dialog label { display: grid; gap: .4rem; color: var(--muted); font-size: .85rem; }
dialog select { width: 100%; padding: .75rem; border: 1px solid var(--panel-border); border-radius: .65rem; color: var(--ink); background: #0e1410; }
.form-note { color: var(--muted); font-size: .78rem; }
.dialog-actions { display: flex; justify-content: flex-end; gap: .5rem; }

.toast { position: fixed; left: 50%; bottom: 1.2rem; z-index: 20; transform: translateX(-50%); padding: .75rem 1rem; border: 1px solid rgba(225,132,119,.4); border-radius: .7rem; color: #ffd8d2; background: #3a211f; box-shadow: 0 10px 30px rgba(0,0,0,.3); }

@media (max-width: 1100px), (max-height: 700px) {
  body { height: auto; min-height: 100vh; overflow: auto; }
  .layout { grid-template-columns: minmax(0, 1fr) 270px; }
  .simulations-widget {
    position: fixed;
    z-index: 9;
    top: 4.5rem;
    right: 0;
    width: min(21rem, calc(100vw - 3rem));
    max-height: calc(100vh - 5.5rem);
    max-height: calc(100dvh - 5.5rem);
    border-radius: 1rem 0 0 1rem;
    transform: translateX(100%);
    transition: transform .18s ease;
  }
  .simulations-widget:hover,
  .simulations-widget:focus-within,
  .simulations-widget.drawer-open,
  .simulations-drawer-handle:hover + .simulations-widget,
  .simulations-drawer-handle:focus + .simulations-widget { transform: translateX(0); }
  .simulations-drawer-handle {
    position: fixed;
    z-index: 10;
    top: 5.5rem;
    right: 0;
    display: block;
    padding: .65rem .45rem;
    border: 1px solid var(--panel-border);
    border-right: 0;
    border-radius: .7rem 0 0 .7rem;
    background: #172019;
    font-size: .7rem;
    letter-spacing: .04em;
    writing-mode: vertical-rl;
  }
  .sidebar { grid-column: 2; grid-row: 1; overflow: visible; }
}

@media (max-width: 700px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { grid-template-columns: 1fr; }
  .sidebar { grid-column: auto; grid-row: auto; display: grid; overflow: visible; }
  .legend { grid-column: auto; }
  .topbar { align-items: flex-end; }
  .chip { max-width: 52%; text-align: right; }
}
