/* PigBal v2: app shell, steps, forms, tables, results. Tokens come from pigbal/ui.css. */
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
svg circle { fill: currentColor; stroke: none; }
.btn.icon-only { width: 34px; padding: 0; }
.btn .chev { width: 14px; height: 14px; margin-right: -3px; }
kbd { font: 11px var(--mono); color: var(--muted); margin-left: auto; }

/* ---------- shell ---------- */
.app { display: grid; grid-template-columns: 232px minmax(0, 1fr) 300px; grid-template-rows: 56px minmax(0, 1fr); grid-template-areas: "top top top" "steps main live"; height: 100vh; }
.topbar { grid-area: top; display: flex; align-items: center; gap: 14px; padding: 0 16px; background: var(--surface); border-bottom: 1px solid var(--line); min-width: 0; }
.topbar .spacer { flex: 1; }
.topbar .actions { display: flex; align-items: center; gap: 8px; }
#navToggle { display: none; }
.runhead { display: flex; align-items: center; gap: 10px; min-width: 0; padding-left: 14px; border-left: 1px solid var(--line); }
.runtitle { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 34vw; }
.avatar { width: 34px; padding: 0; border-radius: 50%; background: var(--accent-soft); color: var(--accent-strong); font-weight: 800; }

.steps { grid-area: steps; overflow-y: auto; padding: 16px 10px; border-right: 1px solid var(--line); background: var(--surface); }
.steps ol { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; }
.steps a { display: grid; grid-template-columns: 26px 1fr auto; align-items: center; gap: 10px; padding: 8px 10px; border-radius: var(--radius-sm); color: var(--ink-2); text-decoration: none; font-weight: 600; }
.steps a:hover { background: var(--surface-2); color: var(--ink); }
.steps a[aria-current="page"] { background: var(--accent-soft); color: var(--accent-strong); }
.steps .n { width: 26px; height: 26px; border-radius: 50%; display: grid; place-items: center; font-size: 12px; font-weight: 700; background: var(--surface-2); color: var(--muted); border: 1px solid var(--line); }
.steps a[aria-current="page"] .n { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.steps .chg { font-size: 11px; font-weight: 700; color: var(--accent-strong); background: var(--accent-soft); border-radius: 99px; padding: 1px 7px; }
.steps a[aria-current="page"] .chg { background: var(--surface); }
.steps .sep { height: 1px; background: var(--line); margin: 10px 8px; }
.steps .foot { margin-top: 18px; padding: 0 10px; font-size: 12px; color: var(--muted); }
.steps .foot a { display: inline; padding: 0; color: var(--accent); font-weight: 600; }

.main { grid-area: main; overflow-y: auto; scroll-behavior: smooth; }
#view { max-width: 1060px; margin: 0 auto; padding: 28px 28px 80px; }
.live { grid-area: live; overflow-y: auto; padding: 18px 16px; border-left: 1px solid var(--line); background: var(--surface); display: flex; flex-direction: column; gap: 14px; }
.live-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.live h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.calcstate { font-size: 12px; color: var(--muted); display: inline-flex; align-items: center; gap: 6px; }
.calcstate.err { color: var(--danger); font-weight: 600; }
.calcstate .retry { color: var(--danger); background: none; border: 0; text-decoration: underline; cursor: pointer; padding: 0; font-weight: 700; }
.kpi { padding: 12px 14px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); transition: background-color .6s; }
.kpi .k { font-size: 12px; color: var(--muted); font-weight: 600; }
.kpi .v { font-size: 22px; font-weight: 800; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.kpi .u { font-size: 12px; color: var(--muted); font-weight: 600; margin-left: 4px; }
.kpi.flash { background: var(--accent-soft); transition: none; }
#liveKpis { display: grid; gap: 8px; }
#liveKpis .kpi .v { font-size: 19px; }

/* ---------- page heading ---------- */
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 20px; }
.eyebrow { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .07em; color: var(--accent); }
.page-head h1 { font-size: 26px; margin-top: 4px; }
.page-head p { margin: 6px 0 0; color: var(--ink-2); max-width: 62ch; }
.pager { display: flex; justify-content: space-between; gap: 12px; margin-top: 28px; }
.pager .btn { height: 40px; }

/* ---------- cards & fields ---------- */
.block { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); margin-bottom: 18px; }
.block > header { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 18px; border-bottom: 1px solid var(--line); }
.block > header h2 { font-size: 15px; }
.block .note { padding: 10px 18px; color: var(--muted); font-size: 12.5px; border-top: 1px solid var(--line); background: var(--surface-2); border-radius: 0 0 var(--radius) var(--radius); }
.block .note.top { border-top: 0; border-bottom: 1px solid var(--line); border-radius: 0; }

