:root {
  color-scheme: dark;
  --canvas: #0b1220;
  --panel: #121a2b;
  --inset: #0e1727;
  --hover: #1a263c;
  --line: #28364e;
  --control-line: #394963;
  --ink: #edf3fc;
  --secondary: #c5d0e2;
  --muted: #9baac1;
  --accent: #a0c3ff;
  --selected: #20375d;
  --blue: #8bb5ff;
  --danger: #ffadac;
  --ease: cubic-bezier(.22, 1, .36, 1);
  --fast: 150ms;
  --motion: 200ms;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 16px;
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; background: var(--canvas); color: var(--ink); font-size: .875rem; line-height: 1.5; }
h1, h2, p, dl, dd, ul { margin: 0; }
button, input, textarea { font: inherit; }
button, summary { cursor: pointer; }
a { color: inherit; text-decoration: none; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
:focus:not(:focus-visible) { outline: none; }
.icon { width: 18px; height: 18px; flex: none; }
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.page { width: min(calc(100% - 64px), 1200px); margin-inline: auto; padding-block: 40px 64px; }
.page-header { margin-bottom: 24px; }
h1 { font-size: 2.25rem; font-weight: 650; letter-spacing: -.035em; line-height: 1.12; }
h2 { font-size: 1.125rem; font-weight: 640; letter-spacing: -.015em; line-height: 1.28; }
.workspace { display: grid; grid-template-columns: minmax(322px, 1fr) minmax(0, 2fr); align-items: start; gap: 20px; }
.entry-tools, .results { min-width: 0; display: grid; gap: 16px; }
.panel { min-width: 0; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; }
.composer, .statistics { padding: 20px; }
footer { margin-top: 24px; color: var(--muted); font-size: .8125rem; }

.composer h2 { margin-bottom: 16px; }
.field { display: grid; gap: 4px; min-width: 0; }
.field label, .alias-group > p { font-weight: 550; color: var(--secondary); }
.field label span { font-weight: 400; color: var(--muted); }
input:not([type=hidden]), textarea {
  width: 100%;
  min-width: 0;
  min-height: 44px;
  padding: 9px 12px;
  background: var(--inset);
  border: 1px solid var(--control-line);
  border-radius: 8px;
  color: var(--ink);
  font-size: 1rem;
  line-height: 1.4;
  transition: border-color var(--fast) var(--ease), background-color var(--fast) var(--ease);
}
textarea { display: block; min-height: 68px; resize: vertical; }
input::placeholder, textarea::placeholder { color: var(--muted); opacity: 1; }
input:hover, textarea:hover { border-color: #5b7194; }
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 2px; border-color: var(--accent); }
input[aria-invalid=true], textarea[aria-invalid=true] { border-color: var(--danger); }
.field-help { font-size: .8125rem; color: var(--muted); overflow-wrap: anywhere; }
.field-error { color: var(--danger); font-size: .8125rem; overflow-wrap: anywhere; }
.fields { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.alias-group { margin-block: 12px 16px; }
.aliases { display: grid; grid-template-columns: repeat(8, minmax(0, 1fr)); gap: 4px; margin-top: 4px; }
.alias, .button, .filters a, .icon-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid transparent;
  border-radius: 8px;
  color: var(--secondary);
  background: transparent;
  transition: color var(--fast) var(--ease), background-color var(--fast) var(--ease),
    border-color var(--fast) var(--ease), transform var(--fast) var(--ease);
}
.alias {
  min-height: 36px;
  padding-inline: 1px;
  border-color: var(--line);
  background: var(--inset);
  font-family: ui-monospace, "SFMono-Regular", Consolas, monospace;
  font-size: .8125rem;
  font-weight: 650;
}
.alias:hover, .alias.is-inserted { background: var(--selected); border-color: #5578ab; color: var(--ink); }
.alias:active, .button:active, .icon-button:active { transform: scale(.97); }
.button { width: fit-content; min-height: 44px; padding: 8px 14px; gap: 8px; font-weight: 620; }
.primary { background: var(--blue); border-color: var(--blue); color: #0b1d39; }
.primary:hover { background: #abcaff; border-color: #abcaff; }
.secondary { background: var(--inset); border-color: var(--control-line); color: var(--ink); }
.secondary:hover, .quiet:hover { background: var(--hover); color: var(--ink); }
.danger { background: #a13343; border-color: #ba4d5b; color: #fff3f3; }
.danger:hover { background: #b14151; }
.add-button { margin-top: 16px; }
button:disabled { cursor: wait; opacity: .62; }

.scale summary {
  min-height: 48px;
  padding: 10px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  border-radius: 14px;
  color: var(--secondary);
  font-weight: 600;
  list-style: none;
  transition: color var(--fast) var(--ease), background-color var(--fast) var(--ease);
}
.scale summary::-webkit-details-marker { display: none; }
.scale summary:hover { background: var(--hover); color: var(--ink); }
.scale summary .icon { color: var(--muted); transition: transform var(--motion) var(--ease); }
.scale[open] summary .icon { transform: rotate(180deg); }
.scale dl { padding: 0 20px 12px; }
.scale[open] dl { animation: arrive var(--motion) var(--ease) both; }
.scale dl div { display: flex; justify-content: space-between; gap: 8px; padding-block: 6px; border-top: 1px solid var(--line); color: var(--secondary); }
.scale dd { color: var(--muted); flex: none; font-variant-numeric: tabular-nums; }

.metrics {
  display: grid;
  grid-template-columns: 1.25fr repeat(5, minmax(0, 1fr));
  margin-top: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--inset);
  font-variant-numeric: tabular-nums;
}
.metric { min-width: 0; padding: 10px 12px; }
.metric + .metric { border-left: 1px solid var(--line); }
.metric dt { color: var(--muted); font-size: .8125rem; }
.metric dd { margin-top: 3px; font-size: clamp(1.0625rem, 1.65vw, 1.375rem); font-weight: 620; letter-spacing: -.02em; line-height: 1.18; overflow-wrap: anywhere; }
.metric.average dd { color: var(--accent); }
.percent-unit { font-size: .72em; margin-left: 1px; }
.estimate { margin-top: 8px; color: var(--muted); font-size: .8125rem; }
.filters, .band-counts { display: flex; flex-wrap: wrap; gap: 6px; }
.filters { margin-top: 16px; }
.filters a {
  min-height: 36px;
  min-width: 0;
  max-width: 100%;
  text-align: start;
  gap: 8px;
  padding: 5px 10px;
  border-color: var(--line);
  border-radius: 20px;
  font-size: .8125rem;
  font-weight: 550;
  overflow-wrap: anywhere;
}
.filters a:hover { background: var(--hover); border-color: var(--control-line); }
.filters a[data-pending] { border-color: #567ab2; }
.filters a[aria-current] { color: var(--ink); background: var(--selected); border-color: #567ab2; }
.chip-count { color: var(--muted); font-variant-numeric: tabular-nums; }
.filters a[aria-current] .chip-count { color: var(--accent); }
.band-counts { padding: 12px 0 0; margin-top: 12px; border-top: 1px solid var(--line); list-style: none; }
.band-counts li {
  display: flex;
  align-items: baseline;
  gap: 8px;
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 3px 8px;
  color: var(--secondary);
  font-size: .75rem;
  line-height: 1.4;
}

.history-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 16px 20px; }
.history-header p { margin-top: 2px; color: var(--muted); font-size: .75rem; }
.clear-link { color: var(--muted); padding-inline: 10px; font-size: .8125rem; font-weight: 550; flex: none; }
.clear-link:hover, .delete-button:hover { color: var(--danger); background: #321f2a; border-color: #583143; }
.entries { width: 100%; border-collapse: collapse; table-layout: fixed; text-align: start; }
.entries thead { background: var(--inset); }
.entries th, .entries td { border-top: 1px solid var(--line); padding: 10px 8px; text-align: start; overflow-wrap: anywhere; }
.entries th { color: var(--muted); font-size: .75rem; font-weight: 550; }
.entries th:first-child, .entries td:first-child { padding-left: 20px; }
.entries th:last-child, .entries td:last-child { padding-right: 12px; }
.entries tbody tr { transition: background-color var(--fast) var(--ease); }
.entries tbody tr:hover, .entries tbody tr:focus-within { background: var(--hover); }
.entries .date-col { width: 16%; }
.entries .subject-col { width: 13%; }
.entries .input-col { width: 12%; }
.entries .percent-col { width: 11%; }
.entries .band-col { width: 16%; }
.entries .actions-col { width: 88px; }
.entries th:nth-child(5), .entries .row-percent { text-align: end; }
.row-when { color: var(--secondary); font-size: .8125rem; font-variant-numeric: tabular-nums; }
.row-time { display: block; color: var(--muted); }
.row-label { color: var(--secondary); }
.row-label.missing { color: var(--muted); }
.row-input code {
  display: inline-block;
  background: var(--inset);
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 2px 5px;
  color: var(--secondary);
  font-family: ui-monospace, "SFMono-Regular", Consolas, monospace;
  font-size: .75rem;
  overflow-wrap: anywhere;
}
.row-percent { font-variant-numeric: tabular-nums; font-weight: 650; }
.row-band { color: var(--secondary); font-size: .8125rem; }
.action-group { display: flex; justify-content: flex-end; gap: 2px; }
.icon-button { width: 34px; height: 36px; padding: 0; color: var(--muted); }
.icon-button:hover { color: var(--ink); background: var(--inset); border-color: var(--line); }
.icon-button.delete-button:hover { color: var(--danger); background: #321f2a; border-color: #583143; }
.mobile-input-label { display: none; }
.empty { border-top: 1px solid var(--line); padding: 20px; color: var(--muted); }

.dialog { width: min(calc(100% - 32px), 440px); max-height: calc(100% - 32px); padding: 20px; color: var(--ink); }
.dialog[open] { animation: arrive var(--motion) var(--ease) both; }
.dialog::backdrop { background: rgb(5 10 19 / .74); }
.dialog-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.dialog-head .icon-button { width: 44px; height: 44px; margin: -8px -8px -8px 0; }
.dialog p, .operation p { color: var(--secondary); }
.dialog .field-error { color: var(--danger); }
.dialog .fields, .operation .fields { margin-top: 12px; }
.dialog-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; margin-top: 20px; }
.operation { max-width: 480px; }
.operation h1 { font-size: 1.5rem; margin-bottom: 16px; }
.operation p + a { margin-top: 20px; }
.inline-notice {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  background: var(--panel);
}
.inline-notice.error { color: var(--danger); }
.toasts {
  position: fixed;
  z-index: 20;
  bottom: max(20px, env(safe-area-inset-bottom));
  left: max(20px, calc((100vw - 1200px) / 2));
  width: min(calc(100% - 40px), 400px);
  display: grid;
  gap: 8px;
  pointer-events: none;
}
.toast {
  display: flex;
  gap: 8px;
  align-items: center;
  border: 1px solid var(--control-line);
  border-radius: 10px;
  padding: 6px 8px 6px 14px;
  background: #19243a;
  pointer-events: auto;
  animation: arrive var(--motion) var(--ease) both;
}
.toast-message { flex: 1; overflow-wrap: anywhere; }
.toast.is-error .toast-message { color: var(--danger); }
.toast-undo { color: var(--accent); padding-inline: 8px; }
.toast-close { flex: none; width: 44px; height: 44px; }

@keyframes arrive { from { opacity: .4; transform: translateY(4px); } to { opacity: 1; transform: translateY(0); } }

/* The shared home control stays available in the calculator's palette. */
body #vivan-back-pill {
  min-width: 44px;
  min-height: 44px;
  background: var(--panel);
  border-color: var(--line);
  padding: 8px;
  box-shadow: none;
  transition: background-color var(--fast) var(--ease), border-color var(--fast) var(--ease);
}
body #vivan-back-pill:hover, body #vivan-back-pill:focus-visible {
  background: var(--hover);
  border-color: var(--control-line);
  padding: 8px;
  transform: none;
  box-shadow: none;
}
body #vivan-back-pill:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
body #vivan-back-pill .vbp-icon { background: var(--selected); color: var(--accent); box-shadow: none; }

@media (min-width: 1121px) {
  .band-counts { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .band-counts li { justify-content: space-between; }
}
@media (max-width: 1120px) {
  .metrics { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .metric:nth-child(4) { border-left: 0; }
  .metric:nth-child(n+4) { border-top: 1px solid var(--line); }
}
@media (max-width: 960px) {
  .page { width: min(calc(100% - 48px), 740px); padding-top: 32px; }
  .workspace { grid-template-columns: minmax(0, 1fr); gap: 16px; }
  .composer h2 { margin-bottom: 12px; }
  .metrics { grid-template-columns: 1.25fr repeat(5, minmax(0, 1fr)); }
  .metric:nth-child(4) { border-left: 1px solid var(--line); }
  .metric:nth-child(n+4) { border-top: 0; }
  .alias, .filters a { min-height: 44px; }
  .icon-button { width: 44px; height: 44px; }
  .entries .actions-col { width: 105px; }
}
@media (max-width: 620px) {
  .page { width: calc(100% - 32px); padding-top: 20px; }
  .page-header { margin-bottom: 14px; }
  h1 { font-size: clamp(1.625rem, 7vw, 1.9375rem); }
  .workspace { gap: 12px; }
  .entry-tools { gap: 0; }
  .composer, .statistics { padding: 16px; }
  .entry-tools .composer { border-bottom-left-radius: 0; border-bottom-right-radius: 0; }
  .scale { border-top: 0; border-top-left-radius: 0; border-top-right-radius: 0; }
  .scale summary { min-height: 44px; border-top-left-radius: 0; border-top-right-radius: 0; }
  textarea { min-height: 64px; }
  .composer .fields { gap: 8px; }
  .alias-group { margin-block: 10px 12px; }
  .aliases { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .add-button { margin-top: 12px; }
  .scale summary { padding-inline: 16px; }
  .scale dl { padding-inline: 16px; }
  .metrics { grid-template-columns: repeat(3, minmax(0, 1fr)); margin-top: 12px; }
  .metric { padding: 8px 10px; }
  .metric dd { font-size: 1.125rem; }
  .metric:nth-child(4) { border-left: 0; }
  .metric:nth-child(n+4) { border-top: 1px solid var(--line); }
  .filters { margin-top: 12px; }
  .history-header { padding: 12px 16px; }
  .entries, .entries tbody { display: block; }
  .entries colgroup { display: none; }
  .entries thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
  .entries tbody tr {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(88px, 36%);
    grid-template-areas: "when actions" "subject percent" "label band" "input band";
    gap: 4px 8px;
    align-items: start;
    padding: 8px 16px 12px;
    border-top: 1px solid var(--line);
  }
  .entries td, .entries td:first-child, .entries td:last-child { border: 0; padding: 0; min-width: 0; }
  .entries .row-when { grid-area: when; align-self: center; }
  .row-time { display: inline; margin-left: 4px; }
  .row-subject { grid-area: subject; font-weight: 650; }
  .row-label { grid-area: label; font-size: .8125rem; }
  .row-label.missing { display: none; }
  .row-input { grid-area: input; }
  .mobile-input-label { display: inline; color: var(--muted); font-size: .75rem; margin-right: 4px; }
  .row-percent { grid-area: percent; font-size: 1rem; }
  .row-band { grid-area: band; text-align: end !important; }
  .row-actions { grid-area: actions; }
  .toasts { bottom: max(64px, calc(env(safe-area-inset-bottom) + 56px)); left: 16px; width: calc(100% - 32px); }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0s !important; transition-duration: 0s !important; }
}

/* On small screens make the outcome visible before the optional reference scale. */
@media (max-width:620px) {
  .workspace { display: flex; flex-direction: column; gap: 12px; }
  .entry-tools, .results { display: contents; }
  .composer { order: 1; width: 100%; }
  .statistics { order: 2; width: 100%; }
  .scale { order: 3; width: 100%; }
  .history { order: 4; width: 100%; }
  .entry-tools .composer { border-radius: 14px; }
  .scale { border: 1px solid var(--line); border-radius: 14px; }
  .scale summary { border-radius: 14px; }
}
