/* UL 508A Helper – one stylesheet, design tokens first. */
:root {
  --bg: #f6f7f9; --surface: #ffffff; --surface-2: #f0f2f5; --border: #dfe3e8;
  --text: #1b1f24; --muted: #5d6673; --accent: #1f5fbf; --accent-soft: #e6eefb;
  --ok: #1d7a46; --ok-soft: #e3f4ea; --warn: #9a6200; --warn-soft: #fdf2dc;
  --bad: #b3261e; --bad-soft: #fbe5e3; --info: #48566a; --info-soft: #eceff3;
  --radius: 8px; --shadow: 0 1px 2px rgba(16,24,40,.06);
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111418; --surface: #181c21; --surface-2: #20252c; --border: #2e353e;
    --text: #e4e8ee; --muted: #9aa4b1; --accent: #78a9f5; --accent-soft: #1d2b40;
    --ok: #6cd197; --ok-soft: #173225; --warn: #f0c05a; --warn-soft: #3a2e12;
    --bad: #f08b84; --bad-soft: #3c1b19; --info: #b5c0ce; --info-soft: #252b33;
    --shadow: none; color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.5 var(--sans); }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
code, .mono { font-family: var(--mono); font-size: .92em; }
h1 { font-size: 1.45rem; margin: 0; }
h2 { font-size: 1.1rem; margin: 0 0 .6rem; }
h3 { font-size: .95rem; margin: 1rem 0 .4rem; }

/* layout */
.shell { display: grid; grid-template-columns: 240px 1fr; min-height: 100vh; }
.side { background: var(--surface); border-right: 1px solid var(--border); padding: 1rem .75rem;
        position: sticky; top: 0; height: 100vh; overflow-y: auto; display: flex; flex-direction: column; }
.brand { font-weight: 700; padding: .25rem .25rem 1rem .5rem; display: flex; justify-content: space-between; align-items: flex-start; gap: .5rem; }
.brand small { display: block; font-weight: 400; color: var(--muted); font-size: .78rem; }
.navgroup { margin-bottom: .9rem; }
.navgroup > div { font-size: .72rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted);
                  padding: 0 .5rem .25rem; }
.navgroup a { display: block; padding: .38rem .5rem; border-radius: 6px; color: var(--text); }
.navgroup a:hover { background: var(--surface-2); text-decoration: none; }
.navgroup a.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.side .who { margin-top: auto; border-top: 1px solid var(--border); padding: .75rem .5rem 0; font-size: .85rem;
             color: var(--muted); display: flex; justify-content: space-between; align-items: center; }
.main { padding: 1.5rem 2rem 1rem; max-width: 1440px; width: 100%; display: flex; flex-direction: column; }
.pagehead { margin-bottom: 1.25rem; }
.pagehead p { margin: .25rem 0 0; color: var(--muted); }
.basis { margin-top: auto; padding-top: 2rem; font-size: .8rem; color: var(--muted); }
.menu-btn { display: none; }

.topbar, .backdrop { display: none; }
@media (max-width: 860px) {
  .shell { grid-template-columns: minmax(0, 1fr); }
  .main { padding: 1rem 1rem calc(1rem + env(safe-area-inset-bottom)); min-width: 0; }
  /* top bar + off-canvas drawer */
  .topbar { display: flex; align-items: center; gap: .6rem; position: sticky; top: 0; z-index: 30;
            padding: calc(.5rem + env(safe-area-inset-top)) 1rem .5rem; background: var(--surface);
            border-bottom: 1px solid var(--border); }
  .menu-btn { display: inline-flex; align-items: center; justify-content: center; width: 2.6rem; height: 2.6rem; padding: 0;
              font-size: 1.25rem; background: transparent; color: var(--text); border: 1px solid var(--border); border-radius: 8px; }
  .topbar-title { color: var(--text); font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .side { display: flex; position: fixed; top: 0; left: 0; bottom: 0; z-index: 50; width: min(85vw, 320px); height: 100%;
          transform: translateX(-105%); transition: transform .2s ease; box-shadow: 0 0 24px rgba(0,0,0,.25);
          padding-top: calc(1rem + env(safe-area-inset-top)); padding-bottom: calc(1rem + env(safe-area-inset-bottom)); }
  .side.open { transform: none; }
  .backdrop { display: block; position: fixed; inset: 0; z-index: 40; background: rgba(15,20,30,.45); }
  .backdrop[hidden] { display: none !important; }
  body.noscroll { overflow: hidden; }
  .navgroup a, .navsub > summary { padding-top: .6rem; padding-bottom: .6rem; }
  .pagehead { margin-bottom: 1rem; }
  .pagehead h1, .calc-head h1 { font-size: 1.3rem; }
}
@media (prefers-reduced-motion: reduce) { .side { transition: none; } }
/* components */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
        box-shadow: var(--shadow); padding: 1rem 1.25rem; margin-bottom: 1rem; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 1rem; }
