:root {
  --paper: #fafafa;
  --ink: #1f1f1f;
  --muted-ink: #6f6f6f;
  --soft-ink: #9a9a9a;
  --line: #e2e2e2;
  --line-strong: #cfcfcf;
  --danger: #9f1d1d;
  --watch: #9a6a10;
  --surface: #ffffff;
  --start-red: #f40009;
  --p1-finish-fill: #ffadad;
  --p1-finish-stroke: #b70a0a;
  --p2-finish-fill: #adadff;
  --p2-finish-stroke: #0a0ab7;
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
  overscroll-behavior: none;
}

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  letter-spacing: 0;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

button,
input,
select {
  font: inherit;
}

button {
  min-height: 40px;
  border-radius: 4px;
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.42;
}

.shell {
  min-height: 100svh;
  min-height: 100dvh;
  display: grid;
  grid-template-rows: auto auto auto minmax(0, 1fr) auto auto auto;
  grid-template-areas:
    "topbar"
    "rules"
    "status"
    "board"
    "action"
    "finish"
    "debug";
  gap: 10px;
  padding:
    max(12px, env(safe-area-inset-top))
    max(12px, env(safe-area-inset-right))
    max(12px, env(safe-area-inset-bottom))
    max(12px, env(safe-area-inset-left));
}

.topbar,
.rules-panel,
.status-line,
.action-bar,
.finish-panel,
.debug-panel {
  width: min(100%, 760px);
  margin: 0 auto;
}

.topbar {
  grid-area: topbar;
}

.rules-panel {
  grid-area: rules;
}

.status-line {
  grid-area: status;
}

.board-wrap {
  grid-area: board;
}

.action-bar {
  grid-area: action;
}

.finish-panel {
  grid-area: finish;
}

.debug-panel {
  grid-area: debug;
}

.topbar,
.status-line,
.action-bar,
.finish-panel {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.topbar {
  flex-wrap: wrap;
}

.brand-lockup {
  display: flex;
  align-items: center;
  gap: 9px;
  margin-right: auto;
}

.logo-mark {
  display: block;
  width: 34px;
  height: 34px;
  aspect-ratio: 1;
  flex: 0 0 auto;
}

.brand-title {
  margin: 0;
  font-size: 20px;
  font-weight: 700;
  line-height: 1;
}

.mode-switch {
  display: grid;
  grid-template-columns: repeat(2, minmax(64px, 1fr));
  gap: 1px;
  border: 1px solid var(--ink);
  border-radius: 4px;
  overflow: hidden;
}

.input-switch {
  display: grid;
  grid-template-columns: repeat(2, minmax(76px, 1fr));
  gap: 1px;
  border: 1px solid var(--ink);
  border-radius: 4px;
  overflow: hidden;
}

.color-switch {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 40px;
  padding: 0 2px;
}

.color-chip {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--muted-ink);
  font-size: 11px;
  line-height: 1;
}

.color-chip input {
  width: 28px;
  height: 28px;
  min-height: 28px;
  padding: 0;
  border: 1px solid var(--ink);
  border-radius: 50%;
  background: transparent;
  cursor: pointer;
}

.color-chip input::-webkit-color-swatch-wrapper {
  padding: 0;
}

.color-chip input::-webkit-color-swatch {
  border: 0;
  border-radius: 50%;
}

.round-actions {
  display: flex;
  gap: 8px;
}

.mode-button,
.input-button,
.ghost-button,
.solid-button {
  border: 1px solid var(--ink);
  padding: 0 12px;
  background: transparent;
  color: var(--ink);
}

.mode-button,
.input-button {
  min-height: 38px;
  border: 0;
  border-radius: 0;
}

.mode-button.is-active,
.input-button.is-active,
.solid-button {
  background: var(--ink);
  color: var(--surface);
}

.primary-status,
.secondary-status,
.triangle-count,
.finish-title,
.finish-count {
  margin: 0;
}

.primary-status {
  font-size: 18px;
  line-height: 1.2;
}

.secondary-status,
.triangle-count,
.selection-status,
.finish-count {
  color: var(--muted-ink);
  font-size: 12px;
  line-height: 1.35;
}

.rules-panel {
  display: grid;
  grid-template-columns: 136px minmax(0, 1fr);
  align-items: start;
  gap: 14px;
  padding: 10px 0 12px;
  border-top: 1px solid var(--line-strong);
  border-bottom: 1px solid var(--line);
}

.rules-panel[hidden] {
  display: none;
}

.rules-title,
.rules-subtitle,
.rules-list {
  margin: 0;
}

.rules-title {
  font-size: 13px;
  font-weight: 700;
  line-height: 1.3;
}

.rules-subtitle {
  margin-top: 4px;
  color: var(--muted-ink);
  font-size: 11px;
  line-height: 1.45;
}

.rules-list {
  display: grid;
  gap: 4px;
  padding-left: 20px;
  color: var(--ink);
  font-size: 12px;
  line-height: 1.5;
}

.rules-meta {
  grid-column: 1 / -1;
  margin: 0;
  color: var(--muted-ink);
  font-size: 10px;
  line-height: 1.4;
}

