/* ==========================================================================
   LedgerApp — two themes (dark / light green) driven by CSS variables.
   The theme is set on <html data-theme="dark|light"> by site.js.
   Dr (owes the business) is always orange, Cr (business owes) always blue.
   ========================================================================== */

:root,
:root[data-theme="dark"] {
  --bg: #0B0F14;
  --sidebar: #0F151C;
  --surface: #131A22;
  --surface-2: #0F151C;
  --input-bg: #0B0F14;
  --border: #222C38;
  --border-strong: #3A4757;
  --row-line: #1C2530;
  --text: #E6EDF3;
  --text-strong: #FFFFFF;
  --text-2: #C9D3DD;
  --muted: #9AA7B4;
  --accent: #F5B83D;
  --accent-hover: #FCD34D;
  --on-accent: #0B0F14;
  --nav-active-bg: #1F2A36;
  --link: #F5B83D;
  --dr: #FB923C;
  --cr: #60A5FA;
  --dr-soft: rgba(251, 146, 60, 0.12);
  --cr-soft: rgba(96, 165, 250, 0.12);
  --ok: #4ADE80;
  --ok-soft: rgba(74, 222, 128, 0.12);
  --warn: #FBBF24;
  --warn-soft: rgba(251, 191, 36, 0.12);
  --danger: #F87171;
  --danger-soft: rgba(248, 113, 113, 0.12);
  --shadow: 0 20px 40px rgba(0, 0, 0, 0.35);
  --card-shadow: none;
  color-scheme: dark;
}

:root[data-theme="light"] {
  --bg: #F3F9F5;
  --sidebar: #DCEFE3;
  --surface: #FFFFFF;
  --surface-2: #EEF6F1;
  --input-bg: #F6FBF8;
  --border: #D6E8DC;
  --border-strong: #B9D5C3;
  --row-line: #E4F0E8;
  --text: #0F2A1D;
  --text-strong: #0F2A1D;
  --text-2: #2F4A3B;
  --muted: #4D6B5A;
  --accent: #15803D;
  --accent-hover: #166534;
  --on-accent: #FFFFFF;
  --nav-active-bg: #FFFFFF;
  --link: #166534;
  --dr: #C2410C;
  --cr: #1D4ED8;
  --dr-soft: #FFF7ED;
  --cr-soft: #EFF6FF;
  --ok: #15803D;
  --ok-soft: #DCFCE7;
  --warn: #92400E;
  --warn-soft: #FEF3C7;
  --danger: #B91C1C;
  --danger-soft: #FEE2E2;
  --shadow: 0 20px 40px rgba(21, 128, 61, 0.10);
  --card-shadow: 0 1px 3px rgba(15, 42, 29, 0.05);
  color-scheme: light;
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'IBM Plex Sans', 'Segoe UI', system-ui, sans-serif;
  font-size: 15px;
  line-height: 1.5;
}

a { color: var(--link); text-decoration: none; }
a:hover { text-decoration: underline; }

h1, h2, h3 { color: var(--text-strong); margin: 0; line-height: 1.25; }
h1 { font-size: 30px; font-weight: 700; }
h2 { font-size: 19px; font-weight: 700; }
h3 { font-size: 16px; font-weight: 700; }

.mono, .num { font-family: 'IBM Plex Mono', Consolas, monospace; }
.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.nowrap { white-space: nowrap; }
.right { text-align: right; }
.hidden { display: none !important; }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

.dr { color: var(--dr); }
.cr { color: var(--cr); }
.zero { color: var(--muted); }

/* ---------------------------------------------------------------- shell */

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

.sidebar {
  flex: 1 1 236px;
  max-width: 100%;
  background: var(--sidebar);
  border-right: 1px solid var(--border);
  padding: 24px 14px;
  display: flex;
  flex-direction: column;
  gap: 28px;
}

.brand { display: flex; align-items: center; gap: 12px; padding: 0 8px; color: var(--text-strong); }
.brand:hover { text-decoration: none; }
.brand-mark {
  width: 44px; height: 44px; border-radius: 8px; flex: none;
  background: var(--accent); color: var(--on-accent);
  font-family: 'IBM Plex Mono', monospace; font-weight: 700; font-size: 17px;
  display: flex; align-items: center; justify-content: center;
}
.brand-mark.lg { width: 56px; height: 56px; border-radius: 10px; font-size: 22px; }
.brand-name { font-size: 17px; font-weight: 700; line-height: 1.2; }
.brand-sub { font-size: 13px; color: var(--muted); }

