:root {
  --bg: #10151c;
  --panel: #1a2230;
  --panel-2: #212b3d;
  --border: #2c3950;
  --text: #e6ecf5;
  --muted: #8b99ad;
  --accent: #d4a437;
  --accent-dark: #b3882a;
  --ok: #3fbf7f;
  --err: #e05656;
  --warn: #e0a230;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  font-family: "Segoe UI", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  min-height: 100vh;
}

/* ------------------------------------------------------------- sidebar */
.sidebar {
  width: 230px;
  flex-shrink: 0;
  background: var(--panel);
  border-right: 1px solid var(--border);
  padding: 18px 0;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}
.brand {
  padding: 0 18px 14px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 10px;
}
.brand img { max-width: 100%; display: block; margin-bottom: 6px; }
.brand span {
  font-size: 12px;
  letter-spacing: 2px;
  text-transform: uppercase;
  color: var(--muted);
}
nav a {
  display: block;
  padding: 8px 18px;
  color: var(--text);
  text-decoration: none;
  font-size: 14px;
  border-left: 3px solid transparent;
}
nav a:hover { background: var(--panel-2); }
nav a.active {
  border-left-color: var(--accent);
  background: var(--panel-2);
  color: var(--accent);
}
.nav-section {
  padding: 14px 18px 4px;
  font-size: 11px;
  letter-spacing: 1.5px;
  text-transform: uppercase;
  color: var(--muted);
}

/* ---------------------------------------------------------------- main */
main { flex: 1; padding: 26px 32px; min-width: 0; }
h1 { margin: 0 0 18px; font-size: 24px; font-weight: 600; }
h2 { font-size: 15px; font-weight: 600; margin: 18px 0 10px; color: var(--accent); }
.page-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; }
.page-head h1 { margin: 0; }
.muted { color: var(--muted); }
.hint { color: var(--accent); font-size: 12px; min-height: 16px; display: inline-block; }

.flash {
  padding: 10px 14px;
  border-radius: 6px;
  margin-bottom: 14px;
  font-size: 14px;
  border: 1px solid;
}
.flash.ok  { background: rgba(63,191,127,.12); border-color: var(--ok);  color: var(--ok); }
.flash.error { background: rgba(224,86,86,.12); border-color: var(--err); color: var(--err); }

/* --------------------------------------------------------------- cards */
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 14px;
  margin-bottom: 26px;
}
.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px;
  text-decoration: none;
  color: var(--text);
  transition: border-color .15s;
}
.card:hover { border-color: var(--accent); }
.card-label { font-size: 12px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); }
.card-value { font-size: 26px; font-weight: 700; margin: 6px 0 2px; color: var(--accent); }
.card-sub { font-size: 12px; color: var(--muted); }
.badge.warn {
  background: rgba(224,162,48,.15);
  color: var(--warn);
  border-radius: 10px;
  padding: 1px 8px;
  margin-left: 6px;
  font-size: 11px;
}

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 24px; }
@media (max-width: 1100px) { .grid-2 { grid-template-columns: 1fr; } }

/* -------------------------------------------------------------- tables */
table {
  width: 100%;
  border-collapse: collapse;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  font-size: 13.5px;
}
th, td { padding: 8px 12px; text-align: left; border-bottom: 1px solid var(--border); }
th {
  background: var(--panel-2);
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: .8px;
  color: var(--muted);
}
th.sortable { cursor: pointer; user-select: none; white-space: nowrap; }
th.sortable:hover { color: var(--accent); }
th.sortable[data-dir="asc"]::after { content: " ▲"; color: var(--accent); }
th.sortable[data-dir="desc"]::after { content: " ▼"; color: var(--accent); }
tr:last-child td { border-bottom: none; }
tr:hover td { background: rgba(255,255,255,.02); }
tr.inactive td { opacity: .45; }
td.qty { font-weight: 600; }
td.qty.zero { color: var(--err); }
.pos { color: var(--ok); }
.neg { color: var(--err); }
table a { color: var(--accent); }
table.kv th { width: 160px; background: transparent; }

/* --------------------------------------------------------------- forms */
.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 18px;
  margin-bottom: 22px;
}
.panel.narrow { max-width: 560px; }
.panel h2:first-child { margin-top: 0; }

