/* Modern theme: dark glass panels, amber radiation accent, mono readouts. */
:root {
  --bg: #0a0f17;
  --surface: rgba(22, 31, 44, 0.78);
  --surface-2: #1a2433;
  --surface-3: #223047;
  --border: rgba(255, 255, 255, 0.08);
  --border-strong: rgba(255, 255, 255, 0.16);
  --text: #e6edf5;
  --muted: #8b99ad;
  --accent: #f5b83d;
  --accent-ink: #1a1203;
  --cyan: #38bdf8;
  --ok: #34d399;
  --warn: #fbbf24;
  --danger: #f87171;
  --radius: 14px;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
  --mono: "JetBrains Mono", Consolas, "Courier New", monospace;
}

* { box-sizing: border-box; }
html, body { height: 100%; }

body {
  margin: 0;
  color: var(--text);
  background:
    radial-gradient(1200px 600px at 15% -10%, rgba(56, 189, 248, 0.10), transparent 60%),
    radial-gradient(900px 500px at 100% 110%, rgba(245, 184, 61, 0.08), transparent 60%),
    var(--bg);
  font: 14px/1.45 Inter, "Segoe UI", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.app { height: 100vh; display: flex; flex-direction: column; }

/* ---- Top bar ---- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  background: rgba(10, 15, 23, 0.7);
  backdrop-filter: blur(10px);
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand h1 { margin: 0; font-size: 19px; font-weight: 700; letter-spacing: -0.2px; }
.brand p { margin: 0; font-size: 12.5px; color: var(--muted); }
.brand-mark {
  flex: 0 0 auto;
  width: 38px; height: 38px;
  display: grid; place-items: center;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 4px rgba(245, 184, 61, 0.15), 0 0 24px rgba(245, 184, 61, 0.35);
}
.brand-mark svg { width: 30px; height: 30px; fill: #111; }
.brand-mark.small { width: 30px; height: 30px; box-shadow: 0 0 0 3px rgba(245, 184, 61, 0.15); }
.brand-mark.small svg { width: 23px; height: 23px; }

.top-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.look-switch {
  display: inline-flex;
  padding: 3px;
  margin-right: 6px;
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
}
.look-switch a {
  padding: 6px 12px;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--muted);
  text-decoration: none;
  border-radius: 7px;
  transition: color 0.15s, background 0.15s;
}
.look-switch a:hover { color: var(--text); }
.look-switch a[aria-current="page"] { color: var(--text); background: var(--surface-3); box-shadow: 0 1px 4px rgba(0, 0, 0, 0.4); cursor: default; }
.look-switch a:focus-visible { outline: 2px solid var(--cyan); outline-offset: 1px; }

/* ---- Buttons ---- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  height: 36px;
  padding: 0 16px;
  font: 600 13.5px/1 Inter, "Segoe UI", sans-serif;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.08s, box-shadow 0.15s;
}
.btn:hover:not(:disabled) { background: var(--surface-3); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: 0.4; cursor: default; }
.btn:focus-visible, .icon-btn:focus-visible { outline: 2px solid var(--cyan); outline-offset: 2px; }
.btn.ghost { background: transparent; }
.btn.ghost:hover:not(:disabled) { background: rgba(255, 255, 255, 0.06); }
.btn.primary {
  color: var(--accent-ink);
  background: linear-gradient(180deg, #ffd06a, var(--accent));
  border-color: transparent;
  box-shadow: 0 4px 14px rgba(245, 184, 61, 0.3);
}
.btn.primary:hover:not(:disabled) { background: linear-gradient(180deg, #ffdb85, #f7c255); }
.btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; }
.btn svg .fill { fill: currentColor; stroke: none; }

.icon-btn {
  margin-left: auto;
  width: 34px; height: 34px;
  display: grid; place-items: center;
  color: var(--muted);
  background: transparent;
  border: 0;
  border-radius: 8px;
  cursor: pointer;
}
.icon-btn:hover { color: var(--text); background: rgba(255, 255, 255, 0.07); }
.icon-btn svg { width: 18px; height: 18px; stroke: currentColor; stroke-width: 2; stroke-linecap: round; fill: none; }

/* ---- Layout ---- */
.layout {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 268px minmax(0, 1fr) 296px;
  grid-template-areas: "settings board instruments";
  gap: 16px;
  padding: 16px;
}
.sidebar { min-height: 0; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; scrollbar-width: thin; }
.settings { grid-area: settings; }
.instruments { grid-area: instruments; }

.card {
  padding: 14px 16px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.card h2 {
  margin: 0 0 12px;
  font-size: 11.5px;
  font-weight: 700;
  letter-spacing: 1.1px;
  text-transform: uppercase;
  color: var(--muted);
}
.card h2 small { font-weight: 500; letter-spacing: 0.3px; text-transform: none; opacity: 0.8; margin-left: 4px; }

.field { display: block; margin-top: 10px; }
.field:first-of-type { margin-top: 0; }
.field > span { display: block; font-size: 12.5px; color: var(--muted); margin-bottom: 5px; }
select {
  width: 100%;
  height: 36px;
  padding: 0 32px 0 10px;
  font: 500 13.5px Inter, "Segoe UI", sans-serif;
  color: var(--text);
  background: var(--surface-2) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%238b99ad' stroke-width='2.5' stroke-linecap='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E") no-repeat right 10px center;
  border: 1px solid var(--border-strong);
  border-radius: 9px;
  appearance: none;
  cursor: pointer;
}
select:focus-visible { outline: 2px solid var(--cyan); outline-offset: 1px; }
select option { background: #1a2433; color: var(--text); }

/* Segmented difficulty picker */
.segmented {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  margin-bottom: 12px;
}
.segmented label { cursor: pointer; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented span {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 8px 4px;
  font-weight: 600;
  font-size: 13px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  transition: all 0.15s;
}
.segmented span small { font: 500 11px var(--mono); color: var(--muted); }
.segmented label:hover span { border-color: var(--border-strong); }
.segmented input:checked + span {
  color: var(--accent);
  background: rgba(245, 184, 61, 0.1);
  border-color: rgba(245, 184, 61, 0.65);
  box-shadow: inset 0 0 0 1px rgba(245, 184, 61, 0.3);
}
.segmented input:checked + span small { color: var(--accent); }
.segmented input:focus-visible + span { outline: 2px solid var(--cyan); outline-offset: 1px; }

/* Toggle switches */
.switch { display: flex; align-items: center; gap: 10px; padding: 5px 0; cursor: pointer; font-size: 13.5px; }
.switch input { position: absolute; opacity: 0; pointer-events: none; }
.switch .track {
  position: relative;
  flex: 0 0 auto;
  width: 34px; height: 20px;
  background: var(--surface-3);
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  transition: background 0.18s;
}
.switch .track::after {
  content: "";
  position: absolute;
  top: 2px; left: 2px;
  width: 14px; height: 14px;
  background: #cbd5e1;
  border-radius: 50%;
  transition: transform 0.18s, background 0.18s;
}
.switch input:checked + .track { background: rgba(56, 189, 248, 0.35); border-color: rgba(56, 189, 248, 0.6); }
.switch input:checked + .track::after { transform: translateX(14px); background: var(--cyan); }
.switch input:focus-visible + .track { outline: 2px solid var(--cyan); outline-offset: 2px; }

.best-card p { margin: 0; font-size: 13px; color: var(--muted); }
.best-card b { color: var(--accent); font: 700 20px var(--mono); display: block; }
#bestScore { color: var(--text); font-family: var(--mono); font-weight: 700; }

/* ---- Board ---- */
.board { grid-area: board; min-width: 0; display: flex; flex-direction: column; align-items: center; }
.board-card {
  display: inline-flex;
  flex-direction: column;
  gap: 10px;
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: var(--shadow);
  transition: box-shadow 0.4s, border-color 0.4s;
}
.board[data-state="won"] .board-card { border-color: rgba(52, 211, 153, 0.6); box-shadow: 0 0 0 1px rgba(52, 211, 153, 0.3), 0 0 40px rgba(52, 211, 153, 0.25); }
.board[data-state="dead"] .board-card { border-color: rgba(248, 113, 113, 0.6); box-shadow: 0 0 40px rgba(248, 113, 113, 0.25); }
.board[data-state="timeout"] .board-card { border-color: rgba(251, 191, 36, 0.6); box-shadow: 0 0 40px rgba(251, 191, 36, 0.2); }

.hud { display: flex; justify-content: space-between; align-items: flex-end; padding: 0 4px; }
.stat { display: flex; flex-direction: column; gap: 1px; }
.stat.right { align-items: flex-end; }
.stat-label { font-size: 11px; font-weight: 700; letter-spacing: 1.1px; text-transform: uppercase; color: var(--muted); }
.stat-value { font: 700 26px/1.05 var(--mono); font-variant-numeric: tabular-nums; letter-spacing: 1px; }
.stat-value.blink { color: var(--danger); animation: blink 0.8s steps(1) infinite; }
@keyframes blink { 50% { opacity: 0.35; } }

.canvas-wrap {
  position: relative;
  line-height: 0;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 0 0 1px var(--border-strong);
  background: #111;
}
#gridCanvas { display: block; touch-action: none; cursor: crosshair; }

.overlay { position: absolute; inset: 0; pointer-events: none; display: flex; justify-content: center; line-height: 1.5; }
.overlay.hidden { display: none; }
.overlay.ready { align-items: flex-start; padding-top: 14px; }
.overlay.paused { align-items: center; background: rgba(8, 12, 18, 0.88); backdrop-filter: blur(6px); }
.ov-box {
  max-width: 90%;
  padding: 10px 16px;
  font-size: 13px;
  text-align: center;
  color: var(--text);
  background: rgba(12, 18, 27, 0.85);
  border: 1px solid var(--border-strong);
  border-radius: 12px;
  backdrop-filter: blur(6px);
  box-shadow: var(--shadow);
}
.ov-box b { font-size: 14.5px; color: var(--accent); }

.status-line { margin: 10px 0 0; min-height: 1.4em; text-align: center; color: var(--muted); font-size: 13px; }

/* ---- Instruments ---- */
.gauge {
  display: flex;
  justify-content: center;
  line-height: 0;
  border-radius: 12px;
  overflow: hidden;
  background: linear-gradient(#1c2635, #0f151e);
  box-shadow: inset 0 0 0 1px var(--border);
}
#gaugeCanvas { display: block; max-width: 100%; }

.readout {
  margin-top: 10px;
  padding: 8px 12px;
  display: flex;
  align-items: baseline;
  justify-content: flex-end;
  gap: 8px;
  background: #0b1119;
  border: 1px solid var(--border);
  border-radius: 10px;
}
.readout-value { font: 700 32px/1.05 var(--mono); font-variant-numeric: tabular-nums; color: var(--cyan); text-shadow: 0 0 14px rgba(56, 189, 248, 0.45); }
.readout-unit { font-size: 12px; color: var(--muted); }

.trend-row { display: flex; justify-content: space-between; margin: 10px 2px 0; font-size: 13px; color: var(--muted); }
.trend { font-family: var(--mono); font-weight: 700; color: var(--text); }
.trend.up { color: var(--danger); }
.trend.down { color: var(--cyan); }

.life-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 6px; color: var(--muted); font-size: 13px; }
.life-head b { font: 700 18px var(--mono); color: var(--text); }
.life-bar { height: 12px; border-radius: 999px; background: #0b1119; box-shadow: inset 0 0 0 1px var(--border); overflow: hidden; }
.life-fill { height: 100%; border-radius: 999px; transition: background 0.4s; }
.life-fill.ok { background: linear-gradient(90deg, #10b981, var(--ok)); box-shadow: 0 0 12px rgba(52, 211, 153, 0.5); }
.life-fill.warn { background: linear-gradient(90deg, #f59e0b, var(--warn)); box-shadow: 0 0 12px rgba(251, 191, 36, 0.5); }
.life-fill.danger { background: linear-gradient(90deg, #ef4444, var(--danger)); box-shadow: 0 0 12px rgba(248, 113, 113, 0.6); animation: pulse 0.9s ease-in-out infinite; }
@keyframes pulse { 50% { filter: brightness(1.4); } }

.readings { margin: 12px 0 0; }
.readings div { display: flex; justify-content: space-between; padding: 6px 0; border-top: 1px solid var(--border); font-size: 13px; }
.readings div:first-child { border-top: 0; }
.readings dt { color: var(--muted); }
.readings dd { margin: 0; font-family: var(--mono); font-weight: 700; }

/* ---- Modals ---- */
.modal {
  width: min(560px, calc(100vw - 32px));
  max-height: calc(100vh - 48px);
  padding: 0;
  color: var(--text);
  background: #121a26;
  border: 1px solid var(--border-strong);
  border-radius: 18px;
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.6);
}
.modal[open] { display: flex; flex-direction: column; animation: pop 0.22s ease-out; }
.modal::backdrop { background: rgba(4, 7, 12, 0.65); backdrop-filter: blur(4px); }
@keyframes pop { from { opacity: 0; transform: translateY(10px) scale(0.98); } }

.modal-head { display: flex; align-items: center; gap: 12px; padding: 18px 20px 6px; }
.modal-head h2 { margin: 0; font-size: 19px; font-weight: 700; }
.modal-body { padding: 8px 20px 4px; overflow-y: auto; }
.modal-foot { display: flex; justify-content: flex-end; gap: 10px; padding: 14px 20px 18px; }

.lead { margin: 0 0 16px; color: #c7d2e0; }
.help-modal h3 { margin: 18px 0 10px; font-size: 11.5px; letter-spacing: 1.1px; text-transform: uppercase; color: var(--muted); }

.steps { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.steps li { display: flex; gap: 12px; padding: 12px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 12px; }
.steps p { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.step-num {
  flex: 0 0 auto;
  width: 26px; height: 26px;
  display: grid; place-items: center;
  font: 700 13px var(--mono);
  color: var(--accent-ink);
  background: var(--accent);
  border-radius: 50%;
}

.controls-grid { display: grid; gap: 6px; }
.controls-grid > div { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.controls-grid > div > span:last-child { margin-left: auto; color: var(--text); font-weight: 500; }
.keys { display: inline-flex; gap: 4px; }
kbd {
  min-width: 24px;
  padding: 2px 6px;
  font: 600 12px var(--mono);
  text-align: center;
  color: var(--text);
  background: var(--surface-3);
  border: 1px solid var(--border-strong);
  border-bottom-width: 2px;
  border-radius: 6px;
}

.formula {
  padding: 10px 12px;
  font: 600 13px var(--mono);
  color: var(--accent);
  background: rgba(245, 184, 61, 0.08);
  border: 1px solid rgba(245, 184, 61, 0.3);
  border-radius: 10px;
}
.score-list { margin: 10px 0; padding: 0; list-style: none; display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.score-list b { display: inline-block; min-width: 64px; color: var(--text); }
.muted { color: var(--muted); font-size: 13px; }

/* Result modal: content comes from game.js */
.result-modal { width: min(460px, calc(100vw - 32px)); }
.result-icon { width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center; font-size: 18px; font-weight: 700; }
.result-modal[data-result="won"] .result-icon { background: rgba(52, 211, 153, 0.15); color: var(--ok); }
.result-modal[data-result="won"] .result-icon::before { content: "✓"; }
.result-modal[data-result="dead"] .result-icon { background: rgba(248, 113, 113, 0.15); color: var(--danger); }
.result-modal[data-result="dead"] .result-icon::before { content: "✕"; }
.result-modal[data-result="timeout"] .result-icon { background: rgba(251, 191, 36, 0.15); color: var(--warn); }
.result-modal[data-result="timeout"] .result-icon::before { content: "⏱"; font-size: 16px; }

table.stats { width: 100%; border-collapse: collapse; margin-bottom: 12px; font-size: 13.5px; }
table.stats th { text-align: left; font-weight: 400; color: var(--muted); padding: 5px 10px 5px 0; white-space: nowrap; }
table.stats td { padding: 5px 0; }
table.stats .muted { font-size: 12px; }
table.stats.score { background: var(--surface-2); border: 1px solid var(--border); border-radius: 12px; border-collapse: separate; border-spacing: 0; overflow: hidden; }
table.stats.score th, table.stats.score td { padding: 6px 12px; }
table.stats.score td:last-child { text-align: right; font-family: var(--mono); font-weight: 700; }
table.stats.score tr.total th, table.stats.score tr.total td { border-top: 1px solid var(--border-strong); font-weight: 700; color: var(--text); }
table.stats.score tr.total td:last-child { font-size: 20px; color: var(--accent); }
.newbest { margin: 0 0 6px; color: var(--accent); font-weight: 600; }

/* ---- Responsive ---- */
@media (max-width: 1180px) {
  .layout {
    grid-template-columns: 268px minmax(0, 1fr);
    grid-template-rows: auto 1fr;
    grid-template-areas: "instruments board" "settings board";
    overflow-y: auto;
  }
  .sidebar { overflow: visible; }
}

@media (max-width: 820px) {
  .app { height: auto; min-height: 100vh; }
  .topbar { flex-direction: column; align-items: stretch; padding: 12px 16px; }
  .top-actions { justify-content: flex-start; }
  .top-actions .btn span { display: none; }
  .look-switch { order: 9; margin-left: auto; margin-right: 0; }
  .layout {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto;
    grid-template-areas: "settings" "board" "instruments";
    padding: 12px 16px;
  }
}
