/* ============================================================
   DYVA app shell + components
   ============================================================ */

/* ---------- App layout ---------- */
.app { display: grid; grid-template-columns: var(--sidebar-w) 1fr; height: 100vh; overflow: hidden; }

/* ---------- Sidebar ---------- */
.sidebar {
  background: var(--sidebar);
  border-right: 1px solid var(--hairline);
  display: flex; flex-direction: column;
  padding: 20px 14px;
  min-width: 0;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 18px; }
.brand .mark {
  width: 34px; height: 34px; border-radius: 9px; flex: none;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  display: grid; place-items: center; color: #fff; font-weight: 700; font-size: 1rem;
  box-shadow: var(--shadow-sm);
}
.brand .brand-mark { width: 34px; height: 34px; flex: none; border-radius: 9px; box-shadow: var(--shadow-sm); display: block; }
.brand .name { font-size: 1.12rem; font-weight: 700; letter-spacing: -0.03em; }
.brand .name sup { font-size: .5rem; color: var(--ink-3); font-weight: 600; }
.brand .sub { font-size: var(--fs-xs); color: var(--ink-3); margin-top: 1px; }

.nav { display: flex; flex-direction: column; gap: 2px; margin-top: 6px; }
/* Until the server has said which build this is, the menu would list every
   page; on a cold start that is long enough to read. app.js adds .ready. */
.nav:not(.ready) { visibility: hidden; }
.nav-label { font-size: var(--fs-xs); font-weight: 600; text-transform: uppercase; letter-spacing: .06em;
  color: var(--ink-3); padding: 14px 10px 6px; }