.row { display: flex; gap: .75rem; flex-wrap: wrap; align-items: end; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.badge { display: inline-block; padding: .08rem .5rem; border-radius: 999px; font-size: .75rem; font-weight: 600;
         background: var(--info-soft); color: var(--info); white-space: nowrap; }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.callout { border-left: 3px solid var(--info); background: var(--info-soft); padding: .6rem .9rem;
           border-radius: 0 var(--radius) var(--radius) 0; margin: .75rem 0; }
.callout.warn { border-color: var(--warn); background: var(--warn-soft); }
.callout.bad { border-color: var(--bad); background: var(--bad-soft); }
.callout.ok { border-color: var(--ok); background: var(--ok-soft); }

/* forms */
label { display: block; font-size: .82rem; font-weight: 600; margin-bottom: .2rem; }
label .hint { font-weight: 400; color: var(--muted); }
input, select, textarea { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
                          border-radius: 6px; padding: .45rem .6rem; width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.field { flex: 1 1 160px; min-width: 140px; margin-bottom: .75rem; }
.field.narrow { flex: 0 1 120px; min-width: 100px; }
.check { display: flex; gap: .5rem; align-items: flex-start; font-weight: 400; font-size: .9rem; line-height: 1.35;
         padding: .2rem 0; cursor: pointer; }
.check input { width: auto; margin: .15rem 0 0; accent-color: var(--accent); flex: none; }
button, .btn { font: inherit; font-weight: 600; border: 1px solid var(--accent); background: var(--accent); color: #fff;
               padding: .45rem 1rem; border-radius: 6px; cursor: pointer; }
button.ghost, .btn.ghost { background: transparent; color: var(--accent); }
button.link { border: 0; background: none; color: var(--muted); padding: 0; font-weight: 400; }
button.danger { border-color: var(--bad); background: transparent; color: var(--bad); }
button:disabled { opacity: .55; cursor: default; }
fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: .9rem 1rem .4rem; margin: 0 0 1rem;
           background: var(--surface-2); }
legend { font-weight: 700; font-size: .72rem; letter-spacing: .06em; text-transform: uppercase; color: var(--muted);
         padding: .1rem .5rem; background: var(--surface); border: 1px solid var(--border); border-radius: 999px; }
fieldset input, fieldset select, fieldset textarea { background: var(--surface); }
/* long checkbox lists flow into columns */
.calc-form :is(fieldset, div):has(> .check ~ .check ~ .check) {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); column-gap: 1.25rem; row-gap: .1rem; align-items: start; }
.calc-form :is(fieldset, div):has(> .check ~ .check ~ .check) > :not(.check):not(legend) { grid-column: 1 / -1; }
.sub { background: var(--surface-2); border: 1px solid var(--border); border-radius: 6px; padding: .6rem .75rem .1rem;
       margin-bottom: .6rem; }
.sub-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: .4rem; font-weight: 600; font-size: .88rem; }

/* tables */
.tbl-wrap { overflow-x: auto; }
table.data { border-collapse: collapse; width: 100%; font-size: .88rem; }
table.data th, table.data td { border: 1px solid var(--border); padding: .35rem .55rem; text-align: left; vertical-align: top; }
table.data th { background: var(--surface-2); font-weight: 600; }
table.data tr.grp td { background: var(--surface-2); font-weight: 600; }
table.data tbody tr:hover td { background: var(--accent-soft); }
sup.fn { color: var(--accent); font-weight: 600; }
dl.fns { margin: .5rem 0 0; display: grid; grid-template-columns: 2rem 1fr; gap: .25rem .5rem; font-size: .88rem; }
dl.fns dt { font-weight: 700; color: var(--accent); }
dl.fns dd { margin: 0; }

