:root {
  --paper: #F7F4EC;
  --surface: #FFFFFF;
  --surface-sunken: #F1EDE0;
  --ink: #1E241F;
  --ink-muted: #5B6357;
  --border: #E3DDC8;
  --brass: #8F6A2E;
  --brass-strong: #6E5220;
  --brass-contrast: #FFFFFF;
  --success: #3E7D5C;
  --success-bg: #E4EEE7;
  --warning: #B4791F;
  --warning-bg: #F5E9D6;
  --critical: #A8433A;
  --critical-bg: #F5E1DE;
  --info: #3B6E93;
  --info-bg: #E2EBF1;
  --shadow: 0 1px 2px rgba(30,36,31,0.06), 0 6px 16px rgba(30,36,31,0.06);
  --radius: 10px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  background: var(--paper);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3 {
  font-family: Georgia, "Iowan Old Style", "Noto Serif", "Times New Roman", serif;
  font-weight: 600;
  text-wrap: balance;
  margin: 0;
  color: var(--ink);
}
.num { font-variant-numeric: tabular-nums; font-feature-settings: "tnum" 1; }
.mono { font-family: "SF Mono", "Cascadia Mono", Consolas, "Roboto Mono", monospace; }
button { font-family: inherit; }
a { color: var(--brass-strong); }
:focus-visible { outline: 2px solid var(--brass); outline-offset: 2px; }

.app { display: flex; min-height: 100vh; }

.rail {
  width: 208px;
  flex-shrink: 0;
  background: var(--surface-sunken);
  border-right: 1px solid var(--border);
  padding: 20px 14px;
  display: flex;
  flex-direction: column;
  gap: 22px;
}
.wordmark { font-family: Georgia, "Iowan Old Style", serif; font-size: 18px; padding: 0 8px; }
.wordmark small {
  display: block; font-family: -apple-system, BlinkMacSystemFont, sans-serif;
  font-size: 11px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--ink-muted); margin-top: 2px;
}
nav.nav { display: flex; flex-direction: column; gap: 2px; }
.nav a {
  display: flex; align-items: center; gap: 10px;
  color: var(--ink-muted); padding: 9px 10px; border-radius: 8px; font-size: 14px;
  text-align: left; text-decoration: none;
}
.nav a svg { flex-shrink: 0; stroke: currentColor; }
.nav a:hover { background: var(--border); color: var(--ink); }
.nav a.active { background: var(--brass); color: var(--brass-contrast); }

.rail-foot { margin-top: auto; padding: 10px 8px; font-size: 12px; color: var(--ink-muted); border-top: 1px solid var(--border); }
.rail-foot strong { color: var(--ink); font-weight: 600; }

main { flex: 1; padding: 24px 26px 32px; min-width: 0; }

.view-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; margin-bottom: 20px; flex-wrap: wrap; }
.view-head h1 { font-size: 22px; }
.view-head p { margin: 4px 0 0; color: var(--ink-muted); font-size: 13.5px; }

.btn { border-radius: 8px; padding: 9px 16px; font-size: 13.5px; font-weight: 600; cursor: pointer; border: 1px solid transparent; text-decoration: none; display: inline-block; }
.btn-primary { background: var(--brass); color: var(--brass-contrast); }
.btn-primary:hover { background: var(--brass-strong); }
.btn-secondary { background: transparent; border-color: var(--border); color: var(--ink); }
.btn-secondary:hover { border-color: var(--brass); color: var(--brass-strong); }

.tiles { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 24px; }
.tile { border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; background: var(--surface); }
.tile .label { font-size: 12px; color: var(--ink-muted); }
.tile .value { font-family: Georgia, serif; font-size: 24px; margin-top: 6px; }
.tile .sub { font-size: 12px; margin-top: 4px; color: var(--ink-muted); }
.tile .sub.accent { color: var(--brass-strong); }

.pill { display: inline-flex; align-items: center; gap: 5px; padding: 3px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.pill::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.pill-neutral { background: var(--surface-sunken); color: var(--ink-muted); }
.pill-info { background: var(--info-bg); color: var(--info); }
.pill-success { background: var(--success-bg); color: var(--success); }
.pill-warning { background: var(--warning-bg); color: var(--warning); }
.pill-critical { background: var(--critical-bg); color: var(--critical); }

.tag { font-size: 11px; border: 1px solid var(--border); color: var(--ink-muted); padding: 1px 7px; border-radius: 5px; }

.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; min-width: 560px; }
thead th { text-align: left; font-size: 11.5px; letter-spacing: 0.05em; text-transform: uppercase; color: var(--ink-muted); font-weight: 600; padding: 10px 14px; border-bottom: 1px solid var(--border); }
tbody td { padding: 11px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tbody tr:last-child td { border-bottom: none; }
tbody tr.row-clickable { cursor: pointer; }
tbody tr.row-clickable:hover { background: var(--surface-sunken); }
tbody tr.row-active { background: var(--info-bg); }
td.amount, th.amount { text-align: right; }
.client-cell strong { display: block; }
.client-cell .tag { margin-top: 2px; }

.two-col { display: grid; grid-template-columns: 1.5fr 1fr; gap: 18px; align-items: start; }

.detail-card { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); padding: 18px; position: sticky; top: 20px; }
.detail-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; margin-bottom: 4px; }
.detail-head h3 { font-size: 17px; }
.detail-sub { color: var(--ink-muted); font-size: 13px; margin-top: 2px; }
.detail-amount { font-family: Georgia, serif; font-size: 26px; margin: 14px 0 18px; }

