:root {
  --bg: #14181C;
  --surface: #1B2126;
  --surface-alt: #232B31;
  --border: #2E383F;
  --ink: #E7ECEF;
  --ink-muted: #93A1AB;
  --ink-faint: #62707A;

  --accent: #1F5F8E;
  --accent-strong: #3B84BA;
  --accent-ink: #FFFFFF;
  --accent-soft: rgba(59, 132, 186, 0.16);
  --accent-border: rgba(59, 132, 186, 0.45);

  --good: #3FAE79;
  --good-soft: rgba(63, 174, 121, 0.14);
  --good-border: rgba(63, 174, 121, 0.4);
  --warn: #D9A441;
  --warn-soft: rgba(217, 164, 65, 0.14);
  --warn-border: rgba(217, 164, 65, 0.4);
  --critical: #E2726C;
  --critical-soft: rgba(226, 114, 108, 0.14);
  --critical-border: rgba(226, 114, 108, 0.4);

  --shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
  --font-display: "IBM Plex Sans", system-ui, sans-serif;
  --font-mono: "IBM Plex Mono", ui-monospace, monospace;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-display);
  font-size: 13.5px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3 { font-family: var(--font-display); margin: 0; }
input, select, textarea, button { font-family: inherit; font-size: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent-strong); }
.mono { font-family: var(--font-mono); font-variant-numeric: tabular-nums; }

/* ---------- Login ---------- */
.login-page {
  display: flex; align-items: center; justify-content: center; min-height: 100%;
  padding: 24px; padding-top: calc(24px + env(safe-area-inset-top, 0px));
}
.login-card {
  width: 100%; max-width: 340px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; padding: 30px 28px; box-shadow: var(--shadow); text-align: center;
}
.login-logo { font-size: 30px; margin-bottom: 6px; }
.login-card h1 { font-size: 18px; margin-bottom: 4px; }
.login-sub { color: var(--ink-muted); font-size: 12.5px; margin: 0 0 18px; }
.login-card label { display: block; text-align: left; font-size: 12px; color: var(--ink-muted); margin: 12px 0 5px; font-weight: 500; }
.login-card input { width: 100%; padding: 9px 11px; border-radius: 8px; border: 1px solid var(--border); background: var(--bg); color: var(--ink); }
.login-error { background: var(--critical-soft); color: var(--critical); border: 1px solid var(--critical-border); border-radius: 8px; padding: 9px 11px; font-size: 12.5px; margin-bottom: 4px; text-align: left; }
.login-error.login-ok { background: var(--good-soft); color: var(--good); border-color: var(--good-border); }
.btn-block { width: 100%; margin-top: 18px; }
.back-link { display: inline-block; margin-top: 14px; font-size: 12.5px; color: var(--ink-muted); text-decoration: none; }
.back-link:hover { color: var(--ink); }