/* clause text */
.clause-text { white-space: pre-wrap; }
.exception { font-style: italic; border-left: 3px solid var(--warn); padding-left: .75rem; margin: .5rem 0; }
.crumb { font-size: .82rem; color: var(--muted); margin-bottom: .4rem; }
.hit { padding: .75rem 0; border-bottom: 1px solid var(--border); }
.hit:last-child { border-bottom: 0; }
.hit .t { font-weight: 600; }
.hit p { margin: .25rem 0 0; color: var(--muted); font-size: .9rem; }
.pager { display: flex; justify-content: space-between; margin-top: .75rem; }

/* results */
.result-head { display: flex; justify-content: space-between; align-items: center; gap: 1rem; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: .3rem 1rem; margin: .75rem 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; font-weight: 600; }
.big { font-size: 1.35rem; font-weight: 700; }
ol.steps { padding-left: 1.4rem; margin: .4rem 0; }
ol.steps li { margin: .35rem 0; }
.cites { display: flex; flex-wrap: wrap; gap: .3rem; margin-top: .2rem; }
.cite { font-size: .75rem; border: 1px solid var(--border); background: var(--surface-2); border-radius: 4px;
        padding: 0 .35rem; cursor: pointer; color: var(--text); font-weight: 400; }
.cite-text { font-size: .85rem; background: var(--surface-2); border-radius: 6px; padding: .5rem .75rem; margin-top: .3rem;
             white-space: pre-wrap; }

/* chat */
.chat { display: flex; flex-direction: column; gap: 1rem; }
.msg { max-width: 100%; }
.msg.user { align-self: flex-end; background: var(--accent-soft); border-radius: var(--radius); padding: .6rem .9rem; max-width: 80%; white-space: pre-wrap; }
.msg.bot { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: .75rem 1.1rem; }
.msg.bot table { border-collapse: collapse; margin: .5rem 0; font-size: .88rem; }
.msg.bot th, .msg.bot td { border: 1px solid var(--border); padding: .25rem .5rem; }
.toolline { font-size: .78rem; color: var(--muted); font-family: var(--mono); }
.tools { border-bottom: 1px dashed var(--border); padding-bottom: .4rem; margin-bottom: .5rem; }
.tools summary { cursor: pointer; font-size: .8rem; color: var(--muted); }
.composer { position: sticky; bottom: 0; background: var(--bg); padding: .75rem 0 .25rem; display: flex; gap: .5rem; align-items: flex-end; }
.composer textarea { resize: vertical; min-height: 3.2rem; max-height: 14rem; }
.examples { display: flex; flex-wrap: wrap; gap: .5rem; }
.examples button { font-weight: 400; }
.spin { display: inline-block; width: .8rem; height: .8rem; border: 2px solid var(--border); border-top-color: var(--accent);
        border-radius: 50%; animation: spin .8s linear infinite; vertical-align: -1px; }
@keyframes spin { to { transform: rotate(360deg); } }

/* login */
.login { max-width: 360px; margin: 12vh auto 0; }
.login h1 { margin-bottom: 1rem; }

/* utilities (no inline styles: CSP style-src 'self') */
.calc-grid { display: grid; grid-template-columns: minmax(0, 7fr) minmax(0, 5fr); gap: 1.25rem; align-items: start; }
.result-col { position: sticky; top: 1rem; max-height: calc(100vh - 2rem); overflow-y: auto; border-radius: var(--radius); }
@media (max-width: 1100px) { .calc-grid { grid-template-columns: 1fr; } .result-col { position: static; max-height: none; } }
.spread { justify-content: space-between; }
.mt { margin-top: 1rem; }
.m0 { margin: 0; }
.grow { flex: 1 1 400px; }
.full { width: 100%; }
.tight { margin: .3rem 0; }
.maxw { max-width: 360px; }
.stack { display: flex; flex-direction: column; gap: .4rem; }
.sub.branch { background: var(--surface); }
.toolline.bad { color: var(--bad); }
[hidden] { display: none !important; }

/* table search */
.tres { border-top: 1px solid var(--border); padding: .75rem 0; }
.tres:first-child { margin-top: .75rem; }
.tres-head { display: flex; gap: .5rem; align-items: baseline; flex-wrap: wrap; }
.tres-head a { font-weight: 600; }
.tres .where { font-size: .75rem; color: var(--muted); white-space: nowrap; padding-right: .5rem; }
.tres table.data { margin-top: .4rem; font-size: .82rem; }
table.data tr:target td { background: var(--warn-soft); }
mark { background: var(--warn-soft); color: inherit; padding: 0 1px; border-radius: 2px; }
.cite.owner_rule { border-color: var(--warn); background: var(--warn-soft); color: var(--warn); font-weight: 600; }

