:root {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-raised: #273449;
  --surface-hover: #334155;
  --line: rgba(148, 163, 184, 0.18);
  --line-strong: rgba(148, 163, 184, 0.32);
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #84cc16;
  --accent-hover: #a3e635;
  --on-accent: #0f172a;
  --danger: #f87171;
  --radius: 12px;
  --radius-small: 10px;
  --shadow: 0 1px 2px rgba(2, 6, 23, 0.4), 0 12px 32px rgba(2, 6, 23, 0.35);
  --speed: 180ms;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Consolas, "Liberation Mono", monospace;
  color-scheme: dark;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

button,
input,
select {
  font: inherit;
  color: inherit;
}

[hidden] {
  display: none !important;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Header */

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  max-width: 1360px;
  margin: 0 auto;
  padding: 12px 16px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  margin: 0;
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.brand span {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.brand-mark {
  flex: none;
  width: 28px;
  height: 28px;
  padding: 3px;
  border-radius: 8px;
  background: var(--surface);
  fill: var(--accent);
  box-shadow: inset 0 0 0 1px var(--line);
}

.topbar-actions {
  display: flex;
  gap: 8px;
  margin-left: auto;
}

/* Buttons */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius-small);
  background: var(--surface-raised);
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
  touch-action: manipulation;
  transition: background-color var(--speed) ease, border-color var(--speed) ease, color var(--speed) ease,
    transform var(--speed) ease;
}

.btn:hover {
  background: var(--surface-hover);
  border-color: var(--line-strong);
}

.btn:active {
  transform: translateY(1px);
}

.btn-primary {
  border-color: transparent;
  background: var(--accent);
  color: var(--on-accent);
}

.btn-primary:hover {
  border-color: transparent;
  background: var(--accent-hover);
}

.btn-ghost {
  background: transparent;
}

.btn-lang {
  min-width: 48px;
  padding: 0 10px;
  font-weight: 700;
  letter-spacing: 0.06em;
}

.icon {
  flex: none;
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon-solid,
.icon .dot {
  fill: currentColor;
  stroke: none;
}

/* Layout */

.layout {
  flex: 1 0 auto;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  align-content: start;
  gap: 16px;
  width: 100%;
  max-width: 1360px;
  margin: 0 auto;
  padding: 0 16px;
}

/* Stage: canvas and the bar below it */

.stage {
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  user-select: none;
  -webkit-user-select: none;
}

.board-wrap {
  position: relative;
  aspect-ratio: 3 / 2;
  max-height: 72vh;
  max-height: 72svh;
}

.board {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  cursor: crosshair;
  touch-action: none;
  -webkit-touch-callout: none;
}

.board.is-panning {
  cursor: grab;
}

.board.is-panning:active {
  cursor: grabbing;
}

.board:focus-visible {
  outline-offset: -3px;
}

.stage-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 6px 8px;
  padding: 6px 4px 6px 14px;
  border-top: 1px solid var(--line);
}

.stats {
  display: flex;
  gap: 14px;
  margin: 0;
}

.stat {
  display: flex;
  flex-direction: column;
  min-width: 64px;
}

.stat dt {
  color: var(--muted);
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.stat dd {
  margin: 0;
  font-size: 17px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  line-height: 1.25;
}

.tools {
  display: flex;
  margin-left: auto;
}

.tool {
  display: inline-grid;
  place-items: center;
  width: 40px;
  height: 40px;
  padding: 0;
  border: 1px solid transparent;
  border-radius: var(--radius-small);
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  touch-action: manipulation;
  transition: background-color var(--speed) ease, color var(--speed) ease, border-color var(--speed) ease;
}

.tool:hover {
  background: var(--surface-raised);
  color: var(--text);
}

.tool[aria-pressed="true"] {
  border-color: rgba(132, 204, 22, 0.5);
  background: rgba(132, 204, 22, 0.14);
  color: var(--accent-hover);
}

/* Side panel */

.panel {
  display: grid;
  align-content: start;
  gap: 16px;
  min-width: 0;
  padding: 16px;
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

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

.actions .btn {
  flex: 1 1 auto;
  gap: 6px;
  padding: 0 8px;
}

.actions .btn-primary {
  flex-grow: 2;
}

.field {
  display: grid;
  gap: 6px;
  min-width: 0;
}

.speed {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
}

.field-label,
.switch-title {
  font-weight: 600;
}

.field-value {
  min-width: 5.6em;
  color: var(--accent-hover);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  text-align: right;
}

.field-row {
  display: flex;
  gap: 8px;
  min-width: 0;
}

.help-text {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.4;
}

.help-text.is-error {
  color: var(--danger);
}

.input,
.select {
  min-width: 0;
  min-height: 44px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-small);
  background-color: var(--bg);
  transition: border-color var(--speed) ease, background-color var(--speed) ease;
}

.input:hover,
.select:hover {
  border-color: var(--line-strong);
}

.input-rule {
  flex: 0 0 9.25em;
  padding: 0 10px;
  font-family: var(--mono);
  font-size: 14px;
}

.input[aria-invalid="true"] {
  border-color: var(--danger);
}

.select {
  flex: 1 1 auto;
  padding-right: 34px;
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%2394a3b8' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-position: right 10px center;
  background-repeat: no-repeat;
  background-size: 16px;
  text-overflow: ellipsis;
  cursor: pointer;
}

.select option {
  background: var(--surface);
  color: var(--text);
}

.field-row .btn {
  flex: none;
}

/* Speed slider */

.range {
  --fill: 15.25%;
  width: 100%;
  height: 40px;
  margin: 0;
  background: transparent;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  touch-action: manipulation;
}

.range::-webkit-slider-runnable-track {
  height: 6px;
  border-radius: 999px;
  background: linear-gradient(to right, var(--accent) var(--fill), var(--surface-hover) var(--fill));
}

.range::-webkit-slider-thumb {
  width: 22px;
  height: 22px;
  margin-top: -8px;
  border: 4px solid var(--accent);
  border-radius: 50%;
  background: var(--text);
  box-shadow: 0 2px 6px rgba(2, 6, 23, 0.5);
  appearance: none;
  -webkit-appearance: none;
  transition: transform var(--speed) ease;
}

.range:active::-webkit-slider-thumb {
  transform: scale(1.12);
}

.range::-moz-range-track {
  height: 6px;
  border-radius: 999px;
  background: var(--surface-hover);
}

.range::-moz-range-progress {
  height: 6px;
  border-radius: 999px;
  background: var(--accent);
}

.range::-moz-range-thumb {
  width: 14px;
  height: 14px;
  border: 4px solid var(--accent);
  border-radius: 50%;
  background: var(--text);
}

/* Wrap switch */

.switch {
  position: relative;
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 44px;
  cursor: pointer;
}

.switch-input {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: 0;
  opacity: 0;
}

.switch-track {
  position: relative;
  flex: none;
  width: 44px;
  height: 26px;
  border-radius: 999px;
  background: var(--surface-hover);
  transition: background-color var(--speed) ease;
}

.switch-track::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--text);
  box-shadow: 0 1px 3px rgba(2, 6, 23, 0.5);
  transition: transform var(--speed) ease, background-color var(--speed) ease;
}