/* ---------- App shell ---------- */
.app { height: 100%; display: flex; flex-direction: column; }

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  padding: 11px 18px; padding-top: calc(11px + env(safe-area-inset-top, 0px));
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.topbar-title { display: flex; align-items: baseline; gap: 9px; }
.topbar-title h1 { font-size: 16.5px; font-weight: 700; letter-spacing: -0.01em; }
.topbar-title .subtitle { color: var(--ink-muted); font-size: 12px; }
.topbar-right { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.topbar-stats { display: flex; gap: 8px; flex-wrap: wrap; }
.stat-pill {
  display: inline-flex; align-items: center; gap: 6px; padding: 4px 9px; border-radius: 100px;
  font-size: 11.5px; font-weight: 500; background: var(--surface-alt); border: 1px solid var(--border);
  color: var(--ink-muted); white-space: nowrap;
}
.stat-pill.due { background: var(--warn-soft); border-color: var(--warn-border); color: var(--warn); }
.stat-pill strong { color: var(--ink); font-weight: 700; }
.stat-pill.due strong { color: var(--warn); }
.icon-link { color: var(--ink-muted); text-decoration: none; font-size: 12px; padding: 5px 9px; border-radius: 7px; }
.icon-link:hover { background: var(--surface-alt); color: var(--ink); }

.workspace { flex: 1; min-height: 0; display: flex; }

.sidebar {
  width: 350px; flex-shrink: 0; display: flex; flex-direction: column;
  background: var(--surface); border-right: 1px solid var(--border); min-height: 0;
}
.sidebar-controls { padding: 12px 14px 9px; display: flex; flex-direction: column; gap: 9px; border-bottom: 1px solid var(--border); }
.search-row { display: flex; gap: 7px; }
.search-row input[type="search"] { flex: 1; min-width: 0; padding: 8px 11px; border-radius: 8px; border: 1px solid var(--border); background: var(--bg); color: var(--ink); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 13px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface-alt); color: var(--ink); font-weight: 600; font-size: 12.5px; white-space: nowrap;
}
.btn:hover { filter: brightness(1.15); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { background: var(--accent-strong); border-color: var(--accent-strong); filter: none; }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-ghost:hover { background: var(--surface-alt); }
.btn-danger { color: var(--critical); border-color: var(--critical-border); background: var(--critical-soft); }
.btn-sm { padding: 5px 9px; font-size: 11.5px; border-radius: 7px; }
.btn:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, .row:focus-visible, .chip:focus-visible { outline: 2px solid var(--accent-strong); outline-offset: 1px; }

.chip-row { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 2px; }
.chip { flex-shrink: 0; padding: 5px 10px; border-radius: 100px; border: 1px solid var(--border); background: var(--surface-alt); color: var(--ink-muted); font-size: 11.5px; font-weight: 500; }
.chip.active { background: var(--accent-soft); border-color: var(--accent-border); color: var(--accent-strong); }

.supplier-list { flex: 1; overflow-y: auto; padding: 4px 0; }
.row { display: flex; flex-direction: column; gap: 4px; padding: 10px 14px; border-bottom: 1px solid var(--border); }
.row:hover { background: var(--surface-alt); }
.row.selected { background: var(--accent-soft); }
.row-top { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.row-name { font-weight: 600; font-size: 13.5px; }
.dot { width: 7px; height: 7px; border-radius: 50%; flex-shrink: 0; }
.dot.good { background: var(--good); }
.dot.warn { background: var(--warn); }
.dot.critical { background: var(--ink-faint); }
.row-meta { display: flex; align-items: center; gap: 7px; flex-wrap: wrap; }
.row-cat { font-size: 11px; color: var(--ink-muted); background: var(--surface-alt); padding: 2px 7px; border-radius: 6px; }
.row-next { font-size: 11px; padding: 2px 7px; border-radius: 6px; font-weight: 500; }
.row-next.overdue { background: var(--critical-soft); color: var(--critical); }
.row-next.soon { background: var(--warn-soft); color: var(--warn); }
.row-next.later { background: var(--surface-alt); color: var(--ink-muted); }
.row-contact { font-size: 11.5px; color: var(--ink-muted); }
.empty-list { padding: 28px 18px; color: var(--ink-muted); font-size: 12.5px; text-align: center; }

.detail { flex: 1; min-width: 0; overflow-y: auto; }
.detail-inner { max-width: 740px; margin: 0 auto; padding: 22px 26px 60px; }
.back-btn { display: none; margin-bottom: 12px; }

.empty-state { height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 9px; color: var(--ink-muted); text-align: center; padding: 40px; }
.empty-state .glyph { font-size: 32px; opacity: 0.5; }
.empty-state h2 { font-size: 15px; color: var(--ink); }
.empty-state p { max-width: 320px; font-size: 12.5px; margin: 0; }

.detail-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 14px; margin-bottom: 16px; }
.detail-header h2 { font-size: 20px; font-weight: 700; letter-spacing: -0.01em; }
.detail-header .cat { color: var(--ink-muted); font-size: 12.5px; margin-top: 3px; }
.detail-actions { display: flex; gap: 7px; flex-shrink: 0; }

.status-line { display: flex; align-items: center; gap: 7px; margin-bottom: 18px; flex-wrap: wrap; }
.status-badge { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 100px; font-size: 11.5px; font-weight: 600; border: 1px solid transparent; }
.status-badge.good { background: var(--good-soft); color: var(--good); border-color: var(--good-border); }
.status-badge.critical { background: var(--critical-soft); color: var(--critical); border-color: var(--critical-border); }
.status-badge.neutral { background: var(--surface-alt); color: var(--ink-muted); border-color: var(--border); }
.status-badge.warn { background: var(--warn-soft); color: var(--warn); border-color: var(--warn-border); }

.next-step-card { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 11px 15px; border-radius: 10px; margin-bottom: 20px; border: 1px solid; }
.next-step-card.overdue { background: var(--critical-soft); border-color: var(--critical-border); }
.next-step-card.soon { background: var(--warn-soft); border-color: var(--warn-border); }
.next-step-card.later { background: var(--surface-alt); border-color: var(--border); }
.next-step-card .label { font-size: 10.5px; text-transform: uppercase; letter-spacing: 0.06em; font-weight: 600; opacity: 0.8; }
.next-step-card .text { font-size: 13px; font-weight: 500; margin-top: 2px; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 16px 18px; margin-bottom: 16px; }
.card h3 { font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--ink-muted); font-weight: 600; margin-bottom: 12px; }
.card-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.card-head h3 { margin-bottom: 0; }
.info-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 13px 18px; }
.info-field .k { font-size: 11px; color: var(--ink-faint); margin-bottom: 3px; }
.info-field .v { font-size: 13.5px; }
.info-field .v.empty { color: var(--ink-faint); font-style: italic; }
.info-field .v a { text-decoration: none; }
.info-field .v a:hover { text-decoration: underline; }
.account-note { margin-top: 11px; font-size: 11.5px; color: var(--ink-faint); }
.notes-text { font-size: 13.5px; white-space: pre-wrap; }

