:root {
  --bg: #f6f7f9;
  --surface: #fff;
  --text: #15171a;
  --muted: #5b6470;
  --line: #dfe3e8;
  --accent: #191a7d;
  --accent-ink: #fff;
  --accent-soft: #fdecec;
  --alert: #c62828;
  --select: #e8edf5;
  --flag-ink: #9f1f1f;
  --ok-ink: #1f6b3a;
  --panel-w: 720px;
  font-family: Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
}
* { box-sizing: border-box; }
body { margin: 0; font-size: 14px; line-height: 1.45; }
button, input, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--text); outline-offset: 2px; }
[hidden] { display: none !important; }

/* top bar */
.topbar { position: sticky; top: 0; z-index: 10; height: 56px; display: flex; align-items: center; gap: 12px; padding: 0 20px; background: var(--surface); border-bottom: 1px solid var(--line); }
.topbar h1 { margin: 0; font-size: 16px; font-weight: 650; }
.logo { height: 34px; width: auto; flex: none; display: block; border-radius: 3px; }
.chip { display: inline-flex; align-items: center; height: 24px; padding: 0 10px; border: 1px solid var(--line); border-radius: 12px; background: #eef0f3; color: var(--text); font-size: 12px; font-weight: 600; }
.clock { margin-left: auto; color: var(--muted); font-size: 13px; }

/* shell */
.shell { display: grid; grid-template-columns: 208px minmax(0, 1fr); min-height: calc(100vh - 56px); }
.nav { padding: 16px 12px; background: var(--surface); border-right: 1px solid var(--line); display: flex; flex-direction: column; gap: 2px; }
.nav a { display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: 6px; color: var(--muted); text-decoration: none; font-weight: 550; border-left: 3px solid transparent; }
.nav a:hover { background: var(--bg); color: var(--text); }
.nav a[aria-current="page"] { background: var(--select); color: var(--text); font-weight: 650; border-left-color: var(--accent); }
.nav-icon { width: 18px; height: 18px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.nav a[aria-current="page"] .nav-icon { stroke: var(--accent); }
.nav .count { color: var(--muted); font-size: 12px; font-weight: 500; }
.main { padding: 24px; min-width: 0; }
.main h2 { margin: 0 0 16px; font-size: 20px; font-weight: 650; }
.loading { color: var(--muted); }

/* KPI cards */
.kpis { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 16px; margin-bottom: 24px; }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 16px 18px; }
.kpi dt { color: var(--muted); font-size: 13px; }
.kpi small { display: block; margin-top: 2px; color: var(--muted); font-size: 12px; }
.kpi dd { margin: 4px 0 0; font-size: 26px; font-weight: 650; letter-spacing: -0.01em; font-variant-numeric: tabular-nums; }

/* table */
.table-wrap { background: var(--surface); border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
table { width: 100%; border-collapse: collapse; }
th { text-align: left; font-size: 12px; font-weight: 600; color: var(--muted); padding: 10px 12px; border-bottom: 1px solid var(--line); background: #fafbfc; white-space: nowrap; }
td { padding: 11px 12px; border-bottom: 1px solid var(--line); vertical-align: middle; }
table { font-variant-numeric: tabular-nums; }
.clamp { display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; line-clamp: 2; overflow: hidden; overflow-wrap: anywhere; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr.static { cursor: default; }
.place { display: block; color: var(--muted); font-size: 12px; }
.cell-item strong { display: block; }
.cell-item span { color: var(--muted); font-size: 13px; }
.catalogue th.num, .catalogue td.num { text-align: right; }
.catalogue .hint { padding: 0 2px; }
tbody tr[data-id] { cursor: pointer; }
tbody tr[data-id] { transition: background-color 0.12s; }
tbody tr[data-id]:hover { background: #f1f4f8; }
tbody tr[aria-selected="true"], tbody tr[aria-selected="true"]:hover { background: var(--select); }
td a.id { color: var(--text); font-weight: 650; text-decoration: none; white-space: nowrap; }
td a.id:hover { text-decoration: underline; }
.muted { color: var(--muted); }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.channel { width: 22px; height: 22px; display: block; }
.pill { display: inline-flex; align-items: center; gap: 6px; padding: 2px 10px 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; line-height: 18px; white-space: nowrap; }
.pill::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; flex: none; }
.pill-alert { background: #fdecec; color: #9f1f1f; }
.pill-action { background: #e6eefc; color: #1a4aa8; }
.pill-ok { background: #e3f3e8; color: #1b6035; }
.pill-idle { background: #eceef2; color: #454d58; }
.has-panel .col-order, .has-panel .col-amount, .has-panel .col-next, .has-panel .col-owner, .has-panel .col-day, .has-panel .col-due { display: none; }
.has-panel td, .has-panel th { padding-left: 10px; padding-right: 10px; }
.has-panel .cell-customer { min-width: 120px; }

/* rules */
.rules { background: var(--surface); border: 1px solid var(--line); border-radius: 8px; margin: 0; }
.rules div { display: grid; grid-template-columns: 200px 1fr; gap: 16px; padding: 14px 18px; border-bottom: 1px solid var(--line); }
.rules div:last-child { border-bottom: 0; }
.rules dt { font-weight: 600; }
.rules dd { margin: 0; }
/* detail panel */
.panel { position: fixed; top: 56px; right: 0; bottom: 0; width: var(--panel-w); z-index: 15; background: var(--surface); border-left: 1px solid var(--line); box-shadow: -8px 0 24px rgba(21, 23, 26, 0.06); overflow-y: auto; }
body.has-panel .main { padding-right: calc(var(--panel-w) + 16px); padding-left: 16px; }
body.has-panel .kpis { gap: 8px; }
body.has-panel .kpi { padding: 10px 12px; }
body.has-panel .kpi dd { font-size: 18px; white-space: nowrap; }
.panel-head { position: sticky; top: 0; z-index: 2; display: flex; align-items: center; gap: 12px; padding: 14px 20px; background: var(--surface); border-bottom: 1px solid var(--line); }
.panel-head h2 { margin: 0; font-size: 16px; font-weight: 650; }
.panel-head p { margin: 0; color: var(--muted); font-size: 13px; }
.panel-head .grow { flex: 1; min-width: 0; }
.panel-body { padding: 20px; display: flex; flex-direction: column; gap: 20px; }
.section h3 { margin: 0 0 8px; font-size: 13px; font-weight: 650; }
.facts { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; margin: 0; }
.facts dt { color: var(--muted); font-size: 12px; }
.facts dd { margin: 2px 0 0; font-weight: 550; }
.icon-btn { display: inline-flex; align-items: center; justify-content: center; min-width: 36px; height: 36px; padding: 0 10px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); cursor: pointer; }
.icon-btn:hover { background: var(--bg); }
.back { display: none; }

.thread { display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 88%; padding: 9px 12px; border-radius: 10px; background: var(--bg); border: 1px solid var(--line); white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.from-agent, .msg.from-owner { align-self: flex-end; background: #fff; border-color: #c9ced6; }
.msg > span { display: block; }
.msg small { display: block; margin-bottom: 2px; color: var(--muted); font-size: 12px; }
.reply-box { display: flex; gap: 8px; margin-top: 12px; }
.reply-box input { flex: 1; min-width: 0; height: 38px; padding: 0 12px; border: 1px solid #aab1bb; border-radius: 6px; background: #fff; }
.hint { margin: 6px 0 0; color: var(--muted); font-size: 13px; }

.btn { height: 38px; padding: 0 16px; border: 1px solid #aab1bb; border-radius: 6px; background: var(--surface); font-weight: 600; cursor: pointer; transition: background-color 0.12s, border-color 0.12s; }
.btn:hover { background: var(--bg); border-color: #8a93a0; }
.btn.primary { padding: 0 20px; background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 650; }
.btn.primary:hover { background: #12135f; border-color: #12135f; }
.btn.primary:active { background: #0d0e48; }
.btn:disabled { opacity: 0.6; cursor: default; }

.draft { border: 1px solid #aab1bb; border-radius: 8px; overflow: hidden; background: #fff; }
.draft-head { display: flex; justify-content: space-between; gap: 12px; padding: 8px 14px; background: #fafbfc; border-bottom: 1px solid var(--line); font-size: 13px; }
.draft-head .label { font-weight: 600; }
.draft-text { margin: 0; padding: 12px 14px; white-space: pre-wrap; overflow-wrap: anywhere; }
.draft textarea { display: block; width: 100%; min-height: 190px; padding: 12px 14px; border: 0; border-bottom: 1px solid var(--line); resize: vertical; }
.draft-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; padding: 10px 14px; border-top: 1px solid var(--line); }
.done { color: var(--ok-ink); font-weight: 600; }
.owner-note { padding: 10px 14px; border-left: 3px solid var(--alert); background: var(--accent-soft); border-radius: 0 6px 6px 0; }
.owner-note strong { display: block; margin-bottom: 2px; color: var(--flag-ink); }

.checks { margin: 0; padding: 0; list-style: none; display: flex; flex-direction: column; gap: 8px; }
.checks li { display: grid; grid-template-columns: 22px 1fr; gap: 8px; }
.mark { font-weight: 700; }
.mark.pass { color: var(--ok-ink); }
.mark.flag { color: var(--flag-ink); }
.checks .rule { font-weight: 600; }

.lines { width: 100%; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; font-size: 13px; }
.lines th, .lines td { padding: 7px 8px; vertical-align: top; }
.lines th { background: #fafbfc; white-space: nowrap; }
.lines .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.lines small { display: block; color: var(--muted); font-size: 12px; font-weight: 400; }
.lines td.cell-matched strong { display: block; }
.lines td.cell-matched span { color: var(--muted); font-size: 12px; }
.lines tr.line td { border-bottom: 1px solid var(--line); }
.lines tr.line-note td { padding: 6px 10px 8px; background: #fafbfc; border-bottom: 1px solid var(--line); color: var(--text); }
.lines tr.line-note p { margin: 2px 0; }
.lines tr.sum td { border-bottom: 0; padding-top: 5px; padding-bottom: 5px; }
.lines tr.total td { font-weight: 650; border-top: 2px solid var(--line); border-bottom: 0; }
.lines td.unclear { color: var(--text); }
.stock { font-weight: 600; }
.stock-ok { color: var(--ok-ink); }
.stock-short { color: var(--flag-ink); }

/* phone: the table becomes cards, the panel goes full screen */
@media (max-width: 760px) {
  .topbar { padding: 0 12px; gap: 8px; }
  .topbar h1 { font-size: 13px; line-height: 1.15; min-width: 0; }
  .logo { height: 26px; }
  .chip { flex: none; }
  .clock { display: none; }
  .shell { display: block; }
  .nav { flex-direction: row; overflow-x: auto; padding: 8px; border-right: 0; border-bottom: 1px solid var(--line); gap: 4px; }
  .nav a { flex: none; border-left: 0; border-bottom: 3px solid transparent; border-radius: 6px 6px 0 0; gap: 6px; }
  .nav-icon { display: none; }
  .clamp { display: block; }
  .nav a[aria-current="page"] { border-bottom-color: var(--accent); }
  .main { padding: 16px 12px; }
  body.has-panel .main { padding-right: 12px; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; margin-bottom: 16px; }
  .kpi { padding: 12px; }
  .kpi dd { font-size: 20px; }
  .table-wrap { border: 0; background: transparent; }
  .table-wrap thead { display: none; }
  .table-wrap table, .table-wrap tbody { display: block; }
  .table-wrap tbody tr { display: grid; grid-template-columns: auto 1fr auto; gap: 2px 10px; margin-bottom: 10px; padding: 12px; background: var(--surface); border: 1px solid var(--line); border-radius: 8px; }
  .table-wrap td { padding: 0; border: 0; grid-column: 1 / -1; min-width: 0; }
  .table-wrap tbody tr { align-items: center; }
  .table-wrap .cell-id { order: 1; grid-column: 1; }
  .table-wrap .cell-channel { order: 2; grid-column: 2; }
  .table-wrap .cell-due { order: 3; grid-column: 3; text-align: right; color: var(--muted); }
  .cell-customer { order: 4; font-weight: 650; margin-top: 4px; }
  .cell-job { order: 5; }
  .place { display: block; }
  .cell-amount { order: 7; text-align: left; font-weight: 600; }
  .cell-stage { order: 8; margin-top: 6px; }
  .cell-next { order: 9; color: var(--muted); }
  .cell-owner { order: 10; color: var(--muted); }
  .cell-owner::before { content: attr(data-label) ": "; }
  .cell-day { order: 9; color: var(--muted); }
  .cell-day::before { content: attr(data-label) ": "; }
  .has-panel .col-order, .has-panel .col-amount, .has-panel .col-next, .has-panel .col-owner, .has-panel .col-day, .has-panel .col-due { display: block; }
  .rules div { grid-template-columns: 1fr; gap: 2px; padding: 12px 14px; }
  .panel { top: 0; width: 100%; border-left: 0; z-index: 30; }
  .panel-head { padding: 10px 12px; }
  .back { display: inline-flex; }
  .icon-btn[aria-label="Close details"] { display: none; }
  .panel-body { padding: 16px 12px; }
  .facts { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .msg { max-width: 94%; }
  .lines { border: 0; }
  .lines thead { display: none; }
  .lines, .lines tbody { display: block; }
  .lines tr.line { display: block; margin-bottom: 10px; padding: 10px 12px; background: var(--surface); border: 1px solid var(--line); border-radius: 8px; }
  .lines tr.line td { display: flex; justify-content: space-between; gap: 12px; padding: 3px 0; border: 0; text-align: right; }
  .lines tr.line td::before { content: attr(data-label); color: var(--muted); font-weight: 500; text-align: left; flex: none; }
  .lines tr.line td.cell-asked { font-weight: 650; text-align: left; }
  .lines tr.line td.cell-asked::before { content: none; }
  .lines tr.line td.unclear { display: block; text-align: left; }
  .lines tr.line td.unclear::before { content: none; }
  .lines td.cell-matched span { display: block; }
  .lines td small { display: inline; margin-left: 6px; }
  .lines tr.line-note, .lines tr.line-note td { display: block; }
  .lines tr.line-note { margin: -4px 0 10px; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
  .lines tr.sum, .lines tr.total { display: flex; justify-content: space-between; gap: 12px; padding: 4px 12px; background: var(--surface); border-left: 1px solid var(--line); border-right: 1px solid var(--line); }
  .lines tr.sum td, .lines tr.total td { display: block; padding: 2px 0; border: 0; }
  .lines tr.sum td[colspan], .lines tr.total td[colspan] { width: auto; }
  .lines tr.sum:first-of-type { border-top: 1px solid var(--line); border-radius: 8px 8px 0 0; }
  .lines tr.total { border-bottom: 1px solid var(--line); border-radius: 0 0 8px 8px; }
  .lines tr.total td { border-top: 0; }
  .catalogue tr.static td { display: flex; justify-content: space-between; gap: 12px; padding: 4px 0; text-align: right; }
  .catalogue tr.static td::before { content: attr(data-label); color: var(--muted); text-align: left; }
  .catalogue tr.static td.cell-item { display: block; text-align: left; }
  .catalogue tr.static td.cell-item::before { content: none; }
  .draft-actions .btn { flex: 1; }
}