.switch-input:checked + .switch-track {
  background: var(--accent);
}

.switch-input:checked + .switch-track::after {
  background: var(--on-accent);
  transform: translateX(18px);
}

.switch-input:focus-visible + .switch-track {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.switch-text {
  display: grid;
  gap: 1px;
}

/* Help */

.help {
  padding-top: 12px;
  border-top: 1px solid var(--line);
}

.help-title {
  margin: 0 0 8px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.keys {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 16px;
  margin: 0 0 10px;
  font-size: 13px;
}

.keys div {
  display: flex;
  align-items: center;
  gap: 6px;
}

.keys dt {
  display: flex;
  gap: 3px;
}

.keys dd {
  margin: 0;
  color: var(--muted);
}

kbd {
  display: inline-block;
  min-width: 22px;
  padding: 1px 6px;
  border: 1px solid var(--line-strong);
  border-bottom-width: 2px;
  border-radius: 6px;
  background: var(--surface-raised);
  font-family: var(--font);
  font-size: 12px;
  font-weight: 600;
  text-align: center;
}

.gestures {
  display: grid;
  gap: 4px;
  margin: 0;
  padding-left: 18px;
  color: var(--muted);
  font-size: 13px;
}

.gestures li::marker {
  color: var(--accent);
}

/* Footer */

.footer {
  padding: 2px 16px;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
}

.footer a {
  display: inline-block;
  padding: 11px 4px;
  color: var(--text);
  text-decoration-color: rgba(132, 204, 22, 0.6);
  text-underline-offset: 3px;
  transition: color var(--speed) ease;
}

.footer a:hover {
  color: var(--accent-hover);
}

/* Wider layouts */

@media (min-width: 600px) and (max-width: 899px) {
  .panel {
    grid-template-columns: 1fr 1fr;
    column-gap: 20px;
  }

  .actions,
  .help {
    grid-column: 1 / -1;
  }
}

@media (min-width: 900px) {
  .layout {
    grid-template-columns: minmax(0, 1fr) 340px;
    grid-template-rows: 1fr;
    align-content: stretch;
    padding-bottom: 4px;
  }

  .board-wrap {
    flex: 1 1 auto;
    aspect-ratio: auto;
    min-height: 380px;
    max-height: none;
  }
}

/* Desktop windows tall enough for the board: the page fits the window and the side panel scrolls by itself. */
@media (min-width: 900px) and (min-height: 600px) {
  body {
    height: 100vh;
    height: 100dvh;
  }

  .layout {
    flex: 1 1 auto;
    min-height: 0;
    grid-template-rows: minmax(0, 1fr);
  }

  .panel {
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    scrollbar-width: thin;
    scrollbar-color: var(--surface-hover) transparent;
  }
}

@media (hover: none) and (pointer: coarse) {
  .keys {
    display: none;
  }
}

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