.nav { display: flex; flex-direction: column; gap: 4px; }
.nav-section { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); padding: 14px 14px 4px; font-weight: 600; }
.nav a {
  display: flex; align-items: center; gap: 12px;
  padding: 11px 14px; border-radius: 8px;
  color: var(--text-2); font-size: 16px; font-weight: 500;
}
.nav a:hover { background: var(--nav-active-bg); text-decoration: none; color: var(--text-strong); }
.nav a.active {
  background: var(--nav-active-bg); color: var(--text-strong); font-weight: 700;
  box-shadow: inset 4px 0 0 var(--accent);
}
.nav svg { width: 20px; height: 20px; flex: none; }

.sidebar-foot { margin-top: auto; border-top: 1px solid var(--border); padding: 14px 10px 0; display: flex; flex-direction: column; gap: 6px; }
.sidebar-foot .user { font-size: 15px; font-weight: 600; color: var(--text-strong); }
.sidebar-foot a, .sidebar-foot button.linklike { font-size: 14px; color: var(--text-2); }

.main { flex: 999 1 560px; min-width: 0; padding: 28px 32px; display: flex; flex-direction: column; gap: 22px; }

.page-head { display: flex; flex-wrap: wrap; gap: 14px; justify-content: space-between; align-items: center; }
.page-head .sub { font-size: 15px; color: var(--muted); margin-top: 4px; }
.actions { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }

/* ---------------------------------------------------------------- buttons */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  height: 46px; padding: 0 18px; border-radius: 8px;
  border: 1px solid var(--border-strong); background: var(--surface); color: var(--text);
  font: inherit; font-size: 15px; font-weight: 600; cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: var(--accent); text-decoration: none; color: var(--text-strong); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); font-weight: 700; }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); color: var(--on-accent); }
.btn-danger { color: var(--danger); }
.btn-danger:hover { border-color: var(--danger); color: var(--danger); }
.btn-sm { height: 36px; padding: 0 12px; font-size: 14px; }
.btn-icon { width: 46px; padding: 0; color: var(--accent); }
.btn-icon svg { width: 22px; height: 22px; }
.btn-block { width: 100%; height: 54px; font-size: 17px; }
.linklike { background: none; border: 0; padding: 0; font: inherit; color: var(--link); cursor: pointer; text-align: left; }
.linklike:hover { text-decoration: underline; }

.theme-toggle .icon-sun { display: none; }
.theme-toggle .icon-moon { display: block; }
:root[data-theme="dark"] .theme-toggle .icon-sun { display: block; }
:root[data-theme="dark"] .theme-toggle .icon-moon { display: none; }

/* ---------------------------------------------------------------- cards / stats */

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  box-shadow: var(--card-shadow);
}
.card-head {
  display: flex; flex-wrap: wrap; gap: 8px 16px; justify-content: space-between; align-items: center;
  padding: 16px 20px; border-bottom: 1px solid var(--border);
}
.card-body { padding: 20px; }
.card-foot { padding: 14px 20px; border-top: 1px solid var(--border); display: flex; flex-wrap: wrap; gap: 12px; justify-content: space-between; align-items: center; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 14px; }
.stat {
  background: var(--surface); border: 1px solid var(--border); border-top: 4px solid var(--border-strong);
  border-radius: 12px; padding: 18px 20px; display: flex; flex-direction: column; gap: 6px;
  box-shadow: var(--card-shadow);
}
.stat-label { font-size: 13px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; }
.stat-value { font-family: 'IBM Plex Mono', monospace; font-size: 28px; font-weight: 700; line-height: 1.2; color: var(--text-strong); word-break: break-word; }
.stat-sub { font-size: 14px; color: var(--muted); }
.stat.is-dr { border-top-color: var(--dr); } .stat.is-dr .stat-value { color: var(--dr); }
.stat.is-cr { border-top-color: var(--cr); } .stat.is-cr .stat-value { color: var(--cr); }
.stat.is-accent { border-top-color: var(--accent); }

.grid-2 { display: flex; flex-wrap: wrap; gap: 16px; align-items: flex-start; }
.grid-2 > .wide { flex: 2 1 520px; min-width: 0; }
.grid-2 > .narrow { flex: 1 1 300px; min-width: 0; }

/* ---------------------------------------------------------------- tables */

