:root {
  --bg: #0e1116;
  --bg-elev: #161b23;
  --bg-elev-2: #1c232d;
  --line: #262e3a;
  --line-soft: #1e2530;
  --text: #e6edf6;
  --text-dim: #93a1b5;
  --text-faint: #6b7a8f;
  --accent: #4f8ef7;
  --accent-soft: rgba(79, 142, 247, .14);
  --good: #3fbf7f;
  --bad: #f2555a;
  --warn: #e0a53a;
  --radius: 10px;
  --radius-sm: 7px;
  --shadow: 0 10px 30px rgba(0, 0, 0, .45);
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f6fa;
    --bg-elev: #ffffff;
    --bg-elev-2: #eef1f6;
    --line: #dde3ec;
    --line-soft: #e8ecf3;
    --text: #17202e;
    --text-dim: #566274;
    --text-faint: #8792a3;
    --accent: #2b6fe0;
    --accent-soft: rgba(43, 111, 224, .1);
    --good: #10864f;
    --bad: #cf2f37;
    --warn: #9a6b0d;
    --shadow: 0 10px 30px rgba(20, 32, 55, .14);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

/* ---------------------------------------------------------- topbar */

.topbar {
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 12px clamp(12px, 2.5vw, 20px);
  background: var(--bg-elev);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 30;
}

.brand { display: flex; align-items: center; gap: 9px; font-weight: 650; letter-spacing: -.01em; }
.brand-mark { font-size: 20px; }
.brand-name { font-size: 15px; }

.tabs { display: flex; gap: 2px; }

.tab {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text-dim);
  font: inherit;
  font-weight: 550;
  padding: 7px 14px;
  border-radius: var(--radius-sm);
  cursor: pointer;
}
.tab:hover { color: var(--text); background: var(--bg-elev-2); }
.tab.is-active { color: var(--accent); background: var(--accent-soft); }

/* ------------------------------------------------------- filterbar */

.filterbar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  padding: 12px clamp(12px, 2.5vw, 20px);
  background: var(--bg);
  border-bottom: 1px solid var(--line-soft);
  position: sticky;
  top: 53px;
  z-index: 20;
}
.spacer { flex: 1 1 auto; }

.seg {
  display: inline-flex;
  background: var(--bg-elev-2);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 2px;
  gap: 2px;
}
.seg button {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text-dim);
  font: inherit;
  font-weight: 550;
  padding: 5px 13px;
  border-radius: 5px;
  cursor: pointer;
}
.seg button:hover { color: var(--text); }
.seg button.is-active { background: var(--accent); color: #fff; }
.seg.small button { padding: 5px 11px; min-width: 38px; }

select, input, .search {
  background: var(--bg-elev);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 7px 10px;
  font: inherit;
}
select:focus-visible, input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

.search { display: flex; align-items: center; gap: 7px; padding: 0 10px; min-width: 300px; }
.search svg { width: 15px; height: 15px; flex: none; fill: none; stroke: var(--text-faint); stroke-width: 2; stroke-linecap: round; }
.search input { border: 0; background: transparent; padding: 7px 0; flex: 1; min-width: 0; }
.search input:focus-visible { outline: 0; }
.search:focus-within { border-color: var(--accent); }

.btn {
  appearance: none;
  font: inherit;
  font-weight: 600;
  padding: 7px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--line);
  background: var(--bg-elev);
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: var(--bg-elev-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { color: var(--text-dim); }
.btn.ghost:hover { color: var(--text); }

/* ----------------------------------------------------------- views */

main { padding: clamp(12px, 2vw, 18px) clamp(12px, 2.5vw, 20px) 60px; }

.statrow {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 10px;
  margin-bottom: 16px;
}
.stat {
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 11px 14px;
}
.stat .k { color: var(--text-faint); font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .05em; }
.stat .v { font-size: 19px; font-weight: 650; margin-top: 3px; font-variant-numeric: tabular-nums; letter-spacing: -.02em; }
.statrow.big .stat .v { font-size: 23px; }
.stat .v.good { color: var(--good); }
.stat .v.bad { color: var(--bad); }
.stat .sub { color: var(--text-faint); font-size: 12px; margin-top: 2px; }

/* ---------------------------------------------------------- tables */

.tablewrap {
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow-x: auto;
}

table.grid { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }

table.grid th {
  text-align: left;
  font-size: 11px;
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--text-faint);
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
  position: sticky;
  top: 0;
  background: var(--bg-elev);
}
table.grid td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--line-soft);
  white-space: nowrap;
}
table.grid tbody tr:last-child td { border-bottom: 0; }
table.grid tbody tr:hover { background: var(--bg-elev-2); }
table.grid.compact td, table.grid.compact th { padding: 8px 12px; }