.fields { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); column-gap: 28px; padding: 8px 18px 12px; }
.subhead { grid-column: 1 / -1; font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); padding: 16px 0 4px; }
.f { display: grid; grid-template-columns: minmax(0, 1fr) 168px; align-items: center; gap: 12px; padding: 7px 0; border-bottom: 1px dashed var(--line); min-height: 46px; }
.f.wide { grid-template-columns: minmax(0, 1fr) minmax(0, 1.4fr); }
.f label, .f .lab { font-size: 13px; color: var(--ink); line-height: 1.35; }
.f .unit { display: block; font-size: 11.5px; color: var(--muted); }
.f .out { text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; padding-right: 10px; }
.f .out .u { font-weight: 500; color: var(--muted); font-size: 12px; }

/* input control: value box with optional suffix + changed/reset affordance */
.ctl { position: relative; display: flex; align-items: center; }
.ctl input, .ctl select { width: 100%; height: 34px; border: 1px solid var(--line-strong); border-radius: var(--radius-sm); background: var(--surface); padding: 0 10px; font-variant-numeric: tabular-nums; }
.ctl input.num { text-align: right; }
.ctl.pct input { padding-right: 26px; }
.ctl.pct::after { content: "%"; position: absolute; right: 10px; color: var(--muted); font-size: 12.5px; pointer-events: none; }
.ctl input:hover, .ctl select:hover { border-color: var(--muted); }
.ctl input:focus, .ctl select:focus { border-color: var(--accent); box-shadow: var(--focus); outline: none; }
.ctl.changed input, .ctl.changed select { border-color: var(--accent); background: var(--accent-soft); }
.ctl.invalid input { border-color: var(--danger); background: var(--danger-soft); }
.ctl .reset { position: absolute; left: -24px; width: 20px; height: 20px; border: 0; border-radius: 50%; background: transparent; color: var(--accent); cursor: pointer; display: none; place-items: center; padding: 0; }
.ctl .reset svg { width: 14px; height: 14px; }
.ctl.changed .reset { display: grid; }
.ctl .reset:hover { background: var(--accent-soft); }
.ctl .msg { position: absolute; top: 100%; right: 0; font-size: 11px; color: var(--danger); font-weight: 600; white-space: nowrap; z-index: 2; }

/* ---------- tables ---------- */
.tablewrap { overflow-x: auto; }
table.grid { width: 100%; border-collapse: separate; border-spacing: 0; font-variant-numeric: tabular-nums; }
table.grid th, table.grid td { padding: 6px 10px; border-bottom: 1px solid var(--line); text-align: right; white-space: nowrap; }
table.grid thead th { font-size: 11.5px; font-weight: 700; color: var(--muted); background: var(--surface-2); vertical-align: bottom; white-space: normal; min-width: 90px; position: sticky; top: 0; z-index: 1; }
table.grid thead th .u { display: block; font-weight: 500; text-transform: none; }
table.grid th.rl, table.grid td.rl { text-align: left; font-weight: 600; position: sticky; left: 0; background: var(--surface); z-index: 1; min-width: 150px; white-space: normal; }
table.grid thead th.rl { background: var(--surface-2); z-index: 2; }
table.grid td.rl .u { display: block; font-size: 11px; color: var(--muted); font-weight: 500; }
table.grid tbody tr:hover td { background: var(--surface-2); }
table.grid tfoot td { font-weight: 800; border-bottom: 0; border-top: 2px solid var(--line-strong); }
table.grid td.in { padding: 3px 6px; }
table.grid td .ctl { min-width: 96px; }
table.grid td .ctl input, table.grid td .ctl select { height: 30px; }
table.grid td .ctl .reset { left: -20px; }
table.grid td.na { color: var(--line-strong); }
table.grid td.calc { color: var(--ink-2); }
table.grid td.err, .out.err { color: var(--muted); }

/* ---------- diet editor ---------- */
.diet-tools { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; padding: 12px 18px; border-bottom: 1px solid var(--line); }
.diet-tools .input { width: 220px; }
.diet-tools .grow { flex: 1; }
.seg { display: inline-flex; border: 1px solid var(--line-strong); border-radius: var(--radius-sm); overflow: hidden; }
.seg button { border: 0; background: var(--surface); padding: 0 12px; height: 32px; font-weight: 600; font-size: 12.5px; cursor: pointer; color: var(--ink-2); }
.seg button + button { border-left: 1px solid var(--line-strong); }
.seg button[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); }
.diet table.grid td .ctl { min-width: 70px; }
table.grid .ctl.pct::after { display: none; }
table.grid .ctl.pct input { padding-right: 8px; }
.diet-tools label.inline { font-weight: 600; font-size: 13px; }
.diet-tools .dietname { width: 160px; }
.diet table.grid thead th { min-width: 78px; }
.diet tr.grp td { background: var(--surface-2); font-size: 11.5px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); text-align: left; position: static; }
.diet tr.grp td:first-child { position: sticky; left: 0; }
.diet tr.totals td { font-weight: 800; }
.tot.ok { color: var(--ok); }
.tot.warn { color: var(--warn); }
.tot.off { color: var(--muted); font-weight: 500; }
.diet .empty { padding: 28px 18px; text-align: center; color: var(--muted); }