.board-wrap {
  width: min(100%, 760px);
  min-height: 0;
  margin: 0 auto;
  display: grid;
  align-items: center;
  justify-items: center;
  overflow: hidden;
}

.board {
  width: min(100%, calc(100svh - 206px));
  width: min(100%, calc(100dvh - 206px));
  height: auto;
  max-height: none;
  aspect-ratio: 1 / 1;
  display: block;
  background: #fdfdfd;
  border: 1px solid var(--line-strong);
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
}

.grid-line {
  stroke: var(--line);
  stroke-width: 1;
  vector-effect: non-scaling-stroke;
}

.grid-line.major {
  stroke: #d8d8d8;
}

.triangle {
  fill: transparent;
  stroke: var(--ink);
  stroke-width: 1.45;
  vector-effect: non-scaling-stroke;
  pointer-events: none;
  transition: fill 420ms ease, stroke 420ms ease, opacity 420ms ease;
}

.triangle.owner-start {
  fill: none;
  stroke: var(--ink);
}

.triangle.owner-p2 {
  stroke: var(--ink);
}

.triangle.owner-solo {
  stroke: var(--ink);
}

.candidate {
  fill: rgba(31, 31, 31, 0.015);
  stroke: rgba(31, 31, 31, 0.38);
  stroke-width: 1;
  stroke-dasharray: 0.16 0.12;
  vector-effect: non-scaling-stroke;
  pointer-events: none;
}

.candidate-hit {
  fill: transparent;
  stroke: transparent;
  stroke-width: 0.36;
  vector-effect: non-scaling-stroke;
  cursor: pointer;
  pointer-events: all;
  outline: none;
  -webkit-tap-highlight-color: transparent;
}

.candidate-hit:focus,
.candidate-hit:focus-visible,
.candidate-hit:active {
  outline: none;
}

.candidate-hit:hover + .candidate,
.candidate-hit:focus-visible + .candidate {
  fill: rgba(31, 31, 31, 0.07);
  stroke: rgba(31, 31, 31, 0.72);
}

.candidate.is-selected {
  fill: rgba(31, 31, 31, 0.16);
  stroke: #050505;
  stroke-width: 2.6;
  stroke-dasharray: none;
}

.candidate-selected-vertex {
  fill: var(--ink);
  stroke: var(--surface);
  stroke-width: 0.04;
  pointer-events: none;
}

.trace-line {
  fill: none;
  stroke: var(--ink);
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
  vector-effect: non-scaling-stroke;
  pointer-events: none;
}

.trace-point {
  fill: var(--ink);
  stroke: var(--surface);
  stroke-width: 0.04;
  pointer-events: none;
}

.trace-preview {
  fill: rgba(31, 31, 31, 0.1);
  stroke: #050505;
  stroke-width: 2.4;
  vector-effect: non-scaling-stroke;
  pointer-events: none;
}

.vertex-hit {
  fill: transparent;
  stroke: transparent;
  cursor: pointer;
  outline: none;
  -webkit-tap-highlight-color: transparent;
}

.vertex-hit:focus,
.vertex-hit:focus-visible,
.vertex-hit:active {
  fill: transparent;
  stroke: transparent;
  outline: none;
}

.vertex-dot {
  fill: var(--ink);
  opacity: 0.2;
  pointer-events: none;
}

.vertex-dot.is-selected {
  opacity: 1;
}

.debug .triangle.count-2 {
  stroke: var(--watch);
  fill: rgba(154, 106, 16, 0.06);
}

.debug .triangle.count-3 {
  stroke: var(--danger);
  fill: rgba(159, 29, 29, 0.08);
}

.debug .candidate.causes-out {
  stroke: var(--danger);
}

.debug-count {
  fill: var(--ink);
  font-size: 0.44px;
  text-anchor: middle;
  dominant-baseline: central;
  pointer-events: none;
  paint-order: stroke;
  stroke: var(--surface);
  stroke-width: 0.12px;
}

.action-buttons {
  display: flex;
  gap: 8px;
}

.finish-panel {
  padding: 2px 0;
}

.finish-title {
  font-size: 20px;
  line-height: 1.2;
}

.debug-panel {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  padding-top: 2px;
}

.debug-panel[hidden],
.finish-panel[hidden] {
  display: none;
}

.debug-panel label {
  display: grid;
  gap: 4px;
}

.debug-panel span {
  color: var(--muted-ink);
  font-size: 11px;
}

.debug-panel input,
.debug-panel select {
  width: 100%;
  min-height: 38px;
  border: 1px solid var(--line-strong);
  border-radius: 4px;
  background: var(--surface);
  color: var(--ink);
  padding: 0 8px;
}

.debug-panel input:disabled {
  background: #f4f4f4;
  color: var(--muted-ink);
}

.game-finished .action-bar {
  display: none;
}

.game-finished .board {
  width: min(100%, calc(100svh - 150px));
  width: min(100%, calc(100dvh - 150px));
  max-height: none;
  background: #fffdf8;
  border-color: transparent;
}