.num { text-align: right; }
td.name { white-space: normal; min-width: 220px; max-width: 380px; font-weight: 550; }
td.doc { font-family: var(--mono); font-size: 12px; color: var(--text-dim); }
td.muted, .muted { color: var(--text-faint); }
.pos { color: var(--good); font-weight: 600; }
.neg { color: var(--bad); font-weight: 600; }

.chip {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 650;
  background: var(--bg-elev-2);
  border: 1px solid var(--line);
  color: var(--text-dim);
}
.chip.ok { color: var(--good); border-color: color-mix(in srgb, var(--good) 40%, transparent); background: color-mix(in srgb, var(--good) 12%, transparent); }
.chip.part { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 40%, transparent); background: color-mix(in srgb, var(--warn) 12%, transparent); }

.rowactions { display: flex; gap: 4px; justify-content: flex-end; }
.iconbtn {
  appearance: none;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-faint);
  border-radius: 5px;
  padding: 4px 8px;
  font: inherit;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}
.iconbtn:hover { background: var(--bg-elev-2); border-color: var(--line); color: var(--text); }
.iconbtn.danger:hover { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 45%, transparent); }

.empty { padding: 44px 20px; text-align: center; color: var(--text-faint); }
.empty strong { display: block; color: var(--text-dim); margin-bottom: 4px; font-size: 15px; }

.panels { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: 16px; }
.panel h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .05em; color: var(--text-faint); margin: 0 0 8px; }

/* --------------------------------------------------------- dialogs */

.sheet {
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--bg-elev);
  color: var(--text);
  padding: 0;
  width: min(560px, calc(100vw - 32px));
  box-shadow: var(--shadow);
}
.sheet::backdrop { background: rgba(4, 7, 12, .62); backdrop-filter: blur(2px); }

.sheet header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px;
  border-bottom: 1px solid var(--line);
}
.sheet header h2 { margin: 0; font-size: 16px; font-weight: 650; }
.sheet .x { appearance: none; border: 0; background: transparent; color: var(--text-faint); font-size: 22px; line-height: 1; cursor: pointer; padding: 0 4px; }
.sheet .x:hover { color: var(--text); }

.sheet .body { padding: 18px; display: flex; flex-direction: column; gap: 14px; max-height: min(66vh, 620px); overflow-y: auto; }
.sheet footer { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 18px; border-top: 1px solid var(--line); }

.row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field > span { font-size: 12px; font-weight: 650; color: var(--text-dim); }
.field input, .field select { width: 100%; }
.field input[readonly] { color: var(--text-faint); font-family: var(--mono); font-size: 12px; }
.hint { margin: 0; font-size: 11.5px; color: var(--text-faint); }

.inputgroup { display: flex; gap: 8px; align-items: stretch; }
.inputgroup input { flex: 1; min-width: 0; }

.totals {
  background: var(--bg-elev-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-variant-numeric: tabular-nums;
}
.totals .line { display: flex; justify-content: space-between; color: var(--text-dim); }
.totals .line.total { color: var(--text); font-weight: 700; font-size: 15px; border-top: 1px solid var(--line); padding-top: 8px; margin-top: 2px; }
.totals .line.total .pos, .totals .line.total .neg { font-size: 15px; }

/* ----------------------------------------------------------- toast */

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 11px 16px;
  max-width: min(560px, calc(100vw - 32px));
  z-index: 60;
  font-weight: 550;
}
.toast.err { border-left-color: var(--bad); }
.toast.ok { border-left-color: var(--good); }

/* responsive rules live at the end of this file */

/* ----------------------------------------------------------- login */

