:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #fafbfc;
  --text: #1f2430;
  --text-soft: #374151;
  --muted: #6b7280;
  --border: #d9dde3;
  --accent: #4f46e5;
  --accent-dark: #4338ca;
  --accent-soft: #eef2ff;
  --danger: #dc2626;
  --danger-soft: #fef2f2;
  --success: #15803d;
  --radius: 8px;
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #12141a;
  --surface: #1b1e26;
  --surface-2: #232733;
  --text: #e7e9f0;
  --text-soft: #c3c8d4;
  --muted: #9aa1af;
  --border: #343a48;
  --accent: #7b83f0;
  --accent-dark: #6069ee;
  --accent-soft: rgba(123, 131, 240, 0.16);
  --danger: #f47171;
  --danger-soft: rgba(244, 113, 113, 0.12);
  --success: #58ba7d;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html { font-size: 16px; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
}

/* ---------- layout ---------- */

.container {
  max-width: 64rem;
  margin: 0 auto;
  padding: 1rem;
}

.topnav {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.25rem 1rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0.5rem 1rem;
  position: sticky;
  top: 0;
  z-index: 40;
}

.brand {
  font-weight: 700;
  text-decoration: none;
  color: var(--text);
  white-space: nowrap;
}

.nav-links {
  display: flex;
  gap: 0.25rem;
  flex-wrap: wrap;
}

.nav-links a {
  text-decoration: none;
  color: var(--muted);
  padding: 0.35rem 0.6rem;
  border-radius: var(--radius);
  white-space: nowrap;
}

.nav-links a:hover { color: var(--text); background: var(--bg); }
.nav-links a.active { color: var(--accent); font-weight: 600; }

.logout-form {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.user-email { color: var(--muted); font-size: 0.85rem; }

.page-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}

h1 { font-size: 1.4rem; margin: 0.5rem 0; }
h2 { font-size: 1.1rem; margin: 1.5rem 0 0.5rem; }

/* ---------- flash messages ---------- */

.flash {
  padding: 0.6rem 0.9rem;
  border-radius: var(--radius);
  margin: 0.5rem 0;
  border: 1px solid;
}