.game-finished .grid-line {
  animation: gridFade 700ms ease forwards;
}

.game-finished .triangle {
  stroke-width: 1.7;
  animation: shapeSettle 580ms ease-out both;
}

.game-finished .triangle.owner-start {
  fill: none;
  stroke: var(--ink);
  animation: none;
}

.game-finished .triangle.owner-p1,
.game-finished .triangle.owner-solo {
  fill: var(--p1-finish-fill);
  stroke: var(--p1-finish-stroke);
}

.game-finished .triangle.owner-p2 {
  fill: var(--p2-finish-fill);
  stroke: var(--p2-finish-stroke);
}

.game-finished .debug-count {
  opacity: 0;
}

@keyframes gridFade {
  from {
    opacity: 1;
  }

  to {
    opacity: 0;
  }
}

@keyframes shapeSettle {
  from {
    fill-opacity: 0;
    stroke-width: 1.2;
  }

  to {
    fill-opacity: 0.95;
  }
}

@media (min-width: 720px) {
  .shell {
    padding: 18px;
  }

  .debug-panel {
    grid-template-columns: repeat(8, minmax(0, 1fr));
    align-items: end;
  }
}

@media (max-width: 600px) {
  body:not(.rules-open) {
    overflow: hidden;
  }

  .shell {
    height: 100dvh;
    min-height: 100dvh;
    grid-template-rows: auto auto auto minmax(0, 1fr) auto auto auto;
    gap: 7px;
    overflow: hidden;
    padding:
      max(8px, env(safe-area-inset-top))
      max(10px, env(safe-area-inset-right))
      max(8px, env(safe-area-inset-bottom))
      max(10px, env(safe-area-inset-left));
  }

  .topbar {
    display: grid;
    grid-template-columns: minmax(0, 0.9fr) minmax(0, 1.1fr);
    grid-template-areas:
      "brand actions"
      "mode input"
      "color color";
    gap: 6px;
  }

  .brand-lockup {
    grid-area: brand;
    width: auto;
    margin-right: 0;
  }

  .mode-switch {
    grid-area: mode;
    width: 100%;
  }

  .input-switch {
    grid-area: input;
    width: 100%;
  }

  .color-switch {
    grid-area: color;
    justify-content: center;
    min-height: 30px;
  }

  .round-actions {
    grid-area: actions;
    justify-content: flex-end;
  }

  button {
    min-height: 42px;
  }

  .mode-button,
  .input-button {
    min-height: 40px;
  }

  .round-actions .ghost-button {
    min-height: 34px;
  }

  .status-line {
    min-height: 34px;
  }

  .board-wrap {
    height: 100%;
  }

  .board {
    width: min(100%, calc(100dvh - 238px));
    max-height: 100%;
  }

  .action-bar,
  .finish-panel {
    min-height: 44px;
    background: var(--paper);
  }

  .action-buttons .ghost-button,
  .action-buttons .solid-button,
  .finish-panel .solid-button {
    min-width: 88px;
  }

  .game-finished .board {
    width: min(100%, calc(100dvh - 196px));
    max-height: 100%;
  }

  body.rules-open {
    overflow: auto;
    overscroll-behavior: auto;
  }

  .rules-open .shell {
    height: auto;
    min-height: 100dvh;
    overflow: visible;
  }

  .rules-open .board,
  .rules-open.game-finished .board {
    width: 100%;
    max-height: none;
  }
}

@media (max-width: 420px) {
  .shell {
    gap: 8px;
    padding:
      max(10px, env(safe-area-inset-top))
      max(10px, env(safe-area-inset-right))
      max(10px, env(safe-area-inset-bottom))
      max(10px, env(safe-area-inset-left));
  }

  .primary-status,
  .finish-title {
    font-size: 16px;
  }

  .brand-title {
    font-size: 18px;
  }

  .logo-mark {
    width: 30px;
    height: 30px;
  }

  .ghost-button,
  .solid-button {
    padding: 0 9px;
  }

  .mode-switch {
    grid-template-columns: repeat(2, 56px);
  }

  .input-switch {
    grid-template-columns: repeat(2, 72px);
  }

  .color-switch,
  .round-actions {
    gap: 6px;
  }

  .rules-panel {
    grid-template-columns: 1fr;
    gap: 8px;
  }
}

@media (max-height: 500px) and (orientation: landscape) {
  body:not(.rules-open) {
    overflow: hidden;
  }

  .shell {
    height: 100dvh;
    min-height: 100dvh;
    gap: 4px;
    overflow: hidden;
    padding:
      max(8px, env(safe-area-inset-top))
      max(8px, env(safe-area-inset-right))
      max(8px, env(safe-area-inset-bottom))
      max(8px, env(safe-area-inset-left));
  }

  .board,
  .game-finished .board {
    width: min(100%, calc(100dvh - 160px));
    max-height: none;
  }

  body.rules-open {
    overflow: auto;
  }

  .rules-open .shell {
    height: auto;
    min-height: 100dvh;
    overflow: visible;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