.loginwrap {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.loginbox {
  width: min(380px, 100%);
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 26px 24px;
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.loginbrand {
  display: flex;
  align-items: center;
  gap: 9px;
  justify-content: center;
  font-weight: 650;
  font-size: 16px;
  margin-bottom: 6px;
}

.loginerror {
  margin: 0;
  padding: 9px 12px;
  border-radius: var(--radius-sm);
  background: color-mix(in srgb, var(--bad) 12%, transparent);
  border: 1px solid color-mix(in srgb, var(--bad) 40%, transparent);
  color: var(--bad);
  font-size: 13px;
  font-weight: 550;
}

.btn.block { width: 100%; padding: 10px 14px; }
.btn:disabled { opacity: .6; cursor: default; }

/* --------------------------------------------------------- account */

.account { display: flex; align-items: center; gap: 9px; margin-left: auto; }
.account .who { color: var(--text-dim); font-size: 13px; font-weight: 550; }

/* ------------------------------------------------------ calculator */

.calcgrid {
  display: grid;
  grid-template-columns: minmax(320px, 420px) 1fr;
  gap: 16px;
  align-items: start;
}

.panel.card {
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.panel.card h2 { margin: 0; font-size: 13px; text-transform: uppercase; letter-spacing: .05em; color: var(--text-faint); }

.rule { border: 0; border-top: 1px solid var(--line); margin: 2px 0; width: 100%; }

.check { display: flex; align-items: flex-start; gap: 9px; cursor: pointer; font-weight: 550; }
.check input { width: 16px; height: 16px; margin: 2px 0 0; flex: none; accent-color: var(--accent); }

.inputgroup .suffix {
  display: flex;
  align-items: center;
  padding: 0 11px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--bg-elev-2);
  color: var(--text-dim);
  font-weight: 650;
}

/* results waterfall */

.waterfall { display: flex; flex-direction: column; gap: 1px; font-variant-numeric: tabular-nums; }
.waterfall .wf {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 16px;
  padding: 8px 0;
}
.waterfall .wf .lbl { color: var(--text-dim); }
.waterfall .wf .val { font-weight: 600; white-space: nowrap; }
.waterfall .wf.sub .lbl { padding-left: 14px; color: var(--text-faint); font-size: 13px; }
.waterfall .wf.sub .val { color: var(--text-faint); font-weight: 550; font-size: 13px; }
.waterfall .wf.rule-top { border-top: 1px solid var(--line); margin-top: 4px; padding-top: 11px; }
.waterfall .wf.grand {
  border-top: 2px solid var(--line);
  margin-top: 6px;
  padding-top: 13px;
  font-size: 19px;
}
.waterfall .wf.grand .lbl { color: var(--text); font-weight: 650; }
.waterfall .wf.grand .val { font-size: 22px; letter-spacing: -.02em; }

.kpis {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-top: 4px;
}
.kpis .stat { padding: 10px 12px; background: var(--bg-elev-2); }

.calcnote {
  margin: 0;
  font-size: 12px;
  color: var(--text-faint);
  border-top: 1px solid var(--line);
  padding-top: 12px;
  line-height: 1.55;
}

@media (max-width: 900px) {
  .calcgrid { grid-template-columns: 1fr; }
}

/* ------------------------------------------------------- pre-orders */

.preorderbox {
  border: 1px dashed var(--line);
  border-radius: var(--radius);
  padding: 13px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: var(--bg-elev-2);
}

.preordersection { margin-bottom: 18px; }
.preordersection > h2 {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--text-faint);
  margin: 0 0 8px;
}
.preordersection > h2 .muted { text-transform: none; letter-spacing: 0; font-weight: 500; }
.preordersection .stat { border-style: dashed; }
.preordersection .calcnote { border: 0; padding-top: 8px; }

/* A pre-ordered row is money out on something not in hand yet. */
.chip.pre {
  color: var(--accent);
  border-color: color-mix(in srgb, var(--accent) 45%, transparent);
  background: color-mix(in srgb, var(--accent) 14%, transparent);
}
tr.is-preorder td { background: color-mix(in srgb, var(--accent) 4%, transparent); }

/* ----------------------------------------------------------- trades */

.tradepicker {
  max-height: 190px;
  overflow-y: auto;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--bg-elev);
  padding: 6px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.tradepicker label {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 6px 8px;
  border-radius: 5px;
  cursor: pointer;
  font-size: 13px;
}
.tradepicker label:hover { background: var(--bg-elev-2); }
.tradepicker input { width: 15px; height: 15px; margin: 0; flex: none; accent-color: var(--accent); }
.tradepicker .po { font-family: var(--mono); font-size: 11.5px; color: var(--text-dim); flex: none; }
.tradepicker .nm { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tradepicker .bs { color: var(--text-faint); font-variant-numeric: tabular-nums; flex: none; }
.tradepicker .none { padding: 10px; color: var(--text-faint); font-size: 13px; }

.tradeitem {
  display: grid;
  grid-template-columns: 1fr 62px 92px 34px;
  gap: 6px;
  margin-bottom: 6px;
  align-items: center;
}
.tradeitem input { padding: 6px 8px; font-size: 13px; }
.tradeitem .x {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--bg-elev);
  color: var(--text-faint);
  border-radius: 5px;
  height: 32px;
  cursor: pointer;
  font-size: 15px;
  line-height: 1;
}
.tradeitem .x:hover { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 45%, transparent); }