.nav-item {
  display: flex; align-items: center; gap: 11px;
  padding: 9px 11px; border-radius: var(--r-sm);
  color: var(--ink-2); font-size: .9rem; font-weight: 500; cursor: pointer;
  transition: background var(--dur) var(--ease), color var(--dur) var(--ease);
  user-select: none; border: none; background: none; width: 100%; text-align: left;
}
.nav-item:hover { background: #eef0f3; color: var(--ink); }
.nav-item.active { background: var(--accent-soft); color: var(--accent-ink); font-weight: 600; }
.nav-item .ic { width: 18px; height: 18px; flex: none; opacity: .9; }
.nav-item.active .ic { opacity: 1; }

.sidebar .spacer { flex: 1; }
.userchip {
  display: flex; align-items: center; gap: 10px; padding: 9px 10px;
  border-radius: var(--r-sm); background: var(--bg-elev); box-shadow: var(--shadow-xs);
  border: 1px solid var(--hairline);
}
.userchip .avatar { width: 30px; height: 30px; border-radius: 50%; flex: none;
  background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: #fff;
  display: grid; place-items: center; font-weight: 600; font-size: .82rem; }
.userchip .who { min-width: 0; flex: 1; }
.userchip .who b { font-size: .82rem; font-weight: 600; display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.userchip .who span { font-size: var(--fs-xs); color: var(--ink-3); }
.userchip .logout { color: var(--ink-3); cursor: pointer; padding: 4px; border-radius: 8px; display: grid; place-items: center; }
.userchip .logout:hover { color: var(--red); background: var(--red-soft); }
.userchip .who span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
/* Hosted build: the account pages, as plain links above the user chip. */
.side-links { display: flex; gap: 16px; padding: 0 8px 8px; font-size: var(--fs-xs); font-weight: 600; }
.side-links a { color: var(--ink-3); text-decoration: none; }
.side-links a:hover { color: var(--accent-ink); }
/* A field the server refused. */
.field input.invalid, .field select.invalid { border-color: var(--red); box-shadow: 0 0 0 3px var(--red-soft); }

/* ---------- Main region ---------- */
.main { display: flex; flex-direction: column; min-width: 0; overflow: hidden; }
.topbar {
  height: var(--topbar-h); flex: none;
  display: flex; align-items: center; gap: 16px;
  padding: 0 28px; background: rgba(255,255,255,.72);
  backdrop-filter: saturate(180%) blur(20px); -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 1px solid var(--hairline);
}
.topbar h1 { font-size: 1.22rem; font-weight: 650; }
.topbar .crumb { color: var(--ink-3); font-weight: 500; }
.topbar .grow { flex: 1; }
.topbar-logo { height: 28px; width: auto; flex: none; margin-left: 14px; opacity: .95; }
.content { flex: 1; overflow-y: auto; padding: 28px; }
.content-inner { max-width: 1340px; margin: 0 auto; }
.page { animation: fade .28s var(--ease); }
.page[hidden] { display: none; }
@keyframes fade { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
.page-lead { color: var(--ink-2); font-size: .95rem; margin: 2px 0 22px; max-width: 60ch; }

/* ---------- Cards ---------- */
.card { background: var(--card); border-radius: var(--r); box-shadow: var(--shadow-sm); padding: 22px 24px; }
.card + .card { margin-top: 18px; }
.card-h { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; }
.card-h h3 { font-size: var(--fs-h2); font-weight: 650; }
.card-h .grow { flex: 1; }
.card-sub { color: var(--ink-3); font-size: var(--fs-sm); margin-top: -8px; margin-bottom: 16px; }
.grid { display: grid; gap: 18px; }
.grid-2 { grid-template-columns: 1fr 1fr; }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.grid-4 { grid-template-columns: repeat(4, 1fr); }
@media (max-width: 900px){ .grid-2,.grid-3 { grid-template-columns: 1fr; } }

/* ---------- KPI cards ---------- */
/* Auto-fit rather than a fixed four: the Contracts page carries five tiles and
   a hard repeat(4) pushed the fifth onto its own full-width row. */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 16px; }
@media (max-width: 1000px){ .kpis { grid-template-columns: repeat(2,1fr); } }
.kpi { background: var(--card); border-radius: var(--r); box-shadow: var(--shadow-sm); padding: 18px 20px; position: relative; overflow: hidden; }
.kpi .k-label { font-size: var(--fs-xs); text-transform: uppercase; letter-spacing: .05em; color: var(--ink-3); font-weight: 600; }
.kpi .k-val { font-size: 1.7rem; font-weight: 680; margin-top: 8px; letter-spacing: -0.03em; }
.kpi .k-val small { font-size: .82rem; color: var(--ink-3); font-weight: 500; letter-spacing: 0; }
.kpi .k-sub { font-size: var(--fs-xs); color: var(--ink-3); margin-top: 3px; }
.kpi .k-ic { position: absolute; top: 16px; right: 16px; width: 30px; height: 30px; border-radius: 9px;
  background: var(--accent-soft); color: var(--accent-ink); display: grid; place-items: center; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 9px 16px; border-radius: var(--r-sm); border: 1px solid transparent;
  font-size: .88rem; font-weight: 600; cursor: pointer; white-space: nowrap;
  transition: all var(--dur) var(--ease); background: var(--bg-elev); color: var(--ink);
}
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn .ic { width: 16px; height: 16px; }
.btn-primary { background: var(--accent); color: #fff; box-shadow: 0 1px 2px rgba(10,132,192,.35), 0 6px 16px rgba(10,132,192,.28); }
.btn-primary:hover:not(:disabled) { background: var(--accent-ink); transform: translateY(-1px); box-shadow: 0 2px 4px rgba(10,132,192,.4), 0 10px 24px rgba(10,132,192,.32); }
.btn-secondary { background: var(--bg-elev); color: var(--ink); border-color: var(--hairline); box-shadow: var(--shadow-xs); }
.btn-secondary:hover:not(:disabled) { background: #f3f4f6; border-color: #dcdfe4; }
.btn-ghost { background: transparent; color: var(--ink-2); }
.btn-ghost:hover:not(:disabled) { background: #eef0f3; color: var(--ink); }
.btn-danger { background: var(--red-soft); color: var(--red); }
.btn-danger:hover:not(:disabled) { background: #ffdedb; }
.btn-lg { padding: 12px 22px; font-size: .95rem; border-radius: 12px; }
.btn-sm { padding: 6px 12px; font-size: .8rem; border-radius: 9px; }
.btn-block { width: 100%; }

/* ---------- Forms ---------- */
.field { margin-bottom: 14px; }
.field:last-child { margin-bottom: 0; }
label.lbl { display: block; font-size: var(--fs-sm); font-weight: 600; color: var(--ink-2); margin-bottom: 6px; }
.hint { font-size: var(--fs-xs); color: var(--ink-3); margin-top: 5px; }
input:not([type]), input[type=text], input[type=search], input[type=date], input[type=password], input[type=number], select, textarea {
  width: 100%; padding: 10px 13px; font-size: .9rem; font-family: inherit; color: var(--ink);
  background: var(--bg); border: 1px solid var(--hairline); border-radius: var(--r-sm);
  transition: border var(--dur) var(--ease), box-shadow var(--dur) var(--ease), background var(--dur) var(--ease);
  outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); background: #fff; box-shadow: 0 0 0 4px var(--accent-soft); }
textarea { resize: vertical; min-height: 90px; line-height: 1.5; }
select { appearance: none; background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath d='M3 4.5L6 7.5L9 4.5' stroke='%2386868b' stroke-width='1.5' fill='none' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 12px center; padding-right: 32px; }

/* Range slider */
.range-row { display: flex; align-items: center; gap: 14px; }
input[type=range] { -webkit-appearance: none; appearance: none; height: 5px; border-radius: 999px; background: var(--hairline-2); flex: 1; }
input[type=range]::-webkit-slider-thumb { -webkit-appearance: none; width: 18px; height: 18px; border-radius: 50%; background: #fff;
  border: 1px solid var(--hairline); box-shadow: var(--shadow-sm); cursor: pointer; transition: transform .12s; }
input[type=range]::-webkit-slider-thumb:hover { transform: scale(1.12); }
.range-val { font-variant-numeric: tabular-nums; font-weight: 600; min-width: 46px; text-align: right; color: var(--accent-ink); }

/* Toggle */
.toggle { position: relative; width: 42px; height: 25px; flex: none; }
.toggle input { opacity: 0; width: 0; height: 0; }
.toggle .track { position: absolute; inset: 0; background: #d5d8de; border-radius: 999px; transition: background var(--dur); }
.toggle .track::before { content:''; position: absolute; width: 21px; height: 21px; left: 2px; top: 2px; background: #fff; border-radius: 50%; box-shadow: var(--shadow-sm); transition: transform var(--dur) var(--ease); }
.toggle input:checked + .track { background: var(--accent); }
.toggle input:checked + .track::before { transform: translateX(17px); }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; border-radius: var(--r-sm); border: 1px solid var(--hairline); }
table.tbl { width: 100%; border-collapse: collapse; font-size: .86rem; }
table.tbl th { text-align: left; font-weight: 600; color: var(--ink-3); font-size: var(--fs-xs); text-transform: uppercase; letter-spacing: .04em;
  padding: 11px 14px; background: #fafbfc; border-bottom: 1px solid var(--hairline); white-space: nowrap; }
table.tbl td { padding: 11px 14px; border-bottom: 1px solid var(--hairline-2); color: var(--ink); white-space: nowrap; }
table.tbl tr:last-child td { border-bottom: none; }
table.tbl tbody tr { transition: background .12s; }
table.tbl tbody tr:hover { background: #fafbfd; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
/* `table.tbl th` wins the specificity fight against `.num`, so numeric column
   headers were left-aligned over right-aligned cells. */
table.tbl th.num { text-align: right; }

/* Aligned mini-fields for the sales-mix shares: a grid with top alignment,
   because .row centers unequal-height items and staggers them. */
.mix-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; align-items: start; }
.mix-grid .field { margin-bottom: 0; }
.mix-grid input[type=number] { width: 100%; text-align: right; }

/* ---------- Badges / pills ---------- */
.badge { display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: 999px; font-size: var(--fs-xs); font-weight: 600; }
.badge-ok { background: var(--green-soft); color: #1a7f37; }
.badge-warn { background: var(--amber-soft); color: #9a6400; }
.badge-err { background: var(--red-soft); color: #c4271c; }
.badge-neutral { background: #eef0f3; color: var(--ink-2); }
.dot { width: 8px; height: 8px; border-radius: 50%; }
.dot.ok { background: var(--green); } .dot.warn { background: var(--amber); } .dot.err { background: var(--red); }

/* ---------- Alerts ---------- */
.alert { padding: 12px 16px; border-radius: var(--r-sm); font-size: .88rem; display: flex; gap: 10px; align-items: flex-start; }
.alert-ok { background: var(--green-soft); color: #1a7f37; }
.alert-warn { background: var(--amber-soft); color: #9a6400; }
.alert-err { background: var(--red-soft); color: #c4271c; }
.alert-info { background: var(--accent-soft); color: var(--accent-ink); }

/* ---------- Dropzone ---------- */
.dropzone {
  border: 1.5px dashed #cdd2da; border-radius: var(--r); padding: 34px; text-align: center;
  background: #fbfcfe; transition: all var(--dur) var(--ease); cursor: pointer;
}
.dropzone:hover, .dropzone.drag { border-color: var(--accent); background: var(--accent-soft); }
.dropzone .dz-ic { width: 46px; height: 46px; margin: 0 auto 12px; border-radius: 13px; background: var(--accent-soft); color: var(--accent-ink); display: grid; place-items: center; }
.dropzone h4 { font-size: .98rem; margin-bottom: 4px; }
.dropzone p { font-size: var(--fs-sm); color: var(--ink-3); }

/* ---------- Tabs (results sub-nav) ---------- */
.tabs { display: flex; gap: 4px; background: #eef0f3; padding: 4px; border-radius: 12px; margin-bottom: 20px; overflow-x: auto; }
.tab { padding: 8px 15px; border-radius: 9px; font-size: .85rem; font-weight: 600; color: var(--ink-2); cursor: pointer; white-space: nowrap; border: none; background: none; transition: all .16s var(--ease); }
.tab:hover { color: var(--ink); }
.tab.active { background: #fff; color: var(--accent-ink); box-shadow: var(--shadow-xs); }

/* ---------- Results layout (scenario rail + charts) ---------- */
/* One column. The scenario picker sits on the tab row rather than in a rail of
   its own, which previously left an empty 236px column beside every tab. */
.results-layout { display: block; }
.results-main { min-width: 0; }
.results-bar { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; }
.results-bar .tabs { flex: 1 1 auto; min-width: 0; }
.results-scn { display: flex; align-items: center; gap: 9px; flex: 0 0 auto; }
.results-scn select { min-width: 190px; }
.rail-title { font-size: var(--fs-xs); text-transform: uppercase; letter-spacing: .05em; color: var(--ink-3); font-weight: 600; white-space: nowrap; }
.rail-hint { font-size: var(--fs-xs); color: var(--ink-3); margin: 8px 0 2px; line-height: 1.5; }
@media (max-width: 760px) {
  .results-bar { align-items: stretch; }
  .results-scn { justify-content: space-between; }
}

/* ---------- Filter bar ---------- */
/* Fields sit on one row and wrap as a group; each keeps a sane minimum so a
   long option label cannot squeeze a neighbour into two lines. */
.filter-bar { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 14px;
              padding: 14px 16px; margin-bottom: 16px; }
.filter-bar .field { margin: 0; flex: 0 1 auto; min-width: 168px; }
.filter-bar .field select { width: 100%; }
.filter-bar .grow { flex: 1 1 40px; }
.filter-bar .btn { flex: 0 0 auto; }
@media (max-width: 720px) { .filter-bar .field { min-width: 140px; flex: 1 1 140px; } }

/* ---------- Charts ---------- */
.chart { background: var(--card); border-radius: var(--r); box-shadow: var(--shadow-sm); padding: 16px 18px; }
.chart h4 { font-size: .95rem; font-weight: 600; margin-bottom: 4px; }
.chart .c-sub { font-size: var(--fs-xs); color: var(--ink-3); margin-bottom: 8px; }
.chart .plot { width: 100%; min-height: 340px; }

/* ---------- Chat (AI assistant) ---------- */
.chat { display: flex; flex-direction: column; gap: 12px; }
.bubble { max-width: 82%; padding: 12px 15px; border-radius: 16px; font-size: .9rem; line-height: 1.5; }
.bubble.user { align-self: flex-end; background: var(--accent); color: #fff; border-bottom-right-radius: 5px; }
.bubble.ai { align-self: flex-start; background: #f1f3f6; color: var(--ink); border-bottom-left-radius: 5px; }
.scn-card { border: 1px solid var(--hairline); border-radius: var(--r-sm); padding: 14px 16px; background: #fff; box-shadow: var(--shadow-xs); }
.scn-card h5 { font-size: .92rem; margin-bottom: 8px; }
.scn-metrics { display: flex; flex-wrap: wrap; gap: 8px; }
.scn-metric { background: var(--bg); border-radius: 8px; padding: 5px 10px; font-size: var(--fs-xs); color: var(--ink-2); }
.scn-metric b { color: var(--ink); font-variant-numeric: tabular-nums; }

/* ---------- Misc ---------- */
.row { display: flex; align-items: center; gap: 12px; }
.row.wrap { flex-wrap: wrap; }
.stack { display: flex; flex-direction: column; gap: 8px; }
.spin { width: 18px; height: 18px; border: 2.5px solid var(--accent-soft); border-top-color: var(--accent); border-radius: 50%; animation: rot .7s linear infinite; }
.spin.lg { width: 34px; height: 34px; border-width: 3.5px; }
@keyframes rot { to { transform: rotate(360deg); } }
.center-load { display: grid; place-items: center; gap: 12px; padding: 48px; color: var(--ink-3); }
.empty { text-align: center; padding: 44px 20px; color: var(--ink-3); }
.empty .e-ic { width: 52px; height: 52px; margin: 0 auto 12px; border-radius: 15px; background: #eef0f3; display: grid; place-items: center; }
.mono { font-variant-numeric: tabular-nums; }
.file-row { display: flex; align-items: center; gap: 12px; padding: 12px 4px; border-bottom: 1px solid var(--hairline-2); }
.file-row:last-child { border-bottom: none; }
.file-row .f-ic { width: 34px; height: 34px; border-radius: 9px; background: var(--accent-soft); color: var(--accent-ink); display: grid; place-items: center; flex: none; }
.file-row .f-name { flex: 1; font-size: .88rem; font-weight: 500; }
.file-row .f-size { font-size: var(--fs-xs); color: var(--ink-3); }

/* Toast */
#toast-wrap { position: fixed; bottom: 24px; right: 24px; display: flex; flex-direction: column; gap: 10px; z-index: 9999; }
.toast { background: var(--brand-navy); color: #fff; padding: 13px 18px; border-radius: 12px; font-size: .88rem; box-shadow: var(--shadow-lg);
  display: flex; align-items: center; gap: 10px; animation: toastin .3s var(--ease); max-width: 380px; }
.toast.ok { background: #10321f; } .toast.err { background: #3a1512; }
@keyframes toastin { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }

/* Event-scenario narrative. The paragraph explaining why an event is worth
   asking, shown under its name on the Scenario page. */
.ev-story {
  margin-top: 8px;
  color: var(--ink-2);
  font-size: .84rem;
  line-height: 1.55;
  max-width: 82ch;
}
.ev-story + .ev-story { margin-top: 6px; }

/* Step chips and the locked state on the two analysis pages.
   Locked is dimmed, not disabled: the page still opens and explains that it
   needs a run, which is a better failure in front of a client than a nav item
   that silently stops responding. */
.nav-item .step {
  flex: none;
  width: 18px; height: 18px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 50%;
  border: 1px solid var(--hairline);
  font-size: 10px; font-weight: 700; line-height: 1;
  color: var(--ink-3);
}
.nav-item.active .step { border-color: currentColor; color: inherit; }
.nav-item.locked { opacity: .5; }
.nav-item.locked .step { border-style: dashed; }
