/* ---------- colour tokens: light is the default ---------- */
:root {
  color-scheme: light;
  --bg: #f6f8f7;
  --card: #ffffff;
  --surface: #ffffff;      /* inputs, grid cells, buttons */
  --surface-2: #f1f4f2;    /* grid headers, sticky cells */
  --surface-3: #fafcfb;    /* subtle panels, placeholders */
  --hover: #f2f5f3;
  --code-bg: #eef2f0;
  --ink: #1d2b26;
  --text-soft: #33443d;
  --text-body: #26362f;
  --muted: #62736c;
  --line: #d9e1dd;
  --chip-edge: #b9c6c0;
  --accent: #5b4bdb;       /* the brand colour: indigo */
  --accent-dark: #4636b8;  /* used for text and links */
  --accent-hover: #4636b8; /* used for button hover backgrounds */
  --accent-soft: #ecebfb;
  --good: #2f9e63;         /* success only: ticks, completed, correct answers */
  --good-dark: #1f7a49;
  --good-soft: #e3f4ea;
  --ok-edge: #b6e0c7;
  --bad: #c5392b;
  --bad-soft: #fdecea;
  --bad-edge: #f3c3bd;
  --hint-bg: #fff8e1;
  --hint-edge: #f1dc92;
  --sel: #d8ecfb;
  --sel-edge: #1a73e8;
  --sel-fill: rgba(26, 115, 232, .13);
  --hero-from: #f1effd;
  --band-plain: #ffffff;
  --band-alt: #efedfa;
  --deep-a: #3a2d9e;
  --deep-b: #6a58f0;
  --cap-from: #f4f2fe;
  --cap-badge: #1d2b26;
  --shadow-sm: rgba(20, 60, 40, .1);
  --shadow-lg: rgba(15, 80, 50, .12);
}

