:root {
  --bg: #f3f6f8;
  --surface: #ffffff;
  --ink: #14212b;
  --ink-2: #51606c;
  --line: #dde4ea;
  --line-2: #eef2f5;
  --accent: #0e5e78;
  --accent-ink: #ffffff;
  --accent-soft: #e3f1f6;
  --ok: #1d7a4c;
  --ok-soft: #e4f4ea;
  --warn: #9a6200;
  --warn-soft: #fdf3dc;
  --bad: #b3261e;
  --bad-soft: #fbe7e5;
  --radius: 10px;
  --font: 'IBM Plex Sans', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --mono: 'IBM Plex Mono', ui-monospace, Consolas, monospace;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { font: 14px/1.5 var(--font); color: var(--ink); background: var(--bg); -webkit-font-smoothing: antialiased; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 22px; font-weight: 600; margin: 0; letter-spacing: -0.01em; }
h2 { font-size: 15px; font-weight: 600; margin: 0 0 12px; }
h3 { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em; color: var(--ink-2); margin: 0 0 6px; }
code, .mono { font-family: var(--mono); font-size: 0.95em; }
.muted { color: var(--ink-2); }
.small { font-size: 12px; }
.pad { padding: 16px 20px; margin: 0; }
.mt { margin-top: 16px; }
.error-text { color: var(--bad); }
.pre { white-space: pre-line; }

/* Layout */
#app { display: flex; min-height: 100vh; }
.side { width: 220px; flex: none; background: #0d2a36; color: #d9e6ec; display: flex; flex-direction: column; padding: 20px 14px; position: sticky; top: 0; height: 100vh; }
.side[hidden] { display: none; }
.brand { display: flex; align-items: center; gap: 10px; padding: 0 8px 22px; }
.brand strong { display: block; font-size: 16px; color: #fff; line-height: 1.1; }
.brand small { display: block; font-size: 11px; letter-spacing: 0.18em; color: #8fb3c2; }
.brand-mark { width: 34px; height: 34px; border-radius: 9px; background: #1b7a99; color: #fff; display: grid; place-items: center; font-size: 18px; }
.brand.big { justify-content: center; padding-bottom: 8px; }
.brand.big strong { color: var(--ink); font-size: 20px; }
.brand.big small { color: var(--ink-2); }
.side nav { display: flex; flex-direction: column; gap: 2px; }
.side nav a { color: #c8d9e0; padding: 8px 10px; border-radius: 7px; font-weight: 500; }
.side nav a:hover { background: rgba(255,255,255,0.06); text-decoration: none; }
.side nav a.active { background: rgba(255,255,255,0.12); color: #fff; }
.side nav a.nav-cta { color: #9fe0f2; }
.side-foot { margin-top: auto; padding: 8px 10px; font-size: 12px; display: flex; flex-direction: column; gap: 4px; }
.side-foot .muted { color: #8fb3c2; overflow: hidden; text-overflow: ellipsis; }
.side-foot .link { color: #c8d9e0; text-align: left; }
main { flex: 1; min-width: 0; }
.page { max-width: 1180px; margin: 0 auto; padding: 28px 28px 60px; }
.page.wide { max-width: 1320px; }
.page.narrow { max-width: 560px; }
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.page-head h1 { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.back { font-size: 13px; display: inline-block; margin-bottom: 4px; }
.stack { display: flex; flex-direction: column; gap: 16px; }
.stack.tight { gap: 10px; }
.split { display: grid; grid-template-columns: 1fr 360px; gap: 16px; align-items: start; }
.split > :only-child { grid-column: 1 / -1; }
.invoice-layout { display: grid; grid-template-columns: minmax(0, 1fr) 380px; gap: 16px; align-items: start; }
.detail-layout { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 16px; align-items: start; }
.sticky { position: sticky; top: 16px; }

/* Cards */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px 20px; }
.card.flush { padding: 0; overflow: auto; }
.card.note { background: var(--warn-soft); border-color: #f1dca8; margin-bottom: 16px; }
.error-card { border-color: #f0c4c0; }
.empty { padding: 36px 20px; text-align: center; color: var(--ink-2); margin: 0; }

/* Forms */
label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; font-weight: 500; color: var(--ink-2); }
input, select, textarea { font: 14px var(--font); color: var(--ink); background: #fff; border: 1px solid var(--line); border-radius: 7px; padding: 8px 10px; width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
textarea { resize: vertical; }
.grid2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.grid3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.span2 { grid-column: span 2; }
.span3 { grid-column: 1 / -1; }
label.cert input { background: #fffbea; border-color: #ecd68a; }
.hint { font-weight: 400; font-size: 11px; color: var(--warn); }
.side-form { display: flex; flex-direction: column; gap: 12px; position: sticky; top: 16px; }

/* Buttons */
.btn { font: 500 14px var(--font); display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 8px 14px; border-radius: 7px; border: 1px solid var(--line); background: #fff; color: var(--ink); cursor: pointer; text-decoration: none; white-space: nowrap; }
.btn:hover { background: var(--line-2); text-decoration: none; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { background: #0b4d63; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.danger { color: var(--bad); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--bad-soft); }
.btn.small { padding: 5px 10px; font-size: 13px; }
.link { background: none; border: 0; padding: 0; font: inherit; color: var(--accent); cursor: pointer; text-align: left; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.actions.column { flex-direction: column; align-items: stretch; }
.actions.column p { margin: 0; text-align: center; }
.sticky-actions { position: sticky; bottom: 0; background: var(--bg); padding: 12px 0; }

/* Tables */
.table { width: 100%; border-collapse: collapse; }
.table th { text-align: left; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em; color: var(--ink-2); padding: 10px 14px; border-bottom: 1px solid var(--line); background: #fafcfd; white-space: nowrap; }
.table td { padding: 10px 14px; border-bottom: 1px solid var(--line-2); vertical-align: top; }
.table tr:last-child td { border-bottom: 0; }
.table .num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.table tr.click { cursor: pointer; }
.table tr.click:hover td { background: #f7fafc; }
.table tr.selected td { background: var(--accent-soft); }
.table.compact td, .table.compact th { padding: 6px 8px; font-size: 13px; }
.table.lines { margin: 4px 0 10px; }
.table.lines td { padding: 6px 8px 6px 0; border: 0; }
.table.lines th { background: none; padding: 6px 8px 4px 0; border: 0; }

/* Status */
.pill { display: inline-block; font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 99px; background: var(--line-2); color: var(--ink-2); white-space: nowrap; letter-spacing: 0.02em; }
.pill-issued, .pill-open { background: var(--accent-soft); color: var(--accent); }
.pill-partly_paid { background: var(--warn-soft); color: var(--warn); }
.pill-paid { background: var(--ok-soft); color: var(--ok); }
.pill-cancelled { background: var(--bad-soft); color: var(--bad); }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-bottom: 18px; }
.stat { display: flex; flex-direction: column; gap: 2px; }
.stat strong { font-size: 20px; font-weight: 600; font-variant-numeric: tabular-nums; }
.stat.warn strong { color: var(--bad); }
.tabs { display: flex; gap: 4px; margin-bottom: 10px; }
.tab { font: 500 13px var(--font); border: 0; background: none; padding: 6px 12px; border-radius: 7px; color: var(--ink-2); cursor: pointer; }
.tab.active { background: var(--surface); color: var(--ink); box-shadow: 0 0 0 1px var(--line); }

/* Invoice builder */
.drop { border: 2px dashed #b8cbd6; border-radius: var(--radius); padding: 26px; text-align: center; cursor: pointer; background: #f8fbfc; color: var(--ink); font-size: 14px; gap: 6px; align-items: center; }
.drop:hover, .drop.over { border-color: var(--accent); background: var(--accent-soft); }
.drop .muted { font-weight: 400; font-size: 13px; }
.files { list-style: none; padding: 0; margin: 12px 0 0; display: flex; flex-direction: column; gap: 4px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.chip { font-size: 12px; font-weight: 500; padding: 3px 10px; border-radius: 99px; }
.chip.ok { background: var(--ok-soft); color: var(--ok); }
.chip.missing { background: var(--warn-soft); color: var(--warn); }
.terms { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 12px 0; font-size: 13px; }
.terms dt { color: var(--ink-2); }
.terms dd { margin: 0; text-align: right; }
.amount { display: flex; flex-direction: column; align-items: flex-end; border-top: 1px solid var(--line); padding-top: 10px; }
.amount strong { font-size: 24px; font-variant-numeric: tabular-nums; }
.checks { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; font-size: 13px; }
.checks li { display: grid; grid-template-columns: 20px 1fr; gap: 6px; padding: 6px 8px; border-radius: 7px; }
.checks .ico { font-weight: 700; text-align: center; }
.checks li.ok { color: var(--ink-2); }
.checks li.ok .ico { color: var(--ok); }
.checks li.block { background: var(--bad-soft); color: var(--bad); }
.checks li.warn { background: var(--warn-soft); color: var(--warn); }
.passed { margin-top: 10px; font-size: 13px; }
.passed summary { cursor: pointer; color: var(--ok); font-weight: 500; padding: 4px 0; }
.passed .checks { margin-top: 6px; }
.all-ok { color: var(--ok); font-weight: 600; margin: 0; }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 20px; width: 100%; }
.login-card { width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: 14px; }

/* Toasts */
#toasts { position: fixed; bottom: 20px; right: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 50; max-width: min(420px, calc(100vw - 40px)); }
.toast { background: var(--ink); color: #fff; padding: 10px 14px; border-radius: 8px; font-size: 13px; opacity: 0; transform: translateY(8px); transition: 0.25s; box-shadow: 0 6px 20px rgba(0,0,0,0.15); }
.toast.show { opacity: 1; transform: none; }
.toast-error { background: var(--bad); }

/* Invoice paper */
.paper { position: relative; background: #fff; border: 1px solid var(--line); border-radius: 4px; padding: 36px 40px; font-size: 12.5px; color: #111; overflow: hidden; }
.watermark { position: absolute; top: 40%; left: 50%; transform: translate(-50%, -50%) rotate(-24deg); font-size: 96px; font-weight: 600; color: rgba(179, 38, 30, 0.08); pointer-events: none; letter-spacing: 0.08em; }
.paper-head { display: flex; justify-content: space-between; gap: 20px; border-bottom: 2px solid #111; padding-bottom: 16px; margin-bottom: 16px; }
.paper-company { font-size: 15px; font-weight: 600; }
.paper-trade { font-weight: 600; color: var(--accent); margin-bottom: 4px; }
.paper-title h2 { font-size: 20px; text-transform: uppercase; letter-spacing: 0.06em; margin: 0 0 8px; text-align: right; }
.kv { margin-left: auto; border-collapse: collapse; }
.kv th { text-align: left; font-weight: 500; color: #555; padding: 1px 12px 1px 0; }
.kv td { text-align: right; font-weight: 600; }
.paper-parties { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; margin-bottom: 16px; }
.paper-ship { display: grid; grid-template-columns: repeat(3, 1fr); border: 1px solid #cfd6dc; border-radius: 4px; margin-bottom: 16px; }
.paper-ship > div { padding: 6px 10px; border-bottom: 1px solid #e3e8ec; border-right: 1px solid #e3e8ec; }
.paper-ship > div:nth-child(3n) { border-right: 0; }
.paper-ship > div:nth-last-child(-n+3) { border-bottom: 0; }
.paper-ship > div > span:first-child { display: block; font-size: 10px; text-transform: uppercase; letter-spacing: 0.05em; color: #666; }
.paper-goods { width: 100%; border-collapse: collapse; margin-bottom: 18px; }
.paper-goods th { font-size: 10px; text-transform: uppercase; letter-spacing: 0.05em; color: #555; text-align: left; border-bottom: 1px solid #111; padding: 6px; }
.paper-goods td { padding: 8px 6px; border-bottom: 1px solid #e3e8ec; vertical-align: top; }
.paper-goods tfoot td { border-top: 1px solid #111; border-bottom: 0; font-size: 14px; }
.paper-goods .num { text-align: right; white-space: nowrap; }
.paper-foot { display: grid; grid-template-columns: 1fr 1fr; gap: 24px; }
.paper-foot p { margin: 0 0 10px; }
.sign { margin-top: 18px; border-top: 1px solid #111; padding-top: 6px; width: 70%; margin-left: auto; text-align: center; font-size: 11px; }

@media (max-width: 1000px) {
  .invoice-layout, .detail-layout, .split { grid-template-columns: 1fr; }
  .sticky, .side-form { position: static; }
}
@media (max-width: 720px) {
  #app { flex-direction: column; }
  .side { width: 100%; height: auto; position: static; flex-direction: row; flex-wrap: wrap; align-items: center; padding: 10px 14px; }
  .brand { padding: 0 8px 0 0; }
  .side nav { flex-direction: row; flex-wrap: wrap; }
  .side-foot { margin: 0 0 0 auto; flex-direction: row; align-items: center; gap: 10px; }
  .page { padding: 18px 16px 48px; }
  .grid3, .grid2, .stats, .paper-parties, .paper-ship, .paper-foot { grid-template-columns: 1fr; }
  .span2, .span3 { grid-column: auto; }
  .paper { padding: 20px; }
}

@media print {
  @page { size: A4; margin: 12mm; }
  body { background: #fff; }
  .no-print, .side, #toasts { display: none !important; }
  .page { padding: 0; max-width: none; }
  .detail-layout { display: block; }
  .paper { border: 0; padding: 0; font-size: 11.5px; }
}