form.entry-form { display: flex; flex-direction: column; gap: 9px; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; }
.field-row-3 { display: grid; grid-template-columns: 1fr 1fr auto; gap: 9px; align-items: end; }
.field label { display: block; font-size: 11.5px; color: var(--ink-muted); margin-bottom: 4px; font-weight: 500; }
.field input, .field select, .field textarea { width: 100%; padding: 7px 10px; border-radius: 7px; border: 1px solid var(--border); background: var(--bg); color: var(--ink); }
.field textarea { resize: vertical; min-height: 56px; }
.form-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 3px; }

.timeline { display: flex; flex-direction: column; margin-top: 14px; }
.tl-entry { display: flex; gap: 13px; padding: 13px 0; border-top: 1px solid var(--border); }
.tl-entry:first-child { border-top: none; }
.tl-date { flex-shrink: 0; width: 74px; font-size: 12px; color: var(--ink-muted); padding-top: 1px; }
.tl-body { flex: 1; min-width: 0; }
.tl-top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.tl-author { font-size: 12px; font-weight: 600; color: var(--ink-muted); }
.tl-note { font-size: 13.5px; margin-top: 3px; white-space: pre-wrap; }
.tl-tags { display: flex; gap: 6px; margin-top: 6px; flex-wrap: wrap; }
.tl-tag { font-size: 11px; padding: 2px 7px; border-radius: 6px; background: var(--surface-alt); color: var(--ink-muted); }
.tl-tag.discount { background: var(--good-soft); color: var(--good); }
.tl-tag.next { background: var(--accent-soft); color: var(--accent-strong); }
.tl-del { opacity: 0.4; }
.tl-del:hover { opacity: 1; }
.no-history { color: var(--ink-faint); font-size: 12.5px; padding: 4px 0 2px; }

.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.55); display: flex; align-items: flex-start; justify-content: center; padding: 36px 16px; padding-top: calc(36px + env(safe-area-inset-top, 0px)); overflow-y: auto; z-index: 50; }
.modal { background: var(--surface); border-radius: 14px; box-shadow: var(--shadow); width: 100%; max-width: 560px; padding: 22px 24px; border: 1px solid var(--border); }
.modal.modal-wide { max-width: 660px; }
.modal h2 { font-size: 16px; margin-bottom: 4px; }
.modal .modal-sub { color: var(--ink-muted); font-size: 12.5px; margin-bottom: 16px; }
.modal-grid { display: flex; flex-direction: column; gap: 13px; }
.required-mark { color: var(--accent-strong); }
.helper-note { font-size: 11.5px; color: var(--ink-faint); background: var(--surface-alt); border-radius: 7px; padding: 8px 10px; margin-top: 2px; }
.modal-actions { display: flex; justify-content: space-between; gap: 8px; margin-top: 18px; }
.modal-actions .right { display: flex; gap: 8px; }
.ares-row { display: flex; gap: 8px; align-items: end; }
.ares-row .field { flex: 1; }
.ares-status { font-size: 11.5px; margin-top: 6px; }
.ares-status.error { color: var(--critical); }
.ares-status.ok { color: var(--good); }

.toast { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); background: var(--surface-alt); border: 1px solid var(--border); color: var(--ink); padding: 9px 16px; border-radius: 8px; font-size: 12.5px; box-shadow: var(--shadow); z-index: 100; opacity: 0; pointer-events: none; transition: opacity 0.2s ease, transform 0.2s ease; }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(-4px); }

.db-banner { background: var(--warn-soft); color: var(--warn); border-bottom: 1px solid var(--warn-border); padding: 7px 18px; font-size: 12px; text-align: center; }

.import-textarea { width: 100%; min-height: 220px; font-family: var(--font-mono); font-size: 12px; padding: 10px; border-radius: 8px; border: 1px solid var(--border); background: var(--bg); color: var(--ink); resize: vertical; }
.import-hint { font-size: 11.5px; color: var(--ink-faint); line-height: 1.6; margin: 0 0 10px; }
.import-hint code { background: var(--surface-alt); padding: 1px 5px; border-radius: 4px; }

@media (max-width: 720px) {
  .sidebar { width: 100%; }
  .info-grid, .field-row { grid-template-columns: 1fr; }
  .detail-inner { padding: 16px 14px 60px; }
  .workspace.has-selection .sidebar { display: none; }
  .workspace:not(.has-selection) .detail { display: none; }
  .back-btn { display: inline-flex; }
  .topbar-stats { width: 100%; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
