/* ChemSweeper — retro lab desktop theme. Original styling inspired by late-90s desktop software. */

:root {
  --desk: #2f5d62;          /* lab-bench teal desktop */
  --desk-2: #24494d;
  --face: #c9c6bd;          /* warm instrument grey */
  --face-2: #b9b5aa;
  --hi: #ffffff;            /* bevel highlight */
  --lo: #7d796f;            /* bevel shadow */
  --dark: #2b2925;          /* bevel outer shadow */
  --ink: #1b1a17;
  --ink-2: #4a4740;
  --title-a: #1f4f5a;       /* title bar gradient */
  --title-b: #4f8a8b;
  --title-ink: #ffffff;
  --paper: #f7f4ea;         /* notebook paper */
  --rule: #c9d8e0;
  --accent: #b8521f;        /* lab orange */
  --good: #1f6b3a;
  --good-bg: #dcefdc;
  --bad: #8c1f1f;
  --bad-bg: #f6dcd8;
  --focus: #0a5cff;
  --lcd-bg: #10170f;
  --lcd-on: #7cff6b;
  --lcd-off: #1f2b1d;
  --tile: #c9c6bd;
  --tile-open: #e6e3da;
  --n1: #1d4fb8; --n2: #1d7a32; --n3: #b3261e; --n4: #3b1f8f;
  --n5: #7a2d12; --n6: #0e6e73; --n7: #1b1a17; --n8: #5c5a55;
  --tile-size: 30px;
  --radius: 0px;
  color-scheme: light;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

:root[data-theme="dark"] {
  --desk: #10201f;
  --desk-2: #0b1716;
  --face: #3b3d40;
  --face-2: #33353a;
  --hi: #5b5e63;
  --lo: #222326;
  --dark: #0d0d0e;
  --ink: #ecebe6;
  --ink-2: #bdbab2;
  --title-a: #183c44;
  --title-b: #2f6668;
  --paper: #23262a;
  --rule: #34424a;
  --accent: #f08a4b;
  --good: #8fe0a5;
  --good-bg: #183424;
  --bad: #ffaaa0;
  --bad-bg: #3d1f1d;
  --focus: #7fb2ff;
  --tile: #4a4d52;
  --tile-open: #2a2c30;
  --n1: #8fb4ff; --n2: #8fe0a5; --n3: #ff9d92; --n4: #c7b2ff;
  --n5: #f5b98c; --n6: #7fe0e6; --n7: #ecebe6; --n8: #a9a7a1;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  min-height: 100dvh;
  background:
    radial-gradient(circle at 20% 10%, rgba(255,255,255,.06) 0 2px, transparent 3px) 0 0 / 28px 28px,
    linear-gradient(var(--desk), var(--desk-2));
  color: var(--ink);
  font-size: 16px;
  line-height: 1.45;
}

.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.skip-link { position: absolute; left: 8px; top: -40px; background: var(--paper); color: var(--ink); padding: 6px 10px; z-index: 10; }
.skip-link:focus { top: 8px; }
.noscript { color: #fff; text-align: center; }

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

/* ---------- Bevels ---------- */
.raised, button, .window {
  background: var(--face);
  border: 2px solid;
  border-color: var(--hi) var(--dark) var(--dark) var(--hi);
  box-shadow: inset -1px -1px 0 var(--lo), inset 1px 1px 0 var(--face-2);
}
.inset {
  border: 2px solid;
  border-color: var(--lo) var(--hi) var(--hi) var(--lo);
  box-shadow: inset 1px 1px 0 var(--dark);
}

button {
  font: inherit;
  color: var(--ink);
  padding: 6px 12px;
  min-height: 36px;
  cursor: pointer;
}
button:active:not(:disabled), button[aria-pressed="true"] {
  border-color: var(--dark) var(--hi) var(--hi) var(--dark);
  box-shadow: inset 1px 1px 0 var(--lo);
}
button:disabled { color: var(--ink-2); cursor: not-allowed; opacity: .75; }
button.primary { font-weight: 700; outline: 1px solid var(--dark); outline-offset: -5px; }
a { color: var(--n1); }
:root[data-theme="dark"] a { color: #9cc4ff; }

/* ---------- Desktop + window ---------- */
.desktop {
  display: flex; flex-direction: column; align-items: center;
  padding: max(12px, env(safe-area-inset-top)) 10px 18px;
  min-height: 100dvh;
}
.window { padding: 3px; }
.app-window { width: min(100%, max-content); min-width: min(100%, 340px); max-width: 100%; }
.desktop-foot { color: #e6efe9; font-size: .85rem; text-align: center; margin: 12px 0 0; }
.desktop-foot a { color: #fff; }

.titlebar {
  display: flex; align-items: center; gap: 8px;
  background: linear-gradient(90deg, var(--title-a), var(--title-b));
  color: var(--title-ink);
  padding: 4px 8px;
  min-height: 30px;
}
.titlebar h1, .titlebar h2 { font-size: 1rem; margin: 0; letter-spacing: .02em; }
.titlebar-tag { margin-left: auto; font-size: .8rem; opacity: .9; font-style: italic; }
.titlebar .close {
  margin-left: auto; min-height: 26px; min-width: 30px; padding: 0 8px; font-weight: 700; line-height: 1;
}

.menubar { display: flex; flex-wrap: wrap; gap: 2px; padding: 3px 2px; }
.menubar button { border-color: transparent; box-shadow: none; background: transparent; padding: 4px 10px; min-height: 34px; }
.menubar button:hover, .menubar button:focus-visible { background: var(--title-a); color: #fff; }

/* ---------- Readouts ---------- */
.game-area { padding: 6px; display: flex; flex-direction: column; gap: 6px; }
.readouts { display: flex; align-items: center; justify-content: space-between; padding: 6px 8px; gap: 8px; }
.readout { display: flex; flex-direction: column; align-items: center; gap: 2px; }
.readout-label { font-size: .7rem; text-transform: uppercase; letter-spacing: .08em; color: var(--ink-2); }
.lcd { display: inline-flex; gap: 2px; background: var(--lcd-bg); padding: 3px 4px; border: 2px solid; border-color: var(--lo) var(--hi) var(--hi) var(--lo); }
.lcd svg { width: 15px; height: 26px; display: block; }
.lcd .on { fill: var(--lcd-on); }
.lcd .off { fill: var(--lcd-off); }
.face-button { width: 52px; height: 52px; padding: 4px; display: grid; place-items: center; }
.face-button svg { width: 36px; height: 36px; }
.face-button[data-state="won"] { background: var(--good-bg); }
.face-button[data-state="lost"] { background: var(--bad-bg); }

/* ---------- Instruments ---------- */
.instruments { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; }
.energy {
  display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; min-height: 36px;
  border: 2px solid; border-color: var(--lo) var(--hi) var(--hi) var(--lo);
  background: var(--paper); font-variant-numeric: tabular-nums;
}
.energy svg { width: 16px; height: 16px; color: var(--accent); }
.energy.flash { animation: energy-flash .9s ease-out; }
@keyframes energy-flash { from { background: #fff3b0; } to { background: var(--paper); } }
.tool { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; }
.tool svg { width: 16px; height: 16px; }
.tool .cost { font-size: .78rem; color: var(--ink-2); }
.badge { background: var(--accent); color: #fff; border-radius: 9px; padding: 0 6px; font-size: .75rem; font-weight: 700; }
.tool.ready { animation: ready-pulse 2.4s ease-in-out infinite; }
@keyframes ready-pulse { 50% { background: #f2e2b3; } }
:root[data-theme="dark"] .tool.ready { animation-name: ready-pulse-dark; }
@keyframes ready-pulse-dark { 50% { background: #5a4a22; } }

/* ---------- Board ---------- */
.board-frame { position: relative; padding: 0; max-width: calc(100vw - 32px); align-self: center; }
.board-scroll { overflow: auto; max-height: min(72dvh, 820px); overscroll-behavior: contain; touch-action: pan-x pan-y; }
.board { display: grid; grid-template-columns: repeat(var(--cols), var(--tile-size)); width: max-content; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
.board [role="row"] { display: contents; }
.tile {
  width: var(--tile-size); height: var(--tile-size); min-height: 0; padding: 0;
  display: grid; place-items: center;
  font-weight: 800; font-size: calc(var(--tile-size) * .55); line-height: 1;
  background: var(--tile);
  border: 3px solid; border-color: var(--hi) var(--lo) var(--lo) var(--hi);
  box-shadow: none;
  font-family: "Trebuchet MS", system-ui, sans-serif;
}
.tile:focus-visible { outline: 3px solid var(--focus); outline-offset: -3px; position: relative; z-index: 1; }
.tile.open { background: var(--tile-open); border: 1px solid var(--lo); border-width: 0 1px 1px 0; cursor: default; }
.tile.open:active { box-shadow: none; }
.tile svg { width: 70%; height: 70%; pointer-events: none; }
.tile.n1 { color: var(--n1); } .tile.n2 { color: var(--n2); } .tile.n3 { color: var(--n3); } .tile.n4 { color: var(--n4); }
.tile.n5 { color: var(--n5); } .tile.n6 { color: var(--n6); } .tile.n7 { color: var(--n7); } .tile.n8 { color: var(--n8); }
.tile.exploded { background: #e4553b; }
.tile.wrong-flag { background: var(--bad-bg); }
.tile.hint { background: #fff1a8; box-shadow: inset 0 0 0 3px #c79200; }
:root[data-theme="dark"] .tile.hint { background: #5e5220; box-shadow: inset 0 0 0 3px #e0b43a; }
.tile.hint-clue { box-shadow: inset 0 0 0 3px var(--n1); }
.tile.probed-safe { box-shadow: inset 0 0 0 3px var(--good); }
.board.probing .tile:not(.open) { cursor: crosshair; }
.board-overlay {
  position: absolute; inset: 0; display: grid; place-content: center; justify-items: center; gap: 8px;
  background: rgba(201, 198, 189, .92); text-align: center; padding: 16px;
}
:root[data-theme="dark"] .board-overlay { background: rgba(43, 45, 48, .92); }
.spinner { width: 34px; height: 34px; border: 4px solid var(--lo); border-top-color: var(--accent); border-radius: 50%; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.zoom { display: flex; gap: 4px; justify-content: flex-end; }
.zoom button { min-width: 40px; }

/* ---------- Status bar ---------- */
.statusbar { display: flex; gap: 3px; padding: 3px 2px 1px; font-size: .85rem; }
.status-cell { border: 1px solid; border-color: var(--lo) var(--hi) var(--hi) var(--lo); padding: 2px 8px; white-space: nowrap; }
.status-cell.grow { flex: 1; white-space: normal; min-width: 0; }
.status-cell:empty { display: none; }

/* ---------- Dialog windows ---------- */
dialog.window {
  padding: 3px; color: var(--ink);
  width: min(560px, calc(100vw - 20px));
  max-height: calc(100dvh - 24px);
}
dialog.wide { width: min(980px, calc(100vw - 20px)); }
dialog::backdrop { background: rgba(10, 25, 25, .45); }
dialog[open] { display: flex; flex-direction: column; }
.dialog-body { padding: 14px 16px 16px; overflow: auto; background: var(--face); }
.dialog-body.paper { background: var(--paper); }
.dialog-actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-end; padding: 10px 16px 12px; border-top: 1px solid var(--lo); }
.dialog-body h3 { font-size: 1rem; margin: 1em 0 .35em; }
.dialog-body h3:first-child { margin-top: 0; }
.dialog-body p { margin: .4em 0 .7em; }
.fine { font-size: .85rem; color: var(--ink-2); }

@media (max-width: 640px) {
  dialog.window { width: 100vw; max-width: 100vw; margin: auto 0 0; max-height: 88dvh; }
  .titlebar-tag { display: none; }
}

/* ---------- Questions ---------- */
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.chip { font-size: .75rem; padding: 2px 8px; border: 1px solid var(--lo); background: var(--paper); }
.prompt { font-size: 1.08rem; font-weight: 600; margin: 4px 0 12px; }
.choices { display: grid; gap: 8px; margin: 0; padding: 0; border: 0; }
.choices legend { padding: 0; margin-bottom: 6px; font-size: .85rem; color: var(--ink-2); }
.choice {
  display: flex; gap: 10px; align-items: flex-start; text-align: left; width: 100%;
  padding: 10px 12px; min-height: 44px; background: var(--paper);
}
.choice .letter { font-weight: 800; min-width: 1.4em; }
.choice input { margin: 3px 0 0; width: 18px; height: 18px; flex: none; }
label.choice { cursor: pointer; border: 2px solid; border-color: var(--hi) var(--dark) var(--dark) var(--hi); }
.choice.is-right { background: var(--good-bg); border-color: var(--good); }
.choice.is-wrong { background: var(--bad-bg); border-color: var(--bad); }
.choice .tag { margin-left: auto; font-size: .8rem; font-weight: 700; white-space: nowrap; }
.numeric-row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.numeric-row input { font: inherit; font-size: 1.1rem; padding: 8px 10px; width: 12em; max-width: 100%; border: 2px solid; border-color: var(--lo) var(--hi) var(--hi) var(--lo); background: var(--paper); color: var(--ink); }
.given { background: var(--paper); border: 1px dashed var(--lo); padding: 6px 10px; font-size: .9rem; margin: 0 0 10px; }
.equation-row { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; font-size: 1.15rem; margin: 6px 0 4px; }
.coef { width: 3.2em; font: inherit; text-align: center; padding: 6px 4px; border: 2px solid; border-color: var(--lo) var(--hi) var(--hi) var(--lo); background: var(--paper); color: var(--ink); }
.atom-table { border-collapse: collapse; margin: 8px 0; font-size: .9rem; }
.atom-table th, .atom-table td { border: 1px solid var(--lo); padding: 3px 10px; text-align: center; }
.atom-table .mismatch { background: var(--bad-bg); }
.order-pool, .order-list { display: grid; gap: 6px; margin: 6px 0; padding: 0; list-style: none; }
.order-list li { display: flex; gap: 6px; align-items: center; }
.order-list .step-n { font-weight: 800; min-width: 1.6em; }
.order-list button.step { flex: 1; text-align: left; background: var(--paper); }
.order-list .step-move { min-width: 40px; padding: 4px; }
.feedback { margin-top: 12px; padding: 10px 12px; border-left: 5px solid var(--lo); background: var(--paper); }
.feedback:empty { display: none; }
.feedback.good { border-color: var(--good); }
.feedback.bad { border-color: var(--accent); }
.feedback strong { display: block; margin-bottom: 4px; }
.feedback .reward { color: var(--good); font-weight: 700; }
.calc { margin-top: 10px; padding: 8px; border: 1px dashed var(--lo); }
.calc[hidden] { display: none; }
.calc input { font: inherit; width: 100%; padding: 6px 8px; background: var(--paper); color: var(--ink); border: 2px solid; border-color: var(--lo) var(--hi) var(--hi) var(--lo); }
.chem sub, .chem sup, sub, sup { line-height: 0; }

/* ---------- Notebook ---------- */
.tabs { display: flex; flex-wrap: wrap; gap: 2px; padding: 6px 8px 0; }
.tabs [role="tab"] { border-bottom: none; padding: 6px 12px; min-height: 36px; }
.tabs [role="tab"][aria-selected="true"] { background: var(--paper); position: relative; top: 2px; font-weight: 700; }
.notebook-body { display: grid; grid-template-columns: 220px 1fr; gap: 0; min-height: 0; flex: 1; overflow: hidden; }
.notebook-nav { overflow: auto; border-right: 2px solid var(--lo); padding: 8px; background: var(--face); }
.notebook-nav button { display: block; width: 100%; text-align: left; margin-bottom: 4px; font-size: .9rem; }
.notebook-nav button[aria-current="true"] { background: var(--paper); font-weight: 700; }
.notebook-page {
  overflow: auto; padding: 16px 20px;
  background: repeating-linear-gradient(var(--paper) 0 27px, var(--rule) 27px 28px);
}
.notebook-page h3 { margin-top: 1.2em; }
.notebook-page .worked { background: var(--paper); border: 1px solid var(--lo); padding: 8px 12px; margin: 8px 0; }
.meter { display: inline-block; width: 120px; height: 12px; border: 1px solid var(--lo); background: var(--paper); vertical-align: middle; }
.meter > span { display: block; height: 100%; background: var(--good); }
.progress-table { border-collapse: collapse; width: 100%; font-size: .92rem; }
.progress-table th, .progress-table td { text-align: left; padding: 4px 8px; border-bottom: 1px solid var(--rule); }
@media (max-width: 720px) {
  .notebook-body { grid-template-columns: 1fr; }
  .notebook-nav { border-right: none; border-bottom: 2px solid var(--lo); max-height: 160px; }
}

/* ---------- Periodic table ---------- */
.ptable { display: grid; grid-template-columns: repeat(18, minmax(30px, 1fr)); gap: 2px; min-width: 620px; }
.ptable-wrap { overflow: auto; }
.el {
  min-height: 40px; padding: 2px; font-size: .72rem; display: flex; flex-direction: column; align-items: center; justify-content: center;
  border-width: 1px; line-height: 1.1;
}
.el b { font-size: .95rem; }
.el[data-cat~="metal"] { background: #d9e3ef; }
.el[data-cat~="nonmetal"] { background: #e3efd9; }
.el[data-cat~="noble"] { background: #ecdff2; }
.el[data-cat~="metalloid"] { background: #f2ecd6; }
.el[data-cat~="unknown"] { background: #e2e0da; }
:root[data-theme="dark"] .el[data-cat~="metal"] { background: #2d3a4a; }
:root[data-theme="dark"] .el[data-cat~="nonmetal"] { background: #2f4029; }
:root[data-theme="dark"] .el[data-cat~="noble"] { background: #3e2f48; }
:root[data-theme="dark"] .el[data-cat~="metalloid"] { background: #47402a; }
:root[data-theme="dark"] .el[data-cat~="unknown"] { background: #393a3d; }
.el[aria-pressed="true"] { outline: 3px solid var(--accent); outline-offset: -2px; }
.el-detail { margin-top: 12px; background: var(--paper); border: 1px solid var(--lo); padding: 10px 14px; }
.el-detail dl { display: grid; grid-template-columns: max-content 1fr; gap: 2px 14px; margin: 0; }
.el-detail dt { font-weight: 700; }
.legend-row { display: flex; flex-wrap: wrap; gap: 10px; font-size: .8rem; margin: 6px 0; }
.legend-row span::before { content: ""; display: inline-block; width: 12px; height: 12px; margin-right: 4px; vertical-align: -1px; border: 1px solid var(--lo); background: var(--sw); }

/* ---------- Forms ---------- */
fieldset { border: 2px groove var(--hi); margin: 0 0 12px; padding: 8px 12px 10px; }
legend { font-weight: 700; padding: 0 4px; }
.field-row { display: flex; flex-wrap: wrap; gap: 6px 16px; }
.field-row label, .check { display: inline-flex; align-items: center; gap: 6px; min-height: 32px; }
select, input[type="number"] { font: inherit; padding: 4px 6px; background: var(--paper); color: var(--ink); border: 2px solid; border-color: var(--lo) var(--hi) var(--hi) var(--lo); min-height: 34px; }
input[type="number"] { width: 5.5em; }
.radio-cards { display: grid; gap: 6px; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }
.radio-cards label { display: flex; gap: 8px; align-items: flex-start; padding: 8px; background: var(--paper); border: 1px solid var(--lo); }
.radio-cards small { display: block; color: var(--ink-2); }

/* ---------- Results ---------- */
.result-stats { display: grid; grid-template-columns: max-content 1fr; gap: 4px 16px; margin: 8px 0; }
.result-stats dt { color: var(--ink-2); }
.result-stats dd { margin: 0; font-weight: 700; }
.banner { background: var(--paper); border: 1px solid var(--accent); padding: 8px 12px; margin: 6px; font-size: .92rem; display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.banner p { margin: 0; flex: 1 1 220px; }

/* ---------- Study ---------- */
.study-area { padding: 12px; background: var(--face); min-width: min(100vw - 40px, 620px); max-width: 760px; }

/* ---------- Motion ---------- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001ms !important; animation-iteration-count: 1 !important; transition-duration: .001ms !important; }
}
:root[data-motion="reduce"] *, :root[data-motion="reduce"] *::before, :root[data-motion="reduce"] *::after {
  animation-duration: .001ms !important; animation-iteration-count: 1 !important; transition-duration: .001ms !important;
}

@media (max-width: 560px) {
  :root { --tile-size: 34px; }
  .desktop { padding: 0 0 12px; }
  .app-window { width: 100%; }
  .menubar button { padding: 4px 8px; }
  .tool span:not(.cost):not(.badge) { font-size: .85rem; }
}
@media (forced-colors: active) {
  .tile, button { border-color: ButtonText; }
  .tile.hint { outline: 3px solid Highlight; }
}

/* Notebook shell */
.notebook-shell { display: flex; flex-direction: column; min-height: 0; flex: 1; overflow: hidden; background: var(--face); }
.notebook-panel { flex: 1; min-height: 0; overflow: auto; display: flex; flex-direction: column; background: var(--paper); border-top: 2px solid var(--lo); }
.notebook-panel > .notebook-body { flex: 1; min-height: 50dvh; }
dialog.wide { height: min(86dvh, 820px); }
.notebook-page h4 { margin: .8em 0 .2em; font-size: .95rem; }
.achievements { list-style: none; padding: 0; display: grid; gap: 8px; }
.unlock { font-weight: 600; color: var(--accent); }
.report { margin-top: 12px; }
.report textarea { width: 100%; font: inherit; font-size: .85rem; background: var(--paper); color: var(--ink); }
.calc-tools { margin-top: 10px; }
.units { font-size: 1.05rem; }