.chip.trade {
  color: var(--warn);
  border-color: color-mix(in srgb, var(--warn) 45%, transparent);
  background: color-mix(in srgb, var(--warn) 14%, transparent);
}

/* ==================================================================== */
/* responsive                                                           */
/*                                                                      */
/* The ledger tables carry 14 columns, which no phone can show as a     */
/* table. Below the table breakpoint each row becomes a card and every  */
/* cell prints its own label from data-label, so nothing is dropped —   */
/* the same data, stacked instead of scrolled sideways.                 */
/* ==================================================================== */

/* ---- tablet: fewer, roomier columns in the grids ---- */

@media (max-width: 1100px) {
  .statrow { grid-template-columns: repeat(auto-fit, minmax(132px, 1fr)); }
  .panels { grid-template-columns: 1fr; }
}

/* ---- stacked-card tables ---- */

/* A small paired input that sits under its parent field rather than beside it,
   so the ACO row does not get three controls wide on a phone. */
.field.inline {
  flex-direction: row;
  align-items: center;
  gap: 10px;
  margin-top: 8px;
}
.field.inline > span { flex: none; margin: 0; }
.field.inline > input { flex: none; width: 88px; }

/* ---- aco fee picker ---------------------------------------------- */

.acolist {
  /* The dialog body is a flex column, so without flex:none the list gets
     squeezed to a sliver the moment "Other" expands below it. Let it keep its
     height and scroll the body instead. */
  flex: none;
  max-height: 46vh;
  overflow-y: auto;
  margin: 4px 0 2px;
}

.acogroup + .acogroup { margin-top: 18px; }
.acogroup h3 {
  margin: 0;
  font-size: 12px;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--text-dim);
}
.acogroup .hint { margin: 2px 0 8px; }

.acorow {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 14px;
  width: 100%;
  padding: 9px 11px;
  text-align: left;
  color: var(--text);
  background: var(--bg-elev-2);
  border: 1px solid var(--line-soft);
  border-radius: var(--radius-sm);
  cursor: pointer;
  font: inherit;
}
.acorow + .acorow { margin-top: 5px; }
.acorow:hover, .acorow:focus-visible {
  border-color: var(--accent);
  background: var(--accent-soft);
  outline: none;
}

.aconame { flex: 1; min-width: 0; }
.acofee { flex: none; text-align: right; font-variant-numeric: tabular-nums; }
.acofee small { display: block; color: var(--text-faint); font-size: 11px; }

.tag {
  display: inline-block;
  margin-left: 6px;
  padding: 1px 6px;
  font-size: 10px;
  letter-spacing: .05em;
  text-transform: uppercase;
  color: var(--accent);
  background: var(--accent-soft);
  border-radius: 999px;
}

#acoOther summary { cursor: pointer; color: var(--accent); padding: 4px 0; }
#acoOther[open] summary { margin-bottom: 10px; }
#acoNewSave { margin-top: 10px; }

@media (max-width: 700px) {
  /* Basis toggle + amount + Search fee list will not fit one phone row — the
     amount box gets crushed to a couple of characters. Let the button drop
     under them instead. */
  .acogroup-row { flex-wrap: wrap; }
  .acogroup-row > input { flex: 1 1 90px; min-width: 90px; }
  .acogroup-row > [data-aco-search] { flex: 1 0 100%; }

  .acolist { max-height: 40vh; }
  /* The fee has to stay readable next to a long product name on a phone. */
  .acorow { flex-direction: column; gap: 2px; }
  .acofee { text-align: left; }
  .acofee small { display: inline; margin-left: 6px; }
}

