.bt-form { display: flex; flex-wrap: wrap; gap: 12px; align-items: end; }
.bt-form label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: #8a94a6; }
.bt-form input, .bt-form select { background: #0e1116; color: #e6edf3; border: 1px solid #283041; border-radius: 6px; padding: 6px 8px; min-width: 110px; }
.bt-limitations { white-space: pre-wrap; font-size: 11px; margin-top: 10px; }

.candlebox { height: 340px; width: 100%; }
.gatebox { height: 150px; width: 100%; margin-top: 4px; }
.candle-toolbar { display: flex; gap: 12px; flex-wrap: wrap; margin: 6px 0; color: #8a94a6; font-size: 12px; }
.candle-toolbar label { display: inline-flex; align-items: center; gap: 4px; cursor: pointer; }
.candle-note { margin: 4px 0; }

.seg { display: inline-flex; border: 1px solid #283041; border-radius: 6px; overflow: hidden; }
.segbtn { background: transparent; color: #8a94a6; border: 0; padding: 4px 10px; cursor: pointer; font: inherit; }
.segbtn.active { background: #4c8dff; color: #fff; }

:root {
  --bg: #0e1116;
  --panel: #161b22;
  --panel2: #1c2330;
  --border: #283041;
  --text: #d6dde7;
  --muted: #8a94a6;
  --accent: #4c8dff;
  --green: #3fb950;
  --red: #f85149;
  --yellow: #d29922;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}
header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 18px; border-bottom: 1px solid var(--border); background: var(--panel);
  position: sticky; top: 0; z-index: 10;
}
.brand { font-weight: 600; font-size: 16px; }
.muted { color: var(--muted); font-weight: 400; }
.status { display: flex; gap: 8px; align-items: center; }
.pill {
  font-size: 12px; padding: 3px 9px; border-radius: 999px;
  background: var(--panel2); border: 1px solid var(--border); color: var(--muted);
}
.pill.on { color: var(--green); border-color: #1f6f33; }
.pill.off { color: var(--muted); }
.pill.warn { color: var(--yellow); border-color: #6b5310; }
.pill.bad { color: var(--red); border-color: #6e2320; }

.tabs { display: flex; gap: 2px; padding: 0 12px; border-bottom: 1px solid var(--border); background: var(--panel); overflow-x: auto; }
.tab {
  background: none; border: none; color: var(--muted); padding: 11px 16px; cursor: pointer;
  font-size: 14px; border-bottom: 2px solid transparent; white-space: nowrap;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--text); border-bottom-color: var(--accent); }
/* Each tab carries a long and a short label. The short one is mobile-only, so
   it is hidden here and swapped in by the media query at the bottom of this
   file — which means this rule MUST come first: both selectors have the same
   specificity, so whichever is declared last would win. */
.t-sm { display: none; }

main { padding: 16px; max-width: 1500px; margin: 0 auto; }
.panel { display: none; }
.panel.active { display: block; }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 14px; }
.kpi { background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px; }
.kpi .label { color: var(--muted); font-size: 12px; }
.kpi .value { font-size: 22px; font-weight: 600; margin-top: 4px; }

.card { background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 14px 16px; margin-bottom: 14px; }
.card h3 { margin: 0 0 10px; font-size: 14px; font-weight: 600; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
@media (max-width: 900px) { .grid2 { grid-template-columns: 1fr; } }
canvas { max-height: 320px; }

.toolbar { display: flex; gap: 16px; align-items: center; margin-bottom: 12px; flex-wrap: wrap; }
select { background: var(--panel2); color: var(--text); border: 1px solid var(--border); border-radius: 6px; padding: 5px 8px; }

.tablewrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: right; padding: 7px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
th:first-child, td:first-child { text-align: left; }
th { color: var(--muted); font-weight: 500; position: sticky; top: 0; background: var(--panel); }
tbody tr:hover { background: var(--panel2); }
tr.clickable { cursor: pointer; }
/* The mobile row-expander has no purpose at full width — every column is
   already visible there. */
.mexp-cell { display: none; }
tr.selected { background: #1b2740; }

.mono { font-family: var(--mono); font-size: 12px; }
.pos { color: var(--green); }
.neg { color: var(--red); }
.dim { color: var(--muted); }
.tag { font-size: 11px; padding: 1px 7px; border-radius: 6px; background: var(--panel2); border: 1px solid var(--border); }
.tag.green { color: var(--green); } .tag.red { color: var(--red); } .tag.yellow { color: var(--yellow); } .tag.blue { color: var(--accent); }

.bar { height: 7px; background: var(--panel2); border-radius: 4px; overflow: hidden; min-width: 80px; display: inline-block; vertical-align: middle; }
.bar > span { display: block; height: 100%; background: var(--accent); }

.lesson { border-left: 3px solid var(--accent); padding: 8px 12px; margin-bottom: 8px; background: var(--panel2); border-radius: 0 8px 8px 0; }
.lesson .meta { color: var(--muted); font-size: 12px; margin-top: 3px; }

.errbar { position: fixed; bottom: 0; left: 0; right: 0; background: #2b1416; color: var(--red); padding: 8px 16px; border-top: 1px solid #6e2320; font-size: 13px; }
.empty { color: var(--muted); padding: 18px; text-align: center; }

/* pool cell: name + quick links + copyable addresses */
.poolcell .poolname { font-weight: 500; }
.poolcell .links { margin-left: 6px; white-space: nowrap; }
a.lnk { font-size: 11px; padding: 1px 5px; margin-left: 4px; border: 1px solid var(--border); border-radius: 5px; color: var(--accent); text-decoration: none; }
a.lnk:hover { background: var(--panel2); }
.poolcell .addrs { margin-top: 3px; }
.addr { font-family: var(--mono); font-size: 11px; color: var(--muted); cursor: pointer; }
.addr:hover { color: var(--text); text-decoration: underline; }
#toast {
  position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%);
  background: var(--panel2); border: 1px solid var(--border); color: var(--text);
  padding: 7px 14px; border-radius: 8px; font-size: 13px; opacity: 0;
  transition: opacity .2s; pointer-events: none; z-index: 100;
}
#toast.show { opacity: 1; }

/* ---- decision-record panels (transparency) ---- */
.records-row { display: flex; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.record { flex: 1 1 280px; background: var(--panel2); border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; font-size: 13px; }
.rec-title { font-weight: 600; margin-bottom: 6px; }
.rec-sec { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .04em; margin: 8px 0 4px; }
.rec-row { display: flex; justify-content: space-between; gap: 10px; padding: 1px 0; }
.rec-row.neg { color: var(--red); }
.small { font-size: 11px; }
.gate { padding: 2px 0; }
.gate-line { display: flex; align-items: center; gap: 8px; }
.gate .gmark { width: 14px; text-align: center; }
.gate.ok .gmark { color: var(--green); }
.gate.bad .gmark { color: var(--red); }
.gate.bad { color: var(--text); }
.gate .gname { flex: 1; cursor: help; }
.gate .gval { font-family: var(--mono); font-size: 12px; color: var(--muted); }
.gate .ghint { color: var(--accent); font-size: 10px; }
/* Plain-language gate description. It is also in the title attribute, but a
   title needs hover — on a touch screen tapping the gate name is the only way
   to reach it, which is what this block is for. */
.gdoc {
  color: var(--muted); font-size: 12px; line-height: 1.5;
  margin: 4px 0 8px 22px; padding-left: 8px; border-left: 2px solid var(--border);
}
.gate-legend { margin-top: 10px; background: var(--panel2); border: 1px solid var(--border); border-radius: 8px; padding: 6px 12px; font-size: 12px; }
.gate-legend > summary { cursor: pointer; color: var(--muted); }
.gate-legend > summary:hover { color: var(--text); }
.leg-row { display: flex; gap: 10px; padding: 4px 0; border-top: 1px solid var(--border); }
.leg-row:first-of-type { border-top: none; margin-top: 6px; }
.leg-row .gname { flex: 0 0 110px; font-family: var(--mono); color: var(--accent); }
.leg-desc { flex: 1; color: var(--muted); }

/* ---- settings forms ---- */
.btn { background: var(--panel2); color: var(--text); border: 1px solid var(--border); border-radius: 6px; padding: 5px 12px; cursor: pointer; font-size: 13px; }
.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn:disabled { opacity: .4; cursor: not-allowed; }
.btn.danger:hover:not(:disabled) { border-color: var(--red); color: var(--red); }
.btn.save { padding: 4px 10px; }
.restoggle { display: inline-flex; gap: 4px; margin-right: 8px; }
.restoggle .btn { padding: 3px 9px; font-size: 12px; }
.restoggle .btn.on { border-color: var(--accent); color: var(--accent); }
.exec-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
/* Settings groups are <details>; the summary replaces what used to be an h3. */
.setgroup > summary {
  font-size: 14px; font-weight: 600; cursor: pointer; list-style: none;
  display: flex; align-items: center; gap: 8px; padding: 2px 0;
}
.setgroup > summary::-webkit-details-marker { display: none; }
.setgroup > summary::before { content: '▸'; color: var(--muted); font-weight: 400; }
.setgroup[open] > summary::before { content: '▾'; }
.setgroup > summary:hover { color: var(--accent); }
.setgroup[open] > summary { margin-bottom: 10px; }
.setgrid { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 8px 18px; }
.setrow { display: grid; grid-template-columns: 1fr 110px auto; align-items: center; gap: 8px; padding: 3px 0; }
.setrow label { color: var(--text); font-size: 13px; }
.setrow .hint { display: block; color: var(--muted); font-size: 11px; line-height: 1.4; margin-top: 2px; max-width: 46em; }
.setrow input, .setrow select { width: 100%; }
code { font-family: var(--mono); font-size: 12px; background: var(--panel2); padding: 1px 5px; border-radius: 4px; }

/* ---- login overlay ---- */
#loginOverlay { position: fixed; inset: 0; z-index: 1000; display: grid; place-items: center; background: var(--bg); }
/* The id-selector display:grid above outranks the UA's [hidden]{display:none},
   so without this rule the overlay would never hide after a successful login. */
#loginOverlay[hidden] { display: none; }
.login-card { display: flex; flex-direction: column; gap: 12px; min-width: 280px; background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 28px 32px; }
.login-card h2 { margin: 0 0 4px; font-size: 16px; }
.login-card input { background: var(--panel2); color: var(--text); border: 1px solid var(--border); border-radius: 6px; padding: 8px 10px; font-size: 14px; }
.login-card input:focus { outline: none; border-color: var(--accent); }
.login-err { color: var(--red); font-size: 12px; margin: 0; }

/* Manual position close — deliberately understated. It is destructive and
   irreversible, so it should not read as a primary action; it turns red only
   on hover, when the pointer is already committed to it. */
button.close-pos {
  background: transparent;
  border: 1px solid #30363d;
  color: #8a94a6;
  border-radius: 4px;
  padding: 1px 6px;
  font-size: 12px;
  line-height: 1.4;
  cursor: pointer;
}
button.close-pos:hover:not(:disabled) {
  border-color: #f85149;
  color: #f85149;
}
button.close-pos:disabled { opacity: 0.5; cursor: default; }
/* Spelled out only on a phone (see the mobile block), where a bare 20px glyph
   is neither readable nor a safe tap target for an irreversible action. */
button.close-pos .cp-label { display: none; }

/* ==========================================================================
   Mobile / narrow screens
   --------------------------------------------------------------------------
   The dashboard is reachable from the public internet over Tailscale Funnel,
   so it gets read from a phone. One breakpoint, mirrored in app.js by
   isNarrow() / MOBILE_MAX_PX — keep the two numbers in step.

   The governing decision: tables keep their <table> markup and are re-laid-out
   as one card per row here in CSS. Every row handler in app.js is bound to the
   <tr> (row select, manual close), so replacing the markup with <div>s would
   have broken all of them.
   ========================================================================== */
@media (max-width: 700px) {

  /* ---- chrome: the header and the tab strip are both sticky, so every pixel
     is permanently spent out of 812. Measured at 375px this block takes the
     header from 129px to ~90px; the biggest single win is NOT letting the
     generic .btn min-height below apply to the header's logout button. ---- */
  header { padding: 6px 10px; flex-wrap: wrap; gap: 4px 8px; align-items: center; }
  .brand { font-size: 14px; flex: 0 1 auto; white-space: nowrap; }
  .brand .muted { display: none; }        /* the word "admin"; the role pill says it */
  .status { flex: 1 1 auto; flex-wrap: wrap; justify-content: flex-end; gap: 4px 4px; }
  .pill { font-size: 10px; padding: 2px 6px; }
  /* Tight enough that book-switch + 4 pills + logout share a single row at
     375px, which is what keeps the header at two rows instead of four. */
  #statusBar { padding-right: 0; }
  /* Logout is rare and non-destructive, so it does not need a 44px target and
     must not push the sticky header down by 40px. */
  header .btn { min-height: 28px; padding: 4px 9px; font-size: 12px; }
  /* The book switch DOES stay a comfortable target — it is toggled often. */
  .seg .segbtn { padding: 6px 10px; }
  /* The staleness readout matters more on a phone (longer poll interval), but it
     does not deserve a row of sticky header. The brand only needs ~90px, so
     park it in that row's dead space instead. header is position:sticky, i.e.
     already a containing block for this. */
  #refreshNote {
    position: absolute; top: 8px; right: 10px;
    font-size: 10px; text-align: right;
  }

  .tabs {
    padding: 0 6px;
    scroll-snap-type: x proximity;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    /* Fade the trailing edge so it reads as "there is more this way" — with 9
       tabs only about half fit at this width. */
    mask-image: linear-gradient(to right, #000 0, #000 calc(100% - 24px), transparent 100%);
  }
  .tabs::-webkit-scrollbar { display: none; }
  .tab { padding: 10px 12px; font-size: 13px; scroll-snap-align: center; }
  .t-lg { display: none; }
  .t-sm { display: inline; }

  main { padding: 10px 8px; }
  .card { padding: 12px 10px; margin-bottom: 10px; }
  .kpis { grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 8px; }
  .kpi { padding: 10px; }
  .kpi .value { font-size: 19px; }

  /* ---- one card per table row ---- */
  .tablewrap { overflow-x: visible; }
  table, tbody, tr, td { display: block; width: 100%; }
  thead { display: none; }          /* the label lives in td::before instead */
  table { border-collapse: separate; }
  tbody tr {
    position: relative;
    border: 1px solid var(--border); border-radius: 8px;
    background: var(--panel2); padding: 8px 10px; margin-bottom: 8px;
  }
  tbody tr:hover { background: var(--panel2); }   /* no hover state on touch */
  tr.selected { background: #1b2740; border-color: var(--accent); }
  td {
    display: flex; align-items: baseline; justify-content: space-between;
    gap: 12px; padding: 3px 0; border: 0;
    text-align: right; white-space: normal;       /* long reasons wrap, not clip */
  }
  td::before {
    content: attr(data-label);
    color: var(--muted); font-size: 11px; text-align: left;
    flex: 0 0 auto; max-width: 55%;
  }
  /* The action column has no header text; a dangling label would just be an
     empty gap above the button. */
  td[data-label=""] { display: block; text-align: left; }
  td[data-label=""]::before { display: none; }
  /* Folded columns (m: 2 in app.js) appear only once the row is expanded. */
  tbody tr:not(.mexp) td[data-m="2"] { display: none; }
  th { position: static; }

  .mexp-cell { display: block; padding: 0; margin-top: 4px; }
  .mexp-btn {
    width: 100%; min-height: 34px;
    background: transparent; border: 0; border-top: 1px solid var(--border);
    color: var(--muted); font: inherit; font-size: 16px; line-height: 1;
    cursor: pointer;
  }
  tr.mexp .mexp-btn { transform: rotate(180deg); }

  /* ---- touch targets ---- */
  /* Scoped to main so the header's logout does not inflate the sticky header by
     40px — which then means the login card, being outside main, needs saying
     explicitly. It is the first button anyone taps on a phone. */
  main .btn { min-height: 40px; padding: 9px 14px; }
  main .btn.save { min-height: 40px; padding: 9px 14px; }
  .login-card .btn { min-height: 44px; }
  .addr {
    display: inline-block; min-height: 30px; line-height: 30px;
    padding: 0 6px; margin-right: 2px;
    border: 1px solid var(--border); border-radius: 5px;
  }
  a.lnk { display: inline-block; min-height: 30px; line-height: 28px; padding: 0 8px; }
  .gate .gname { min-height: 30px; line-height: 30px; }

  /* Manual close: an irreversible action, so at this size it gets a real
     labelled button instead of a 20px glyph. It is inside a folded column, so
     it is only reachable after an explicit expand. */
  button.close-pos { width: 100%; min-height: 44px; font-size: 14px; }
  button.close-pos .cp-label { display: inline; }

  /* Below 16px, iOS Safari zooms the viewport on focus and leaves the layout
     stranded. Every control that can take focus stays at 16px. */
  input, select, textarea { font-size: 16px; }
  .bt-form input, .bt-form select, .setrow input, .setrow select,
  .login-card input { font-size: 16px; }

  /* ---- settings: give the hint text the full width ---- */
  /* The summary is now the primary way to navigate this tab, so it gets a real
     target rather than the 24px a bare text line came out to. */
  .setgroup > summary { min-height: 40px; }
  .setgrid { grid-template-columns: 1fr; gap: 4px; }
  .setrow {
    grid-template-columns: 1fr auto;
    padding: 8px 0; border-bottom: 1px solid var(--border);
  }
  .setrow label { grid-column: 1 / -1; }
  .setrow .hint { max-width: none; }
  .exec-row { gap: 6px; }

  /* ---- backtest form ---- */
  .bt-form { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; align-items: end; }
  .bt-form label { min-width: 0; }
  .bt-form input, .bt-form select { min-width: 0; width: 100%; }
  /* The grid items are the wrapping <label>s, not the inputs, so the two
     free-text fields are marked in the HTML rather than selected via :has(). */
  .bt-form label.wide { grid-column: 1 / -1; }
  #btRun { grid-column: 1 / -1; }

  /* ---- charts ---- */
  .candlebox { height: 260px; }
  .gatebox { height: 120px; }
  canvas { max-height: 260px; }
  .candle-toolbar { gap: 8px; font-size: 11px; }

  /* ---- records / detail panes ---- */
  .records-row { gap: 8px; }
  .record { flex: 1 1 100%; }
  .leg-row { flex-direction: column; gap: 2px; }
  .leg-row .gname { flex: none; }

  /* ---- fixed furniture clears the iOS home indicator in standalone mode ---- */
  .errbar { padding-bottom: calc(8px + env(safe-area-inset-bottom)); }
  #toast { bottom: calc(18px + env(safe-area-inset-bottom)); }
  .login-card { width: min(340px, calc(100vw - 32px)); padding: 24px 20px; }
}