/* ---------- dark theme: chosen explicitly with the toggle ... ---------- */
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121212;
  --card: #1c1c1e;
  --surface: #202022;
  --surface-2: #2a2a2d;
  --surface-3: #18181a;
  --hover: #2c2c2f;
  --code-bg: #2a2a2d;
  --ink: #ececee;
  --text-soft: #c8c8cc;
  --text-body: #d4d4d8;
  --muted: #9a9aa1;
  --line: #333336;
  --chip-edge: #48484c;
  --accent-dark: #a99dff;
  --accent-hover: #4a3cc0;
  --accent-soft: #232042;
  --good: #2f9e63;
  --good-dark: #5fd596;
  --good-soft: #14301f;
  --ok-edge: #2a5a3d;
  --bad: #ff8f80;
  --bad-soft: #351b18;
  --bad-edge: #6b2f28;
  --hint-bg: #2b2813;
  --hint-edge: #5c5326;
  --sel: #1d3a52;
  --sel-edge: #6cb0ff;
  --sel-fill: rgba(108, 176, 255, .17);
  --hero-from: #17171a;
  --band-plain: #121212;
  --band-alt: #1f1f24;
  --deep-a: #2a2078;
  --deep-b: #4636b8;
  --cap-from: #1a1a1d;
  --cap-badge: #3a3a3e;
  --syntax-bg: #0f0f10;
  --shadow-sm: rgba(0, 0, 0, .45);
  --shadow-lg: rgba(0, 0, 0, .55);
}
/* ... or automatically from the system setting when nothing has been chosen */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121212;
    --card: #1c1c1e;
    --surface: #202022;
    --surface-2: #2a2a2d;
    --surface-3: #18181a;
    --hover: #2c2c2f;
    --code-bg: #2a2a2d;
    --ink: #ececee;
    --text-soft: #c8c8cc;
    --text-body: #d4d4d8;
    --muted: #9a9aa1;
    --line: #333336;
    --chip-edge: #48484c;
    --accent-dark: #a99dff;
    --accent-hover: #4a3cc0;
    --accent-soft: #232042;
    --good: #2f9e63;
    --good-dark: #5fd596;
    --good-soft: #14301f;
    --ok-edge: #2a5a3d;
    --bad: #ff8f80;
    --bad-soft: #351b18;
    --bad-edge: #6b2f28;
    --hint-bg: #2b2813;
    --hint-edge: #5c5326;
    --sel: #1d3a52;
    --sel-edge: #6cb0ff;
    --sel-fill: rgba(108, 176, 255, .17);
    --hero-from: #17171a;
    --band-plain: #121212;
    --band-alt: #1f1f24;
    --deep-a: #2a2078;
    --deep-b: #4636b8;
    --cap-from: #1a1a1d;
    --cap-badge: #3a3a3e;
    --syntax-bg: #0f0f10;
    --shadow-sm: rgba(0, 0, 0, .45);
    --shadow-lg: rgba(0, 0, 0, .55);
  }
}
input, select, button { color: inherit; }
* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
.top {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 10px 20px; background: var(--card); border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; font-size: 18px; letter-spacing: .2px; }
.logo { display: inline-grid; place-items: center; width: 24px; height: 24px; margin-right: 2px; border-radius: 7px; background: linear-gradient(135deg, var(--deep-a), var(--deep-b)); color: #fff; font-weight: 800; font-size: 15px; line-height: 1; vertical-align: -5px; }
.lesson-pick { color: var(--muted); font-size: 13px; display: flex; align-items: center; gap: 8px; }
.lesson-pick select { font: inherit; padding: 6px 8px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }

.layout { display: grid; grid-template-columns: minmax(300px, 380px) 1fr; gap: 20px; padding: 20px; max-width: 1300px; margin: 0 auto; }
.panel, .sheet { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 18px; }
.panel { align-self: start; }
.lesson-title { font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); font-weight: 600; }
.stepper { list-style: none; display: flex; gap: 6px; padding: 0; margin: 10px 0 4px; }
.stepper li button {
  width: 26px; height: 26px; border-radius: 50%; border: 2px solid var(--line); background: var(--surface);
  font-size: 12px; font-weight: 600; color: var(--muted); cursor: pointer; padding: 0;
}
.stepper li button[disabled] { cursor: default; opacity: .5; }
.stepper li button.current { border-color: var(--accent); color: var(--accent-dark); }
.stepper li button.done { background: var(--good); border-color: var(--good); color: #fff; }
.step-count { font-size: 12px; color: var(--muted); }
h2 { margin: 6px 0 8px; font-size: 20px; }
.prompt { margin-bottom: 12px; }
code, kbd {
  background: var(--code-bg); border: 1px solid var(--line); border-radius: 5px; padding: 1px 5px;
  font-family: ui-monospace, Consolas, monospace; font-size: .92em;
}
kbd { border-bottom-width: 2px; }
.hint { background: var(--hint-bg); border: 1px solid var(--hint-edge); border-radius: 10px; padding: 8px 12px; margin-bottom: 12px; font-size: 14px; }
.feedback { min-height: 0; margin-bottom: 12px; }
.feedback .msg { border-radius: 10px; padding: 9px 12px; font-size: 14px; }
.feedback .ok { background: var(--good-soft); color: var(--good-dark); border: 1px solid var(--ok-edge); }
.feedback .no { background: var(--bad-soft); color: var(--bad); border: 1px solid var(--bad-edge); }
.actions { display: flex; flex-wrap: wrap; gap: 8px; }
.btn { font: inherit; padding: 8px 14px; border-radius: 9px; border: 1px solid var(--line); background: var(--surface); cursor: pointer; }
.btn:hover { background: var(--hover); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.primary:hover { background: var(--accent-hover); }
.btn.ghost { color: var(--muted); }
.link { background: none; border: 0; color: var(--muted); font: inherit; font-size: 13px; text-decoration: underline; cursor: pointer; margin-top: 14px; padding: 0; }

.toolbar { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.tb-group { display: inline-flex; flex-wrap: wrap; align-items: center; gap: 6px; padding-left: 8px; border-left: 1px solid var(--line); }
.tb-group[hidden] { display: none; }
.tb-label { font-size: 12px; color: var(--muted); margin-left: 2px; }
.tb.sw { min-width: 28px; width: 28px; padding: 0; font-weight: 700; }
.tb.sw.x { color: var(--muted); font-weight: 400; }
.tb.on { background: var(--accent-soft); border-color: var(--accent); }
.tb { font: inherit; min-width: 34px; height: 32px; padding: 0 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); cursor: pointer; }
.tb:hover { background: var(--hover); }
.fbar { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.namebox { min-width: 64px; text-align: center; padding: 4px 8px; border: 1px solid var(--line); border-radius: 6px; font-size: 13px; background: var(--surface-3); }
.fx { color: var(--muted); font-style: italic; font-family: serif; }
#formulaBar { flex: 1; font: inherit; font-size: 14px; padding: 5px 8px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); cursor: text; }

.grid-wrap { overflow: auto; border: 1px solid var(--line); border-radius: 8px; outline: none; max-height: 520px; }
.grid-wrap:focus-visible { box-shadow: 0 0 0 2px var(--sel-edge); }
table#grid { border-collapse: collapse; user-select: none; font-size: 14px; }
#grid th {
  background: var(--surface-2); color: var(--muted); font-weight: 500; font-size: 12px;
  border: 1px solid var(--line); padding: 3px 6px; position: sticky; top: 0; z-index: 1; cursor: pointer;
}
#grid th.rowh { position: sticky; left: 0; top: auto; min-width: 36px; z-index: 1; }
#grid th.corner { left: 0; z-index: 2; cursor: default; }
#grid td {
  border: 1px solid var(--line); min-width: 96px; width: 96px; height: 28px; padding: 0 6px;
  white-space: nowrap; overflow: hidden; text-overflow: clip; background: var(--surface); position: relative;
}
#grid td.num { text-align: right; }
#grid td.err { text-align: center; color: var(--bad); }
#grid td.bold { font-weight: 700; }
/* Selection: a translucent fill, one border around the whole range, and a white active cell inside it. */
#grid td.sel {
  background-image: linear-gradient(var(--sel-fill), var(--sel-fill));
  box-shadow: var(--e-t, 0 0 #0000), var(--e-b, 0 0 #0000), var(--e-l, 0 0 #0000), var(--e-r, 0 0 #0000);
}
#grid td.e-t { --e-t: inset 0 2px 0 0 var(--sel-edge); }
#grid td.e-b { --e-b: inset 0 -2px 0 0 var(--sel-edge); }
#grid td.e-l { --e-l: inset 2px 0 0 0 var(--sel-edge); }
#grid td.e-r { --e-r: inset -2px 0 0 0 var(--sel-edge); }
#grid td.active { outline: 2px solid var(--sel-edge); outline-offset: -2px; background: var(--surface); }
#grid.multi td.active { outline: none; }
#grid th.hl { background: var(--sel); color: var(--ink); }
/* Editing: the editor sits above the grid, grows with the text and casts a soft shadow. */
#grid td.editing { padding: 0; overflow: visible; outline: none; box-shadow: none; }
#grid td input { width: 100%; height: 100%; border: 0; outline: 0; font: inherit; padding: 0 6px; background: var(--surface); color: var(--ink); }
#grid td.editing input { position: absolute; left: 0; top: 0; min-width: 100%; z-index: 4; outline: 2px solid var(--sel-edge); outline-offset: -1px; box-shadow: 0 4px 14px var(--shadow-sm); }
.tip { color: var(--muted); font-size: 13px; margin: 10px 2px 0; }

@media (max-width: 820px) {
  .layout { grid-template-columns: 1fr; padding: 12px; }
  #grid td { min-width: 80px; width: 80px; }
}

/* tools added for the interface lessons */
#grid td.cb { text-align: center; font-size: 18px; cursor: pointer; line-height: 1; }
#grid td.frz-b { border-bottom: 3px solid var(--sel-edge); }
#grid td.frz-r { border-right: 3px solid var(--sel-edge); }
#grid .fbtn, #grid .dvbtn { position: absolute; right: 3px; top: 50%; transform: translateY(-50%); font-size: 11px; color: var(--muted); border: 1px solid var(--line); border-radius: 3px; padding: 0 4px; background: var(--surface-2); cursor: pointer; }
#grid .fbtn.on { background: var(--accent-soft); color: var(--accent-dark); border-color: var(--accent); }
.grid-wrap { position: relative; }
.fpop { position: absolute; z-index: 6; min-width: 180px; max-height: 280px; display: flex; flex-direction: column; background: var(--surface); border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 6px 20px var(--shadow-sm); padding: 8px; font-size: 14px; }
.fpop-list { overflow: auto; display: flex; flex-direction: column; gap: 4px; margin-bottom: 8px; }
.fpop-list label { display: flex; align-items: center; gap: 6px; cursor: pointer; }
.fpop-actions { display: flex; gap: 6px; }
.fpop-actions .btn { padding: 4px 10px; font-size: 13px; }
dialog.ask { border: 1px solid var(--line); border-radius: 12px; padding: 18px 20px; background: var(--surface); color: var(--ink); min-width: 300px; }
dialog.ask::backdrop { background: rgba(0, 0, 0, .35); }
dialog.ask h3 { margin: 0 0 12px; }
.ask-row { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; font-size: 14px; }
.ask-row.inline { flex-direction: row; align-items: center; gap: 8px; }
.ask-row input[type=text], .ask-row select { font: inherit; padding: 6px 8px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); color: var(--ink); }
.ask-actions { display: flex; gap: 8px; margin-top: 6px; }
.chart-box { margin-top: 12px; border: 1px solid var(--line); border-radius: 8px; padding: 8px 12px 4px; background: var(--surface); }
.chart-box[hidden] { display: none; }
.chart-svg { width: 100%; max-width: 440px; height: auto; display: block; margin: 0 auto; }
.chart-svg .ch-title { font-size: 14px; font-weight: 600; fill: var(--ink); }
.chart-svg .ch-lab { font-size: 10px; fill: var(--muted); }
.chart-svg .ch-axis { stroke: var(--line); stroke-width: 1; }

/* grid items may shrink below the width of their content, so the sheet scrolls inside its box instead of widening the page */
.layout > * { min-width: 0; }

/* several sheets: tabs under the grid */
.sheet-tabs { display: flex; gap: 2px; padding: 6px 6px 0; border: 1px solid var(--line); border-top: 0; background: var(--surface-3, var(--surface-2)); border-radius: 0 0 8px 8px; overflow-x: auto; }
.sheet-tab { font: inherit; font-size: 13px; padding: 6px 14px; border: 1px solid transparent; border-bottom: 0; border-radius: 6px 6px 0 0; background: transparent; color: var(--muted); cursor: pointer; white-space: nowrap; }
.sheet-tab:hover { color: var(--ink); }
.sheet-tab.on { background: var(--surface); color: var(--accent-dark); border-color: var(--line); font-weight: 600; box-shadow: inset 0 -2px 0 var(--accent); }
.dvpop { display: flex; flex-direction: column; min-width: 140px; padding: 4px; }
.dv-item { text-align: left; font: inherit; font-size: 13px; padding: 6px 10px; border: 0; background: transparent; color: var(--ink); border-radius: 4px; cursor: pointer; }
.dv-item:hover { background: var(--accent-soft); color: var(--accent-dark); }
