:root {
  --bg: #f6f4ef;
  --void: #ebe7de;
  --ink: #1d1d22;
  --ink-soft: #5d5b57;
  --mine: #2563eb;
  --draft: #7c3aed;
  --wrong: #dc2626;
  --line: rgba(29, 29, 34, 0.16);
  --line-box: rgba(29, 29, 34, 0.5);
  --done-fill: #fffdf8;
  --open-fill: #ffffff;
  --peer: rgba(37, 99, 235, 0.08);
  --same: rgba(37, 99, 235, 0.2);
  --select: rgba(37, 99, 235, 0.3);
  --panel: rgba(255, 255, 255, 0.86);
  --panel-solid: #ffffff;
  --panel-line: rgba(29, 29, 34, 0.1);
  --shadow: 0 8px 30px rgba(30, 24, 10, 0.12), 0 1px 3px rgba(30, 24, 10, 0.08);
  --accent: #1d1d22;
  --accent-ink: #ffffff;
  --ui: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
  --display: "Fraunces", Georgia, serif;
  color-scheme: light;
}

/* Dark: when the device is dark and light wasn't picked, or when dark was picked (theme.js).
   The two blocks have to match. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121216;
    --void: #0b0b0e;
    --ink: #ecebe8;
    --ink-soft: #a3a19c;
    --mine: #6ea8ff;
    --draft: #c4a1ff;
    --wrong: #ff6b6b;
    --line: rgba(236, 235, 232, 0.13);
    --line-box: rgba(236, 235, 232, 0.42);
    --done-fill: #1b1b20;
    --open-fill: #202027;
    --peer: rgba(110, 168, 255, 0.09);
    --same: rgba(110, 168, 255, 0.22);
    --select: rgba(110, 168, 255, 0.34);
    --panel: rgba(30, 30, 36, 0.86);
    --panel-solid: #1e1e24;
    --panel-line: rgba(255, 255, 255, 0.09);
    --shadow: 0 8px 30px rgba(0, 0, 0, 0.45), 0 1px 3px rgba(0, 0, 0, 0.3);
    --accent: #ecebe8;
    --accent-ink: #121216;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121216;
  --void: #0b0b0e;
  --ink: #ecebe8;
  --ink-soft: #a3a19c;
  --mine: #6ea8ff;
  --draft: #c4a1ff;
  --wrong: #ff6b6b;
  --line: rgba(236, 235, 232, 0.13);
  --line-box: rgba(236, 235, 232, 0.42);
  --done-fill: #1b1b20;
  --open-fill: #202027;
  --peer: rgba(110, 168, 255, 0.09);
  --same: rgba(110, 168, 255, 0.22);
  --select: rgba(110, 168, 255, 0.34);
  --panel: rgba(30, 30, 36, 0.86);
  --panel-solid: #1e1e24;
  --panel-line: rgba(255, 255, 255, 0.09);
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.45), 0 1px 3px rgba(0, 0, 0, 0.3);
  --accent: #ecebe8;
  --accent-ink: #121216;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.45 var(--ui);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

button,
select {
  font: inherit;
  color: inherit;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.s-canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  cursor: grab;
  outline: none;
}

.s-canvas.dragging {
  cursor: grabbing;
}

.s-canvas.over-cell {
  cursor: pointer;
}

/* Floating panels */

.s-top,
.s-legend,
.s-zoom,
.s-pad {
  position: fixed;
  z-index: 2;
}

.s-top {
  top: max(12px, env(safe-area-inset-top));
  left: max(12px, env(safe-area-inset-left));
  right: max(12px, env(safe-area-inset-right));
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
  pointer-events: none;
}

.s-top > * {
  pointer-events: auto;
}

.s-brand,
.s-actions,
.s-settings,
.s-legend,
.s-zoom,
.s-pad {
  background: var(--panel);
  backdrop-filter: blur(14px) saturate(1.4);
  -webkit-backdrop-filter: blur(14px) saturate(1.4);
  border: 1px solid var(--panel-line);
  box-shadow: var(--shadow);
  border-radius: 14px;
}

.s-brand {
  padding: 9px 14px 8px;
}

.s-brand h1 {
  margin: 0;
  font: 600 20px/1.1 var(--display);
  letter-spacing: -0.01em;
}

