:root {
  --bg: #f5f6f4;
  --surface: #ffffff;
  --ink: #1e2521;
  --muted: #6b7570;
  --line: #e1e5e1;
  --accent: #1f6f54;        /* diep groen: hosting/infra, geen template-terracotta */
  --accent-ink: #ffffff;
  --sidebar: #16201b;
  --sidebar-ink: #b9c4bd;
  --danger: #a33c2e;
  --hover: #fafbfa;
  --tint-accent: #e8f2ed;      /* betaald/positief */
  --tint-warn: #fdf6e8;        /* attentie */
  --warn-ink: #8a6414;
  --warn-border: #d9a441;
  --tint-info: #e8f0f7;
  --info-ink: #1c5687;
  --tint-current: #f3f8f5;     /* huidig jaar */
  --input-bg: #ffffff;
  color-scheme: light;
}

/* Donker palet: volgt het systeem, tenzij handmatig gekozen */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #141a17; --surface: #1d2621; --ink: #dce4df; --muted: #93a099;
    --line: #2d3a33; --accent: #3fa07a; --accent-ink: #0b120e;
    --sidebar: #101713; --sidebar-ink: #a7b3ac; --danger: #e08a7c;
    --hover: #232e28; --tint-accent: #1c3a2d; --tint-warn: #3a2f14;
    --warn-ink: #e5c06a; --warn-border: #a8842f; --tint-info: #17293b;
    --info-ink: #86b8e2; --tint-current: #1e2f27; --input-bg: #161e1a;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #141a17; --surface: #1d2621; --ink: #dce4df; --muted: #93a099;
  --line: #2d3a33; --accent: #3fa07a; --accent-ink: #0b120e;
  --sidebar: #101713; --sidebar-ink: #a7b3ac; --danger: #e08a7c;
  --hover: #232e28; --tint-accent: #1c3a2d; --tint-warn: #3a2f14;
  --warn-ink: #e5c06a; --warn-border: #a8842f; --tint-info: #17293b;
  --info-ink: #86b8e2; --tint-current: #1e2f27; --input-bg: #161e1a;
  color-scheme: dark;
}

* { box-sizing: border-box; }
input, select, textarea, button {
  color: var(--ink);
  font: inherit;
}
input::placeholder, textarea::placeholder { color: var(--muted); }
html, body { margin: 0; min-height: 100%; }

body {
  font: 15px/1.5 "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--ink);
  display: grid;
  grid-template-columns: 200px 1fr;
}

/* --- Sidebar --- */
.sidebar {
  background: var(--sidebar);
  color: var(--sidebar-ink);
  padding: 24px 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-height: 100vh;
}
.logo {
  font-size: 18px;
  font-weight: 700;
  color: #fff;
  padding: 0 20px 20px;
  letter-spacing: 0.02em;
}
.logo span { color: #6fbf9a; font-weight: 400; }
.sidebar a, .nav-soon {
  padding: 9px 20px;
  color: var(--sidebar-ink);
  text-decoration: none;
  border-left: 3px solid transparent;
}
.sidebar a:hover { color: #fff; background: rgba(255,255,255,0.05); }
.sidebar a.current {
  color: #fff;
  border-left-color: #6fbf9a;
  background: rgba(255,255,255,0.07);
}
.nav-soon { opacity: 0.35; cursor: default; }
.nav-soon::after { content: " ·"; }

/* --- Main --- */
main { padding: 32px 40px; max-width: 1100px; }
h1 { font-size: 24px; margin: 0 0 20px; }
.page-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }
.page-head h1 { margin: 0; }
.hint { color: var(--muted); font-size: 13px; margin-top: 28px; }

/* --- Dashboard --- */
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 14px; }
.stat {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 18px;
  display: flex;
  flex-direction: column;
  text-decoration: none;
  color: inherit;
}
a.stat:hover { border-color: var(--accent); }
.stat-value { font-size: 30px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat-label { color: var(--muted); font-size: 13px; }

/* --- Tabellen --- */
table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
}
th, td { text-align: left; padding: 10px 14px; border-bottom: 1px solid var(--line); }
th { font-size: 12px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--hover); }
tr.inactive td { opacity: 0.45; }
td.num { font-variant-numeric: tabular-nums; }
td .sub { display: block; color: var(--muted); font-size: 12px; }
td.empty { text-align: center; color: var(--muted); padding: 32px; }
.row-actions { text-align: right; white-space: nowrap; }
.row-actions button { font-size: 12px; padding: 4px 10px; }
td a { color: var(--accent); }