label { font-size: 13px; color: var(--muted); }
label.stack { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
label.check { display: flex; align-items: center; gap: 8px; margin: 10px 0; color: var(--text); }

input, select {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  padding: 8px 10px;
  font-size: 14px;
  font-family: inherit;
}
input:focus, select:focus { outline: none; border-color: var(--accent); }
input[disabled] { opacity: .5; }

.inline-form { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; }
.inline-form label { display: flex; flex-direction: column; gap: 4px; }
.inline-form input, .inline-form select { width: 150px; }
.inline-form select.wide { width: 320px; }

details.new-product { margin-top: 14px; }
details.new-product summary { cursor: pointer; color: var(--accent); font-size: 14px; margin-bottom: 10px; }
.btn.danger { color: var(--err); border-color: var(--err); }
.btn.danger:hover { background: rgba(224,86,86,.1); }

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
  gap: 4px 18px;
}
.form-actions { display: flex; gap: 10px; align-items: center; margin-top: 14px; }
.split { display: flex; gap: 6px; }
.split input { flex: 1; min-width: 0; }

.btn {
  display: inline-block;
  background: var(--accent);
  color: #14100a;
  border: 1px solid var(--accent);
  border-radius: 6px;
  padding: 8px 16px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  font-family: inherit;
}
.btn:hover { background: var(--accent-dark); }
.btn.ghost { background: transparent; color: var(--accent); }
.btn.ghost:hover { background: rgba(212,164,55,.1); }
.btn.small { padding: 4px 10px; font-size: 12px; }

.row-actions { white-space: nowrap; }
.adjust { display: inline-block; position: relative; }
.adjust summary { list-style: none; user-select: none; }
.adjust summary::-webkit-details-marker { display: none; }
.adjust-form {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  z-index: 10;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
  display: flex;
  gap: 6px;
  box-shadow: 0 8px 24px rgba(0,0,0,.45);
}
.adjust-form input { width: 80px; }

fieldset.case-source {
  border: 1px solid var(--border);
  border-radius: 8px;
  margin: 14px 0;
  padding: 12px 16px;
}
fieldset.case-source legend { color: var(--accent); font-size: 13px; padding: 0 6px; }
.traceback span:first-child { font-size: 13px; color: var(--muted); }

details.qc { margin: 12px 0; }
details.qc summary { cursor: pointer; color: var(--accent); font-size: 14px; margin-bottom: 8px; }

/* ----------------------------------------------------------- test data */
.nav-search { padding: 14px 18px; border-top: 1px solid var(--border); margin-top: 12px; }
.nav-search input { width: 100%; font-size: 13px; }
.nav-user { padding: 10px 18px 16px; font-size: 12px; color: var(--muted);
  display: flex; flex-direction: column; gap: 4px; border-top: 1px solid var(--border); }
.nav-user a { color: var(--accent); }
.nav-user {
  padding: 12px 18px; border-top: 1px solid var(--border);
  display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: var(--muted);
}
.nav-user a { color: var(--accent); text-decoration: none; }
.nav-user a:hover { text-decoration: underline; }

/* auth pages (login / setup) */
.auth-body { display: flex; align-items: center; justify-content: center;
  min-height: 100vh; padding: 20px; }