/* ---------- results ---------- */
.kpis { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 12px; margin-bottom: 18px; }
.kpis .kpi { box-shadow: var(--shadow); }
.chart { padding: 16px 18px; }
.bars { display: grid; gap: 10px; }
.bar-row { display: grid; grid-template-columns: 170px minmax(0, 1fr) 110px; align-items: center; gap: 12px; font-size: 13px; }
.bar-row .lab { font-weight: 600; }
.bar-row .val { text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; }
.bar-track { height: 18px; background: var(--surface-2); border-radius: 5px; overflow: hidden; display: flex; }
.bar-track span { display: block; height: 100%; transition: width .35s ease; }
.legend { display: flex; flex-wrap: wrap; gap: 14px; margin-top: 12px; font-size: 12px; color: var(--ink-2); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }

/* ---------- menus, toast, dialogs ---------- */
.menu { position: fixed; z-index: 50; min-width: 220px; padding: 6px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow-lg); }
.menu button, .menu a { display: flex; align-items: center; gap: 8px; width: 100%; padding: 8px 10px; border: 0; background: none; border-radius: 6px; text-align: left; cursor: pointer; color: var(--ink); text-decoration: none; font-weight: 500; }
.menu button:hover, .menu a:hover, .menu button:focus-visible { background: var(--surface-2); }
.menu h4 { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); padding: 8px 10px 4px; }
.menu hr { border: 0; border-top: 1px solid var(--line); margin: 6px 0; }
.menu .menu-head { padding: 8px 10px; display: grid; }
.menu .sm-only { display: none; }
@media (max-width: 860px) { .menu .sm-only { display: flex; } .menu hr.sm-only { display: block; } }
.menu .menu-head small { color: var(--muted); }
.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 60; display: flex; align-items: center; gap: 14px; padding: 10px 16px; border-radius: var(--radius); background: var(--ink); color: var(--bg); box-shadow: var(--shadow-lg); font-weight: 600; max-width: calc(100vw - 32px); }
.toast button { background: none; border: 0; color: inherit; text-decoration: underline; font-weight: 800; cursor: pointer; }
.toast[hidden] { display: none; }
.dialog { width: min(620px, calc(100vw - 32px)); padding: 0; border: 1px solid var(--line); border-radius: 14px; background: var(--surface); color: var(--ink); box-shadow: var(--shadow-lg); }
.dialog.narrow { width: min(420px, calc(100vw - 32px)); }
.dialog::backdrop { background: rgb(10 16 13 / .45); }
.dialog header { display: flex; align-items: center; justify-content: space-between; padding: 16px 18px; border-bottom: 1px solid var(--line); }
.dialog header h2 { font-size: 17px; }
.dialog-body { padding: 16px 18px; max-height: 65vh; overflow-y: auto; }
.dialog footer { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 18px; border-top: 1px solid var(--line); }
.runs { display: grid; gap: 8px; margin-top: 10px; }
.run { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 4px 12px; align-items: center; padding: 12px 14px; border: 1px solid var(--line); border-radius: var(--radius); }
.run.cur { border-color: var(--accent); background: var(--accent-soft); }
.run b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.run small { grid-column: 1; color: var(--muted); }
.run .acts { grid-row: 1 / span 2; grid-column: 2; display: flex; gap: 6px; }
.runs .empty { padding: 24px; text-align: center; color: var(--muted); border: 1px dashed var(--line-strong); border-radius: var(--radius); }

.loading { display: flex; align-items: center; gap: 10px; color: var(--muted); padding: 40px 0; font-weight: 600; }
.spinner { width: 16px; height: 16px; border: 2px solid var(--line-strong); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation: none; } .bar-track span, .kpi { transition: none; } .main { scroll-behavior: auto; } }

/* ---------- responsive ---------- */
@media (max-width: 1280px) {
  .app { grid-template-columns: 220px minmax(0, 1fr); grid-template-rows: 56px auto minmax(0, 1fr); grid-template-areas: "top top" "steps live" "steps main"; }
  .live { flex-direction: row; align-items: center; overflow-x: auto; padding: 10px 16px; border-left: 0; border-bottom: 1px solid var(--line); }
  .live-head { flex-direction: column; gap: 2px; min-width: 110px; }
  #liveKpis { display: flex; gap: 8px; }
  #liveKpis .kpi { min-width: 150px; padding: 8px 12px; }
  #liveKpis .kpi .v { font-size: 16px; }
  .live > .btn.block { width: auto; flex: none; }
}
@media (max-width: 860px) {
  .app { grid-template-columns: minmax(0, 1fr); grid-template-areas: "top" "live" "main"; }
  #navToggle { display: inline-flex; }
  .steps { position: fixed; z-index: 40; top: 56px; left: 0; bottom: 0; width: 260px; transform: translateX(-100%); transition: transform .2s; box-shadow: var(--shadow-lg); }
  body.navopen .steps { transform: none; }
  .runhead { display: none; }
  #view { padding: 20px 16px 64px; }
  .f { grid-template-columns: minmax(0, 1fr) 140px; }
  .two { grid-template-columns: 1fr; }
  .bar-row { grid-template-columns: 110px minmax(0, 1fr) 80px; }
  .topbar .actions .btn:not(.primary):not(.avatar):not(#moreBtn) { display: none; }
}