.s-brand h1 em {
  font-style: normal;
  font-weight: 800;
}

.s-stats {
  margin: 2px 0 0;
  color: var(--ink-soft);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.s-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px;
}

.s-find {
  display: flex;
  align-items: center;
  gap: 2px;
}

.s-find select {
  appearance: none;
  border: 0;
  background: transparent;
  padding: 8px 22px 8px 10px;
  border-radius: 10px;
  cursor: pointer;
  background-image: linear-gradient(45deg, transparent 50%, currentColor 50%), linear-gradient(135deg, currentColor 50%, transparent 50%);
  background-position: calc(100% - 12px) 52%, calc(100% - 8px) 52%;
  background-size: 4px 4px;
  background-repeat: no-repeat;
}

.s-find select:hover {
  background-color: var(--peer);
}

.s-find button,
.s-icon {
  border: 0;
  border-radius: 10px;
  cursor: pointer;
  padding: 8px 12px;
  font-weight: 600;
}

.s-find button {
  background: var(--accent);
  color: var(--accent-ink);
}

.s-icon {
  background: transparent;
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  padding: 0;
}

.s-icon:hover,
.s-icon[aria-expanded="true"] {
  background: var(--peer);
}

.s-about-btn {
  font: 700 16px/1 var(--display);
}

.s-settings {
  position: absolute;
  top: 52px;
  right: 0;
  width: min(340px, calc(100vw - 24px));
  padding: 8px;
  background: var(--panel-solid);
}

.s-switch {
  display: flex;
  gap: 10px;
  padding: 10px;
  border-radius: 10px;
  cursor: pointer;
}

.s-switch:hover {
  background: var(--peer);
}

.s-switch input {
  flex: none;
  appearance: none;
  width: 34px;
  height: 20px;
  margin: 1px 0 0;
  border-radius: 99px;
  background: var(--line);
  position: relative;
  cursor: pointer;
  transition: background 0.15s;
}

.s-switch input::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  transition: transform 0.15s;
}

.s-switch input:checked {
  background: var(--mine);
}

.s-switch input:checked::after {
  transform: translateX(14px);
}

.s-switch input:focus-visible {
  outline: 2px solid var(--mine);
  outline-offset: 2px;
}

.s-switch span {
  display: grid;
  gap: 2px;
}

.s-switch small {
  color: var(--ink-soft);
  font-size: 12px;
  line-height: 1.4;
}

.s-mistakes {
  margin: 4px 10px 6px;
  color: var(--ink-soft);
  font-size: 12px;
}

.s-mistakes:empty {
  display: none;
}

.s-legend {
  left: max(12px, env(safe-area-inset-left));
  bottom: max(12px, env(safe-area-inset-bottom));
  display: flex;
  gap: 12px;
  padding: 8px 12px;
  font-size: 12px;
  color: var(--ink-soft);
}

.s-legend span {
  display: flex;
  align-items: center;
  gap: 6px;
}

.s-legend i {
  width: 10px;
  height: 10px;
  border-radius: 3px;
  background: var(--c);
}

.s-legend i.done {
  background: var(--done-fill);
  border: 1.5px solid var(--line-box);
}

.s-zoom {
  right: max(12px, env(safe-area-inset-right));
  bottom: max(12px, env(safe-area-inset-bottom));
  display: grid;
  padding: 4px;
  gap: 2px;
}

.s-zoom button {
  width: 36px;
  height: 36px;
  border: 0;
  background: transparent;
  border-radius: 10px;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
}

.s-zoom button:hover {
  background: var(--peer);
}

