/* 深色是默认主题；浅色主题由 :root[data-theme="light"] 覆盖同一批变量。 */
:root {
  color-scheme: dark;

  --bg: #080b12;
  --bg-soft: #0e1320;
  --surface: rgba(255, 255, 255, 0.045);
  --surface-strong: rgba(255, 255, 255, 0.085);
  --border: rgba(255, 255, 255, 0.09);
  --border-strong: rgba(255, 255, 255, 0.18);
  --text: #e9eefb;
  --text-soft: #cdd6ea;
  --muted: #93a0bb;
  --accent: #6ea8fe;
  --accent-2: #7ee7c7;
  --warn: #ffb457;
  --bad: #ff7a8a;

  --body-bg:
    radial-gradient(1100px 620px at 12% -10%, rgba(94, 140, 255, 0.16), transparent 62%),
    radial-gradient(900px 520px at 105% 8%, rgba(126, 231, 199, 0.1), transparent 58%),
    radial-gradient(800px 800px at 50% 120%, rgba(255, 180, 87, 0.06), transparent 60%), var(--bg);

  --board-bg: #0f1420;
  --board-ring: rgba(255, 255, 255, 0.1);
  --board-shadow: rgba(0, 0, 0, 0.5);
  --board-glow: rgba(90, 130, 255, 0.07);
  --grid-line: rgba(255, 255, 255, 0.075);
  --clue-color: #f5f8ff;
  --clue-shadow: 0 2px 7px rgba(0, 0, 0, 0.55);

  --rect-sat: 74%;
  --rect-light: 62%;
  --rect-frame: 74%;
  --rect-fill: 0.2;
  --rect-border: 0.62;
  --rect-glow: 0.16;
  --rect-fill-won: 0.24;
  --rect-glow-won: 0.28;

  --preview-ok-border: rgba(126, 231, 199, 0.95);
  --preview-ok-bg: rgba(126, 231, 199, 0.16);
  --preview-bad-border: rgba(255, 122, 138, 0.95);
  --preview-bad-bg: rgba(255, 122, 138, 0.16);
  --cursor-ring: rgba(160, 195, 255, 0.95);
  --cursor-halo: rgba(110, 168, 254, 0.16);

  --dialog-bg: linear-gradient(180deg, #18202f, #0e1420);
  --dialog-shadow: 0 34px 90px rgba(0, 0, 0, 0.62);
  --backdrop: rgba(4, 7, 12, 0.66);
  --toast-bg: rgba(21, 28, 43, 0.96);
  --toast-shadow: 0 16px 40px rgba(0, 0, 0, 0.45);

  --radius: 14px;
  --font: "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", system-ui, -apple-system,
    sans-serif;
}

:root[data-theme="light"] {
  color-scheme: light;

  --bg: #eef2f9;
  --bg-soft: #ffffff;
  --surface: rgba(15, 23, 42, 0.045);
  --surface-strong: rgba(15, 23, 42, 0.09);
  --border: rgba(15, 23, 42, 0.12);
  --border-strong: rgba(15, 23, 42, 0.26);
  --text: #101828;
  --text-soft: #33405a;
  --muted: #5b6884;
  --accent: #2f6fdc;
  --accent-2: #0f9b7d;
  --warn: #a85f05;
  --bad: #c9314a;

  --body-bg:
    radial-gradient(1100px 620px at 12% -10%, rgba(88, 133, 255, 0.18), transparent 62%),
    radial-gradient(900px 520px at 105% 8%, rgba(16, 185, 129, 0.12), transparent 58%),
    radial-gradient(800px 800px at 50% 120%, rgba(249, 168, 79, 0.12), transparent 60%), var(--bg);

  --board-bg: #ffffff;
  --board-ring: rgba(15, 23, 42, 0.12);
  --board-shadow: rgba(24, 39, 75, 0.16);
  --board-glow: rgba(59, 130, 246, 0.05);
  --grid-line: rgba(15, 23, 42, 0.1);
  --clue-color: #16203a;
  --clue-shadow: none;

  --rect-sat: 70%;
  --rect-light: 45%;
  --rect-frame: 38%;
  --rect-fill: 0.16;
  --rect-border: 0.5;
  --rect-glow: 0.1;
  --rect-fill-won: 0.2;
  --rect-glow-won: 0.18;

  --preview-ok-border: rgba(13, 148, 118, 0.9);
  --preview-ok-bg: rgba(16, 185, 129, 0.12);
  --preview-bad-border: rgba(201, 49, 74, 0.85);
  --preview-bad-bg: rgba(225, 29, 72, 0.1);
  --cursor-ring: rgba(47, 111, 220, 0.95);
  --cursor-halo: rgba(47, 111, 220, 0.14);

  --dialog-bg: linear-gradient(180deg, #ffffff, #f4f7fd);
  --dialog-shadow: 0 34px 90px rgba(24, 39, 75, 0.28);
  --backdrop: rgba(20, 30, 50, 0.38);
  --toast-bg: rgba(255, 255, 255, 0.98);
  --toast-shadow: 0 16px 40px rgba(24, 39, 75, 0.18);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
}

body {
  background:
    radial-gradient(1100px 620px at 12% -10%, rgba(94, 140, 255, 0.16), transparent 62%),
    radial-gradient(900px 520px at 105% 8%, rgba(126, 231, 199, 0.1), transparent 58%),
    radial-gradient(800px 800px at 50% 120%, rgba(255, 180, 87, 0.06), transparent 60%),
    var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

.app {
  width: min(100%, 1040px);
  min-height: 100%;
  margin: 0 auto;
  padding: clamp(12px, 2vw, 26px);
  display: flex;
  flex-direction: column;
  gap: clamp(10px, 1.8vh, 18px);
}

/* ---------------------------------------------------------------- 顶栏 */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: 10px;
  background: linear-gradient(135deg, rgba(110, 168, 254, 0.9), rgba(126, 231, 199, 0.85));
  color: #07101d;
  font-size: 18px;
  font-weight: 700;
  box-shadow: 0 6px 18px rgba(80, 140, 255, 0.28);
}

.brand h1 {
  margin: 0;
  font-size: clamp(17px, 2.4vw, 20px);
  letter-spacing: 0.02em;
}

.brand-sub {
  color: var(--muted);
  font-size: 12px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  padding-top: 3px;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

/* ---------------------------------------------------------------- 控件 */

.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  padding: 8px 15px;
  border-radius: 10px;
  cursor: pointer;
  transition:
    background 0.16s ease,
    border-color 0.16s ease,
    transform 0.12s ease,
    opacity 0.16s ease;
}

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

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

.btn:disabled {
  opacity: 0.38;
  cursor: not-allowed;
}

.btn-primary {
  background: linear-gradient(135deg, #5b8cff, #57c6ff);
  border-color: transparent;
  color: #061222;
}

.btn-primary:hover:not(:disabled) {
  background: linear-gradient(135deg, #6d9aff, #6dd0ff);
  border-color: transparent;
}

.btn-ghost {
  background: transparent;
}

.btn-icon {
  width: 40px;
  height: 38px;
  padding: 0;
  display: grid;
  place-items: center;
  line-height: 0;
}

.btn-icon svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* 深色主题下显示太阳（点一下切浅色），浅色主题下显示月亮 */
:root[data-theme="light"] .icon-sun,
:root:not([data-theme="light"]) .icon-moon {
  display: none;
}

.select {
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px 6px 12px;
  border-radius: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
}

.select::after {
  content: "▾";
  position: absolute;
  right: 10px;
  font-size: 10px;
  color: var(--muted);
  pointer-events: none;
}

.select-label {
  font-size: 11px;
  letter-spacing: 0.12em;
  color: var(--muted);
  text-transform: uppercase;
  white-space: nowrap;
  flex: none;
}

.select select {
  appearance: none;
  background: transparent;
  border: 0;
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
  padding: 2px 18px 2px 0;
  white-space: nowrap;
  cursor: pointer;
  outline: none;
}

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

/* ---------------------------------------------------------------- 主体 */

.stage {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.stats {
  display: flex;
  justify-content: center;
  gap: 8px;
  flex-wrap: wrap;
}

.stat {
  display: flex;
  align-items: baseline;
  gap: 7px;
  padding: 6px 13px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
}

.stat-label {
  font-size: 10px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--muted);
}

.stat-value {
  font-size: 15px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.board-wrap {
  flex: 1;
  min-height: 230px;
  display: grid;
  place-items: center;
  padding: 2px;
}

/* ---------------------------------------------------------------- 棋盘 */

.board {
  position: relative;
  border-radius: 12px;
  background: var(--board-bg);
  box-shadow:
    0 0 0 2px var(--board-ring),
    0 22px 55px var(--board-shadow),
    inset 0 0 60px var(--board-glow);
  overflow: hidden;
  isolation: isolate;
  touch-action: none;
  user-select: none;
  outline: none;
}

.board:focus-visible {
  box-shadow:
    0 0 0 2px var(--accent),
    0 22px 55px var(--board-shadow);
}

.board-grid {
  position: absolute;
  inset: 0;
  background-image:
    linear-gradient(to right, var(--grid-line) var(--line, 1px), transparent var(--line, 1px)),
    linear-gradient(to bottom, var(--grid-line) var(--line, 1px), transparent var(--line, 1px));
  background-size: var(--cell) var(--cell);
}

.rect-layer,
.clue-layer,
.preview-layer,
.cursor-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.rect {
  position: absolute;
  will-change: transform;
}

.rect-inner {
  position: absolute;
  inset: 2px;
  border-radius: 9px;
  background: hsl(var(--hue) var(--rect-sat) var(--rect-light) / var(--rect-fill));
  border: 1.5px solid hsl(var(--hue) var(--rect-sat) var(--rect-frame) / var(--rect-border));
  box-shadow: inset 0 0 16px hsl(var(--hue) 92% 66% / var(--rect-glow));
}

.board.is-won .rect-inner {
  background: hsl(var(--hue) var(--rect-sat) var(--rect-light) / var(--rect-fill-won));
  box-shadow:
    inset 0 0 20px hsl(var(--hue) 90% 70% / var(--rect-glow-won)),
    0 0 14px hsl(var(--hue) 90% 68% / var(--rect-glow-won));
}

.clue {
  position: absolute;
  display: grid;
  place-items: center;
  font-size: calc(var(--cell) * 0.42);
  font-weight: 800;
  line-height: 1;
  color: var(--clue-color);
  text-shadow: var(--clue-shadow);
  font-variant-numeric: tabular-nums;
  transition: opacity 0.18s ease;
}

.clue.is-covered {
  opacity: 0.42;
}

.preview {
  position: absolute;
  display: none;
}

.preview-inner {
  position: absolute;
  inset: 2px;
  border-radius: 9px;
  border: 2px dashed rgba(255, 122, 138, 0.95);
  background: var(--preview-bad-bg);
}

.preview[data-state="ok"] .preview-inner {
  border-color: var(--preview-ok-border);
  background: var(--preview-ok-bg);
}

.preview.flash {
  animation: deny 0.42s ease;
}

@keyframes deny {
  0%,
  100% {
    transform: translateX(0);
  }
  20% {
    transform: translateX(-4px);
  }
  45% {
    transform: translateX(4px);
  }
  70% {
    transform: translateX(-2px);
  }
}

.cursor {
  position: absolute;
  display: none;
}

.cursor::after {
  content: "";
  position: absolute;
  inset: 3px;
  border-radius: 9px;
  border: 2px solid var(--cursor-ring);
  box-shadow: 0 0 0 3px var(--cursor-halo);
  animation: pulse 1.5s ease-in-out infinite;
}

@keyframes pulse {
  0%,
  100% {
    opacity: 0.9;
  }
  50% {
    opacity: 0.45;
  }
}

/* ---------------------------------------------------------------- 提示与底部 */

.message {
  margin: 0;
  min-height: 22px;
  text-align: center;
  font-size: 13.5px;
  color: var(--muted);
  transition: color 0.16s ease;
}

.message[data-tone="warn"] {
  color: var(--warn);
}

.message[data-tone="info"] {
  color: var(--accent-2);
}

.controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  flex-wrap: wrap;
}

.seed {
  font-size: 12px;
  color: var(--muted);
  padding: 6px 12px;
  border-radius: 999px;
  border: 1px dashed var(--border);
  font-variant-numeric: tabular-nums;
}

.seed b {
  color: var(--text);
  font-weight: 600;
}

.footer {
  text-align: center;
  font-size: 12px;
  color: var(--muted);
  opacity: 0.85;
  line-height: 1.7;
}

/* ---------------------------------------------------------------- 对话框 */

dialog {
  width: min(92vw, 440px);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 18px;
  background: var(--dialog-bg);
  color: var(--text);
  box-shadow: var(--dialog-shadow);
}

dialog::backdrop {
  background: var(--backdrop);
  backdrop-filter: blur(6px);
}

.dialog-body {
  padding: 22px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.dialog-body h2 {
  margin: 0;
  font-size: 20px;
}

.dialog-body h3 {
  margin: 4px 0 0;
  font-size: 13px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
}

.rules-list {
  margin: 0;
  padding-left: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 14px;
  line-height: 1.65;
  color: var(--text-soft);
}

.rules-list strong {
  color: var(--text);
}

kbd {
  display: inline-block;
  padding: 1px 6px;
  border-radius: 6px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  font-family: inherit;
  font-size: 12px;
  color: var(--text);
}

.win-sub {
  margin: -6px 0 0;
  color: var(--muted);
  font-size: 13.5px;
}

.win-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.win-stats > div {
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 10px 12px;
  border-radius: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
}

.win-stats strong {
  font-size: 15px;
  font-variant-numeric: tabular-nums;
}

.dialog-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
  margin-top: 4px;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translate(-50%, 12px);
  padding: 10px 18px;
  border-radius: 999px;
  background: var(--toast-bg);
  border: 1px solid var(--border);
  box-shadow: var(--toast-shadow);
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition:
    opacity 0.2s ease,
    transform 0.2s ease;
  z-index: 40;
}

.toast.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

/* ---------------------------------------------------------------- 自适应 */

@media (max-width: 560px) {
  .brand-sub,
  .footer {
    display: none;
  }

  .topbar-actions {
    width: 100%;
  }

  .topbar-actions .btn {
    flex: 1;
    text-align: center;
  }

  .select {
    flex: 1;
    justify-content: space-between;
  }

  .controls .btn {
    padding: 9px 12px;
    font-size: 13px;
  }

  .stat {
    padding: 5px 10px;
  }

  .win-stats {
    grid-template-columns: 1fr;
  }
}

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