:root {
  color-scheme: dark;
  --background: #0c1424;
  --glass: #152137;
  --panel: #19263e;
  --field: #101b30;
  --line: #334864;
  --line-soft: #283a53;
  --text: #f0f5ff;
  --muted: #aabbcf;
  --accent: #87c6ff;
  --green: #63e3b2;
  --amber: #ffce8b;
  --red: #ff9eaa;
  --font: Inter, "Segoe UI", system-ui, sans-serif;
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
body {
  margin: 0;
  color: var(--text);
  font: 1rem/1.5 var(--font);
  background: radial-gradient(ellipse at 12% 1%, #193555 0, transparent 39%),
    radial-gradient(ellipse at 91% 19%, #152344 0, transparent 40%), var(--background);
  min-height: 100vh;
}

#header, #main, #footer { width: min(1300px, calc(100% - 3rem)); margin-inline: auto; }
#header { padding-top: 1.3rem; }
#main { min-height: 62vh; }
#footer { color: var(--muted); font-size: .875rem; padding: 1.65rem 0 2.2rem; }
a { color: var(--accent); text-underline-offset: .2em; }
.skip { position: absolute; transform: translateY(-160%); z-index: 3; background: var(--field); padding: .6rem; }
.skip:focus { transform: none; }

.visually-hidden:where(:not(:focus-within, :active)) {
  position: absolute !important;
  clip-path: inset(50%) !important;
  overflow: hidden !important;
  width: 1px !important;
  height: 1px !important;
  margin: -1px !important;
  padding: 0 !important;
  border: 0 !important;
  white-space: nowrap !important;
}

.topbar, .tools, .actions, .page-top, .inline-group, .choice-title {
  display: flex; align-items: center; gap: .6rem; flex-wrap: wrap;
}
.topbar { justify-content: space-between; gap: 1rem; padding-bottom: .9rem; }
.brand { display: flex; align-items: center; gap: .75rem; min-width: 0; }
.brand img { width: 42px; height: 42px; border-radius: 12px; flex: none; }
.brand strong { display: block; font-size: 1.2rem; letter-spacing: -.025em; }
.brand span { display: block; font-size: .86rem; color: var(--muted); }
.quick-preset { min-width: 170px; max-width: 260px; }

/* Single-row horizontal scrollable navigation per tabs/navigation guidance */
.nav {
  display: flex;
  align-items: center;
  flex-wrap: nowrap;
  overflow-x: auto;
  scrollbar-width: thin;
  scroll-snap-type: inline proximity;
  padding: .45rem 0;
  border-top: 1px solid var(--line-soft);
  border-bottom: 1px solid var(--line-soft);
  gap: .3rem;
}
.nav button {
  position: relative;
  flex: none;
  scroll-snap-align: start;
  background: transparent;
  padding: .48rem .74rem;
  border-color: transparent;
  color: var(--muted);
  white-space: nowrap;
}
.nav button:hover { color: var(--text); background: #203753; }
.nav button[aria-current="page"] {
  color: var(--accent);
  background: #1b314c;
}
.nav button[aria-current="page"]::after {
  content: "";
  position: absolute;
  inset: auto 0 0;
  border-block-end: 3px solid var(--accent);
  border-radius: 2px;
}

.page-top { justify-content: space-between; margin-block: 1.15rem .95rem; }
h1, h2, h3, p { margin: 0; }
h1 { font-size: clamp(1.62rem, 3vw, 2.3rem); letter-spacing: -.04em; line-height: 1.13; }
h2 { font-size: 1.13rem; letter-spacing: -.015em; }
h3 { font-size: 1rem; }
p + p { margin-top: .5rem; }
.sub { color: var(--muted); font-size: .94rem; margin-top: .28rem; max-width: 75ch; }
.muted, .meta { color: var(--muted); }
.meta { font-size: .85rem; }

.well {
  border: 1px solid var(--line);
  background: linear-gradient(135deg, #1a2a43, #142037 85%);
  border-radius: 15px;
  padding: 1.05rem 1.12rem;
  min-width: 0;
}
.well > h2 { margin-bottom: .82rem; }
.well > * + .actions, .well > .actions + p, .well > * + .stack { margin-top: .7rem; }
.stack { display: grid; gap: .85rem; min-width: 0; }
.two-col { display: grid; gap: .85rem; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); align-items: start; }
.home-grid { grid-template-columns: minmax(0, 1.68fr) minmax(290px, 1fr); }
.metrics { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: .65rem; margin-bottom: 1rem; }
.metric {
  border: 1px solid var(--line);
  border-radius: 13px;
  padding: .75rem .85rem;
  background: linear-gradient(145deg, #203b5a, #17253f);
  min-width: 0;
}
.metric span { display: block; font-size: .86rem; color: var(--muted); }
.metric strong {
  display: block;
  font-size: clamp(1.03rem, 1.85vw, 1.42rem);
  letter-spacing: -.03em;
  overflow-wrap: anywhere;
  font-variant-numeric: tabular-nums;
}
.gain { color: var(--green); }
.loss, .error { color: var(--red); }
.warning { color: var(--amber); }
.accent { color: var(--accent); }

label { min-width: 0; }
label > span { display: block; margin-bottom: .3rem; font-size: .86rem; color: var(--muted); }
label.check { display: flex; align-items: center; gap: .48rem; min-height: 2rem; cursor: pointer; }
label.check > span { margin: 0; color: var(--text); }

input, select, textarea, button { font: inherit; }
input:not([type="checkbox"]), select, textarea {
  width: 100%; color: var(--text); background: var(--field); border: 1px solid var(--line); border-radius: 8px; padding: .48rem .62rem;
  min-height: 2.5rem; outline-offset: 3px; font-size: .94rem;
}
input[type="checkbox"] { accent-color: var(--accent); width: 1.05rem; height: 1.05rem; flex: none; }
textarea { display: block; resize: vertical; min-height: 112px; }
select {
  appearance: none;
  padding-right: 1.85rem;
  background-image: linear-gradient(45deg, transparent 50%, #c4d2e3 50%), linear-gradient(135deg, #c4d2e3 50%, transparent 50%);
  background-position: calc(100% - 15px) center, calc(100% - 10px) center;
  background-size: 5px 5px; background-repeat: no-repeat;
}
.topbar select { width: auto; min-height: 2.35rem; }

button {
  border: 1px solid #3c5371; background: #1d314d; color: var(--text); font-weight: 600; font-size: .9rem; line-height: 1.3;
  border-radius: 8px; padding: .52rem .72rem; min-height: 2.38rem; cursor: pointer;
}
button:hover { background: #2a4668; }
button.primary { background: var(--accent); border-color: var(--accent); color: #0d2339; }
button.primary:hover { background: #abd8ff; }
button.danger { color: var(--red); }
button.small { min-height: 1.95rem; padding: .28rem .55rem; font-size: .83rem; }
button:disabled, input:disabled { opacity: .48; cursor: default; }
:focus-visible { outline: 2px solid #a6dbff; outline-offset: 3px; }

:is(:user-invalid, [aria-invalid="true"]) {
  border-color: var(--red) !important;
}
.error-msg {
  display: none;
  color: var(--red);
  font-size: .82rem;
  margin-top: .25rem;
}
.error-msg.visible,
:is(:user-invalid, [aria-invalid="true"]) ~ .error-msg {
  display: block;
}

.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(155px, 100%), 1fr)); gap: .7rem; }
.fields-2 { grid-template-columns: 1fr 1fr; }
.filters { grid-template-columns: minmax(0, 1fr) minmax(170px, .75fr); margin-bottom: .7rem; }
.catalog { display: grid; gap: .26rem; max-height: 390px; overflow-y: auto; padding: .12rem .25rem .25rem .12rem; }
.catalog button { width: 100%; text-align: left; background: #142239; border-color: #283f58; padding: .55rem .72rem; }
.catalog button:hover { background: #213954; }
.catalog button[aria-pressed="true"] { background: #284564; border-color: #70a5d6; }
.choice-title { gap: .55rem; }
.choice-title strong { font-weight: 650; }
.choice-title .meta { margin-left: auto; }
.recipe-line { display: flex; justify-content: space-between; gap: .5rem; padding: .36rem 0; border-bottom: 1px solid var(--line-soft); }
.recipe-line:last-child { border-bottom: 0; }

.tentative, .notice { margin-top: .6rem; padding: .55rem .72rem; color: var(--muted); font-size: .9rem; background: #101c31; border-radius: 8px; }
.notice.warning { color: var(--amber); border-left: 3px solid var(--amber); }
.notice.error { color: var(--red); border-left: 3px solid var(--red); }
.notice.gain { color: var(--green); border-left: 3px solid var(--green); }
.empty { color: var(--muted); padding: .7rem 0; }

/* Responsive tables with sticky thead & container queries */
.table-wrap {
  overflow: auto;
  max-inline-size: 100%;
  max-block-size: min(560px, 72vh);
  container-type: inline-size;
  border-radius: 8px;
}
table { width: 100%; border-collapse: separate; border-spacing: 0; font-size: .9rem; font-variant-numeric: tabular-nums; }
caption { text-align: left; color: var(--muted); padding-bottom: .4rem; font-size: .86rem; }
thead { position: sticky; inset-block-start: 0; z-index: 1; background: #17263f; }
th, td { padding: .52rem .5rem; text-align: left; border-bottom: 1px solid var(--line-soft); }
th { color: var(--muted); font-weight: 600; }
td.number, th.number { text-align: right; white-space: nowrap; }
tbody th { position: sticky; inset-inline-start: 0; background: #16233a; font-weight: 600; z-index: 0; }
tbody td:first-child { font-weight: 600; }
tbody tr:hover td, tbody tr:hover th { background: #223752; }
.materials table { min-width: 810px; }
.materials input { min-width: 84px; font-variant-numeric: tabular-nums; }
.chain-table table { min-width: 880px; }

.details { display: grid; gap: .35rem; margin: 0; }
.details > div { display: grid; grid-template-columns: minmax(0, 1.25fr) minmax(0, 1fr); gap: .65rem; padding: .25rem 0; border-bottom: 1px solid var(--line-soft); }
.details > div:last-child { border-bottom: 0; }
dt { color: var(--muted); font-size: .92rem; }
dd { margin: 0; text-align: right; font-weight: 650; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
details > summary { cursor: pointer; color: var(--accent); font-weight: 650; padding: .35rem 0; }
details > .form-grid { margin-top: .55rem; }

progress { display: block; width: 100%; height: .72rem; accent-color: var(--accent); margin: .55rem 0; border-radius: 99px; overflow: hidden; }
progress.good { accent-color: var(--green); }
progress.warn { accent-color: var(--amber); }
progress.bad { accent-color: var(--red); }

.market-entries { font-family: "Cascadia Mono", Consolas, monospace; }
.market-columns { max-width: 580px; margin-bottom: .65rem; }
.market-result table { min-width: 390px; }
.ticker-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(185px, 1fr)); gap: .55rem; }
.ticker-card {
  background: #121f35; border: 1px solid var(--line-soft); border-radius: 10px; padding: .62rem .72rem;
  display: grid; gap: .18rem;
}
.ticker-card strong { font-size: 1.08rem; font-variant-numeric: tabular-nums; }
.ticker-code { font-size: .78rem; color: var(--accent); font-weight: 700; letter-spacing: .04em; }

.badge {
  display: inline-block; padding: .15rem .48rem; border-radius: 99px; font-size: .78rem; font-weight: 650;
  background: #1c3453; color: var(--accent); border: 1px solid #355a85;
}
.badge.make { background: #143731; color: var(--green); border-color: #2d6f5c; }
.badge.buy { background: #362c18; color: var(--amber); border-color: #70572b; }
.badge.raw { background: #1e2a3f; color: var(--muted); border-color: var(--line); }

.days { display: flex; gap: .35rem; flex-wrap: wrap; margin-top: .75rem; }
.days button { min-height: 2rem; padding: .28rem .52rem; }
.periods { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: .55rem; }
.periods div { background: #14233a; border-radius: 8px; padding: .55rem; overflow-wrap: anywhere; }
.periods strong { display: block; }
.section-spacer { margin-top: .85rem; }

dialog {
  color: var(--text);
  background: #17243b;
  border: 1px solid #486683;
  border-radius: 14px;
  padding: 1.15rem;
  width: min(700px, calc(100% - 1.5rem));
  max-height: calc(100% - 1.5rem);
}
dialog:modal { overscroll-behavior: contain; }
dialog::backdrop {
  background-color: rgba(8, 14, 25, 0.74);
  backdrop-filter: blur(2px);
  overflow: hidden;
  overscroll-behavior: contain;
}
dialog h1 { font-size: 1.25rem; margin-bottom: .75rem; }
.recipe-edit-row { display: grid; grid-template-columns: minmax(0, 1fr) minmax(85px, .35fr) auto; gap: .5rem; align-items: end; }
.inline-select { width: auto; min-height: 1.95rem; padding: .24rem 1.7rem .24rem .48rem; font-size: .84rem; }

#status:empty { display: none; }
#status {
  position: fixed; right: 1rem; bottom: 1rem; z-index: 4;
  max-width: min(460px, calc(100% - 2rem)); padding: .7rem .85rem;
  border-radius: 9px; border: 1px solid var(--line); background: #1e3450;
  box-shadow: 0 8px 26px #050c1660; font-size: .92rem;
}
.loading { padding-top: 2rem; color: var(--muted); }

@media (max-width: 780px) {
  #header, #main, #footer { width: min(100% - 1.5rem, 1300px); }
  .two-col, .home-grid { grid-template-columns: minmax(0, 1fr); }
  .metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .market-columns { max-width: none; }
}
@media (max-width: 450px) {
  .well { padding: .82rem; }
  .filters, .fields-2 { grid-template-columns: minmax(0, 1fr); }
  .periods { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .choice-title .meta { margin-left: 0; }
  .nav button { padding-inline: .52rem; }
  .recipe-edit-row { grid-template-columns: minmax(0, 1fr) minmax(76px, .38fr); }
  .recipe-edit-row button { grid-column: 1 / -1; justify-self: start; }
}