/* calculator navigation */
.navsub > summary { cursor: pointer; list-style: none; padding: .3rem .5rem; border-radius: 6px; font-size: .88rem;
                    color: var(--muted); display: flex; justify-content: space-between; }
.navsub > summary::-webkit-details-marker { display: none; }
.navsub > summary::after { content: "▸"; font-size: .75rem; }
.navsub[open] > summary::after { content: "▾"; }
.navsub > summary:hover { background: var(--surface-2); }
.navsub a { padding-left: 1.1rem; font-size: .9rem; }
.calc-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: .75rem; }
.calc-card { display: block; border: 1px solid var(--border); border-radius: var(--radius); padding: .7rem .9rem;
             background: var(--surface); color: var(--text); }
.calc-card:hover { border-color: var(--accent); text-decoration: none; }
.calc-card strong { display: block; color: var(--accent); }
.calc-card span { font-size: .83rem; color: var(--muted); }

/* calculator pages */
.calc-head h1 { margin-top: .15rem; }
.calc-head p:last-child { max-width: 75ch; }
.calc-form { padding: 1.25rem 1.4rem .9rem; }
.calc-form input, .calc-form select { min-height: 2.3rem; }
.calc-form input::placeholder { color: var(--muted); opacity: .7; }
.calc-form .callout { margin-top: 0; }
.form-actions { position: sticky; bottom: 0; display: flex; justify-content: space-between; align-items: center;
                padding: .8rem 0 .2rem; margin-top: .5rem; background: var(--surface); border-top: 1px solid var(--border); }
button.primary { padding: .55rem 1.6rem; }
button.link:hover { color: var(--accent); }
.empty-state { text-align: center; padding: 2.5rem 1.5rem; }
.empty-state p { margin: .3rem auto; max-width: 42ch; }
.empty-icon { width: 3rem; height: 3rem; margin: 0 auto .75rem; border-radius: 50%; display: grid; place-items: center;
              background: var(--accent-soft); color: var(--accent); font-size: 1.4rem; font-weight: 700; }

