/* SoftN Calc
 * Brand: petrol #12343b, gold #c08a2a, warm off-white. Manrope + JetBrains Mono.
 * Modern CSS: cascade layers, native nesting, container queries, :has(), color-mix(), dvh,
 * media range syntax, logical properties. Baseline: Safari 16.5+, Chrome/Edge 112+, Firefox 117+.
 * Mobile first. Page-level layout uses media queries; components adapt with container queries. */

@layer reset, tokens, base, layout, components, tape, meta;

/* ================= Reset ================= */
@layer reset {
  *, *::before, *::after { box-sizing: border-box; }
  html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
  body, h1, h2, p, dl, dd, ol { margin: 0; }
  ol { padding: 0; list-style: none; }
  button, input, select { font: inherit; color: inherit; }
  button { cursor: pointer; -webkit-tap-highlight-color: transparent; }
  [hidden] { display: none !important; }
}

/* ================= Design tokens (light + dark) ================= */
@layer tokens {
  :root {
    --petrol: #12343b;
    --petrol-2: #1a4650;
    --petrol-3: #23545e;
    --gold: #c08a2a;
    --gold-hi: #d9a446;
    --gold-soft: #f4e8d0;
    --gold-ink: #1b1407;
    --bg: #f7f4ee;
    --surface: #ffffff;
    --surface-2: #f2ede3;
    --field: #fbfaf7;
    --text: #1c2a2d;
    --muted: #5d6a6c;
    --line: #e5ddcf;
    --on-petrol: #f5f0e7;
    --on-petrol-muted: #a8bfc2;
    --pos: #1e8449;
    --neg: #c0392b;
    --pos-on-petrol: #7fe0a8;
    --neg-on-petrol: #ff9d91;
    --wa: #1f9d55;
    --on-signal: #ffffff; /* text on green/red buttons */
    --shadow: 0 1px 2px rgb(18 52 59 / .06), 0 4px 16px rgb(18 52 59 / .06);
    --panel-border: transparent;
    --radius: 14px;
    --radius-sm: 10px;
    --gutter: max(16px, env(safe-area-inset-left), env(safe-area-inset-right));
    --font: "Manrope", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    --mono: "JetBrains Mono", ui-monospace, "Cascadia Mono", Consolas, monospace;
    --sticky-top: 104px;
    color-scheme: light;
  }
  /* Dark: follows the phone/PC setting unless the visitor picked a theme with the button. */
  @media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
      --gold: #d29b39; --gold-hi: #e3b25a; --gold-soft: #3a2f1b;
      --bg: #0c1719; --surface: #132327; --surface-2: #1a2e33; --field: #0f1d20;
      --text: #e9e5dc; --muted: #97a8ab; --line: #24393e;
      --pos: #5fd394; --neg: #ff8a7d; --on-signal: #0c1719;
      --shadow: 0 1px 2px rgb(0 0 0 / .3), 0 6px 20px rgb(0 0 0 / .25);
      --panel-border: var(--petrol-3);
      color-scheme: dark;
    }
  }
  :root[data-theme="dark"] {
    --gold: #d29b39; --gold-hi: #e3b25a; --gold-soft: #3a2f1b;
    --bg: #0c1719; --surface: #132327; --surface-2: #1a2e33; --field: #0f1d20;
    --text: #e9e5dc; --muted: #97a8ab; --line: #24393e;
    --pos: #5fd394; --neg: #ff8a7d; --on-signal: #0c1719;
    --shadow: 0 1px 2px rgb(0 0 0 / .3), 0 6px 20px rgb(0 0 0 / .25);
    --panel-border: var(--petrol-3);
    color-scheme: dark;
  }
}

