:root {
  --bg: #14161c;
  --panel: #1e2129;
  --panel-2: #262a34;
  --text: #e8eaf0;
  --muted: #9aa1b1;
  --accent: #4fa3ff;
  --accent-2: #2f7fd6;
  --ok: #3ddc84;
  --bad: #ff5a6a;
  --edge: #8b93a7;
  --node: #ffffff;
  --node-stroke: #4fa3ff;
  --radius: 12px;
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
}

body {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  padding-top: var(--sat);
  padding-bottom: var(--sab);
}

.bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 14px;
  padding: 10px 12px;
  background: var(--panel);
}

.bar h1 {
  font-size: 1.15rem;
  margin: 0;
  letter-spacing: 0.02em;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-left: auto;
}

.stepper {
  display: inline-flex;
  align-items: center;
  background: var(--panel-2);
  border-radius: var(--radius);
  overflow: hidden;
}

.stepper-label {
  padding: 0 8px 0 10px;
  color: var(--muted);
  font-size: 0.85rem;
}

.stepper button {
  width: 40px;
  height: 40px;
  border: 0;
  background: transparent;
  color: var(--text);
  font-size: 1.3rem;
  cursor: pointer;
  touch-action: manipulation;
}
.stepper button:active { background: rgba(255, 255, 255, 0.08); }

.stepper input {
  width: 4.5em;
  height: 40px;
  border: 0;
  background: transparent;
  color: var(--text);
  text-align: center;
  font: inherit;
  font-weight: 600;
  -moz-appearance: textfield;
}
.stepper input::-webkit-outer-spin-button,
.stepper input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.stepper input:focus { outline: 2px solid var(--accent); outline-offset: -2px; border-radius: 6px; }

.btn {
  height: 40px;
  padding: 0 14px;
  border: 0;
  border-radius: var(--radius);
  background: var(--panel-2);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
}
.btn:active { filter: brightness(1.15); }
.btn.primary { background: var(--accent); color: #08111f; }
.btn.primary:active { background: var(--accent-2); }
.btn:disabled { opacity: 0.5; cursor: default; }

.stats {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 18px;
  padding: 6px 14px;
  font-size: 0.9rem;
  color: var(--muted);
  background: var(--panel);
  border-top: 1px solid rgba(255, 255, 255, 0.05);
}
.stat strong { color: var(--text); font-variant-numeric: tabular-nums; }
.stat.good strong { color: var(--ok); }

.board {
  flex: 1;
  min-height: 0;
  position: relative;
  padding: 6px;
}

#svg {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  border-radius: var(--radius);
  background: radial-gradient(circle at 50% 40%, #1b1f28 0%, var(--bg) 75%);
}

#svg line {
  stroke: var(--edge);
  stroke-width: 2.5;
  stroke-linecap: round;
  transition: stroke 120ms;
}
#svg line.cross { stroke: var(--bad); }
#svg.solved line { stroke: var(--ok); }

#svg .node { cursor: grab; }
#svg .node.dragging { cursor: grabbing; }
#svg .node .hit { fill: transparent; }
#svg .node .dot {
  fill: var(--node);
  stroke: var(--node-stroke);
  stroke-width: 3;
}
#svg .node.dragging .dot { stroke-width: 5; }
#svg.solved .node .dot { stroke: var(--ok); }

.overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(3px);
  z-index: 10;
}
.overlay[hidden] { display: none; }

.card {
  width: min(100%, 420px);
  max-height: calc(100dvh - 32px);
  overflow: auto;
  padding: 20px 22px;
  background: var(--panel);
  border-radius: 18px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
  user-select: none;
  -webkit-user-select: none;
}
.card input { user-select: text; -webkit-user-select: text; }
.card h2 { margin: 0 0 8px; }
.card p { margin: 6px 0; }
.card .big { font-size: 1.25rem; }
.card .hint { color: var(--muted); font-size: 0.85rem; min-height: 1.2em; }
.card label { display: block; color: var(--muted); font-size: 0.85rem; margin-bottom: 4px; }

.submit { margin: 14px 0 6px; }
.row { display: flex; gap: 8px; align-items: center; }
.row input[type="text"] {
  flex: 1;
  min-width: 0;
  height: 40px;
  padding: 0 12px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: var(--radius);
  background: var(--panel-2);
  color: var(--text);
  font: inherit;
}
.row input[type="text"]:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.actions { justify-content: flex-end; margin-top: 14px; }
.actions .btn { flex: 1; }

.lb { margin: 8px 0 0; padding-left: 1.6em; }
.lb li { padding: 4px 0; display: flex; gap: 10px; }
.lb li .who { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb li .moves { font-variant-numeric: tabular-nums; color: var(--muted); }
.lb li.me .who { color: var(--accent); font-weight: 600; }

.stepper button,
.stepper-label { flex-shrink: 0; }

@media (max-width: 520px) {
  .bar h1 { flex-basis: 100%; }
  .controls {
    margin-left: 0;
    width: 100%;
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 8px;
  }
  .stepper { display: flex; justify-content: space-between; min-width: 0; }
  .stepper input { flex: 1; min-width: 0; width: auto; }
  .controls > :nth-child(1) { grid-area: 1 / 1; }
  .controls > :nth-child(2) { grid-area: 2 / 1; }
  .controls > :nth-child(3) { grid-area: 1 / 2; }
  .controls > :nth-child(4) { grid-area: 2 / 2; }
  .stats { gap: 4px 12px; font-size: 0.85rem; }
}