/* results */
.res-card { border-top: 4px solid var(--info); }
.res-card.ok { border-top-color: var(--ok); }
.res-card.bad { border-top-color: var(--bad); }
.res-card.warn { border-top-color: var(--warn); }
.hero { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: .6rem; margin: .9rem 0 .4rem; }
.hero > div { background: var(--surface-2); border-radius: var(--radius); padding: .7rem .9rem; }
.hero .lab { font-size: .75rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.hero .val { font-size: 1.45rem; font-weight: 700; line-height: 1.25; margin-top: .15rem; overflow-wrap: anywhere; }
.res-card.ok .hero .val { color: var(--ok); }
.kv.out { grid-template-columns: minmax(9rem, max-content) 1fr; border-top: 1px solid var(--border); padding-top: .75rem; }
.kv.out dd { font-weight: 500; }
.kv.out dd > ul { margin: 0; padding-left: 1.1rem; }
.kv.out dl.kv { margin: .1rem 0; font-size: .9rem; }
.sec-h { font-size: .78rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 1rem 0 .4rem; }
ul.conds { margin: 0; padding-left: 1.1rem; font-size: .88rem; }
ul.conds li { margin: .2rem 0; }
.signoff { font-size: .8rem; color: var(--warn); margin-top: .75rem; }
ol.steps li { padding-left: .2rem; }
ol.steps li::marker { color: var(--muted); font-weight: 600; }
.cite { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cite.table, .cite.row, .cite.footnote, .cite.note { border-color: color-mix(in srgb, var(--accent) 35%, var(--border)); }
.cite.exception { border-color: var(--warn); }
.cite-text { flex-basis: 100%; }
details.steps-box > summary { cursor: pointer; font-weight: 600; list-style: none; display: flex; justify-content: space-between; }
details.steps-box > summary::-webkit-details-marker { display: none; }
details.steps-box > summary::after { content: "Hide"; font-size: .8rem; color: var(--accent); font-weight: 400; }
details.steps-box:not([open]) > summary::after { content: "Show"; }
.res-foot { font-size: .78rem; color: var(--muted); margin: .75rem 0 0; }

/* collapsible sidebar */
.nav-collapse { background: transparent; border: 1px solid var(--border); color: var(--muted); padding: .1rem .5rem;
                font-size: .95rem; line-height: 1.4; border-radius: 6px; }
.nav-collapse:hover { color: var(--accent); border-color: var(--accent); }
.nav-open { display: none; position: fixed; top: .9rem; left: .9rem; z-index: 20; background: var(--surface); color: var(--text);
            border: 1px solid var(--border); box-shadow: var(--shadow); padding: .35rem .7rem; font-size: 1.05rem; }
.nav-open:hover { color: var(--accent); border-color: var(--accent); }
html.nav-collapsed .shell { grid-template-columns: 1fr; }
html.nav-collapsed .side { display: none; }
html.nav-collapsed .nav-open { display: block; }
html.nav-collapsed .main { padding-left: 4.5rem; max-width: 1600px; }
@media (max-width: 860px) {
  .nav-collapse, .nav-open, html.nav-collapsed .nav-open { display: none; }
  html.nav-collapsed .side.open { display: flex; }
  html.nav-collapsed .main { padding-left: 1rem; }
}
fieldset .sub, fieldset .item { background: var(--surface); }
.sub-head { color: var(--text); }

/* phones */
@media (max-width: 640px) {
  input, select, textarea { font-size: 16px; }            /* prevents iOS zoom on focus */
  .card { padding: .9rem 1rem; }
  .calc-form { padding: 1rem .9rem .75rem; }
  fieldset { padding: .8rem .7rem .3rem; }
  .row { gap: .6rem; }
  .field, .field.narrow { flex: 1 1 calc(50% - .6rem); min-width: 0; }
  .field:has(select[data-ctl]), .field:has(textarea) { flex-basis: 100%; }
  label { font-size: .8rem; }
  .calc-form :is(fieldset, div):has(> .check ~ .check ~ .check) { grid-template-columns: 1fr; }
  .check { padding: .35rem 0; font-size: .95rem; }
  .check input { width: 1.15rem; height: 1.15rem; }
  .form-actions { padding-bottom: calc(.4rem + env(safe-area-inset-bottom)); margin: .5rem -.9rem 0; padding-left: .9rem; padding-right: .9rem; }
  .form-actions .primary { flex: 1; margin-right: .75rem; }
  button, .btn { min-height: 2.6rem; }
  button.link { min-height: 0; }
  .hero { grid-template-columns: 1fr 1fr; }
  .hero .val { font-size: 1.25rem; }
  .kv, .kv.out { grid-template-columns: 1fr; gap: .1rem; }
  .kv dd { margin-bottom: .45rem; }
  .cite { font-size: .8rem; padding: .15rem .45rem; }
  .calc-cards { grid-template-columns: 1fr; }
  .composer { padding-bottom: calc(.4rem + env(safe-area-inset-bottom)); }
  .composer textarea { min-height: 2.8rem; }
  .msg.user { max-width: 92%; }
  .msg.bot { padding: .7rem .8rem; }
  .msg.bot table, .cite-text { display: block; overflow-x: auto; }
  table.data { font-size: .82rem; }
  table.data th, table.data td { padding: .3rem .4rem; }
  .tbl-wrap { margin: 0 -1rem; padding: 0 1rem; }
  .examples button { width: 100%; text-align: left; }
  .login { margin-top: 6vh; padding: 0 1rem; }
  dl.fns { grid-template-columns: 1.5rem 1fr; }
}

@media (max-width: 860px) { #result, .msg { scroll-margin-top: 4.5rem; } }
@media (max-width: 640px) {
  .cite, button.cite { min-height: 0; }
  .navsub > summary, .nav-collapse { min-height: 0; }
  /* wide record tables become one card per row */
  table.cards-sm, table.cards-sm tbody, table.cards-sm tr, table.cards-sm td { display: block; width: 100%; }
  table.cards-sm thead { display: none; }
  table.cards-sm tr { border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: .6rem; padding: .4rem .6rem;
                      background: var(--surface); }
  table.cards-sm td { border: 0; padding: .2rem 0; }
  table.cards-sm td[data-label]::before { content: attr(data-label); display: block; font-size: .7rem; text-transform: uppercase;
                                          letter-spacing: .05em; color: var(--muted); }
  table.cards-sm td:empty, table.cards-sm td:has(> :only-child:empty) { display: none; }
  table.cards-sm tbody tr:hover td { background: transparent; }
  .tbl-wrap:has(table.cards-sm) { margin: 0; padding: 0; }
}