.timeline { list-style: none; margin: 0 0 18px; padding: 0; }
.timeline li { position: relative; padding: 0 0 16px 22px; border-left: 2px solid var(--border); margin-left: 5px; font-size: 13px; }
.timeline li:last-child { border-color: transparent; padding-bottom: 0; }
.timeline li::before { content: ""; position: absolute; left: -7px; top: 1px; width: 12px; height: 12px; border-radius: 50%; background: var(--surface); border: 2px solid var(--border); }
.timeline li.done { color: var(--ink); }
.timeline li.done::before { border-color: var(--success); background: var(--success); }
.timeline li.current { color: var(--ink); font-weight: 600; }
.timeline li.current::before { border-color: var(--info); background: var(--info); }
.timeline li.pending { color: var(--ink-muted); }
.timeline time { display: block; font-size: 11.5px; color: var(--ink-muted); font-weight: 400; margin-top: 1px; }

.lines-mini { width: 100%; font-size: 12.5px; border-collapse: collapse; margin-bottom: 14px; }
.lines-mini td { padding: 5px 0; border-bottom: 1px dashed var(--border); }
.lines-mini tr:last-child td { border-bottom: none; }
.lines-mini td.amount { text-align: right; }

.detail-actions { display: flex; gap: 8px; }

.form-card { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); padding: 22px; max-width: 640px; }
.field { margin-bottom: 16px; }
.field label { display: block; font-size: 12.5px; font-weight: 600; margin-bottom: 6px; color: var(--ink); }
.field .hint { font-size: 12px; color: var(--ink-muted); margin-top: 5px; }
.field input, .field select { width: 100%; border: 1px solid var(--border); border-radius: 7px; padding: 9px 11px; font-size: 14px; background: var(--paper); color: var(--ink); font-family: inherit; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.lines-table { width: 100%; border-collapse: collapse; margin-bottom: 10px; }
.lines-table th { text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--ink-muted); padding: 0 8px 6px; }
.lines-table td { padding: 4px 8px 4px 0; }
.lines-table input { padding: 7px 9px; font-size: 13.5px; }
.lines-table td.qty input, .lines-table td.price input { width: 84px; }
.lines-table td.amount { text-align: right; font-family: Georgia, serif; padding-right: 4px; white-space: nowrap; }
.add-line { font-size: 12.5px; color: var(--brass-strong); background: none; border: none; cursor: pointer; padding: 4px 0; }

.total-row { display: flex; justify-content: flex-end; align-items: baseline; gap: 10px; padding: 12px 0; border-top: 1px solid var(--border); margin-top: 4px; }
.total-row .label { font-size: 13px; color: var(--ink-muted); }
.total-row .value { font-family: Georgia, serif; font-size: 20px; }

.legal-note { background: var(--surface-sunken); border-radius: 8px; padding: 10px 12px; font-size: 12px; color: var(--ink-muted); margin: 14px 0 18px; }
.transmit-note { display: flex; gap: 8px; align-items: flex-start; font-size: 12.5px; color: var(--ink-muted); margin: -4px 0 18px; }
.transmit-note .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--info); margin-top: 6px; flex-shrink: 0; }

.form-actions { display: flex; gap: 10px; justify-content: flex-end; }

.settings-card { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); padding: 20px; max-width: 520px; margin-bottom: 14px; }
.settings-card h3 { font-size: 14px; margin-bottom: 12px; }
.settings-row { display: flex; justify-content: space-between; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 13.5px; }
.settings-row:last-child { border-bottom: none; }
.settings-row .k { color: var(--ink-muted); }
.connected { color: var(--success); font-weight: 600; display: inline-flex; align-items: center; gap: 5px; }
.connected::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: var(--success); }
.disconnected { color: var(--critical); font-weight: 600; display: inline-flex; align-items: center; gap: 5px; }
.disconnected::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: var(--critical); }

.activity { list-style: none; margin: 0; padding: 0; }
.activity li { display: flex; justify-content: space-between; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--border); font-size: 13px; }
.activity li:last-child { border-bottom: none; }
.activity time { color: var(--ink-muted); font-size: 12px; white-space: nowrap; }

.panel-title { font-size: 13px; font-weight: 600; margin-bottom: 10px; color: var(--ink-muted); text-transform: uppercase; letter-spacing: 0.04em; }

.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px; }
.login-card { width: 100%; max-width: 340px; }
.login-card .wordmark { padding: 0 0 18px; text-align: center; }
.login-error { color: var(--critical); font-size: 13px; margin-bottom: 12px; }

.tabbar { display: none; }

@media (max-width: 720px) {
  .app { flex-direction: column; min-height: 0; }
  .rail { width: 100%; flex-direction: row; align-items: center; border-right: none; border-bottom: 1px solid var(--border); padding: 12px 16px; }
  .rail .nav, .rail-foot { display: none; }
  main { padding: 18px 16px 84px; }
  .tiles { grid-template-columns: 1fr 1fr; }
  .two-col { grid-template-columns: 1fr; }
  .detail-card { position: static; }
  .tabbar {
    display: flex; position: fixed; bottom: 0; left: 0; right: 0;
    background: var(--surface); border-top: 1px solid var(--border); padding: 6px 4px 8px; justify-content: space-around;
  }
  .tabbar a { display: flex; flex-direction: column; align-items: center; gap: 3px; color: var(--ink-muted); font-size: 10.5px; padding: 4px 6px; text-decoration: none; }
  .tabbar a.active { color: var(--brass); }
  .field-row { grid-template-columns: 1fr; }
}
@media (max-width: 480px) {
  .tiles { grid-template-columns: 1fr; }
  main { padding: 16px 12px 84px; }
  .lines-table td.qty input, .lines-table td.price input { width: 60px; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
}
