:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #eef0f4;
  --border: #dde1e8;
  --text: #161a22;
  --muted: #5d6676;
  --accent: #16a34a;
  --accent-ink: #ffffff;
  --hit: #16a34a;
  --hit-ink: #ffffff;
  --near: #d4a017;
  --near-ink: #1c1400;
  --miss: #e3e6ec;
  --miss-ink: #3a4250;
  --mask: #c9ced8;
  --code-bg: #fbfcfd;
  --code-text: #1f2430;
  --code-num: #a0a7b4;
  --code-string: #b45309;
  --code-number: #7c3aed;
  --shadow: 0 1px 2px rgb(16 24 40 / 6%), 0 8px 24px rgb(16 24 40 / 6%);
  --mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --sans: Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0d1016;
    --surface: #151a22;
    --surface-2: #1c222c;
    --border: #2a313d;
    --text: #e7eaf0;
    --muted: #8b94a5;
    --accent: #22c55e;
    --accent-ink: #052e16;
    --hit: #22a355;
    --hit-ink: #ffffff;
    --near: #c9971a;
    --near-ink: #1c1400;
    --miss: #262d38;
    --miss-ink: #b4bccb;
    --mask: #333b48;
    --code-bg: #10141b;
    --code-text: #dbe1ea;
    --code-num: #4b5465;
    --code-string: #f0a35e;
    --code-number: #b69cff;
    --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 10px 30px rgb(0 0 0 / 25%);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0d1016;
  --surface: #151a22;
  --surface-2: #1c222c;
  --border: #2a313d;
  --text: #e7eaf0;
  --muted: #8b94a5;
  --accent: #22c55e;
  --accent-ink: #052e16;
  --hit: #22a355;
  --hit-ink: #ffffff;
  --near: #c9971a;
  --near-ink: #1c1400;
  --miss: #262d38;
  --miss-ink: #b4bccb;
  --mask: #333b48;
  --code-bg: #10141b;
  --code-text: #dbe1ea;
  --code-num: #4b5465;
  --code-string: #f0a35e;
  --code-number: #b69cff;
  --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 10px 30px rgb(0 0 0 / 25%);
  color-scheme: dark;
}

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

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.5;
  min-height: 100%;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  display: flex;
  flex-direction: column;
}

button { font: inherit; color: inherit; cursor: pointer; }

code { font-family: var(--mono); font-size: 0.9em; }

[hidden] { display: none !important; }

/* ---------- header ---------- */

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  max-width: 760px;
  width: 100%;
  margin: 0 auto;
  padding-block: 18px 10px;
}

.logo {
  margin: 0;
  font-family: var(--mono);
  font-size: 24px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.logo-bracket { color: var(--accent); }

.top-actions { display: flex; gap: 6px; }

.icon-btn {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 16px;
  transition: background 0.15s, border-color 0.15s;
}

.icon-btn:hover { background: var(--surface-2); }

/* ---------- layout ---------- */

.game {
  max-width: 760px;
  width: 100%;
  margin: 0 auto;
  padding-block: 6px 24px;
  flex: 1;
}

.archive-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 14px;
  margin-bottom: 12px;
  border-radius: 12px;
  border: 1px dashed var(--border);
  color: var(--muted);
  font-size: 14px;
}

.archive-banner a { color: var(--accent); font-weight: 600; text-decoration: none; }

/* ---------- editor ---------- */

.editor {
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  overflow: hidden;
}

.editor-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  font-family: var(--mono);
  font-size: 13px;
  color: var(--muted);
}