.s-pad {
  left: 50%;
  bottom: max(12px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px;
  transition: opacity 0.2s, transform 0.2s;
}

.s-pad.idle {
  opacity: 0.55;
}

.s-pad-digits {
  display: flex;
  gap: 4px;
}

.s-pad button {
  border: 0;
  cursor: pointer;
  border-radius: 10px;
  background: transparent;
}

.s-pad-digits button {
  width: 42px;
  height: 46px;
  font: 600 22px/1 var(--ui);
  font-variant-numeric: tabular-nums;
  background: var(--open-fill);
  border: 1px solid var(--panel-line);
}

.s-pad-digits button:hover {
  background: var(--same);
}

.s-pad-digits button:active {
  transform: scale(0.95);
}

.s-pad-digits button.used {
  opacity: 0.35;
}

.s-pad-tools {
  display: flex;
  gap: 4px;
  padding-left: 6px;
  border-left: 1px solid var(--panel-line);
}

.s-pad-tools button {
  display: grid;
  justify-items: center;
  gap: 1px;
  min-width: 52px;
  height: 46px;
  padding: 4px 6px;
  font-size: 11px;
  font-weight: 600;
  color: var(--ink-soft);
}

.s-pad-tools button:hover {
  background: var(--peer);
  color: var(--ink);
}

.s-pad-tools button[aria-pressed="true"] {
  background: var(--draft);
  color: #fff;
}

.s-pad.notes .s-pad-digits button {
  color: var(--draft);
  font-size: 17px;
}

button:focus-visible,
select:focus-visible {
  outline: 2px solid var(--mine);
  outline-offset: 2px;
}

/* About */

.s-dialog {
  width: min(560px, calc(100vw - 24px));
  max-height: calc(100vh - 48px);
  padding: 26px 28px 22px;
  border: 1px solid var(--panel-line);
  border-radius: 18px;
  background: var(--panel-solid);
  color: var(--ink);
  box-shadow: var(--shadow);
}

.s-dialog::backdrop {
  background: rgba(10, 10, 14, 0.4);
  backdrop-filter: blur(3px);
}

.s-dialog h2 {
  margin: 0 0 10px;
  font: 700 26px/1.15 var(--display);
}

.s-dialog h3 {
  margin: 20px 0 6px;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--ink-soft);
}

.s-dialog p {
  margin: 0 0 10px;
}

.s-dialog a {
  color: var(--mine);
}

.s-dialog-close {
  position: absolute;
  top: 12px;
  right: 12px;
  margin: 0;
}

.s-dialog-close button {
  width: 34px;
  height: 34px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  font-size: 22px;
  cursor: pointer;
}

.s-dialog-close button:hover {
  background: var(--peer);
}

.s-levels {
  list-style: none;
  padding: 0;
  margin: 0 0 10px;
  display: grid;
  gap: 6px;
}

.s-levels span {
  display: inline-block;
  min-width: 64px;
  margin-right: 6px;
  padding: 1px 8px;
  border-radius: 6px;
  background: var(--c);
  color: #fff;
  font-size: 12px;
  font-weight: 700;
  text-align: center;
}

/* Toasts */

.s-toasts {
  position: fixed;
  z-index: 3;
  top: 84px;
  left: 50%;
  transform: translateX(-50%);
  display: grid;
  gap: 8px;
  justify-items: center;
  pointer-events: none;
}

.s-toast {
  padding: 9px 16px;
  border-radius: 99px;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
  box-shadow: var(--shadow);
  animation: toast-in 0.25s ease-out;
}

.s-toast.bad {
  background: var(--wrong);
  color: #fff;
}

.s-toast.out {
  opacity: 0;
  transform: translateY(-6px);
  transition: opacity 0.3s, transform 0.3s;
}

@keyframes toast-in {
  from {
    opacity: 0;
    transform: translateY(-8px);
  }
}

.s-noscript {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  margin: 0;
}

/* Phones: brand on its own, pad full width, legend tucked away */

@media (max-width: 720px) {
  .s-top {
    flex-wrap: wrap;
  }

  .s-brand {
    padding: 7px 12px 6px;
  }

  .s-brand h1 {
    font-size: 17px;
  }

  .s-find button {
    padding: 8px 10px;
  }

  .s-legend {
    display: none;
  }

  .s-zoom {
    bottom: calc(max(12px, env(safe-area-inset-bottom)) + 116px);
  }

  .s-pad {
    left: max(8px, env(safe-area-inset-left));
    right: max(8px, env(safe-area-inset-right));
    transform: none;
    flex-direction: column;
    align-items: stretch;
  }

  .s-pad-digits {
    display: grid;
    grid-template-columns: repeat(9, 1fr);
  }

  .s-pad-digits button {
    width: auto;
    height: 50px;
  }

  .s-pad-tools {
    border-left: 0;
    padding-left: 0;
    justify-content: center;
  }

  .s-pad-tools button {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 38px;
    gap: 6px;
    font-size: 13px;
  }

  .s-toasts {
    top: 110px;
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    animation: none !important;
    transition: none !important;
  }
}