/* ================= Base ================= */
@layer base {
  body {
    min-block-size: 100vh;
    min-block-size: 100dvh;
    display: flex;
    flex-direction: column;
    background: var(--bg);
    color: var(--text);
    font: 500 15px/1.45 var(--font);
    -webkit-font-smoothing: antialiased;
    font-variant-numeric: tabular-nums;
  }
  main { flex: 1; }
  h2 { font-size: 15px; font-weight: 800; margin-block-end: 12px; letter-spacing: .01em; }
  a { color: var(--gold); }
  :focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }
  input[type="text"] {
    inline-size: 100%;
    min-block-size: 48px;
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    border: 1.5px solid var(--line);
    background: var(--field);
    font-family: var(--mono);
    font-size: 17px;
    font-weight: 500;
    transition: border-color .15s, box-shadow .15s;

    &:focus { border-color: var(--gold); outline: none; box-shadow: 0 0 0 3px color-mix(in srgb, var(--gold) 22%, transparent); }
    &::placeholder { color: color-mix(in srgb, var(--muted) 60%, transparent); }
  }
  /* iPhone Safari zooms in on fields under 16px: keep them at 16px on touch screens. */
  @media (pointer: coarse) {
    input[type="text"], select { font-size: max(16px, 1em) !important; }
  }
  kbd {
    font-family: var(--mono); font-size: 11.5px; padding: 1px 5px;
    border: 1px solid var(--line); border-block-end-width: 2px; border-radius: 4px; background: var(--surface);
  }
  @media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
  }
}

/* ================= Page layout ================= */
@layer layout {
  .wrap { inline-size: 100%; max-inline-size: 1180px; margin-inline: auto; padding-inline: var(--gutter); }
  @media (width < 360px) { :root { --gutter: 10px; } }

  /* Header */
  .topbar {
    background: var(--petrol);
    color: var(--on-petrol);
    position: sticky;
    inset-block-start: 0;
    z-index: 10;
    padding-block-start: env(safe-area-inset-top);
  }
  .topbar-inner {
    max-inline-size: 1180px; margin-inline: auto; padding: 8px var(--gutter) 2px;
    display: flex; align-items: center; justify-content: space-between;
  }
  .tabs {
    max-inline-size: 1180px; margin-inline: auto; padding-inline: max(0px, calc(var(--gutter) - 8px));
    display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none;
  }

  /* Calculator panels */
  .panel { padding-block: 16px 24px; }
  .split { display: grid; gap: 16px; }
  .inputs { display: grid; gap: 16px; align-content: start; }

  /* Tablet portrait, half-screen laptop windows and up: inputs left, result right. */
  @media (width >= 760px) {
    .panel { padding-block-start: 20px; }
    .split {
      grid-template-columns: minmax(0, 1fr) minmax(300px, 360px);
      align-items: start;
      gap: 20px;
      & .result { position: sticky; inset-block-start: calc(var(--sticky-top) + 16px); }
    }
  }
  @media (width >= 1024px) {
    .panel { padding-block-start: 24px; }
    .split { grid-template-columns: minmax(0, 1fr) 420px; gap: 24px; }
  }
  /* Short screens (phone landscape, small windows): nothing sticky, so everything can scroll. */
  @media (height < 560px) {
    .topbar { position: static; }
    .split .result { position: static !important; }
  }

  .footer {
    display: flex; flex-wrap: wrap; justify-content: space-between; gap: 6px 16px;
    padding-block: 8px max(24px, env(safe-area-inset-bottom));
    font-size: 13px; color: var(--muted);
    & a { color: inherit; font-weight: 700; }
  }
}

