:root {
  --canvas: #f2f3f0;
  --surface: #ffffff;
  --surface-subtle: #f8f8f5;
  --ink: #181b18;
  --ink-soft: #626761;
  --ink-faint: #898e88;
  --sidebar: #171a17;
  --sidebar-soft: #262a26;
  --brass: #a4772e;
  --brass-hover: #896122;
  --brass-soft: #f5ecdc;
  --line: #dde0da;
  --line-strong: #c7cbc4;
  --danger: #a83d43;
  --danger-soft: #f9e9ea;
  --warn: #8b661e;
  --warn-soft: #fbf2df;
  --ok: #376548;
  --ok-soft: #e8f1ea;
  --radius-sm: 8px;
  --radius: 14px;
  --radius-lg: 20px;
  --shadow-sm: 0 1px 2px rgba(20, 24, 20, .05);
  --shadow: 0 12px 30px rgba(20, 24, 20, .07);
  --sans: Inter, "SF Pro Text", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --display: "Iowan Old Style", "Palatino Linotype", Georgia, serif;
  --motion: 180ms ease;
}

* { box-sizing: border-box; }
html { min-width: 320px; }
html, body { margin: 0; padding: 0; }
body {
  min-height: 100vh;
  background: var(--canvas);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}
button, input, select, textarea { font: inherit; }
button, a { touch-action: manipulation; }
a { color: var(--brass-hover); }
h1, h2, h3 { color: var(--ink); }
h1, h2 { font-family: var(--display); font-weight: 600; }
h3 { font-weight: 650; }

.skip-link {
  position: fixed;
  left: 16px;
  top: -80px;
  z-index: 1000;
  padding: 10px 14px;
  background: var(--surface);
  color: var(--ink);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
}
.skip-link:focus { top: 16px; }