.auth-card {
  background: var(--panel); border: 1px solid var(--border); border-radius: 12px;
  padding: 32px; width: 100%; max-width: 380px; box-shadow: 0 8px 30px rgba(0,0,0,.4);
}
.auth-card h1 { font-size: 20px; margin: 4px 0 18px; text-align: center; }
.auth-logo { max-width: 200px; display: block; margin: 0 auto 10px; }
.auth-card .stack { margin-bottom: 12px; }
.hint.shortage { color: var(--err); font-weight: 600; }
.sessions-scroll { max-height: 420px; overflow-y: auto; }
.sessions-scroll table tr:first-child th { position: sticky; top: 0; z-index: 5; }
.image-gallery { display: flex; flex-wrap: wrap; gap: 10px; }
.image-gallery .thumb { position: relative; }
.image-gallery img { max-height: 160px; border-radius: 6px; border: 1px solid var(--border); display: block; }
.image-gallery .del { position: absolute; top: 4px; right: 4px; }
#crop-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.75); z-index: 100;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px;
}
#crop-overlay canvas { max-width: 90vw; max-height: 75vh; cursor: crosshair; border: 1px solid var(--border); }
.testdata-toolbar { display: flex; flex-wrap: wrap; gap: 18px; align-items: flex-end; }
.testdata-toolbar > label { display: flex; flex-direction: column; gap: 4px; }
.testdata-toolbar input#session-search { width: 260px; }
.import-form { display: flex; gap: 10px; align-items: center; margin-left: auto; }
.file-btn { position: relative; overflow: hidden; cursor: pointer; }
.table-scroll { overflow-x: auto; margin-bottom: 10px; }
.table-scroll table { min-width: 100%; }
.session-row { cursor: pointer; }
.session-row.selected td { background: rgba(212,164,55,.14); }
.shot-scroll { max-height: 260px; overflow-y: auto; }
.image-box img { max-width: 100%; max-height: 380px; border-radius: 6px; border: 1px solid var(--border); }
.sql-box {
  width: 100%; box-sizing: border-box;
  background: var(--bg); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px;
  font-family: Consolas, monospace; font-size: 13px; padding: 10px;
}
.stats-output {
  background: var(--bg); border: 1px solid var(--border); border-radius: 6px;
  padding: 12px; font-size: 12.5px; overflow-x: auto; white-space: pre-wrap;
}
.stats-plot { max-width: 100%; border-radius: 6px; background: #fff; }
/* ----------------------------------------------------------------- DOE */
.doe-row { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 8px; align-items: flex-end; }
.doe-row label { display: flex; flex-direction: column; gap: 3px; font-size: 12px; }
.doe-row input, .doe-row select { width: 130px; }
.doe-eq { font-family: Consolas, monospace; font-size: 12.5px; word-break: break-word; }
.doe-opt { font-size: 15px; line-height: 1.5; }
.doe-plots { display: flex; flex-wrap: wrap; gap: 14px; margin-top: 10px; }
.doe-plots img { max-width: 460px; border-radius: 6px; background: #fff; }
.plotly-surf { width: 480px; height: 380px; border: 1px solid var(--border); border-radius: 6px; }
.doe-preds { font-size: 15px; margin-top: 10px; color: var(--text); }
.prof-row { display: flex; align-items: center; gap: 12px; margin-bottom: 8px; }
.prof-name { width: 130px; font-size: 13px; color: var(--muted); }
.prof-slider { width: 220px; }
.prof-val { width: 80px; font-weight: 600; color: var(--accent); }
.prof-canvas { background: var(--bg); border: 1px solid var(--border); border-radius: 6px; }
.info-note {
  background: rgba(212,164,55,.08);
  border-left: 3px solid var(--accent);
  border-radius: 6px;
  padding: 12px 14px;
  font-size: 13px;
  color: var(--muted);
  margin-top: 14px;
}
.info-note strong { color: var(--text); }
#design-table input.run-val { width: 90px; }
#design-table select.run-session { max-width: 220px; }
h3 { font-size: 13.5px; color: var(--accent); margin: 14px 0 6px; }

.linked-session { margin-bottom: 18px; }
.linked-session h3 { font-size: 14px; margin: 10px 0 6px; }
.linked-session h3 a { color: var(--accent); }
.linked-session table { margin-bottom: 8px; }
.analyze-toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 10px; }
.analyze-toolbar label { display: flex; align-items: center; gap: 6px; }
.analyze-toolbar input { width: 80px; }
.canvas-wrap { overflow: auto; }
#target { border: 1px solid var(--border); border-radius: 6px; cursor: crosshair; }

/* ------------------------------------------------------- grouped table */
table.grouped .chev-col { width: 28px; }
.group-row { cursor: pointer; }
.group-row td { background: var(--panel-2); }
.group-label { font-weight: 600; }
.group .chev { color: var(--accent); transition: transform .15s; }
.group.open .chev { transform: rotate(90deg); display: inline-block; }
.group .lot-row { display: none; }
.group.open .lot-row { display: table-row; }
.lot-no { font-family: Consolas, monospace; }
.small-note { font-size: 11px; text-align: right; }
.group.open .small-note { visibility: hidden; }