@media (max-width: 860px) {
  .tablewrap {
    background: transparent;
    border: 0;
    border-radius: 0;
    overflow-x: visible;
  }

  table.grid thead { display: none; }
  table.grid,
  table.grid tbody,
  table.grid tr,
  table.grid td { display: block; width: 100%; }

  table.grid tr {
    background: var(--bg-elev);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    margin-bottom: 10px;
    padding: 4px 0;
  }
  table.grid tbody tr:hover { background: var(--bg-elev); }

  table.grid td {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 14px;
    padding: 7px 13px;
    border: 0;
    text-align: right;
    white-space: normal;
    min-width: 0;
    max-width: none;
  }
  table.grid td + td { border-top: 1px solid var(--line-soft); }

  table.grid td::before {
    content: attr(data-label);
    flex: none;
    text-align: left;
    color: var(--text-faint);
    font-size: 11px;
    font-weight: 650;
    text-transform: uppercase;
    letter-spacing: .05em;
  }

  /* The name is the card's heading, so it gets the full width and no label. */
  table.grid td.name {
    display: block;
    text-align: left;
    font-size: 15px;
    padding-top: 11px;
  }
  table.grid td.name::before { content: none; }

  /* Actions become full-width buttons, comfortably tappable. */
  table.grid td.actions { padding: 9px 13px 11px; }
  table.grid td.actions::before { content: none; }
  .rowactions { justify-content: stretch; gap: 8px; }
  .rowactions .iconbtn {
    flex: 1;
    padding: 9px 8px;
    border-color: var(--line);
    font-size: 13px;
  }

  /* The empty state is a single cell — it must not grow a label. */
  table.grid td[colspan]::before { content: none; }
  table.grid td[colspan] { display: block; text-align: center; padding: 0; }
  table.grid tr:has(td[colspan]) { padding: 0; }
}

/* ---- phone ---- */

@media (max-width: 700px) {
  .topbar {
    gap: 10px;
    /* The tabs scroll rather than wrap, so the bar stays one row tall. */
    flex-wrap: nowrap;
    overflow-x: auto;
    scrollbar-width: none;
  }
  .topbar::-webkit-scrollbar { display: none; }
  .brand-name { display: none; }
  .tabs { flex: none; }
  .tab { padding: 7px 11px; }
  .account { gap: 6px; }
  .account .who { display: none; }

  /* Not sticky on a phone: two stacked sticky bars eat most of the screen. */
  .filterbar { position: static; }

  .search { min-width: 0; flex: 1 1 100%; order: -1; }

  /* Filters collapse behind a toggle; search and Add stay reachable. */
  .filterbar .filter-collapse { display: none; }
  .filterbar.is-open select.filter-collapse { display: block; }
  .filterbar.is-open .seg.filter-collapse { display: inline-flex; }
  .filterbar select { flex: 1 1 calc(50% - 5px); min-width: 0; }
  .filterbar .seg { flex: 1 1 100%; justify-content: center; }
  .spacer { display: none; }
  .filterbar .btn { flex: 1 1 auto; justify-content: center; text-align: center; }

  .statrow { grid-template-columns: repeat(auto-fit, minmax(118px, 1fr)); gap: 8px; }
  .stat { padding: 9px 11px; }
  .stat .v { font-size: 17px; }
  .statrow.big .stat .v { font-size: 19px; }

  /* Dialogs fill the screen instead of floating in a cramped box. */
  .sheet {
    width: 100vw;
    max-width: 100vw;
    height: 100dvh;
    max-height: 100dvh;
    margin: 0;
    border: 0;
    border-radius: 0;
  }
  .sheet .body { max-height: none; flex: 1; }
  .sheet form { display: flex; flex-direction: column; height: 100dvh; }
  .sheet header { position: sticky; top: 0; background: var(--bg-elev); z-index: 2; }
  .sheet footer {
    position: sticky;
    bottom: 0;
    background: var(--bg-elev);
    padding-bottom: max(14px, env(safe-area-inset-bottom));
  }
  .sheet footer .btn { flex: 1; padding: 11px 14px; }

  .tradeitem { grid-template-columns: 1fr 56px 84px 34px; gap: 5px; }
  .calcgrid { gap: 12px; }
  .kpis { grid-template-columns: 1fr; }

  .toast { left: 12px; right: 12px; bottom: 12px; transform: none; max-width: none; }
}

/* ---- touch and small-screen input behaviour ---- */

@media (max-width: 700px) {
  /*
   * 16px is the threshold below which iOS Safari zooms the page on focus,
   * which then leaves the layout scrolled sideways. Worth the extra pixels.
   */
  input, select, textarea { font-size: 16px; }
  .field input, .field select { padding: 10px 11px; }
  .seg button { padding: 8px 13px; }
  .check input { width: 18px; height: 18px; }
}

@media (hover: none) {
  /* Nothing on a touch screen should depend on hovering to be discoverable. */
  .iconbtn { color: var(--text-dim); border-color: var(--line); }
  table.grid tbody tr:hover { background: inherit; }
}
