:root {
  --bg: #f4f2ee;
  --panel: #ffffff;
  --ink: #1d2430;
  --muted: #5d6675;
  --line: #dcd8d0;
  --line-soft: #ebe8e2;
  --nav: #1c2a3a;
  --nav-ink: #c9d3df;
  --nav-active: #2c3f55;
  --accent: #b5651d;
  --accent-ink: #ffffff;
  --accent-soft: #f6e7d8;
  --danger: #b3261e;
  --danger-soft: #fbe6e4;
  --ok: #2e7d4f;
  --ok-soft: #e3f2e8;
  --warn-soft: #fff4d6;
  --focus: #2f6fb3;
  --radius: 8px;
  --shadow: 0 1px 2px rgba(20, 25, 35, .06), 0 4px 16px rgba(20, 25, 35, .06);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  font-size: 15px;
  color: var(--ink);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--focus); }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.boot { padding: 40px; color: var(--muted); }

/* ---------- buttons and inputs ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  border: 1px solid var(--line); background: var(--panel);
  padding: 7px 12px; border-radius: 6px; cursor: pointer; white-space: nowrap;
  text-decoration: none; color: var(--ink); min-height: 36px;
}
.btn:hover { background: #faf8f5; border-color: #c9c4ba; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { background: #9e5717; }
.btn.danger { color: var(--danger); border-color: #e3b6b2; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.small { padding: 3px 8px; min-height: 28px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
input[type=text], input[type=password], input[type=number], input[type=date],
input[type=time], input[type=search], input[type=email], select, textarea {
  width: 100%; border: 1px solid var(--line); border-radius: 6px;
  padding: 7px 9px; background: #fff; min-height: 36px;
}
textarea { min-height: 70px; resize: vertical; }
input:disabled, select:disabled, textarea:disabled { background: #f3f1ed; color: var(--muted); }
label.check { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field > span.lbl { font-size: 13px; font-weight: 600; color: var(--muted); }
.field .req { color: var(--danger); }
.field .help { font-size: 12px; color: var(--muted); }
.field .err { font-size: 12px; color: var(--danger); }
.field.has-err input, .field.has-err select, .field.has-err textarea { border-color: var(--danger); }

/* ---------- auth pages ---------- */
.auth-wrap { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.auth-card {
  width: 100%; max-width: 380px; background: var(--panel); border-radius: 12px;
  box-shadow: var(--shadow); padding: 28px; display: flex; flex-direction: column; gap: 14px;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 18px; }
.brand-mark {
  width: 34px; height: 34px; border-radius: 7px; background: var(--accent);
  display: grid; place-items: center; color: #fff; font-weight: 800; font-size: 14px;
}
.auth-card p { margin: 0; color: var(--muted); }
.form-error { background: var(--danger-soft); color: var(--danger); padding: 8px 10px; border-radius: 6px; font-size: 14px; }

/* ---------- shell ---------- */
.shell { display: grid; grid-template-columns: 250px 1fr; grid-template-rows: minmax(0, 1fr); height: 100vh; height: 100dvh; overflow: hidden; }
.sidebar { background: var(--nav); color: var(--nav-ink); overflow-y: auto; overscroll-behavior: contain; min-height: 0; height: 100%; display: flex; flex-direction: column; }
.sidebar .brand { color: #fff; padding: 16px; font-size: 16px; }
.nav-group { padding: 6px 0; }
.nav-group h4 {
  margin: 10px 16px 4px; font-size: 11px; text-transform: uppercase;
  letter-spacing: .08em; color: #8495a8; font-weight: 600;
}
.nav-link {
  display: flex; justify-content: space-between; align-items: center;
  padding: 8px 16px; color: var(--nav-ink); text-decoration: none; font-size: 14px;
}
.nav-link:hover { background: var(--nav-active); color: #fff; }
.nav-link.active { background: var(--nav-active); color: #fff; box-shadow: inset 3px 0 0 var(--accent); }
.nav-link.disabled { color: #6f8094; cursor: default; }
.nav-link.disabled:hover { background: none; color: #6f8094; }
.tag { font-size: 10px; padding: 2px 6px; border-radius: 10px; background: #33465c; color: #a9b8c8; }
.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; height: 100%; }
.topbar {
  display: flex; align-items: center; gap: 10px; padding: 10px 20px;
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.topbar h1 { font-size: 18px; margin: 0; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.menu-btn { display: none; }
.content { flex: 1; overflow: auto; padding: 18px 20px 40px; }
.user-menu { position: relative; }
.dropdown {
  position: absolute; right: 0; top: calc(100% + 4px); background: var(--panel);
  border: 1px solid var(--line); border-radius: 8px; box-shadow: var(--shadow);
  min-width: 220px; z-index: 50; padding: 4px; display: flex; flex-direction: column;
}
.dropdown button, .dropdown a {
  text-align: left; background: none; border: 0; padding: 8px 10px; border-radius: 5px; cursor: pointer;
  color: var(--ink); text-decoration: none; font-size: 14px;
}
.dropdown button:hover, .dropdown a:hover { background: var(--bg); }
.dropdown hr { border: 0; border-top: 1px solid var(--line-soft); margin: 4px 0; width: 100%; }
.dropdown .dd-label { font-size: 11px; text-transform: uppercase; color: var(--muted); padding: 6px 10px 2px; letter-spacing: .06em; }
.dropdown .on::before { content: "✓ "; color: var(--accent); }

/* ---------- home ---------- */
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 12px; }
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px; text-decoration: none; color: var(--ink); display: flex; flex-direction: column; gap: 4px;
}
a.card:hover { border-color: var(--accent); }
.card .num { font-size: 26px; font-weight: 700; }
.card .lbl { color: var(--muted); font-size: 13px; }
.section-title { font-size: 14px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 26px 0 10px; }
.next-list { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 6px 16px; }
.next-list li { padding: 6px 0; }

/* ---------- toolbar and grid ---------- */
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px; }
.toolbar .search { flex: 1 1 220px; max-width: 340px; }
.toolbar .spacer { flex: 1; }
.layout-dirty { font-size: 12px; background: var(--warn-soft); padding: 4px 8px; border-radius: 6px; }
.grid-wrap {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  overflow: auto; max-height: calc(100vh - 190px);
}
table.grid { border-collapse: separate; border-spacing: 0; table-layout: fixed; width: max-content; min-width: 100%; }
table.grid th, table.grid td {
  border-bottom: 1px solid var(--line-soft); padding: 8px 10px; text-align: left;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 14px; background: var(--panel);
}
table.grid th {
  position: sticky; top: 0; z-index: 2; background: #f8f6f2; font-size: 12px; font-weight: 600;
  color: var(--muted); user-select: none; border-bottom: 1px solid var(--line);
}
table.grid th .th-inner { display: flex; align-items: center; gap: 4px; cursor: pointer; }
table.grid th .th-inner span { overflow: hidden; text-overflow: ellipsis; }
table.grid th .sort { color: var(--accent); }
table.grid th.drag-over { box-shadow: inset 3px 0 0 var(--accent); }
table.grid th .resizer { position: absolute; right: 0; top: 0; bottom: 0; width: 7px; cursor: col-resize; }
table.grid th .resizer:hover { background: var(--accent-soft); }
table.grid .frozen { position: sticky; z-index: 1; }
table.grid th.frozen { z-index: 3; }
table.grid .frozen-last { box-shadow: 2px 0 0 var(--line); }
table.grid tbody tr { cursor: pointer; }
table.grid tbody tr:hover td { background: #fbf6f0; }
table.grid tr.inactive td { color: #9aa0a8; }
table.grid td.num { text-align: right; font-variant-numeric: tabular-nums; }
.empty { padding: 30px; text-align: center; color: var(--muted); }
.pill { display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 10px; background: var(--line-soft); }
.pill.ok { background: var(--ok-soft); color: var(--ok); }
.pill.bad { background: var(--danger-soft); color: var(--danger); }
.pill.warn { background: var(--warn-soft); }
.count { color: var(--muted); font-size: 13px; }

/* ---------- modal ---------- */
.modal-back {
  position: fixed; inset: 0; background: rgba(15, 20, 30, .45); z-index: 100;
  display: grid; place-items: center; padding: 16px;
  grid-template-columns: minmax(0, 1fr);
}
.modal {
  background: var(--panel); border-radius: 12px; box-shadow: var(--shadow);
  width: 100%; max-width: 520px; max-height: calc(100vh - 32px); display: flex; flex-direction: column;
}
.modal.wide { max-width: 900px; }
.modal header { display: flex; align-items: center; gap: 10px; padding: 14px 18px; border-bottom: 1px solid var(--line-soft); }
.modal header h2 { font-size: 17px; margin: 0; flex: 1; }
.modal .body { padding: 16px 18px; overflow: auto; }
.modal footer {
  display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-end; padding: 12px 18px;
  border-top: 1px solid var(--line-soft);
}
.modal footer .left { margin-right: auto; display: flex; gap: 8px; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(230px, 100%), 1fr)); gap: 14px; }
.modal .body > * { min-width: 0; }
.form-grid .wide { grid-column: 1 / -1; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 14px; }
.tabs button { background: none; border: 0; padding: 8px 12px; cursor: pointer; border-bottom: 2px solid transparent; color: var(--muted); }
.tabs button.on { color: var(--ink); border-bottom-color: var(--accent); font-weight: 600; }
.history-item { border-bottom: 1px solid var(--line-soft); padding: 10px 0; font-size: 14px; }
.history-item .meta { color: var(--muted); font-size: 12px; margin-bottom: 4px; }
.change { font-size: 13px; }
.change .old { color: var(--danger); text-decoration: line-through; }
.change .new { color: var(--ok); }
.col-list { display: flex; flex-direction: column; gap: 2px; max-height: 50vh; overflow: auto; }
.col-row { display: flex; align-items: center; gap: 6px; padding: 4px 6px; border-radius: 5px; }
.col-row:hover { background: var(--bg); }
.col-row label { flex: 1; }
.perm-table { border-collapse: collapse; width: 100%; font-size: 14px; }
.perm-table th, .perm-table td { padding: 6px 8px; border-bottom: 1px solid var(--line-soft); text-align: center; }
.perm-table th:first-child, .perm-table td:first-child { text-align: left; }
.perm-table thead th { font-size: 12px; color: var(--muted); position: sticky; top: 0; background: var(--panel); }
.import-summary { display: flex; gap: 8px; flex-wrap: wrap; margin: 10px 0; }
.note { background: var(--accent-soft); padding: 10px 12px; border-radius: 6px; font-size: 14px; }

/* ---------- toasts ---------- */
#toasts {
  position: fixed; top: 12px; left: 50%; transform: translateX(-50%); z-index: 200;
  display: flex; flex-direction: column; align-items: center; gap: 8px; pointer-events: none;
}
.toast { background: var(--ink); color: #fff; padding: 10px 14px; border-radius: 8px; box-shadow: var(--shadow); max-width: 360px; font-size: 14px; }
.toast.error { background: var(--danger); }
.toast.ok { background: var(--ok); }

/* ---------- phone and tablet ---------- */
@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; width: 270px; z-index: 90;
    transform: translateX(-100%); transition: transform .2s;
  }
  .sidebar.open { transform: none; box-shadow: 0 0 40px rgba(0,0,0,.4); }
  .menu-btn { display: inline-flex; }
  .topbar { padding: 8px 12px; }
  .content { padding: 12px 12px 40px; }
  .grid-wrap { max-height: calc(100vh - 230px); }
  .toolbar .search { max-width: none; }
  .modal-back { padding: 0; }
  .modal, .modal.wide { max-width: none; max-height: 100vh; height: 100%; border-radius: 0; }
  .hide-mobile { display: none; }
}

/* ---------- documents: lines, sub-tables, calculated fields ---------- */
input.calc, select.calc, textarea.calc { background: #f1f6ef; color: #23402c; border-style: dashed; font-weight: 600; }
.calc-cell { color: #23402c; font-weight: 600; }
.lines-block { margin-top: 20px; }
.lines-head { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.lines-head h3 { margin: 0; font-size: 15px; flex: 1; }
.tbl-wrap { max-height: 45vh; }
table.grid.lines { table-layout: auto; }
table.grid.lines td.row-actions { white-space: nowrap; text-align: right; }
table.grid tr.row-err td { background: var(--danger-soft); }
table.grid.edit { table-layout: auto; }
table.grid.edit td { padding: 4px 6px; overflow: visible; }
table.grid.edit input, table.grid.edit select { min-width: 90px; min-height: 32px; padding: 4px 6px; }
table.grid.edit select { min-width: 170px; }
.tbl-edit { display: flex; flex-direction: column; gap: 8px; align-items: flex-start; }
.tbl-edit .grid-wrap { width: 100%; }
.lbl.sub { font-size: 13px; font-weight: 600; color: var(--muted); margin-bottom: 6px; }
.tabs.scroll { overflow-x: auto; flex-wrap: nowrap; }
.tabs.scroll button { white-space: nowrap; }
.modal .err div { margin-top: 2px; }
table.grid tr.overdue td { background: var(--danger-soft); color: var(--danger); }
table.grid tr.overdue td:first-child { box-shadow: inset 3px 0 0 var(--danger); }
.modal footer .left { flex-wrap: wrap; }
@media (max-width: 860px) {
  .modal footer .left { width: 100%; margin-right: 0; }
  .modal footer .btn { padding: 6px 10px; }
}

/* ---------- stock screen ---------- */
.stock-filters { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; background: var(--panel);
  border: 1px solid var(--line); border-radius: var(--radius); padding: 10px; }
.stock-filters .row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.stock-filters .row > select, .stock-filters .flt { width: auto; min-width: 110px; flex: 0 1 170px; }
.stock-filters input[type=number].flt { flex-basis: 90px; min-width: 70px; }
.stock-filters .spacer { flex: 1; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; }
.seg-btn { border: 0; background: var(--panel); padding: 6px 10px; cursor: pointer; font-size: 13px; min-height: 34px; }
.seg-btn + .seg-btn { border-left: 1px solid var(--line); }
.seg-btn.on { background: var(--nav); color: #fff; }
.seg-btn.red.on { background: var(--danger); }
.seg-btn.green.on { background: var(--ok); }
table.grid tr.below-min td { background: var(--danger-soft); }
table.grid tr.above-min td { background: var(--ok-soft); }
.stock-totals { display: flex; flex-wrap: wrap; gap: 16px; margin-top: 10px; font-size: 14px; color: var(--muted); }
.stock-totals b { color: var(--ink); }
.stock-totals .legend { margin-left: auto; display: inline-flex; align-items: center; gap: 6px; }
.sw { display: inline-block; width: 12px; height: 12px; border-radius: 3px; }
.sw.red { background: var(--danger-soft); border: 1px solid var(--danger); }
.sw.green { background: var(--ok-soft); border: 1px solid var(--ok); }
h3.sub-h { font-size: 14px; margin: 16px 0 8px; }
h3.sub-h:first-child { margin-top: 0; }
.stock-filters .more { display: flex; flex-direction: column; gap: 8px; }
.stock-filters .more-btn { display: none; }
@media (max-width: 860px) {
  .stock-filters .more { display: none; }
  .stock-filters .more.open { display: flex; }
  .stock-filters .more-btn { display: inline-flex; }
  .stock-filters .row > select, .stock-filters .flt { flex: 1 1 140px; }
  .stock-totals .legend { margin-left: 0; }
}

/* ---------- scanning ---------- */
.scan-panel { background: var(--accent-soft); border: 1px solid #e8c9a8; border-radius: var(--radius); padding: 12px; margin-bottom: 16px; }
.scan-row { display: flex; gap: 8px; flex-wrap: wrap; }
.scan-row .scan-input { flex: 1 1 260px; font-size: 16px; min-height: 42px; }
.scan-msg { min-height: 20px; margin: 6px 0 2px; font-size: 14px; }
.scan-msg.ok { color: var(--ok); }
.scan-msg.bad { color: var(--danger); }
.scan-msg.warn { color: #8a5a00; }
.scan-video { width: 100%; max-height: 60vh; background: #000; border-radius: 8px; }
.pick-list { display: flex; flex-direction: column; gap: 8px; }

/* ---------- shop floor ---------- */
.floor-job { display: flex; flex-direction: column; gap: 10px; }
.floor-job-head { display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap; background: var(--panel);
  border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; }
.floor-no { font-size: 22px; font-weight: 800; }
.floor-meta { text-align: right; }
.step-card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px;
  display: flex; flex-direction: column; gap: 10px; }
.step-card.running { border-color: var(--ok); box-shadow: inset 4px 0 0 var(--ok); }
.step-top { display: flex; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.step-run { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.step-run select { flex: 1 1 220px; width: auto; }
.btn.big { min-height: 46px; padding: 10px 26px; font-size: 16px; font-weight: 600; }

/* ---------- live board ---------- */
.live-machines { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 10px; }
.machine-card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; display: flex; flex-direction: column; gap: 4px; }
.machine-card.busy { border-color: var(--ok); box-shadow: inset 4px 0 0 var(--ok); }
.mc-name { font-weight: 700; }
.mc-job { font-size: 17px; font-weight: 700; color: var(--ok); }
.mc-next { font-size: 12px; color: var(--muted); }
.sw.grey { background: #eceae6; border: 1px solid #9aa0a8; }
.legend { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }

/* ---------- planning board ---------- */
.plan-layout { display: grid; grid-template-columns: 260px 1fr; gap: 14px; align-items: start; }
.plan-unplanned { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px;
  max-height: calc(100vh - 190px); overflow: auto; display: flex; flex-direction: column; gap: 8px; }
.plan-board { display: flex; gap: 12px; overflow-x: auto; padding-bottom: 8px; align-items: flex-start; }
.plan-col { flex: 0 0 min(250px, 78vw); background: #ebe8e2; border-radius: var(--radius); padding: 8px; display: flex; flex-direction: column; gap: 8px; min-height: 200px; }
.plan-col.drop, .plan-card.drop { outline: 2px dashed var(--accent); }
.plan-head { background: var(--panel); border-radius: 6px; padding: 8px; display: flex; flex-direction: column; gap: 2px; }
.plan-card { background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: 8px; font-size: 13px;
  display: flex; flex-direction: column; gap: 3px; cursor: grab; }
.plan-card.late { border-color: var(--danger); background: var(--danger-soft); }
.plan-card.running { box-shadow: inset 3px 0 0 var(--ok); }
.plan-card .pc-top { display: flex; gap: 6px; align-items: center; }
.plan-card .pc-actions { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 4px; }
.late-text { color: var(--danger); font-weight: 600; }
.small-select { width: auto; min-height: 28px; padding: 2px 6px; font-size: 12px; flex: 1; }
.small-link { font-size: 12px; }
.plan-end { text-align: center; padding: 8px; }
@media (max-width: 860px) {
  .plan-layout { grid-template-columns: 1fr; }
  .plan-unplanned { max-height: 40vh; }
  .floor-meta { text-align: left; }
}

table.grid.edit td.na-cell { color: var(--muted); text-align: center; background: #f6f4f0; }
.step-tool { font-weight: 700; color: var(--accent); }
.step-tool.bad { color: var(--danger); }
.step-tool.small { font-size: 12px; }
.inline-check { display: inline-flex; align-items: center; gap: 6px; font-size: 14px; }
.rate-chart table.rates input { width: 110px; min-height: 30px; }
.rate-block { margin-bottom: 18px; }

/* costing dropdown lists */
.grid tr.muted td { color: var(--muted, #888); }
.list-add { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; align-items: center; }
.list-add input { max-width: 240px; }
.list-add input.short { max-width: 150px; }
ul.plain { list-style: none; padding: 0; margin: 0; font-size: 13px; }
ul.plain li { padding: 3px 0; }
