:root {
  --bg: #f6f7f9; --surface: #ffffff; --text: #1b1f24; --muted: #66707c;
  --border: #dde1e6; --accent: #1f6feb; --accent-text: #ffffff;
  --good: #1a7f37; --bad: #cf222e; --code-bg: #f0f2f5;
  --input-bg: #fff8db; --input-border: #e3b341; --calc-bg: #e8f1ff; --calc-text: #0b3d91;
  --heading-grad: linear-gradient(90deg, #0b3d91 0%, #1f6feb 55%, #4fa3ff 100%);
  --banner-grad: linear-gradient(120deg, #0a2a66 0%, #1552c4 60%, #2f86f5 100%);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1216; --surface: #171b21; --text: #e6e9ed; --muted: #97a1ad;
    --border: #2b323b; --accent: #4c8dff; --accent-text: #0b0e12;
    --good: #3fb950; --bad: #ff6b6b; --code-bg: #1f252d;
    --input-bg: #2b2410; --input-border: #9e7a1c; --calc-bg: #13284d; --calc-text: #a9ccff;
    --heading-grad: linear-gradient(90deg, #0d2f6e 0%, #1a5bd0 55%, #3d8bff 100%);
    --banner-grad: linear-gradient(120deg, #081d47 0%, #12439f 60%, #2569d6 100%);
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; } /* beats display rules like dialog label */
body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.45 system-ui, -apple-system, Segoe UI, Roboto, sans-serif; }
button, input, select, textarea { font: inherit; color: inherit; }
button { background: var(--surface); border: 1px solid var(--border); border-radius: 6px; padding: 6px 12px; cursor: pointer; }
button:hover { border-color: var(--accent); }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
button.icon { padding: 2px 8px; }
.danger-text { color: var(--bad); }
.muted { color: var(--muted); }
.error { color: var(--bad); margin: 4px 0 0; font-size: 13px; }

.bar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; padding: 12px 16px; background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 2; }
.bar h1 { font-size: 18px; margin: 0 8px 0 0; }
.bar select { padding: 6px 8px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface); max-width: 60vw; }
.actions { display: flex; gap: 8px; align-items: center; margin-left: auto; flex-wrap: wrap; }

main { display: grid; grid-template-columns: minmax(280px, 380px) 1fr; gap: 20px; padding: 16px; max-width: 1300px; margin: 0 auto; }
@media (max-width: 800px) { main { grid-template-columns: 1fr; } }
.section-head { display: flex; justify-content: space-between; align-items: center; }
h2 { font-size: 16px; margin: 8px 0; }

.group { background: var(--surface); border: 1px solid var(--border); border-radius: 8px; margin-bottom: 14px; overflow: hidden; }
.group h4 { margin: 0; padding: 9px 12px; font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: #ffffff; background: var(--heading-grad); border-bottom: 0; text-shadow: 0 1px 1px rgb(0 0 0 / .2); }
.group h4 button { background: rgb(255 255 255 / .15); border-color: rgb(255 255 255 / .35); color: #fff; }
.group h4 button:disabled { opacity: .4; }
.row { display: grid; grid-template-columns: 1fr auto auto; gap: 8px; align-items: center; padding: 8px 12px; border-bottom: 1px solid var(--border); }
.row:last-child { border-bottom: 0; }
.row input { width: 140px; padding: 5px 8px; border: 1.5px solid var(--input-border); border-radius: 6px; background: var(--input-bg); text-align: right; font-variant-numeric: tabular-nums; }
.row input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.calc-row { grid-template-columns: auto 1fr auto auto; }
.move { display: flex; flex-direction: column; gap: 2px; }
.move button { padding: 0 6px; font-size: 11px; line-height: 16px; }
.calc-label { min-width: 0; }
.calc-label code { display: block; font-size: 12px; color: var(--muted); background: var(--code-bg); padding: 1px 6px; border-radius: 4px; margin-top: 2px; overflow-wrap: anywhere; }
.value { font-weight: 700; font-variant-numeric: tabular-nums; text-align: right; background: var(--calc-bg); color: var(--calc-text); padding: 4px 10px; border-radius: 6px; min-width: 80px; cursor: default; }
.value.pass { color: var(--good); } .value.fail { color: var(--bad); }
.notes { font-size: 12px; color: var(--muted); }
.row-tools { display: flex; gap: 4px; }

dialog { border: 1px solid var(--border); border-radius: 10px; background: var(--surface); color: var(--text); width: min(560px, calc(100vw - 32px)); padding: 18px; }
dialog::backdrop { background: rgb(0 0 0 / .4); }
dialog h3 { margin-top: 0; }
dialog label { display: block; margin-bottom: 10px; font-size: 13px; color: var(--muted); }
dialog input, dialog textarea, dialog select { display: block; width: 100%; margin-top: 3px; padding: 6px 8px; border: 1px solid var(--border); border-radius: 6px; background: var(--bg); }
dialog textarea { font-family: ui-monospace, Menlo, Consolas, monospace; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.preview { font-size: 14px; margin: -4px 0 10px; }
.chips { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; }
.chips button { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12px; padding: 2px 6px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; }
#historyList { list-style: none; padding: 0; max-height: 50vh; overflow: auto; }
#historyList li { padding: 6px 0; border-bottom: 1px solid var(--border); }
.group-head { display: flex; justify-content: space-between; align-items: center; }
.group-head button { font-size: 11px; }

.stock-banner { display: flex; flex-wrap: wrap; align-items: center; gap: 12px 20px; max-width: 1300px; margin: 16px auto 0; padding: 14px 18px; border-radius: 10px; background: var(--banner-grad); color: #fff; width: calc(100% - 32px); }
.stock-id { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; min-width: 0; }
.ticker { font-size: 28px; font-weight: 800; letter-spacing: .02em; }
.company { font-size: 17px; opacity: .9; }
.stock-price { font-size: 18px; font-weight: 600; margin-left: auto; font-variant-numeric: tabular-nums; }
button.on-dark { background: rgb(255 255 255 / .15); border-color: rgb(255 255 255 / .4); color: #fff; }
button.on-dark:hover { background: rgb(255 255 255 / .25); border-color: #fff; }

.source { font-size: 12px; color: var(--muted); font-style: italic; }
h2 { display: flex; align-items: baseline; gap: 6px; flex-wrap: wrap; }
.h2-sub { font-size: 13px; font-weight: 400; color: var(--muted); }
.kind-dot { display: inline-block; width: 12px; height: 12px; border-radius: 3px; align-self: center; }
.input-dot { background: var(--input-bg); border: 1.5px solid var(--input-border); }
.calc-dot { background: var(--calc-bg); border: 1.5px solid var(--calc-text); }

.banner-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.fetch-msg { max-width: 1300px; width: calc(100% - 32px); margin: 10px auto 0; padding: 8px 12px; border-radius: 8px; background: var(--calc-bg); color: var(--calc-text); font-size: 14px; }
.fetch-msg.error { background: transparent; border: 1px solid var(--bad); color: var(--bad); }
.bigfive { max-width: 1300px; width: calc(100% - 32px); margin: 14px auto 0; }
.table-wrap { overflow-x: auto; }
.bigfive table { border-collapse: collapse; width: 100%; font-variant-numeric: tabular-nums; }
.bigfive th, .bigfive td { padding: 7px 12px; border-bottom: 1px solid var(--border); text-align: right; white-space: nowrap; }
.bigfive tbody th { text-align: left; font-weight: 500; }
.bigfive thead th { font-size: 12px; color: var(--muted); font-weight: 600; }
.bigfive td.good { color: var(--good); font-weight: 700; }
.bigfive td.bad { color: var(--bad); font-weight: 700; }
.bigfive td.na { color: var(--muted); }
.bigfive .notes { padding: 6px 12px 10px; margin: 0; }

.schwab-on { background: rgb(26 127 55 / .55) !important; }
.opt-wrap { max-height: 50vh; overflow: auto; }
#optTable { width: 100%; border-collapse: collapse; font-size: 13px; font-variant-numeric: tabular-nums; }
#optTable th, #optTable td { padding: 4px 6px; text-align: right; border-bottom: 1px solid var(--border); }
#optTable th:first-child, #optTable td:first-child { text-align: left; }
#optTable tbody tr { cursor: pointer; }
#optTable tbody tr:hover { background: var(--calc-bg); }
#optTable tr.near td { font-weight: 600; }

.head-tools { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.explain-btn { font-weight: 700; }
.explain-btn.on { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.explain { grid-column: 1 / -1; margin: 4px 0 2px; padding: 10px 12px; border-radius: 8px; background: var(--bg); border-left: 4px solid var(--accent); font-size: 14px; }
.explain-text { margin: 0 0 8px; }
.explain-head { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin-top: 6px; }
.explain-uses { list-style: none; padding: 0; margin: 2px 0 4px; display: flex; flex-wrap: wrap; gap: 4px 14px; }
.explain-uses .tag { padding: 0 6px; border-radius: 4px; }
.explain-uses .tag.in { background: var(--input-bg); border: 1px solid var(--input-border); color: #1b1f24; }
.explain-uses .tag.calc { background: var(--calc-bg); color: var(--calc-text); }
.explain-math { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 13px; overflow-wrap: anywhere; margin-top: 2px; }

.hist-row { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; }
.hist-stock { min-width: 0; }