.flash ul { margin: 0; padding-left: 1.2rem; }
.flash-success { background: #ecfdf5; border-color: #a7f3d0; color: #065f46; }
.flash-error { background: #fef2f2; border-color: #fecaca; color: #991b1b; }
.flash-info { background: #eff6ff; border-color: #bfdbfe; color: #1e40af; }

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

label { display: block; font-size: 0.9rem; font-weight: 500; margin-bottom: 0.5rem; }

input[type="text"], input[type="email"], input[type="password"], input[type="date"],
input[type="search"], select, textarea {
  width: 100%;
  padding: 0.45rem 0.6rem;
  font: inherit;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  margin-top: 0.15rem;
}

input:focus, select:focus, textarea:focus, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.stacked-form { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; }
.stacked-form.narrow { max-width: 26rem; }

.form-row { display: flex; gap: 0.75rem; flex-wrap: wrap; align-items: flex-end; }
.form-row label { flex: 1 1 10rem; }
.form-row .w-narrow { flex: 0 1 11rem; }
.form-row .grow { flex: 3 1 14rem; }

.form-actions { display: flex; gap: 0.5rem; justify-content: flex-end; margin-top: 0.75rem; }

label.check { display: flex; align-items: center; gap: 0.5rem; font-weight: 400; }
label.check input { width: auto; margin: 0; }

.filters {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  align-items: flex-end;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.6rem 0.75rem;
  margin: 0.75rem 0;
}

.filters label { flex: 0 1 auto; margin: 0; font-weight: 400; font-size: 0.85rem; color: var(--muted); }
.filters select, .filters input { width: auto; min-width: 7.5rem; }
.filters .grow { flex: 1 1 10rem; }
.filters input[type="search"] { min-width: 9rem; width: 100%; }

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

.btn {
  display: inline-block;
  font: inherit;
  padding: 0.45rem 0.85rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}

.btn:hover { background: var(--bg); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn-primary:hover { background: var(--accent-dark); }
.btn-danger { color: var(--danger); border-color: #fca5a5; }
.btn-danger:hover { background: var(--danger-soft); }
.btn-sm { padding: 0.2rem 0.55rem; font-size: 0.85rem; }
.btn-link { border: none; background: none; color: var(--accent); padding: 0.35rem 0.5rem; }
.btn-link:hover { text-decoration: underline; background: none; }
.btn-block { width: 100%; }

form.inline { display: inline; }

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

.table-wrap { overflow-x: auto; }

.table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.95rem;
}

.table th, .table td { padding: 0.5rem 0.65rem; text-align: left; vertical-align: top; }

.table thead th {
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--muted);
  border-bottom: 2px solid var(--border);
}

.table tbody tr { border-top: 1px solid var(--border); }
.table .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.table .nowrap { white-space: nowrap; }
.table .empty { text-align: center; color: var(--muted); padding: 1.5rem; }
.table a { color: var(--text); }
.table a:hover { color: var(--accent); }

.w-date { width: 6.5rem; }
.w-actions { width: 9rem; }
.w-amount { width: 8.5rem; }
.w-remove { width: 2.5rem; }

.actions { text-align: right; }

/* ledger grouping */
.entry-group .entry-head { background: var(--surface-2); }
.entry-head .memo { font-weight: 600; }
.entry-head .entry-date { font-weight: 700; }
.voucher { font-weight: 600; font-variant-numeric: tabular-nums; color: var(--text-soft); }
.w-voucher { width: 5.5rem; }
.w-edit { width: 2.8rem; }
.row-edit { font-size: 0.95rem; text-decoration: none; }
.line-memo-cell { color: var(--text-soft); }

.readonly-field {
  background: var(--surface-2) !important;
  color: var(--muted) !important;
  cursor: default;
}
.entry-line td { border-top: none; padding-top: 0.1rem; padding-bottom: 0.35rem; color: var(--text-soft); }
.entry-line .line-account { padding-left: 1.5rem; }

.totals-row td { font-weight: 700; border-top: 2px solid var(--border); }
.num-label { text-align: right; font-weight: 700; }

.row-archived { color: var(--muted); }

/* trial balance — classic two-column report styling */
.tb .w-tbcol { width: 10.5rem; }
.tb thead th { border-bottom: 2px solid var(--text-soft); }
.tb tbody tr { border-top: none; }

.tb-section td {
  font-weight: 700;
  text-transform: uppercase;
  font-size: 0.82rem;
  letter-spacing: 0.06em;
  color: var(--text);
  background: var(--surface-2);
  border-top: 2px solid var(--border);
  padding-top: 0.6rem;
  padding-bottom: 0.45rem;
}

.tb-account td { border-top: 1px dotted var(--border); padding-top: 0.4rem; padding-bottom: 0.4rem; }
.tb-account-name { padding-left: 2.25rem; }
.tb-account-name a { color: var(--text); text-decoration: none; }
.tb-account-name a:hover { color: var(--accent); text-decoration: underline; }

.tb-subtotal td {
  font-weight: 700;
  border-top: 2px solid var(--text-soft);
  padding-top: 0.45rem;
  padding-bottom: 0.6rem;
}
.tb-subtotal-label { padding-left: 1rem; font-size: 0.88rem; }

.tb-grand td {
  font-weight: 800;
  font-size: 1rem;
  border-top: 3px double var(--text);
  padding-top: 0.55rem;
}

.tb-check { font-weight: 600; }
.tb-check.ok { color: var(--success); }
.tb-check.bad { color: var(--danger); }
.report-subtitle { color: var(--muted); margin: 0.25rem 0 0.75rem; }

/* ---------- badges ---------- */

.badge {
  display: inline-block;
  font-size: 0.72rem;
  font-weight: 600;
  padding: 0.1rem 0.5rem;
  border-radius: 999px;
  vertical-align: middle;
}

.badge-type { background: #dbeafe; color: #1e40af; }
.badge-admin { background: #dcfce7; color: #166534; }
.badge-muted { background: #e5e7eb; color: #4b5563; }

.num.neg { color: var(--danger); }

/* ---------- entry form ---------- */

.lines-table td { vertical-align: middle; }
.lines-table .w-account { width: 16rem; }
.lines-table .combobox { min-width: 13rem; }
.lines-table input.amount { text-align: right; font-variant-numeric: tabular-nums; }
.lines-table input.amount.invalid { border-color: var(--danger); background: var(--danger-soft); }

.btn-icon {
  border: none;
  background: none;
  font-size: 1.1rem;
  color: var(--muted);
  cursor: pointer;
  padding: 0.2rem 0.4rem;
  border-radius: var(--radius);
}

.btn-icon:hover { color: var(--danger); background: var(--danger-soft); }

.entry-toolbar { align-items: center; margin-top: 0.75rem; }
.entry-toolbar .spacer { flex: 1; }

.balance-ok { color: var(--success); font-weight: 600; }
.balance-bad { color: var(--danger); font-weight: 600; }

.danger-zone { margin-top: 1rem; text-align: right; }

kbd {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 0 0.3rem;
  font-size: 0.8em;
}

/* ---------- combobox ---------- */

.combobox { position: relative; min-width: 14rem; }

/* Positioned with fixed coordinates from JS so it escapes the table's
   overflow clipping and can show a full list of options. */
.cb-list {
  position: fixed;
  z-index: 70;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 8px 20px rgba(15, 23, 42, 0.14);
  max-height: 20rem;
  overflow-y: auto;
}

.cb-option {
  padding: 0.4rem 0.6rem;
  cursor: pointer;
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
}

.cb-option .cb-type { margin-left: auto; }
.cb-option.highlight { background: var(--accent-soft); }
.cb-option.cb-create { color: var(--accent); font-weight: 600; border-top: 1px solid var(--border); }
.cb-empty { padding: 0.5rem 0.6rem; color: var(--muted); }

.cb-input.cb-invalid { border-color: var(--danger); }

.cb-create-panel { padding: 0.6rem; }
.cb-create-panel label { font-size: 0.8rem; margin-bottom: 0.4rem; }
.cb-create-panel .cb-create-actions { display: flex; gap: 0.4rem; justify-content: flex-end; margin-top: 0.5rem; }
.cb-create-error { color: var(--danger); font-size: 0.82rem; margin-top: 0.35rem; }

/* ---------- misc ---------- */

.muted { color: var(--muted); }
.small { font-size: 0.85rem; }

.pagination { display: flex; align-items: center; gap: 0.75rem; margin: 0.75rem 0; justify-content: center; }

.login-card {
  max-width: 22rem;
  margin: 10vh auto 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  text-align: center;
}

.login-card h1 { margin-top: 0; }
.login-card form { text-align: left; margin-top: 1rem; }
.login-card .btn { margin-top: 0.5rem; }

/* ---------- dashboard ---------- */

.kpi-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(10rem, 1fr));
  gap: 0.75rem;
  margin: 0.75rem 0;
}

.stat-tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.9rem 1rem;
}

.stat-value { font-size: 2rem; font-weight: 700; line-height: 1.15; }
.stat-value .unit { font-size: 1rem; font-weight: 600; color: var(--muted); }
.stat-label { color: var(--muted); font-size: 0.85rem; margin-top: 0.15rem; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  margin: 0.75rem 0;
}

.card-title { margin: 0 0 0.6rem; font-size: 0.95rem; }

.dash-cols {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(22rem, 1fr));
  gap: 0.75rem;
}

.dash-cols .card { margin: 0; }
.dash-cols { margin: 0.75rem 0; }

.heatmap-scroll { overflow-x: auto; }
.hm-label { font-size: 9px; fill: var(--muted); }

/* Sequential indigo ramp, validated: one hue, light->dark, light end >= 2:1 on white. */
.hm-0 { fill: #eceef1; }
svg .hm-0 { stroke: rgba(11, 11, 11, 0.06); }
.hm-1 { fill: #98a8fb; }
.hm-2 { fill: #6366f1; }
.hm-3 { fill: #4338ca; }
.hm-4 { fill: #312e81; }

.hm-legend {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 4px;
  margin-top: 0.4rem;
  color: var(--muted);
  font-size: 0.8rem;
}

.hm-legend .sw {
  width: 11px;
  height: 11px;
  border-radius: 2px;
  display: inline-block;
  background: currentColor;
}

.hm-legend .sw.hm-0 { background: #eceef1; }
.hm-legend .sw.hm-1 { background: #98a8fb; }
.hm-legend .sw.hm-2 { background: #6366f1; }
.hm-legend .sw.hm-3 { background: #4338ca; }
.hm-legend .sw.hm-4 { background: #312e81; }

/* ---------- type-to-confirm modal ---------- */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 18, 26, 0.55);
  z-index: 80;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

/* display:flex above would defeat the hidden attribute without this */
.modal-overlay[hidden] { display: none; }

.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.35);
  max-width: 26rem;
  width: 100%;
  padding: 1.25rem;
}

.modal-title { margin: 0 0 0.5rem; font-size: 1.05rem; }
.modal-text { margin: 0 0 0.9rem; color: var(--text-soft); font-size: 0.92rem; }
.modal-label { font-size: 0.88rem; }
.modal-phrase { font-variant-numeric: tabular-nums; }
.modal-input { margin-top: 0.3rem; }
.modal-actions { display: flex; gap: 0.5rem; justify-content: flex-end; margin-top: 1rem; }
.modal-actions .btn-danger:disabled { opacity: 0.45; cursor: not-allowed; }

/* ---------- keyboard shortcuts panel ---------- */

.shortcuts-panel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(21rem, 92vw);
  background: var(--surface);
  border-left: 1px solid var(--border);
  box-shadow: -8px 0 24px rgba(15, 23, 42, 0.12);
  z-index: 60;
  padding: 1rem 1.25rem;
  overflow-y: auto;
  transform: translateX(105%);
  transition: transform 0.2s ease;
}

.shortcuts-panel.open { transform: none; }

.shortcuts-head { display: flex; align-items: center; justify-content: space-between; }
.shortcuts-head h2 { margin: 0; font-size: 1.05rem; }
.shortcuts-panel h3 {
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  margin: 1.1rem 0 0.4rem;
}

.shortcut-list { margin: 0; }
.shortcut-list > div { display: flex; align-items: baseline; gap: 0.6rem; padding: 0.22rem 0; }
.shortcut-list dt { min-width: 5.5rem; }
.shortcut-list dd { margin: 0; color: var(--text); font-size: 0.9rem; }

.icon-btn {
  border: none;
  background: none;
  cursor: pointer;
  font-size: 1.05rem;
  line-height: 1;
  padding: 0.4rem 0.5rem;
  border-radius: var(--radius);
  color: var(--muted);
  transition: background 0.12s ease;
}

.icon-btn:hover { background: var(--accent-soft); color: var(--text); }

.kb-row.kb-active {
  background: var(--accent-soft) !important;
  box-shadow: inset 3px 0 0 var(--accent);
}

.inline-form { display: flex; gap: 0.4rem; align-items: center; flex-wrap: wrap; }
.inline-form input { width: 11rem; margin: 0; }
.inline-form select { width: auto; margin: 0; }
.inline-form input.w-sort { width: 4.5rem; }
input[type="number"] {
  padding: 0.45rem 0.6rem;
  font: inherit;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  margin-top: 0.15rem;
  width: 100%;
}

/* ---------- mobile ---------- */

@media (max-width: 640px) {
  html { font-size: 15px; }
  .container { padding: 0.6rem; }
  .topnav { padding: 0.4rem 0.6rem; }
  .logout-form .user-email { display: none; }
  .filters label { flex: 1 1 40%; }
  .filters select, .filters input { width: 100%; }
  .filters select { flex: 1 1 100%; }
  .w-actions { width: auto; }
  .combobox { min-width: 11rem; }
  .page-head .btn { flex: 1 1 auto; text-align: center; }
  .entry-author { display: block; }
}

/* Bigger touch targets on touch devices. */
@media (pointer: coarse) {
  .btn { padding: 0.55rem 1rem; }
  .btn-sm { padding: 0.4rem 0.75rem; }
  .btn-icon { padding: 0.4rem 0.6rem; }
  .cb-option { padding: 0.6rem 0.7rem; }
  input, select, textarea { font-size: 16px; } /* prevents iOS zoom-on-focus */
}

/* ---------- dark theme component overrides ---------- */

:root[data-theme="dark"] .flash-success { background: #12291c; border-color: #1f4a30; color: #86e0a8; }
:root[data-theme="dark"] .flash-error { background: #2e1719; border-color: #57282c; color: #f2a0a0; }
:root[data-theme="dark"] .flash-info { background: #14213a; border-color: #27426b; color: #9cc0f5; }
:root[data-theme="dark"] .badge-type { background: #26355c; color: #a3c0f2; }
:root[data-theme="dark"] .badge-admin { background: #163a24; color: #7fdca4; }
:root[data-theme="dark"] .badge-muted { background: #333845; color: #aab1bd; }
:root[data-theme="dark"] .btn-danger { border-color: #6c3a3a; }
:root[data-theme="dark"] .cb-list,
:root[data-theme="dark"] .shortcuts-panel { box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5); }

/* Heatmap dark ramp — validated against the dark surface (#1b1e26). */
:root[data-theme="dark"] .hm-0 { fill: #262b37; }
:root[data-theme="dark"] svg .hm-0 { stroke: rgba(255, 255, 255, 0.05); }
:root[data-theme="dark"] .hm-1 { fill: #4844c7; }
:root[data-theme="dark"] .hm-2 { fill: #6366f1; }
:root[data-theme="dark"] .hm-3 { fill: #98a8fb; }
:root[data-theme="dark"] .hm-4 { fill: #c7d2fe; }
:root[data-theme="dark"] .hm-legend .sw.hm-0 { background: #262b37; }
:root[data-theme="dark"] .hm-legend .sw.hm-1 { background: #4844c7; }
:root[data-theme="dark"] .hm-legend .sw.hm-2 { background: #6366f1; }
:root[data-theme="dark"] .hm-legend .sw.hm-3 { background: #98a8fb; }
:root[data-theme="dark"] .hm-legend .sw.hm-4 { background: #c7d2fe; }

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

.print-header { display: none; }

@media print {
  .no-print { display: none !important; }
  body { background: #fff; }
  .container { max-width: 100%; padding: 0; }
  .table { border: none; font-size: 0.85rem; }
  .table th, .table td { padding: 0.25rem 0.4rem; }
  a { color: inherit; text-decoration: none; }

  /* Print always renders in light ink, even if the dark theme is active. */
  :root[data-theme="dark"] {
    --bg: #ffffff;
    --surface: #ffffff;
    --surface-2: #fafbfc;
    --text: #1f2430;
    --text-soft: #374151;
    --muted: #6b7280;
    --border: #d9dde3;
  }

  .print-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 2rem;
    border-bottom: 2px solid #1f2430;
    padding-bottom: 0.5rem;
    margin-bottom: 0.75rem;
  }

  .ph-name { font-size: 1.3rem; font-weight: 700; }
  .ph-title { font-size: 1.05rem; font-weight: 700; }
  .ph-report { text-align: right; }
  .ph-line { font-size: 0.8rem; color: #52514e; }
}