.table-wrap { overflow-x: auto; }
table.data { width: 100%; border-collapse: collapse; font-size: 15px; }
table.data th {
  text-align: left; color: var(--muted); font-size: 13px; font-weight: 600;
  text-transform: uppercase; letter-spacing: .05em; padding: 12px 10px; background: var(--surface-2); white-space: nowrap;
}
table.data td { padding: 13px 10px; border-top: 1px solid var(--row-line); vertical-align: middle; }
table.data th:first-child, table.data td:first-child { padding-left: 20px; }
table.data th:last-child, table.data td:last-child { padding-right: 20px; }
table.data th.num, table.data td.num { text-align: right; }
table.data td.num { font-family: 'IBM Plex Mono', monospace; font-weight: 600; }
table.data tbody tr:hover { background: var(--surface-2); }
table.data tfoot td { border-top: 2px solid var(--border-strong); font-weight: 700; font-size: 16px; padding-top: 14px; padding-bottom: 14px; background: var(--surface-2); }
table.data .name { color: var(--text-strong); font-weight: 600; }
table.data .code { font-size: 12px; color: var(--muted); font-family: 'IBM Plex Mono', monospace; }
table.data tr.voided td { color: var(--muted); text-decoration: line-through; }
table.data tr.voided td.keep { text-decoration: none; }
table.data tr.opening td { background: var(--surface-2); font-style: italic; }
table.data td.row-actions { white-space: nowrap; text-align: right; }
.empty { padding: 40px 20px; text-align: center; color: var(--muted); }

.pill {
  display: inline-block; padding: 4px 10px; border-radius: 999px; font-size: 13px; font-weight: 700; white-space: nowrap;
  background: var(--surface-2); color: var(--muted);
}
.pill.dr { background: var(--dr-soft); color: var(--dr); }
.pill.cr { background: var(--cr-soft); color: var(--cr); }
.pill.ok { background: var(--ok-soft); color: var(--ok); }
.pill.off { background: var(--danger-soft); color: var(--danger); }

/* ---------------------------------------------------------------- forms */

.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 18px; }
.form-grid .span-all { grid-column: 1 / -1; }
.field { display: flex; flex-direction: column; gap: 7px; min-width: 0; }
.field label, .label { font-size: 14px; font-weight: 600; color: var(--text-2); }
.field .hint { font-size: 13px; color: var(--muted); }
.req::after { content: " *"; color: var(--dr); }

.input, select.input, textarea.input {
  height: 48px; width: 100%; padding: 0 14px; border-radius: 8px;
  border: 1px solid var(--border-strong); background: var(--input-bg); color: var(--text);
  font: inherit; font-size: 15px;
}
textarea.input { height: auto; min-height: 90px; padding: 12px 14px; resize: vertical; }
.input:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: var(--accent); }
.input.amount { font-family: 'IBM Plex Mono', monospace; font-size: 18px; font-weight: 600; }
.input-sm { height: 40px; font-size: 14px; }

.fx-pair { display: flex; gap: 8px; }
.fx-pair .fx-code { flex: 0 0 96px; padding: 0 10px; font-family: 'IBM Plex Mono', monospace; font-weight: 600; }
.fx-pair .input.amount { flex: 1; min-width: 0; }
.fx-line { color: var(--accent); font-weight: 600; }
:root[data-theme="light"] .fx-line { color: var(--accent-hover); }

.check { display: inline-flex; align-items: center; gap: 10px; font-size: 15px; color: var(--text-2); cursor: pointer; }
.check input { width: 18px; height: 18px; accent-color: var(--accent); }

.type-toggle { display: flex; border: 1px solid var(--border-strong); border-radius: 8px; overflow: hidden; height: 48px; }
.type-toggle label { flex: 1; display: flex; align-items: center; justify-content: center; gap: 8px; cursor: pointer; font-weight: 700; font-size: 15px; color: var(--text-2); background: var(--input-bg); }
.type-toggle label + label { border-left: 1px solid var(--border-strong); }
.type-toggle input { position: absolute; opacity: 0; pointer-events: none; }
.type-toggle input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.type-toggle label.is-dr:has(input:checked) { background: var(--dr-soft); color: var(--dr); box-shadow: inset 0 -3px 0 var(--dr); }
.type-toggle label.is-cr:has(input:checked) { background: var(--cr-soft); color: var(--cr); box-shadow: inset 0 -3px 0 var(--cr); }

