:root {
  --bg: #f4f6fa; --card: #fff; --ink: #1d2433; --muted: #667085; --line: #e3e7ef;
  --brand: #0072ce; --brand-ink: #fff; --good: #067647; --good-bg: #e7f6ee;
  --warn: #b54708; --warn-bg: #fff4e5; --bad: #b42318; --bad-bg: #fdecea;
  --info-bg: #eaf2fd; --radius: 10px;
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--ink); font: 14px/1.45 "Segoe UI", system-ui, -apple-system, Roboto, Arial, sans-serif; }
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 22px; margin: 6px 0 10px; }
h2 { font-size: 15px; margin: 0 0 10px; }
small, .muted { color: var(--muted); }
.muted b { color: var(--ink); }

.topbar { display: flex; align-items: center; gap: 18px; padding: 10px 20px; background: #102a43; color: #fff; flex-wrap: wrap; }
.topbar a { color: #dbe7f5; }
.topbar .brand { font-weight: 700; font-size: 17px; color: #fff; }
.topbar nav { display: flex; gap: 14px; align-items: center; flex: 1; }
.topbar .logout { display: flex; gap: 10px; align-items: center; margin: 0; }
.topbar .logout .muted { color: #9fb3c8; }
.testbar { background: #fff4e5; color: var(--warn); text-align: center; padding: 5px 16px; font-weight: 600; font-size: 13px; border-bottom: 1px solid #f5d9b0; }

.wrap { max-width: 1240px; margin: 0 auto; padding: 18px 16px 48px; }
.wrap.wide { max-width: 1500px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 14px; }
.narrow { max-width: 640px; }

.btn { display: inline-block; border: 1px solid var(--line); background: #fff; color: var(--ink); border-radius: 8px; padding: 8px 14px; font: inherit; cursor: pointer; text-decoration: none; }
.btn:hover { border-color: #b8c2d3; text-decoration: none; }
.btn-primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); font-weight: 600; }
.btn-primary:hover { background: #005fae; }
.topbar .btn-primary { color: #fff; }
.btn-good { background: var(--good-bg); border-color: #a6dfc0; color: var(--good); font-weight: 600; }
.btn-ghost { background: transparent; border-color: transparent; color: inherit; }
.topbar .btn-ghost { color: #dbe7f5; border-color: #33506e; }
.btn-sm { padding: 4px 10px; font-size: 13px; }
.btn-row { display: flex; flex-wrap: wrap; gap: 8px; }

input, select, textarea { width: 100%; font: inherit; padding: 8px 10px; border: 1px solid #cfd6e2; border-radius: 8px; background: #fff; color: var(--ink); }
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid #9cc7f0; border-color: var(--brand); }
label { display: block; font-weight: 600; font-size: 13px; }
label input, label select, label textarea { margin-top: 4px; font-weight: 400; }
label.check { display: flex; align-items: center; gap: 8px; font-weight: 400; }
label.check input { width: auto; margin: 0; }
.stack > * + * { margin-top: 10px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid2 > * + * { margin-top: 0; }
.grid2 .full { grid-column: 1 / -1; }

.flash { padding: 10px 14px; border-radius: 8px; margin-bottom: 14px; background: var(--good-bg); color: var(--good); border: 1px solid #a6dfc0; }
.flash-error { background: var(--bad-bg); color: var(--bad); border-color: #f4b8b1; }

.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login { width: 100%; max-width: 360px; }
.login > * + * { margin-top: 12px; }
.login .btn { width: 100%; }

.kpis { display: grid; grid-template-columns: repeat(6, 1fr); gap: 10px; margin-bottom: 14px; }
.kpi { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; color: var(--ink); }
.kpi:hover { text-decoration: none; border-color: #b8c2d3; }
.kpi b { display: block; font-size: 26px; line-height: 1.1; }
.kpi span { color: var(--muted); font-size: 13px; }
.kpi-warn b { color: var(--warn); } .kpi-bad b { color: var(--bad); } .kpi-good b { color: var(--good); }

.filters { display: flex; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; }
.filters input { flex: 1 1 260px; width: auto; }
.filters select { width: auto; }

.table-wrap { overflow-x: auto; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 14px; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 9px 12px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); background: #fafbfd; }
tr:last-child td { border-bottom: 0; }
tr.row-overdue td:first-child { box-shadow: inset 3px 0 var(--bad); }
tr.row-soon td:first-child { box-shadow: inset 3px 0 var(--warn); }
.ref { font-weight: 700; white-space: nowrap; }
.nowrap { white-space: nowrap; }
.empty { text-align: center; padding: 40px; }

.badge { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; background: #eef1f6; color: #344054; white-space: nowrap; }
.st-queued, .st-processing { background: #f2f4f7; color: #475467; }
.st-to_assign { background: #fef0c7; color: #93370d; }
.st-assigned, .st-in_progress { background: var(--info-bg); color: #175cd3; }
.st-draft_received { background: #f4ebff; color: #6941c6; }
.st-responded { background: var(--good-bg); color: var(--good); }
.st-closed { background: #f2f4f7; color: #667085; }
.urg-urgent { background: var(--bad); color: #fff; } .urg-high { background: var(--bad-bg); color: var(--bad); }
.urg-normal { background: #eef1f6; } .urg-low { background: #f8f9fb; color: var(--muted); }
.badge-sens { background: #fff0f6; color: #c11574; }
.due sup { font-size: 9px; color: var(--muted); margin-left: 2px; }
.due small { display: block; font-weight: 700; }
.due-overdue, .due-overdue small { color: var(--bad); font-weight: 700; }
.due-soon, .due-soon small { color: var(--warn); font-weight: 600; }

.letter-head h1 { margin-bottom: 6px; }
.h-sub { font-weight: 400; color: var(--muted); }
.chips { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 14px; }
.letter-grid { display: grid; grid-template-columns: minmax(0, 1.1fr) minmax(360px, 1fr); gap: 16px; align-items: start; }
.scan img { width: 100%; border: 1px solid var(--line); border-radius: 6px; background: #fff; margin-bottom: 10px; display: block; }
.files { display: flex; flex-wrap: wrap; gap: 8px; margin: 6px 0 12px; }
.ocr summary { cursor: pointer; color: var(--muted); }
.ocr pre { white-space: pre-wrap; background: #fff; border: 1px solid var(--line); border-radius: 8px; padding: 12px; max-height: 480px; overflow: auto; font-size: 12.5px; }
.pre { white-space: pre-wrap; background: #f8f9fb; padding: 8px 10px; border-radius: 8px; }
.hint { background: var(--info-bg); padding: 6px 10px; border-radius: 8px; }
.inline-close { display: flex; gap: 8px; margin: -4px 0 14px; }
.timeline { list-style: none; padding: 0; margin: 0; }
.timeline li { padding: 6px 0; border-bottom: 1px dashed var(--line); }
.timeline li:last-child { border-bottom: 0; }
details summary { cursor: pointer; }
.processing { display: flex; gap: 14px; align-items: center; }
.processing p { margin: 4px 0 0; }
.spinner { width: 28px; height: 28px; border: 3px solid #cfe0f5; border-top-color: var(--brand); border-radius: 50%; animation: spin 1s linear infinite; flex: none; }
@keyframes spin { to { transform: rotate(360deg); } }
.row-form { display: flex; gap: 8px; align-items: center; }
.row-form input[name=topics] { min-width: 180px; }

@media (max-width: 1000px) {
  .kpis { grid-template-columns: repeat(3, 1fr); }
  .letter-grid { grid-template-columns: 1fr; }
}
@media (max-width: 560px) {
  .kpis { grid-template-columns: repeat(2, 1fr); }
  .grid2 { grid-template-columns: 1fr; }
  .row-form { flex-wrap: wrap; }
  .topbar nav { order: 3; flex-basis: 100%; }
}
