:root {
  --bg: #f5f7fa;
  --surface: #ffffff;
  --surface-2: #f0f3f7;
  --border: #dde3ea;
  --text: #1b2430;
  --muted: #5f6b7a;
  --primary: #1f5fa8;
  --primary-hover: #184d89;
  --primary-soft: #e6eef8;
  --sidebar: #142236;
  --sidebar-text: #c6d1df;
  --sidebar-active: #ffffff;
  --ok: #1d7a46;
  --ok-soft: #e3f4ea;
  --warn: #9a5b00;
  --warn-soft: #fff3dc;
  --danger: #b42318;
  --danger-soft: #fdeceb;
  --radius: 8px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  font-family: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  color: var(--text);
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.5rem; margin: 0 0 .25rem; }
h2 { font-size: 1.1rem; margin: 0 0 .75rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.mono { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: .85em; }

/* Layout */
.shell { display: grid; grid-template-columns: 236px 1fr; min-height: 100vh; }
.sidebar { background: var(--sidebar); color: var(--sidebar-text); display: flex; flex-direction: column; padding: 1rem .75rem; position: sticky; top: 0; height: 100vh; }
.brand { color: #fff; font-weight: 700; font-size: 1.1rem; padding: .25rem .5rem 1rem; letter-spacing: .02em; }
.brand span { color: #7fb2ec; }
.nav-section { font-size: .7rem; text-transform: uppercase; letter-spacing: .08em; color: #7d8ba0; margin: 1rem .5rem .35rem; }
.nav a { display: flex; align-items: center; gap: .6rem; color: var(--sidebar-text); padding: .5rem .6rem; border-radius: 6px; }
.nav a:hover { background: rgba(255,255,255,.07); text-decoration: none; color: #fff; }
.nav a.active { background: rgba(127,178,236,.18); color: var(--sidebar-active); }
.nav svg { width: 18px; height: 18px; flex: none; }
.sidebar-foot { margin-top: auto; font-size: .85rem; padding: .5rem; border-top: 1px solid rgba(255,255,255,.1); }
.sidebar-foot a, .sidebar-foot button { color: var(--sidebar-text); }
.main { min-width: 0; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 1rem; padding: .75rem 1.5rem; background: var(--surface); border-bottom: 1px solid var(--border); }
.topbar .help-btn { margin-left: auto; }
.content { padding: 1.5rem; max-width: 1400px; }
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem; margin-bottom: 1.25rem; flex-wrap: wrap; }

/* Components */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1.1rem 1.25rem; margin-bottom: 1.25rem; }
.card-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: .75rem; }
.grid { display: grid; gap: 1.25rem; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); }
.grid .card { margin-bottom: 0; }
.stat { font-size: 2rem; font-weight: 700; line-height: 1.1; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: .4rem 1.25rem; margin: 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }

.btn svg { width: 16px; height: 16px; }
.btn { display: inline-flex; align-items: center; gap: .4rem; border: 1px solid var(--border); background: var(--surface); color: var(--text); padding: .45rem .9rem; border-radius: 6px; font: inherit; cursor: pointer; white-space: nowrap; }
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-sm { padding: .25rem .6rem; font-size: .85rem; }
.btn-link { background: none; border: 0; padding: 0; color: inherit; cursor: pointer; font: inherit; }
.actions { display: flex; gap: .5rem; flex-wrap: wrap; }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: .55rem .6rem; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; background: var(--surface-2); }
tr:last-child td { border-bottom: 0; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
.table-wrap { overflow-x: auto; }

.badge { display: inline-block; white-space: nowrap; padding: .1rem .5rem; border-radius: 999px; font-size: .78rem; font-weight: 600; background: var(--surface-2); color: var(--muted); }
.badge-ok { background: var(--ok-soft); color: var(--ok); }
.badge-warn { background: var(--warn-soft); color: var(--warn); }
.badge-danger { background: var(--danger-soft); color: var(--danger); }
.badge-info { background: var(--primary-soft); color: var(--primary); }

.alert { padding: .7rem 1rem; border-radius: 6px; margin-bottom: 1rem; border: 1px solid transparent; }
.alert-success { background: var(--ok-soft); color: var(--ok); border-color: #b9e3c8; }
.alert-warning { background: var(--warn-soft); color: var(--warn); border-color: #f2d9a6; }
.alert-error { background: var(--danger-soft); color: var(--danger); border-color: #f5c2bd; }
.alert-info { background: var(--primary-soft); color: var(--primary); border-color: #c4d7ee; }

/* Forms */
.form-grid { display: grid; gap: 1rem 1.25rem; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); }
.field label { display: block; font-weight: 600; font-size: .88rem; margin-bottom: .3rem; }
.field.full { grid-column: 1 / -1; }
input[type=text], input[type=password], input[type=number], input[type=date], input[type=email], input:not([type]), select, textarea {
  width: 100%; padding: .5rem .6rem; border: 1px solid var(--border); border-radius: 6px; font: inherit; background: #fff; color: var(--text);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary-soft); border-color: var(--primary); }
.helptext { display: block; color: var(--muted); font-size: .8rem; margin-top: .25rem; }
.errorlist { color: var(--danger); margin: .25rem 0 0; padding: 0; list-style: none; font-size: .85rem; }
.filters { display: flex; gap: .6rem; flex-wrap: wrap; align-items: flex-end; margin-bottom: 1rem; }
.filters > * { width: auto; }
.filters input, .filters select { min-width: 160px; }
.radio-list ul, .field ul#id_decision { list-style: none; padding: 0; margin: 0; }
.signature-box { border: 1px solid #c4d7ee; background: var(--primary-soft); border-radius: var(--radius); padding: 1rem 1.25rem; }

.org-switch select { min-width: 220px; }
.pagination { display: flex; gap: .75rem; align-items: center; margin-top: 1rem; }
.changes { margin: 0; padding-left: 1rem; font-size: .85rem; }
.changes del { color: var(--danger); }
.changes ins { color: var(--ok); text-decoration: none; }
.empty { color: var(--muted); padding: 1rem 0; text-align: center; }

/* Auth pages */
.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: 1rem; background: linear-gradient(160deg, #142236, #1f5fa8); }
.auth-card { width: 100%; max-width: 400px; }
.auth-card .brand { color: var(--text); padding: 0 0 .5rem; font-size: 1.4rem; }
.auth-card .brand span { color: var(--primary); }

@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; }
  .content { padding: 1rem; }
}
td.mono, .nowrap { white-space: nowrap; }

/* Organization logos */
.org-logo { display: block; background: #fff; border-radius: 8px; padding: .6rem; margin: 0 .25rem .9rem; text-align: center; }
.org-logo img { max-width: 100%; max-height: 128px; object-fit: contain; vertical-align: middle; }
.logo-preview { border: 1px dashed var(--border); border-radius: var(--radius); padding: 1rem; margin-bottom: 1rem; text-align: center; background: #fff; }
.logo-preview img { max-height: 180px; max-width: 100%; object-fit: contain; }
.auth-logos { display: flex; justify-content: center; align-items: center; gap: 1.5rem; margin-bottom: 1rem; }
.auth-logos img { max-height: 90px; max-width: 150px; object-fit: contain; }
.print-header { display: none; }
@media print {
  .print-header { display: flex; align-items: center; gap: 1rem; margin-bottom: 1rem; padding-bottom: .5rem; border-bottom: 2px solid #000; }
  .print-header img { max-height: 60px; max-width: 200px; object-fit: contain; }
}

/* QC charts */
.lj text { font-size: 11px; fill: var(--muted); }
.lj .mean { stroke: var(--ok); stroke-width: 1.5; }
.lj .sd1 { stroke: var(--muted); stroke-dasharray: 3 3; stroke-opacity: .6; }
.lj .sd2 { stroke: var(--warn); stroke-dasharray: 5 3; }
.lj .sd3 { stroke: var(--danger); stroke-dasharray: 5 3; }
.lj .lot-break { stroke: var(--text); stroke-width: 1.2; stroke-dasharray: 2 2; opacity: .55; }
.lj .lot-label { font-weight: 600; fill: var(--text); }
.lj .tested-lot { font-size: 10px; font-family: ui-monospace, Consolas, monospace; }

/* QC charts */
.lj text { font-size: 11px; fill: var(--muted); }
.lj .mean { stroke: var(--ok); stroke-width: 1.5; }
.lj .sd1 { stroke: var(--muted); stroke-dasharray: 3 3; stroke-opacity: .6; }
.lj .sd2 { stroke: var(--warn); stroke-dasharray: 5 3; }
.lj .sd3 { stroke: var(--danger); stroke-dasharray: 5 3; }
.lj .lot-break { stroke: var(--text); stroke-width: 1.2; stroke-dasharray: 2 2; opacity: .55; }
.lj .lot-label { font-weight: 600; fill: var(--text); }
.lj .tested-lot { font-size: 10px; font-family: ui-monospace, Consolas, monospace; }

/* QC review: report preview beside the signature */
.review-layout { display: grid; grid-template-columns: minmax(0, 1.6fr) minmax(300px, 1fr); gap: 1.25rem; align-items: start; }
.pdf-frame { width: 100%; height: 78vh; border: 0; display: block; }
@media (max-width: 1000px) { .review-layout { grid-template-columns: 1fr; } .pdf-frame { height: 60vh; } }