/* --- Knoppen & formulieren --- */
button {
  font: inherit;
  background: var(--accent);
  color: var(--accent-ink);
  border: none;
  border-radius: 6px;
  padding: 8px 16px;
  cursor: pointer;
}
button:hover { filter: brightness(1.12); }
button.secondary { background: transparent; color: var(--ink); border: 1px solid var(--line); }
.row-actions button { background: transparent; color: var(--ink); border: 1px solid var(--line); }
.row-actions button:hover { border-color: var(--accent); color: var(--accent); filter: none; }

input, select, textarea {
  font: inherit;
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--input-bg);
  margin-top: 4px;
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: 0;
  border-color: var(--accent);
}
label { display: block; font-size: 13px; color: var(--muted); }

/* --- Modal --- */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(22, 32, 27, 0.5);
  display: grid; place-items: center;
  z-index: 10;
}
.modal-card {
  background: var(--surface);
  border-radius: 10px;
  padding: 24px;
  width: min(560px, 92vw);
  max-height: 90vh;
  overflow-y: auto;
}
.modal-card h2 { margin: 0 0 16px; font-size: 18px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-grid .wide { grid-column: 1 / -1; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }

/* ===================== Mobiel ===================== */
@media (max-width: 760px) {
  /* block i.p.v. grid: position sticky werkt niet binnen een grid-rij,
     waardoor het menu wegscrolde op langere pagina's */
  body { display: block; }

  /* Sidebar -> horizontaal scrollbare balk bovenaan */
  .sidebar {
    flex-direction: row;
    align-items: center;
    min-height: 0;
    padding: 0 8px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    position: sticky;
    top: 0;
    z-index: 5;
    gap: 0;
    scrollbar-width: none;
  }
  .sidebar::-webkit-scrollbar { display: none; }
  .logo { padding: 12px 10px 12px 4px; font-size: 16px; white-space: nowrap; }
  .sidebar a, .nav-soon {
    border-left: none;
    border-bottom: 3px solid transparent;
    padding: 14px 12px;
    white-space: nowrap;
    font-size: 14px;
  }
  .sidebar a.current { border-left-color: transparent; border-bottom-color: #6fbf9a; }
  .nav-bottom { margin-top: 0; }

  main { padding: 16px 14px 40px; }
  h1 { font-size: 20px; }

  /* Kop met knoppen: onder elkaar, knoppen vullen de breedte */
  .page-head { flex-direction: column; align-items: stretch; gap: 10px; }
  .page-head .btn-row, .page-head button { width: 100%; }
  .btn-row { flex-wrap: wrap; }
  .btn-row button, .btn-row .btn-link { flex: 1; text-align: center; }

  /* Tabellen: horizontaal scrollen i.p.v. afbreken */
  table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    white-space: nowrap;
  }
  th, td { padding: 12px 12px; }
  .row-actions button, .row-actions .btn-link { padding: 8px 12px; }

  /* Formulieren en modals */
  .form-grid { grid-template-columns: 1fr; }
  .modal-backdrop { align-items: flex-end; display: flex; }
  .modal-card {
    width: 100vw;
    max-height: 92vh;
    border-radius: 14px 14px 0 0;
    padding: 18px 16px calc(18px + env(safe-area-inset-bottom));
  }
  .modal-actions { position: sticky; bottom: 0; background: var(--surface); padding-top: 10px; }
  .modal-actions button { flex: 1; padding: 12px; }

  /* Invoerkaarten (betalingen) en stats */
  .entry-card { min-width: 100%; }
  .entry-card .btn-row input { min-width: 0; flex: 1; }
  .stat-grid { grid-template-columns: 1fr 1fr; }
  .connector-card { padding: 14px; }
  .link-form { flex-wrap: wrap; }
  .link-form select { min-width: 0; flex: 1; }
  .invoice-meta { flex-direction: column; gap: 10px; }
}

/* Aanraakvriendelijk op alle schermen zonder muis */
@media (pointer: coarse) {
  button, .btn-link { min-height: 44px; }
  input, select, textarea { min-height: 44px; font-size: 16px; }
  textarea { min-height: 88px; }
  .row-actions button { min-height: 38px; }
}

/* --- Abonnementen --- */
.tag {
  display: inline-block;
  font-size: 11px;
  background: var(--tint-accent);
  color: var(--accent);
  border-radius: 4px;
  padding: 1px 6px;
  margin-left: 6px;
  vertical-align: middle;
}
fieldset.pricing {
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 10px 12px;
}
fieldset.pricing legend { font-size: 13px; color: var(--muted); padding: 0 4px; }
label.radio {
  display: flex; align-items: center; gap: 8px;
  color: var(--ink); font-size: 14px; margin: 4px 0;
}
label.radio input[type=radio] { width: auto; margin: 0; }
input.inline-num { width: 110px; margin: 0; }
input:disabled { background: var(--bg); color: var(--muted); }

/* --- Facturen --- */
.status {
  display: inline-block; font-size: 12px; font-weight: 400;
  border-radius: 4px; padding: 2px 8px; margin-left: 10px; vertical-align: middle;
}
.status-draft { background: var(--line); color: var(--muted); }
.status-sent { background: var(--tint-warn); color: var(--warn-ink); }
.status-reminded { background: var(--tint-warn); color: var(--danger); }
.status-paid { background: var(--tint-accent); color: var(--accent); }
.status-cancelled { background: var(--line); color: var(--muted); text-decoration: line-through; }

.btn-row { display: flex; gap: 8px; align-items: center; }
.btn-link {
  color: var(--ink); text-decoration: none; border: 1px solid var(--line);
  border-radius: 6px; padding: 7px 14px; background: var(--surface);
}
.btn-link:hover { border-color: var(--accent); color: var(--accent); }
button.danger { background: transparent; color: var(--danger); border: 1px solid var(--danger); }
button.danger:hover { background: var(--danger); color: #fff; filter: none; }

.invoice-meta {
  display: flex; justify-content: space-between; gap: 24px;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 8px; padding: 16px; margin-bottom: 16px;
}
tfoot td { border-bottom: none; }
tfoot .total-label { color: var(--muted); }
tfoot tr.grand td { font-weight: 700; border-top: 2px solid var(--ink); }

.line-form {
  display: grid; grid-template-columns: 3fr 1fr 1fr 1fr auto;
  gap: 10px; align-items: end;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 8px; padding: 14px; margin-top: 8px;
}
h3 { font-size: 15px; margin: 24px 0 8px; }

/* --- Instellingen --- */
.settings-form { max-width: 640px; }
.flash {
  background: var(--tint-accent); color: var(--accent);
  border-radius: 6px; padding: 10px 14px; font-size: 14px;
}
.field-hint { display: block; font-size: 12px; color: var(--muted); margin-top: 2px; }
.nav-bottom { margin-top: auto; }

/* --- Connectoren --- */
.connector-card {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 8px; padding: 20px; max-width: 760px;
}
.connector-card h3 { margin-top: 0; }
h4 { font-size: 14px; margin: 20px 0 6px; }
.flash-error {
  background: #fbe9e6; color: var(--danger);
  border-radius: 6px; padding: 10px 14px; font-size: 14px;
}
.result-table { font-size: 13px; }
.result-table td:first-child { color: var(--muted); white-space: nowrap; }
.result-table tr.warn td { background: var(--tint-warn); }
button:disabled { opacity: 0.4; cursor: not-allowed; }

.tag-muted { background: #eef0ee; color: var(--muted); }

/* --- Betalingen --- */
tr.review td { background: var(--tint-warn); }
.tag-warn { background: var(--tint-warn); color: var(--warn-ink); }
.link-form { display: flex; gap: 6px; align-items: center; }
.link-form select { margin: 0; min-width: 240px; }
.link-form button { font-size: 12px; padding: 6px 10px; }

/* --- Betalingen invoer --- */
.entry-row { display: flex; gap: 14px; flex-wrap: wrap; margin-bottom: 18px; }
.entry-card {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 8px; padding: 14px; flex: 1; min-width: 320px;
}

/* --- Boekhouding --- */
td.neg, .neg { color: var(--danger); }
tr.current-year td { background: var(--tint-current); font-weight: 600; }

/* --- PDF viewer --- */
.viewer-backdrop {
  position: fixed; inset: 0; background: rgba(22,32,27,0.45);
  display: flex; justify-content: flex-end; z-index: 10;
}
.viewer-panel {
  background: var(--surface); width: min(720px, 100vw); height: 100vh;
  display: flex; flex-direction: column;
}
.viewer-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 10px 14px; border-bottom: 1px solid var(--line); gap: 10px;
}
.viewer-head span { font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.viewer-panel iframe { flex: 1; border: none; width: 100%; }
@media (max-width: 760px) {
  .viewer-panel { width: 100vw; }
}

.year-columns { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
@media (max-width: 900px) { .year-columns { grid-template-columns: 1fr; } }

/* --- Cosmetiek boekhouding --- */
td.num, .stat-value { white-space: nowrap; }
tr.note-row td {
  color: var(--muted); font-size: 12px; padding-top: 0;
  border-bottom: 1px solid var(--line);
}
tr.note-row.warn td { background: var(--tint-warn); }
tr.has-note td { border-bottom: none; }

/* --- Inklapbare secties (instellingen) --- */
details.section {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 8px; margin-bottom: 10px;
}
details.section > summary {
  cursor: pointer; padding: 12px 16px; font-weight: 600;
  list-style: none; display: flex; justify-content: space-between;
}
details.section > summary::after { content: "▸"; color: var(--muted); }
details.section[open] > summary::after { content: "▾"; }
details.section > .section-body { padding: 4px 16px 16px; }

/* --- Mobiel: nieuwe boekhoud/controle-elementen --- */
td .sub { white-space: normal; }          /* omschrijvingen mogen wrappen */
tr.note-row td { white-space: normal; min-width: 240px; }
.connector-card { overflow-x: auto; }

@media (max-width: 760px) {
  .stat-grid { grid-template-columns: repeat(2, 1fr) !important; gap: 10px; }
  .stat { padding: 12px; }
  .stat-value { font-size: 18px; }
  .year-columns { grid-template-columns: 1fr; }
  /* eerste kolom (nummer/omschrijving) niet eindeloos breed laten worden */
  td:first-child { max-width: 55vw; overflow-wrap: anywhere; white-space: normal; }
  details.section > summary { padding: 12px; }
}

/* --- Desktop: menu blijft in beeld op lange pagina's --- */
@media (min-width: 761px) {
  /* Vaste schermindeling: main is het scrollgebied, het menu staat
     per definitie stil (robuuster dan position: sticky in een grid). */
  body { height: 100vh; overflow: hidden; }
  .sidebar { height: 100vh; overflow-y: auto; }
  main { height: 100vh; overflow-y: auto; }
}

/* --- Mobiel: eerste kolom vast bij horizontaal scrollen --- */
@media (max-width: 760px) {
  table th:first-child, table td:first-child {
    position: sticky; left: 0; z-index: 1;
    background: var(--surface);
    box-shadow: 2px 0 4px -2px rgba(22,32,27,0.15);
  }
  tr.review td:first-child { background: var(--tint-warn); }
  tr.current-year td:first-child { background: var(--tint-current); }
}

/* --- Stat-kaart met attentiekleur --- */
.stat.review { border-left: 4px solid var(--warn-border); }

/* --- Flash-varianten (dark-mode-proof) --- */
.flash-warn { background: var(--tint-warn); color: var(--warn-ink); }
.flash-info { background: var(--tint-info); color: var(--info-ink); }

/* --- Thema-toggle --- */
.theme-toggle {
  margin: 10px 14px; padding: 6px 10px; font-size: 12px;
  background: transparent; color: var(--sidebar-ink);
  border: 1px solid rgba(255,255,255,0.15); border-radius: 6px; cursor: pointer;
}
.theme-toggle:hover { color: #fff; border-color: rgba(255,255,255,0.35); }
@media (max-width: 760px) {
  .theme-toggle { margin: 0 8px; white-space: nowrap; align-self: center; }
}