.filters { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.filters .field { flex: 1 1 160px; }
.filters .field.grow { flex: 3 1 240px; }

.seg { display: inline-flex; border: 1px solid var(--border-strong); border-radius: 8px; overflow: hidden; }
.seg a { padding: 9px 14px; font-size: 14px; font-weight: 600; color: var(--text-2); }
.seg a + a { border-left: 1px solid var(--border-strong); }
.seg a:hover { text-decoration: none; background: var(--surface-2); }
.seg a.on { background: var(--accent); color: var(--on-accent); }

.validation-summary-errors, .alert {
  border-radius: 10px; padding: 14px 18px; font-size: 15px; border: 1px solid transparent;
}
.validation-summary-errors { background: var(--danger-soft); color: var(--danger); border-color: var(--danger); }
.validation-summary-errors ul { margin: 0; padding-left: 18px; }
.validation-summary-valid { display: none; }
.field-validation-error { color: var(--danger); font-size: 13px; }
.input-validation-error { border-color: var(--danger) !important; }
.alert-success { background: var(--ok-soft); color: var(--ok); border-color: var(--ok); }
.alert-warning { background: var(--warn-soft); color: var(--warn); border-color: var(--warn); }
.alert-danger { background: var(--danger-soft); color: var(--danger); border-color: var(--danger); }

details.inline-form summary { cursor: pointer; list-style: none; display: inline; }
details.inline-form summary::-webkit-details-marker { display: none; }
details.inline-form[open] .popover { display: flex; }
.popover {
  display: none; position: absolute; right: 20px; z-index: 20; margin-top: 8px;
  background: var(--surface); border: 1px solid var(--border-strong); border-radius: 10px; box-shadow: var(--shadow);
  padding: 16px; flex-direction: column; gap: 10px; width: 300px; text-align: left; white-space: normal;
}
td.row-actions { position: relative; }

/* ---------------------------------------------------------------- customer header */

.customer-head { display: flex; flex-wrap: wrap; gap: 18px; justify-content: space-between; align-items: flex-start; }
.customer-meta { display: flex; flex-wrap: wrap; gap: 6px 20px; color: var(--muted); font-size: 15px; margin-top: 8px; }
.customer-meta strong { color: var(--text-2); font-weight: 600; }

.dl { display: grid; grid-template-columns: 160px 1fr; gap: 10px 16px; font-size: 15px; }
.dl dt { color: var(--muted); }
.dl dd { margin: 0; color: var(--text); }

/* ---------------------------------------------------------------- pager */

.pager { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.pager a, .pager span.cur {
  min-width: 38px; height: 38px; padding: 0 10px; border-radius: 8px; display: inline-flex; align-items: center; justify-content: center;
  border: 1px solid var(--border-strong); font-size: 14px; font-weight: 600; color: var(--text-2);
}
.pager a:hover { text-decoration: none; border-color: var(--accent); }
.pager span.cur { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }

/* ---------------------------------------------------------------- login */

.auth {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  padding: 48px 16px; position: relative;
}
.auth-box { width: 100%; max-width: 460px; display: flex; flex-direction: column; gap: 32px; }
.auth-brand { display: flex; align-items: center; gap: 16px; }
.auth-brand .name { font-size: 24px; font-weight: 700; color: var(--text-strong); }
.auth-brand .sub { font-size: 15px; color: var(--muted); }
.auth-card {
  background: var(--surface); border: 1px solid var(--border); border-top: 4px solid var(--accent);
  border-radius: 12px; padding: 36px; display: flex; flex-direction: column; gap: 20px; box-shadow: var(--shadow);
}
.auth-card h1 { font-size: 26px; }
.auth-card .input { height: 50px; font-size: 16px; }
.auth-foot { font-size: 13px; color: var(--muted); font-family: 'IBM Plex Mono', monospace; text-align: center; }
.auth .theme-toggle { position: absolute; top: 20px; right: 20px; }
.row-between { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 8px; }

/* ---------------------------------------------------------------- responsive / print */

@media (max-width: 760px) {
  .main { padding: 20px 16px; }
  .sidebar { padding: 16px 12px; gap: 14px; }
  .nav { flex-direction: row; flex-wrap: wrap; }
  .nav a { padding: 8px 12px; font-size: 14px; }
  .nav-section { display: none; }
  .sidebar-foot { flex-direction: row; justify-content: space-between; align-items: center; }
  h1 { font-size: 24px; }
  .stat-value { font-size: 22px; }
  .dl { grid-template-columns: 1fr; }
  .auth-card { padding: 24px; }
}

.print-only { display: none; }

@media print {
  :root, :root[data-theme] {
    --bg: #fff; --surface: #fff; --surface-2: #f3f4f6; --text: #000; --text-strong: #000; --text-2: #111;
    --muted: #444; --border: #ccc; --border-strong: #999; --row-line: #ddd; --dr: #000; --cr: #000; --card-shadow: none;
  }
  .sidebar, .no-print, .actions, .filters, .pager, .row-actions, .theme-toggle { display: none !important; }
  .main { padding: 0; }
  .print-only { display: block; }
  .card { border: 0; }
  table.data { font-size: 12px; }
  table.data td, table.data th { padding: 6px; }
  .stat { border: 1px solid #ccc; }
  .stat-value { font-size: 18px; }
}