/* App shell */
.app-shell { min-height: 100vh; display: grid; grid-template-columns: 248px minmax(0, 1fr); }
.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  z-index: 30;
  display: flex;
  flex-direction: column;
  padding: 28px 20px 20px;
  background: var(--sidebar);
  color: #f5f2ea;
  border-right: 1px solid #2e322d;
}
.brand-block { padding: 0 10px 28px; border-bottom: 1px solid #30342f; }
.brand {
  font-family: var(--display);
  font-size: 25px;
  line-height: 1;
  letter-spacing: .16em;
  color: #f7f4ec;
}
.brand span { color: #c79a53; }
.brand-caption {
  margin-top: 9px;
  color: #9fa49d;
  font-size: 11px;
  font-weight: 650;
  letter-spacing: .14em;
  text-transform: uppercase;
}
.side-nav { display: grid; gap: 6px; margin-top: 24px; }
.tab {
  min-height: 48px;
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 0 12px;
  border: 1px solid transparent;
  border-radius: 10px;
  background: transparent;
  color: #bec3bc;
  cursor: pointer;
  text-align: left;
  transition: background var(--motion), color var(--motion), border-color var(--motion);
}
.tab svg, .secure-pill svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex: none;
}
.tab:hover { color: #fff; background: var(--sidebar-soft); }
.tab.active {
  color: #fff;
  background: #2a2e29;
  border-color: #3c413b;
  box-shadow: inset 3px 0 0 #c09550;
}
.sidebar-user {
  margin-top: auto;
  display: grid;
  grid-template-columns: 36px minmax(0, 1fr);
  align-items: center;
  gap: 2px 10px;
  padding: 16px 10px 0;
  border-top: 1px solid #30342f;
}
.user-avatar {
  grid-row: 1 / span 2;
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: #c09550;
  color: #18150e;
  font-weight: 750;
}
.who { min-width: 0; color: #f3f0e8; font-size: 13px; overflow-wrap: anywhere; }
.text-button {
  justify-self: start;
  padding: 0;
  border: 0;
  background: transparent;
  color: #999f98;
  cursor: pointer;
  font-size: 12px;
}
.text-button:hover { color: #fff; }

.workspace { min-width: 0; }
.workspace-head {
  min-height: 104px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  padding: 22px clamp(20px, 4vw, 52px);
  background: rgba(255, 255, 255, .82);
  border-bottom: 1px solid var(--line);
}
.workspace-head h1 {
  margin: 3px 0 0;
  font-size: clamp(22px, 2.2vw, 31px);
  line-height: 1.2;
}
.eyebrow, .section-kicker {
  color: var(--brass-hover);
  font-size: 11px;
  font-weight: 750;
  letter-spacing: .13em;
  text-transform: uppercase;
}
.secure-pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  flex: none;
  padding: 8px 12px;
  border: 1px solid #d8ddd6;
  border-radius: 999px;
  background: #f8faf7;
  color: var(--ok);
  font-size: 12px;
  font-weight: 650;
}
.secure-pill svg { width: 16px; height: 16px; }
.wrap { width: min(1280px, 100%); margin: 0 auto; padding: 32px clamp(16px, 4vw, 52px) 72px; }

/* Surfaces */
.card {
  margin-bottom: 20px;
  padding: clamp(20px, 2.5vw, 30px);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
}
.card h2 { margin: 3px 0 6px; font-size: clamp(22px, 2.3vw, 29px); line-height: 1.2; }
.card .sub { max-width: 78ch; margin-bottom: 20px; color: var(--ink-soft); font-size: 13px; }
.grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 20px; }
.empty-state { padding: 42px 16px; color: var(--ink-soft); text-align: center; }
.empty { padding: 48px 20px; color: var(--ink-soft); text-align: center; }
.empty .big { font-size: 36px; opacity: .24; }

/* Forms */
.field { min-width: 0; margin-bottom: 14px; }
.field label {
  display: block;
  margin-bottom: 6px;
  color: #555b55;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .055em;
  text-transform: uppercase;
}
.required { color: var(--danger); }
input, select, textarea {
  width: 100%;
  min-height: 46px;
  padding: 10px 12px;
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  background: var(--surface);
  color: var(--ink);
  font-size: 14px;
  transition: border-color var(--motion), box-shadow var(--motion), background var(--motion);
}
input::placeholder, textarea::placeholder { color: #9ca19b; }
input:hover, select:hover, textarea:hover { border-color: #afb5ad; }
input:focus, select:focus, textarea:focus, button:focus-visible, a:focus-visible {
  outline: none;
  border-color: var(--brass);
  box-shadow: 0 0 0 3px rgba(164, 119, 46, .18);
}
textarea { min-height: 80px; resize: vertical; }
.row, .row-2, .row-3 { display: grid; gap: 16px; margin: 12px 0; }
.row, .row-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.row-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.checks { display: flex; flex-wrap: wrap; gap: 10px 20px; margin: 12px 0; }
label.chk, .checks label {
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--ink);
  font-size: 14px;
  text-transform: none;
}
label.chk input, .checks input { width: 18px; min-height: 18px; accent-color: var(--brass); }

/* Actions */
.btn {
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 9px 17px;
  border: 1px solid var(--brass);
  border-radius: 10px;
  background: var(--brass);
  color: #fff;
  cursor: pointer;
  font-size: 14px;
  font-weight: 700;
  text-decoration: none;
  transition: background var(--motion), border-color var(--motion), box-shadow var(--motion), opacity var(--motion);
}
.btn:hover { background: var(--brass-hover); border-color: var(--brass-hover); }
.btn:active { opacity: .82; }
.btn.ghost { background: var(--surface); color: var(--ink); border-color: var(--line-strong); font-weight: 600; }
.btn.ghost:hover { background: var(--surface-subtle); border-color: #aeb4ac; }
.btn.danger { background: var(--surface); color: var(--danger); border-color: #e5bfc2; }
.btn.danger:hover { background: var(--danger-soft); }
.btn.sm { min-height: 36px; padding: 7px 11px; font-size: 12px; }
.btn:disabled { opacity: .42; cursor: not-allowed; }
.btnbar { display: flex; align-items: center; flex-wrap: wrap; gap: 10px; margin-top: 12px; }

/* Category control */
.cats {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 8px 0 24px;
  padding-bottom: 18px;
  border-bottom: 1px solid var(--line);
}
.cat {
  min-height: 42px;
  padding: 8px 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: #4d534d;
  cursor: pointer;
  font-size: 13px;
  font-weight: 650;
  transition: background var(--motion), border-color var(--motion), color var(--motion);
}
.cat:hover { border-color: #b7a17b; color: var(--brass-hover); }
.cat.active { border-color: #202420; background: #202420; color: #fff; }

/* Tables */
.table-wrap { width: 100%; overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 13px 12px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
th {
  color: var(--ink-soft);
  background: var(--surface-subtle);
  font-size: 10px;
  font-weight: 750;
  letter-spacing: .08em;
  text-transform: uppercase;
  white-space: nowrap;
}
tbody tr { transition: background var(--motion); }
tbody tr:hover { background: #fafaf7; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.total-row td { border-top: 2px solid var(--ink); font-size: 15px; font-weight: 750; }
.result-lines td.num input { width: 120px; min-height: 36px; padding: 6px 8px; text-align: right; }
.overridden { color: var(--brass-hover); font-weight: 700; }

/* Status */
.badge {
  display: inline-flex;
  align-items: center;
  min-height: 26px;
  padding: 3px 9px;
  border: 1px solid transparent;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  white-space: nowrap;
}
.badge.draft { color: #5e625e; background: #eef0ec; }
.badge.checked, .badge.actual, .badge.ok { color: var(--ok); background: var(--ok-soft); border-color: #c8ddcd; }
.badge.test, .badge.needs_confirmation { color: var(--warn); background: var(--warn-soft); border-color: #ecd9ae; }
.badge.manual { color: #625282; background: #f0ebf8; }
.badge.stale, .badge.danger, .badge.requires_min_order_decision {
  color: var(--danger);
  background: var(--danger-soft);
  border-color: #ecc9cc;
}
.composition, .muted { color: var(--ink-soft); font-size: 12px; }
.alert {
  margin-bottom: 12px;
  padding: 12px 14px;
  border: 1px solid;
  border-radius: 10px;
  font-size: 13px;
}
.alert.warn { color: #725417; background: var(--warn-soft); border-color: #ead4a6; }
.alert.danger { color: #842f35; background: var(--danger-soft); border-color: #e8bfc2; }
.alert.info { color: #315b41; background: var(--ok-soft); border-color: #c6ddcc; }
.alert ul { margin: 6px 0 0; padding-left: 18px; }

/* Results */
.price-card {
  border-color: #d6c19f;
  border-left: 4px solid var(--brass);
  background: #fffefa;
  box-shadow: var(--shadow);
}
.price-main { display: flex; align-items: baseline; justify-content: space-between; flex-wrap: wrap; gap: 20px; }
.price-cat { color: var(--ink-soft); font-size: 11px; font-weight: 750; letter-spacing: .09em; text-transform: uppercase; }
.price-big {
  margin: 5px 0;
  color: var(--ink);
  font-family: var(--display);
  font-size: clamp(38px, 5vw, 56px);
  line-height: 1;
  font-variant-numeric: tabular-nums;
}
.price-sub { color: var(--ink-soft); font-size: 14px; }
.price-sub b { color: var(--brass-hover); }
.breakdown { margin-top: 18px; }
.breakdown summary { min-height: 44px; display: flex; align-items: center; color: var(--brass-hover); cursor: pointer; font-weight: 700; }
.breakdown table td { padding: 7px 10px; }
.breakdown tr.strong td { border-top: 1px solid var(--ink); font-weight: 750; }
.kpi { display: flex; flex-wrap: wrap; gap: 12px; margin: 16px 0; }
.kpi .box { min-width: 160px; padding: 13px 15px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); }
.kpi .box .l { color: var(--ink-soft); font-size: 10px; font-weight: 750; letter-spacing: .08em; text-transform: uppercase; }
.kpi .box .v { margin-top: 2px; font-family: var(--display); font-size: 21px; font-variant-numeric: tabular-nums; }
.kpi .box.accent { border-color: #d7bf99; background: var(--brass-soft); }

.owner-decisions { margin-top: 12px; }
.owner-decision, .alt-card, .item-card {
  margin-bottom: 12px;
  padding: 15px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface-subtle);
}
.owner-decision b { display: block; margin-bottom: 4px; }
.owner-decision p { margin: 0 0 4px; font-size: 13px; }
.alt-title { margin-bottom: 6px; color: var(--ink-soft); font-size: 11px; font-weight: 750; letter-spacing: .06em; text-transform: uppercase; }
.item-card h3 { margin: 0 0 4px; font-size: 16px; }
.item-head { display: flex; align-items: flex-start; justify-content: space-between; flex-wrap: wrap; gap: 10px; }
.order-meta {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 1px;
  margin: 20px 0 16px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--line);
}
.order-meta > div { min-width: 0; padding: 13px 15px; background: var(--surface-subtle); }
.order-meta span { display: block; margin-bottom: 4px; color: var(--ink-soft); font-size: 10px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; }
.order-meta b { display: block; overflow-wrap: anywhere; font-size: 13px; }

/* Toast */
#toast {
  position: fixed;
  left: 50%;
  bottom: 20px;
  z-index: 100;
  max-width: min(520px, calc(100vw - 32px));
  padding: 12px 18px;
  border-radius: 10px;
  background: var(--ink);
  color: #fff;
  box-shadow: 0 12px 36px rgba(0, 0, 0, .22);
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 10px);
  transition: opacity var(--motion), transform var(--motion);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.err { background: var(--danger); }

/* Login */
.login-wrap {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
  background: var(--canvas);
}
.login-wrap::before {
  content: "LATUNING";
  position: fixed;
  right: 5vw;
  bottom: -3vw;
  color: rgba(24, 27, 24, .035);
  font-family: var(--display);
  font-size: clamp(90px, 17vw, 260px);
  letter-spacing: .08em;
  pointer-events: none;
}
.login-card {
  position: relative;
  width: min(100%, 420px);
  padding: 38px;
  border: 1px solid var(--line);
  border-top: 4px solid var(--brass);
  border-radius: var(--radius-lg);
  background: var(--surface);
  box-shadow: var(--shadow);
}
.login-card .brand { color: var(--ink); font-size: 28px; text-align: center; }
.login-card .brand b { color: var(--brass); }
.login-card p { margin: 8px 0 26px; color: var(--ink-soft); font-size: 13px; text-align: center; }
.login-card .field { text-align: left; }
.login-card .btn { width: 100%; margin-top: 10px; }

@media (max-width: 1023px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar {
    position: sticky;
    height: auto;
    min-height: 72px;
    flex-direction: row;
    align-items: center;
    gap: 16px;
    padding: 10px 16px;
  }
  .brand-block { min-width: 160px; padding: 0; border: 0; }
  .brand { font-size: 20px; }
  .brand-caption { display: none; }
  .side-nav { grid-template-columns: repeat(3, auto); gap: 4px; margin: 0; }
  .tab { width: auto; padding: 0 10px; }
  .tab.active { box-shadow: inset 0 -3px 0 #c09550; }
  .sidebar-user { margin: 0 0 0 auto; padding: 0; border: 0; grid-template-columns: 32px auto; }
  .user-avatar { width: 32px; height: 32px; }
  .text-button { display: none; }
}

@media (max-width: 767px) {
  body { font-size: 16px; }
  .sidebar { align-items: stretch; flex-wrap: wrap; gap: 8px; }
  .brand-block { min-width: 0; }
  .side-nav { order: 3; width: 100%; grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .tab { min-height: 44px; justify-content: center; padding: 0 6px; font-size: 11px; }
  .tab svg { width: 18px; height: 18px; }
  .sidebar-user { align-self: center; }
  .sidebar-user .who { display: none; }
  .workspace-head { min-height: 88px; padding: 18px 16px; }
  .workspace-head h1 { font-size: 22px; }
  .secure-pill { display: none; }
  .wrap { padding: 20px 12px 56px; }
  .card { padding: 20px 16px; border-radius: 16px; }
  .grid, .row, .row-2, .row-3, .order-meta { grid-template-columns: 1fr; }
  .order-meta { gap: 0; }
  .order-meta > div { border-bottom: 1px solid var(--line); }
  .btn { width: 100%; }
  .btnbar .btn { width: 100%; }
  input, select, textarea { min-height: 48px; font-size: 16px; }
  .cats { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .cat { min-height: 46px; border-radius: 10px; }
}

@media (max-width: 420px) {
  .brand { font-size: 18px; }
  .sidebar-user { display: none; }
  .tab { gap: 5px; }
  .tab svg { display: none; }
  .cats { grid-template-columns: 1fr; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    scroll-behavior: auto !important;
    animation-duration: .01ms !important;
    transition-duration: .01ms !important;
  }
}