.dots { display: flex; gap: 6px; }
.dots i { width: 11px; height: 11px; border-radius: 50%; background: var(--mask); }
.dots i:nth-child(1) { background: #f87171; }
.dots i:nth-child(2) { background: #fbbf24; }
.dots i:nth-child(3) { background: #4ade80; }

.filename { flex: 1; }

.day-tag { font-weight: 600; color: var(--text); }

.code {
  list-style: none;
  margin: 0;
  padding: 14px 0;
  font-family: var(--mono);
  font-size: 14px;
  line-height: 1.85;
  color: var(--code-text);
  overflow-x: auto;
  counter-reset: line;
  font-variant-ligatures: none;
}

.code li {
  display: flex;
  white-space: pre;
  padding: 0 16px 0 0;
  counter-increment: line;
  transition: background 0.4s;
}

.code li::before {
  content: counter(line);
  flex: none;
  width: 3em;
  padding-right: 1.2em;
  text-align: right;
  color: var(--code-num);
  user-select: none;
}

.code li.just-revealed { animation: reveal 0.9s ease-out; }

@keyframes reveal {
  from { background: color-mix(in srgb, var(--accent) 22%, transparent); }
  to { background: transparent; }
}

.mask {
  color: transparent;
  background: linear-gradient(var(--mask), var(--mask)) center / 100% 1.15em no-repeat;
  border-radius: 4px;
  user-select: none;
}

.tok-str { color: var(--code-string); }
.tok-num { color: var(--code-number); }

/* ---------- status + form ---------- */

.status {
  margin: 14px 2px 10px;
  color: var(--muted);
  font-size: 14px;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

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

.pips { display: inline-flex; gap: 4px; align-items: center; }
.pips i { width: 18px; height: 6px; border-radius: 3px; background: var(--miss); }
.pips i.used { background: var(--muted); }
.pips i.won { background: var(--hit); }

.guess-form { display: flex; gap: 8px; }

.combo { position: relative; flex: 1; }

.combo input {
  width: 100%;
  height: 46px;
  padding: 0 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 16px;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.combo input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent);
}

.combo input:disabled { opacity: 0.6; }

.combo.shake { animation: shake 0.35s; }

@keyframes shake {
  20%, 60% { transform: translateX(-6px); }
  40%, 80% { transform: translateX(6px); }
}

.suggestions {
  position: absolute;
  z-index: 10;
  top: calc(100% + 6px);
  left: 0;
  right: 0;
  margin: 0;
  padding: 6px;
  list-style: none;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  max-height: 290px;
  overflow-y: auto;
}

.suggestions li {
  padding: 8px 10px;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  gap: 8px;
}

.suggestions li .alias { color: var(--muted); font-size: 13px; }

.suggestions li[aria-selected="true"], .suggestions li:hover { background: var(--surface-2); }

.btn-primary {
  height: 46px;
  padding: 0 22px;
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 700;
  transition: filter 0.15s;
}

.btn-primary:hover { filter: brightness(1.08); }
.btn-primary:disabled { opacity: 0.5; cursor: default; }

.btn-secondary {
  height: 40px;
  padding: 0 16px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  font-weight: 600;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
}

/* ---------- result ---------- */

.result {
  margin-top: 4px;
  padding: 16px;
  border-radius: 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}

.result h2 { margin: 0; font-size: 18px; }
.result p { margin: 2px 0 0; color: var(--muted); font-size: 14px; }
.result .answer { color: var(--accent); }
.result-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

/* ---------- board ---------- */

.board { margin-top: 18px; }

.board-head, .row {
  display: grid;
  grid-template-columns: 1.5fr repeat(5, 1fr);
  gap: 6px;
}

.board-head {
  padding: 0 0 6px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  text-align: center;
}

.board-head .col-name { text-align: left; }

.rows { display: flex; flex-direction: column; gap: 6px; }

.row-name {
  display: flex;
  align-items: center;
  padding: 0 12px;
  border-radius: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-weight: 600;
  min-height: 52px;
}

.row.correct .row-name { border-color: var(--hit); color: var(--hit); }

.tile {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  min-height: 52px;
  padding: 4px;
  border-radius: 10px;
  font-size: 12.5px;
  font-weight: 600;
  line-height: 1.25;
}

.tile.hit { background: var(--hit); color: var(--hit-ink); }
.tile.near { background: var(--near); color: var(--near-ink); }
.tile.miss { background: var(--miss); color: var(--miss-ink); }

.tile .arrow { font-size: 15px; line-height: 1; }

.row.new .tile { animation: flip 0.5s ease-out both; }
.row.new .tile:nth-child(2) { animation-delay: 0.05s; }
.row.new .tile:nth-child(3) { animation-delay: 0.15s; }
.row.new .tile:nth-child(4) { animation-delay: 0.25s; }
.row.new .tile:nth-child(5) { animation-delay: 0.35s; }
.row.new .tile:nth-child(6) { animation-delay: 0.45s; }

@keyframes flip {
  from { transform: rotateX(90deg); opacity: 0; }
  to { transform: rotateX(0); opacity: 1; }
}

@media (max-width: 560px) {
  .board-head { grid-template-columns: repeat(5, 1fr); }
  .board-head .col-name { display: none; }
  .row { grid-template-columns: repeat(5, 1fr); }
  .row-name { grid-column: 1 / -1; min-height: 34px; font-size: 14px; }
  .tile { font-size: 11.5px; min-height: 48px; }
  .code { font-size: 12.5px; }
  .code li::before { width: 2.4em; padding-right: 0.9em; }
}

/* ---------- modals ---------- */

.modal {
  width: min(460px, calc(100vw - 32px));
  max-height: calc(100vh - 48px);
  padding: 22px 22px 18px;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

.modal::backdrop { background: rgb(5 8 12 / 55%); backdrop-filter: blur(2px); }

.modal h2 { margin: 0 0 12px; font-size: 20px; }
.modal h3 { margin: 18px 0 8px; font-size: 13px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.modal p { margin: 0 0 10px; }
.muted { color: var(--muted); font-size: 13.5px; }

.modal-close-form { position: absolute; top: 12px; right: 12px; margin: 0; }
.modal-close {
  width: 32px; height: 32px; border: 0; border-radius: 8px;
  background: transparent; font-size: 22px; line-height: 1; color: var(--muted);
}
.modal-close:hover { background: var(--surface-2); }

.help-demo {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  margin: 0 0 12px;
  border-radius: 10px;
  background: var(--code-bg);
  border: 1px solid var(--border);
}

.help-demo code { white-space: pre; }
.help-arrow { color: var(--muted); }

.help-legend { list-style: none; padding: 0; margin: 0 0 12px; display: grid; gap: 8px; }
.help-legend li { display: flex; align-items: center; gap: 10px; font-size: 14px; }
.tile.mini { min-height: 30px; min-width: 64px; flex: none; }

.stat-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; text-align: center; }
.stat-grid .num { font-size: 26px; font-weight: 700; font-family: var(--mono); }
.stat-grid .lbl { font-size: 12px; color: var(--muted); }

.dist { display: grid; gap: 5px; }
.dist-row { display: grid; grid-template-columns: 16px 1fr; gap: 8px; align-items: center; font-family: var(--mono); font-size: 13px; }
.dist-bar {
  min-width: 26px;
  padding: 2px 8px;
  border-radius: 5px;
  background: var(--miss);
  color: var(--miss-ink);
  text-align: right;
  font-weight: 600;
}
.dist-bar.today { background: var(--hit); color: var(--hit-ink); }

.stats-foot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-top: 18px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
}

.countdown .lbl { font-size: 12px; color: var(--muted); }
.countdown .num { font-family: var(--mono); font-size: 20px; font-weight: 700; }

/* ---------- misc ---------- */

.toast {
  position: fixed;
  left: 50%;
  top: calc(18px + env(safe-area-inset-top, 0px));
  transform: translate(-50%, -20px);
  padding: 10px 16px;
  border-radius: 10px;
  background: var(--text);
  color: var(--bg);
  font-weight: 600;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 100;
}

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

.foot {
  text-align: center;
  color: var(--muted);
  font-size: 12.5px;
  padding-block: 10px 22px;
}

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