/* ================= Shared components ================= */
@layer components {
  /* Header bits */
  .brand {
    display: flex; align-items: center; gap: 10px; color: inherit; text-decoration: none;
    & .brand-mark { inline-size: 30px; block-size: 30px; flex: none; }
    & .brand-name { font-size: 18px; font-weight: 600; letter-spacing: .01em; }
    & b { color: var(--gold-hi); font-weight: 800; }
  }
  .icon-btn {
    inline-size: 44px; block-size: 44px; display: grid; place-items: center;
    border: 0; border-radius: 12px; background: transparent; color: var(--on-petrol);
    &:hover { background: var(--petrol-2); }
    & svg { inline-size: 22px; block-size: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; }
  }
  .i-moon { display: none; }
  :root[data-theme="dark"] { & .i-sun { display: none; } & .i-moon { display: block; } }
  @media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) { & .i-sun { display: none; } & .i-moon { display: block; } }
  }

  .tab {
    flex: 1 1 0; text-align: center; padding: 12px 6px 11px; white-space: nowrap;
    color: var(--on-petrol-muted); text-decoration: none; font-weight: 700; font-size: 14px;
    border-block-end: 3px solid transparent; transition: color .15s, border-color .15s;
    &:hover { color: var(--on-petrol); }
    &[aria-selected="true"] { color: var(--on-petrol); border-block-end-color: var(--gold); }
    & .short { display: none; }
    @media (width < 480px) {
      & .long { display: none; }
      & .short { display: inline; }
    }
    @media (width >= 720px) { flex: 0 0 auto; padding-inline: 18px; }
  }

  /* Cards: each one is a size container, so its insides adapt to its own width. */
  .card {
    container: card / inline-size;
    background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
    padding: 16px; box-shadow: var(--shadow);
    @media (width < 360px) { padding: 12px; }
  }
  .card-head {
    display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px 12px; margin-block-end: 12px;
    & h2 { margin: 0; }
  }

  /* Fields */
  .field {
    display: block; margin-block-end: 12px;
    &:last-child { margin-block-end: 0; }
  }
  .label { display: block; font-size: 13px; font-weight: 700; color: var(--muted); margin-block-end: 6px; }
  .input-wrap {
    position: relative; display: block;
    & input[type="text"] { padding-inline-end: 70px; }
  }
  .suffix, .prefix {
    position: absolute; inset-block-start: 50%; translate: 0 -50%; pointer-events: none;
    font-size: 13px; font-weight: 700; color: var(--muted);
  }
  .suffix { inset-inline-end: 12px; }
  .prefix { inset-inline-start: 12px; font-family: var(--mono); font-size: 15px; }
  .input-wrap.prefix-wrap input[type="text"] { padding-inline: 40px 12px; }

  .grid-2, .grid-4 { display: grid; gap: 12px 10px; & .field { margin-block-end: 0; } }
  .grid-2 {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    & .input-wrap input[type="text"] { padding-inline-end: 58px; }
    & .suffix { font-size: 11.5px; inset-inline-end: 10px; }
  }
  .grid-4 {
    grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px;
    & input[type="text"] { text-align: center; padding-inline: 4px; }
  }
  @container card (width < 300px) {
    .grid-2 { grid-template-columns: 1fr; }
    .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  }

  .hint { font-size: 12.5px; color: var(--muted); margin-block-start: 10px; text-wrap: pretty; }
  .muted { color: var(--muted); font-weight: 500; }
  .mono { font-family: var(--mono); }
  .convert {
    margin-block-start: 12px; padding: 10px 12px; border-radius: var(--radius-sm); min-block-size: 42px;
    background: var(--gold-soft); color: var(--text); font-family: var(--mono); font-size: 14px;
    &:empty { display: none; }
  }
  .check {
    display: flex; align-items: center; gap: 10px; min-block-size: 44px; margin-block-start: 4px;
    font-weight: 700; cursor: pointer;
    & input { inline-size: 22px; block-size: 22px; accent-color: var(--gold); margin: 0; }
  }

  /* Segmented toggle */
  .seg {
    display: inline-flex; max-inline-size: 100%; gap: 2px; padding: 3px;
    background: var(--surface-2); border-radius: var(--radius-sm);
    & button {
      border: 0; background: transparent; padding: 8px 12px; border-radius: 8px; min-block-size: 38px;
      font-size: 13px; font-weight: 700; color: var(--muted);
      &[aria-pressed="true"] { background: var(--petrol); color: var(--on-petrol); }
    }
  }
  @container card (width < 330px) { .seg button { padding-inline: 8px; font-size: 12px; } }

  /* A label with a small Dr/Cr switch on the right */
  .label-row {
    display: flex; align-items: flex-end; justify-content: space-between; gap: 8px; margin-block-end: 6px;
    & .label { margin: 0; }
  }
  .seg-sm { padding: 2px; & button { min-block-size: 32px; padding: 4px 14px; font-size: 12.5px; font-weight: 800; } }

  /* Chips */
  .chips { display: flex; flex-wrap: wrap; gap: 8px; }
  .chip {
    min-block-size: 40px; min-inline-size: 52px; padding: 6px 12px; border-radius: 999px;
    border: 1.5px solid var(--line); background: var(--surface); font-weight: 700; font-size: 14px;
    &:hover { border-color: var(--gold); }
    &[aria-pressed="true"] { background: var(--gold); border-color: var(--gold); color: var(--gold-ink); }
  }
  .karats { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 6px; margin-block: 4px 14px; }
  .karat {
    display: flex; flex-direction: column; align-items: center; gap: 2px; min-block-size: 56px; padding: 8px 2px;
    border-radius: var(--radius-sm); border: 1.5px solid var(--line); background: var(--surface);
    & b { font-size: 15px; }
    & span { font-family: var(--mono); font-size: 11.5px; color: var(--muted); }
    &:hover { border-color: var(--gold); }
    &[aria-pressed="true"] { background: var(--gold); border-color: var(--gold); color: var(--gold-ink); & span { color: #3b2b0c; } }
  }
  @container card (width < 330px) {
    .karats { gap: 4px; }
    .karat span { font-size: 10px; }
  }

  /* Result panel (petrol) */
  .result {
    container: result / inline-size;
    background: var(--petrol); color: var(--on-petrol);
    border: 1px solid var(--panel-border); border-radius: var(--radius);
    padding: 18px 16px; box-shadow: var(--shadow);
  }
  .result-top { display: flex; flex-direction: column; gap: 2px; padding-block-end: 14px; border-block-end: 1px solid var(--petrol-3); }
  .result-label { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .08em; color: var(--on-petrol-muted); }
  .result-sub { font-size: 13px; color: var(--on-petrol-muted); }
  .big {
    font-family: var(--mono); font-weight: 700; line-height: 1.15; color: var(--gold-hi); overflow-wrap: anywhere;
    font-size: clamp(26px, 9.5cqi, 40px);
    &.pos { color: var(--pos-on-petrol); }
    &.neg { color: var(--neg-on-petrol); }
  }
  .rows {
    margin-block: 8px 14px;
    & > div { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; padding-block: 7px; }
    & dt { color: var(--on-petrol-muted); font-size: 14px; }
    & dd { font-family: var(--mono); font-size: 15px; text-align: end; }
    & .sep { border-block-start: 1px dashed var(--petrol-3); margin-block-start: 4px; padding-block-start: 11px; }
    & .strong { & dt { color: var(--on-petrol); font-weight: 700; } & dd { font-weight: 700; } }
    & .pos { color: var(--pos-on-petrol); }
    & .neg { color: var(--neg-on-petrol); }
    & .off { opacity: .55; }
    & small.move { display: block; font-size: 11.5px; color: var(--on-petrol-muted); font-weight: 500; }
  }
  @container result (width < 330px) { .rows dt { font-size: 13px; } .rows dd { font-size: 14px; } }

  .actions { display: flex; flex-wrap: wrap; gap: 8px; }
  .btn {
    flex: 1 1 auto; min-block-size: 46px; padding: 10px 14px; border-radius: var(--radius-sm);
    border: 1.5px solid transparent; font-weight: 800; font-size: 14px; transition: background .15s, filter .15s;
  }
  .btn-gold { background: var(--gold); color: var(--gold-ink); &:hover { background: var(--gold-hi); } }
  .btn-wa { background: var(--wa); color: #fff; &:hover { filter: brightness(1.08); } }
  .btn-ghost {
    flex-grow: 0; background: transparent; border-color: var(--petrol-3); color: var(--on-petrol);
    &:hover { background: var(--petrol-2); }
    &.armed { background: var(--neg); border-color: var(--neg); color: #fff; flex-grow: 1; }
  }
  .btn-small {
    min-block-size: 38px; padding: 6px 14px; border-radius: var(--radius-sm); border: 1.5px solid var(--gold);
    background: transparent; font-weight: 800; font-size: 13.5px;
    &:hover { background: var(--gold); color: var(--gold-ink); }
  }
  /* The buy/sell result inside its card */
  .whatif { margin-block-start: 14px; padding: 14px; & .rows { margin-block-end: 0; } & .note { margin-block-start: 8px; } }
  .note { margin-block-start: 14px; font-size: 12.5px; color: var(--on-petrol-muted); text-wrap: pretty; }

  .toast {
    position: fixed; inset-inline-start: 50%; inset-block-end: calc(20px + env(safe-area-inset-bottom));
    translate: -50% 20px; opacity: 0; pointer-events: none; z-index: 50;
    max-inline-size: calc(100% - 32px); padding: 10px 18px; border-radius: 999px;
    background: var(--text); color: var(--bg); font-weight: 700; font-size: 14px; text-align: center;
    transition: opacity .2s, translate .2s;
    &.show { opacity: 1; translate: -50% 0; }
  }

  @keyframes flash { from { background: var(--gold-soft); } to { background: transparent; } }
}

/* ================= Tape ================= */
@layer tape {
  .tape-layout {
    display: grid; gap: 12px;
    grid-template-areas: "totals" "main" "pad";
    grid-template-rows: auto minmax(200px, 1fr) auto;
  }
  .tape-totals {
    grid-area: totals; display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; padding: 12px 14px;
    & .tt-current { display: flex; flex-direction: column; min-inline-size: 0; }
    & .big { font-size: clamp(24px, 8cqi, 38px); }
    & .tt-small {
      display: grid; gap: 4px; text-align: end;
      & > div { display: flex; flex-direction: column; }
      & output { font-size: 15px; font-weight: 700; }
      & .result-label { font-size: 10.5px; }
    }
  }
  .tape-main {
    container: tape / inline-size;
    grid-area: main; padding: 0; display: flex; flex-direction: column; overflow: hidden;
  }
  .tape-pad { grid-area: pad; container: pad / inline-size; }

  /* Toolbar */
  .tape-toolbar {
    display: flex; align-items: center; gap: 2px; padding: 6px;
    border-block-end: 1px solid var(--line); background: var(--surface-2);
  }
  .tool {
    flex: 1 1 0; display: flex; align-items: center; justify-content: center; gap: 6px; min-block-size: 40px;
    border: 0; border-radius: 8px; background: transparent; font-size: 13px; font-weight: 700; white-space: nowrap;
    &:hover:not(:disabled) { background: var(--surface); }
    &:disabled { opacity: .4; cursor: default; }
    & svg { inline-size: 18px; block-size: 18px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
    &.armed { background: var(--neg); color: #fff; flex-grow: 2.5; }
  }
  .vat-rate {
    display: flex; align-items: center; gap: 6px; flex: none; padding-inline: 6px 0;
    font-size: 12px; font-weight: 800; color: var(--muted);
    & .input-wrap { inline-size: 66px; }
    & .input-wrap input[type="text"] { min-block-size: 36px; font-size: 15px; padding: 4px 22px 4px 8px; text-align: end; background: var(--surface); }
    & .suffix { inset-inline-end: 8px; font-size: 12px; }
  }
  @container tape (width < 520px) {
    .tool span { display: none; }
    .tool.armed span { display: inline; }
    .tool.armed ~ .vat-rate { display: none; }
  }

  /* Paper: every line, the subtotals and the line being typed sit on ruled rows. */
  .tape-list {
    --row: 42px;
    --rule: color-mix(in srgb, var(--line) 70%, transparent);
    --num-col: minmax(84px, 132px);
    flex: 1; min-block-size: 140px; max-block-size: 46vh; overflow-y: auto; padding-block: 4px 16px;
    overscroll-behavior: contain; cursor: text;
    background: var(--surface) repeating-linear-gradient(to bottom, transparent 0 calc(var(--row) - 1px), var(--rule) calc(var(--row) - 1px) var(--row));
    background-attachment: local;
  }
  @container tape (width >= 520px) { .tape-list { --num-col: 160px; } }

  .t-line, .t-entry-row, .t-sub {
    display: grid; align-items: center; column-gap: 4px;
    grid-template-columns: 30px var(--num-col) minmax(0, 1fr) auto 30px;
    min-block-size: var(--row); padding-inline: 6px 4px;
    background: var(--surface); border-block-end: 1px solid var(--rule);
    cursor: default;
    &.flash { animation: flash .8s ease-out; }

    /* Clean paper: no boxes. Writing shows only the caret; editing a line, a thin gold underline. */
    & input[type="text"] {
      min-block-size: 32px; padding: 2px 6px; border: 0; border-radius: 0;
      background: transparent; font-size: 16px; box-shadow: none; outline: none;
      &:focus, &:focus-visible { background: transparent; outline: none; box-shadow: inset 0 -2px 0 var(--gold); }
    }
    & .t-num { text-align: end; font-weight: 600; letter-spacing: .01em; }
    & .t-note {
      font-family: var(--font); font-size: 15px; font-weight: 600; color: var(--muted);
      &::placeholder { color: transparent; }
    }
    &:hover .t-note::placeholder, & .t-note:focus::placeholder { color: color-mix(in srgb, var(--muted) 45%, transparent); }
  }

  .t-run { font-family: var(--mono); font-size: 12.5px; color: var(--muted); text-align: end; white-space: nowrap; padding-inline-start: 4px; }
  .t-preview { font-style: italic; opacity: .75; }
  @container tape (width < 380px) {
    .t-run { font-size: 11px; }
    .t-line, .t-entry-row, .t-sub { column-gap: 2px; padding-inline: 4px 2px; }
  }

  .sign {
    inline-size: 28px; block-size: 30px; border-radius: 6px; border: 1px solid transparent; background: transparent;
    font-family: var(--mono); font-size: 19px; font-weight: 700; line-height: 1; color: var(--petrol);
    &:hover { border-color: var(--gold); background: var(--surface); }
    &.op-minus, &.op-div { color: var(--neg); }
  }
  :root[data-theme="dark"] .sign:not(.op-minus, .op-div) { color: var(--gold-hi); }
  @media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .sign:not(.op-minus, .op-div) { color: var(--gold-hi); } }

  .t-del {
    inline-size: 30px; block-size: 30px; border: 0; border-radius: 6px; background: transparent;
    color: var(--muted); font-size: 19px; line-height: 1; opacity: .45;
    &:hover, &:focus-visible { background: var(--surface-2); color: var(--neg); opacity: 1; }
  }
  @media (hover: hover) {
    .t-del { opacity: 0; }
    .t-line:hover .t-del, .t-sub:hover .t-del { opacity: .6; }
  }

  /* Subtotal: a double rule over the number, the result in green, its own comment. */
  .t-sub {
    & .sign-eq { text-align: center; font-family: var(--mono); font-size: 19px; font-weight: 700; color: var(--muted); }
    & .t-sub-val {
      align-self: stretch; display: grid; align-content: center; justify-content: end; padding-inline: 6px;
      border-block-start: 4px double var(--text);
      font-family: var(--mono); font-weight: 800; font-size: 18px; color: var(--pos);
    }
    & .t-note { color: var(--text); font-weight: 800; text-transform: none; }
    & .t-note::placeholder { color: var(--pos) !important; opacity: .8; text-transform: uppercase; letter-spacing: .06em; font-size: 13px; }
  }
  .t-use {
    min-block-size: 34px; padding: 2px 8px; border-radius: 6px; border: 0;
    background: transparent; color: var(--gold); font-size: 12.5px; font-weight: 800;
    text-decoration: underline dotted; text-underline-offset: 3px;
    &:hover { background: var(--gold-soft); text-decoration: none; }
  }

  /* The line being typed: highlighted, with a gold bar, directly under the last line. */
  .t-entry-row {
    background: color-mix(in srgb, var(--gold) 9%, var(--surface));
    box-shadow: inset 3px 0 0 var(--gold);
    cursor: text;
    &:focus-within { background: color-mix(in srgb, var(--gold) 15%, var(--surface)); }
    & .t-num { font-size: 17px; font-weight: 800; }
    & input[type="text"]:focus { box-shadow: none; } /* the row highlight already shows where you write */
    & .t-note::placeholder { color: color-mix(in srgb, var(--muted) 45%, transparent) !important; }
    & .flash-field { animation: flash .7s ease-out; }
  }
  /* Grand total: the last line of the paper, pinned to the bottom while the tape scrolls. */
  .t-grand-row {
    position: sticky; inset-block-end: -16px; z-index: 1;
    display: grid; align-items: center; column-gap: 4px;
    grid-template-columns: 30px var(--num-col) minmax(0, 1fr);
    min-block-size: calc(var(--row) + 4px); padding-inline: 6px 4px; margin-block-start: 6px;
    background: var(--surface); border-block-start: 1px solid var(--rule);
    & .sign-eq { text-align: center; font-family: var(--mono); font-size: 19px; font-weight: 700; color: var(--muted); }
    & .t-grand-val {
      align-self: stretch; display: grid; align-content: center; justify-content: end; padding-inline: 6px;
      border-block-start: 4px double var(--text);
      font-family: var(--mono); font-weight: 800; font-size: 20px; color: var(--petrol);
    }
    & .t-grand-label { padding-inline: 6px; font-size: 13px; font-weight: 800; text-transform: uppercase; letter-spacing: .06em; color: var(--petrol); }
  }
  :root[data-theme="dark"] .t-grand-row :is(.t-grand-val, .t-grand-label) { color: var(--gold-hi); }
  @media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .t-grand-row :is(.t-grand-val, .t-grand-label) { color: var(--gold-hi); } }

  .tape-hint {
    padding: 10px 12px 10px 42px; font-size: 13px; color: var(--muted); line-height: 1.5; text-wrap: pretty;
    & i { font-style: normal; font-family: var(--mono); color: var(--text); }
  }

  /* Keypad */
  .keypad { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 6px; }
  .k {
    min-block-size: clamp(40px, 5.8dvh, 56px);
    border-radius: var(--radius-sm); border: 1px solid var(--line); background: var(--surface);
    font-family: var(--mono); font-size: 19px; font-weight: 600; box-shadow: 0 1px 0 var(--line);
    touch-action: manipulation; user-select: none; -webkit-user-select: none;
    &:hover { border-color: var(--gold); }
    &:active { translate: 0 1px; background: var(--surface-2); }
  }
  .k-mem { min-block-size: clamp(32px, 4.4dvh, 44px); font-family: var(--font); font-size: 13.5px; font-weight: 800; background: var(--surface-2); color: var(--muted); }
  .k-fn { font-size: 16px; background: var(--surface-2); }
  .k-op {
    background: var(--petrol); color: var(--on-petrol); border-color: var(--petrol); font-size: 22px;
    &:hover { background: var(--petrol-3); }
    &:active { background: var(--petrol-2); }
  }
  .k-vat { font-family: var(--font); font-size: 12px; font-weight: 800; color: var(--gold); background: var(--surface-2); line-height: 1.15; }
  .k-enter {
    grid-column: span 3; font-family: var(--font); font-size: 15px; font-weight: 800;
    background: var(--petrol-3); color: var(--on-petrol); border-color: var(--petrol-3);
    &:hover { background: var(--petrol-2); }
  }
  .k-eq {
    grid-column: span 2; background: var(--gold); border-color: var(--gold); color: var(--gold-ink); font-size: 24px; font-weight: 700;
    &:hover { background: var(--gold-hi); }
  }
  @container pad (width < 330px) {
    .keypad { gap: 4px; }
    .k { font-size: 17px; }
    .k-vat { font-size: 10.5px; }
    .k-mem { font-size: 12px; }
  }
  .tape-help { display: none; }
  @media (hover: hover) and (pointer: fine) { .tape-help { display: block; } }

  /* Mid widths in one column: keep the keypad and totals from stretching too wide. */
  @media (560px <= width < 760px) {
    .tape-pad, .tape-totals { inline-size: 100%; max-inline-size: 520px; justify-self: center; }
  }

  /* Tablet and wider: tape left, totals + keypad right. */
  @media (width >= 760px) {
    .tape-layout {
      grid-template-columns: minmax(0, 1fr) 340px;
      grid-template-areas: "main totals" "main pad";
      grid-template-rows: auto 1fr; align-items: start; gap: 16px 20px;
    }
    .tape-totals {
      flex-direction: column; align-items: stretch; padding: 18px 16px;
      & .tt-small {
        grid-template-columns: 1fr 1fr; text-align: start; padding-block-start: 12px; border-block-start: 1px solid var(--petrol-3);
        & output { font-size: 17px; }
      }
    }
    .tape-list { min-block-size: 300px; max-block-size: calc(100dvh - 300px); }
  }
  @media (width >= 1024px) { .tape-layout { grid-template-columns: minmax(0, 1fr) 380px; gap: 16px 24px; } }

  /* App-like tape on any screen tall enough: the page fits the screen, only the tape scrolls. */
  @media (height >= 560px) {
    body:has(#panel-tape:not([hidden])) {
      block-size: 100dvh;
      & main { display: flex; flex-direction: column; min-block-size: 0; }
      & .footer { display: none; }
      & #panel-tape { flex: 1; min-block-size: 0; display: flex; flex-direction: column; padding-block: 10px max(10px, env(safe-area-inset-bottom)); }
      & .tape-layout { flex: 1; min-block-size: 0; }
      & .tape-main { block-size: 100%; min-block-size: 200px; } /* tiny screens scroll a little instead of hiding the tape */
      & .tape-list { min-block-size: 96px; max-block-size: none; }
    }
  }
  @media (height >= 560px) and (width < 760px) {
    body:has(#panel-tape:not([hidden])) {
      & .topbar-inner { padding-block: 4px 0; }
      & .tab { padding-block: 9px 8px; }
      & .tape-layout { gap: 8px; }
      & .tape-totals { padding: 8px 12px; }
      & .tape-toolbar { padding: 4px; }
      & .tool { min-block-size: 36px; }
      & .tape-list { --row: 40px; }
      & .keypad { gap: 5px; }
    }
  }
  @media (height >= 560px) and (width >= 760px) {
    body:has(#panel-tape:not([hidden])) {
      & #panel-tape { padding-block: 20px; }
      & .tape-layout { grid-template-rows: auto minmax(0, 1fr); }
      & .tape-main { grid-row: 1 / span 2; }
    }
  }
}

/* ================= MT5 margin ================= */
@layer meta {
  .positions {
    container: positions / inline-size;
    display: grid; gap: 10px;
  }
  .positions-empty {
    padding: 16px; text-align: center; color: var(--muted); font-size: 14px;
    border: 1.5px dashed var(--line); border-radius: 12px;
  }
  .position {
    border: 1.5px solid var(--line); border-inline-start-width: 4px; border-radius: 12px; padding: 10px; background: var(--field);
    &.buy { border-inline-start-color: var(--pos); }
    &.sell { border-inline-start-color: var(--neg); }
    & select {
      inline-size: 100%; min-block-size: 40px; padding: 6px 8px; border-radius: 9px; border: 1.5px solid var(--line);
      background: var(--surface); font: 700 14px var(--font);
      &:focus { border-color: var(--gold); outline: none; }
    }
    & .seg button { padding: 6px 10px; min-block-size: 34px; }
    & [data-side="buy"][aria-pressed="true"] { background: var(--pos); color: var(--on-signal); }
    & [data-side="sell"][aria-pressed="true"] { background: var(--neg); color: var(--on-signal); }
  }
  .pos-head { display: grid; grid-template-columns: minmax(0, 1fr) auto 36px; gap: 6px; align-items: center; margin-block-end: 8px; }
  .pos-del {
    inline-size: 36px; block-size: 36px; border: 0; border-radius: 8px; background: transparent;
    color: var(--muted); font-size: 22px; line-height: 1;
    &:hover { background: var(--surface-2); color: var(--neg); }
  }
  .pos-fields {
    display: grid; grid-template-columns: .8fr 1fr 1fr; gap: 6px;
    & label { display: block; min-inline-size: 0; }
    & .label { font-size: 11.5px; margin-block-end: 3px; }
    & input[type="text"] { min-block-size: 40px; padding: 6px 8px; font-size: 15px; background: var(--surface); }
    &.custom { grid-template-columns: .8fr 1fr 1fr 1fr; }
  }
  @container positions (width < 440px) {
    .pos-fields { grid-template-columns: 64px minmax(0, 1fr) minmax(0, 1fr); gap: 4px; & input[type="text"] { padding-inline: 5px; } }
    .pos-fields.custom { grid-template-columns: 1fr 1fr; }
  }
  @container positions (width < 300px) {
    .pos-fields { grid-template-columns: 1fr 1fr; }
    .pos-head { grid-template-columns: minmax(0, 1fr) 36px; }
    .pos-head .seg { grid-row: 2; grid-column: 1 / -1; }
  }
  .pos-out {
    display: flex; flex-wrap: wrap; justify-content: space-between; align-items: baseline; gap: 4px 12px;
    margin-block-start: 8px; font-size: 12.5px; color: var(--muted);
    & .mono { color: var(--text); }
    & .pl { font-family: var(--mono); font-size: 17px; font-weight: 700; margin-inline-start: auto; }
  }
  .pos-v { color: var(--pos); }
  .neg-v { color: var(--neg); }
}
