/* ============ Design tokens ============ */
:root {
  --bg: #f7f8fa;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #e3e6eb;
  --fg: #1f2937;
  --muted: #6b7280;
  --faint: #9ca3af;
  --primary: #6366f1;
  --primary-fg: #ffffff;
  --danger: #ef4444;
  --cell-empty: #e9ecf1;
  --cell-past: #d3d7de;
  --today: #f59e0b;
  --shadow: 0 8px 30px rgba(20, 20, 40, 0.12);
  --radius: 12px;
  --cell: 13px;
  --cell-gap: 3px;
}

:root[data-theme='dark'] {
  --bg: #0f1115;
  --surface: #171a21;
  --surface-2: #1f232b;
  --border: #2a2f3a;
  --fg: #e5e7eb;
  --muted: #9aa3b2;
  --faint: #6b7280;
  --primary: #818cf8;
  --primary-fg: #0f1115;
  --danger: #f87171;
  --cell-empty: #1f232b;
  --cell-past: #2f3542;
  --today: #fbbf24;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--fg);
  -webkit-font-smoothing: antialiased;
  /* Niets selecteerbaar behalve waar het nuttig is (invoervelden, notitie-tekst) */
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

/* Wél selecteerbaar: invoer en notitie-tekst */
input, textarea, [contenteditable], .note-body, .list-sub, .detail-date, .cell-tip {
  -webkit-user-select: text;
  user-select: text;
}

.hidden { display: none !important; }

button { font-family: inherit; cursor: pointer; }

/* ============ Buttons ============ */
.btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--fg);
  padding: 9px 16px;
  border-radius: 9px;
  font-size: 14px;
  font-weight: 500;
  transition: transform .08s, background .15s, border-color .15s;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--primary); color: var(--primary-fg); border-color: transparent; }
.btn-primary:hover { filter: brightness(1.05); background: var(--primary); }
.btn-danger { background: transparent; color: var(--danger); border-color: var(--danger); }
.btn-danger:hover { background: color-mix(in srgb, var(--danger) 12%, transparent); }
.btn-block { width: 100%; display: block; }

.icon-btn {
  border: none;
  background: transparent;
  color: var(--fg);
  width: 38px; height: 38px;
  border-radius: 9px;
  font-size: 17px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background .15s;
  text-decoration: none;
}
.icon-btn:hover { background: var(--surface-2); }

.linklike {
  border: none; background: none; color: var(--primary);
  font-size: 13px; padding: 2px 4px;
}

/* ============ Auth ============ */
.auth-screen {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(1200px 600px at 20% -10%, color-mix(in srgb, var(--primary) 18%, transparent), transparent),
    var(--bg);
}
.auth-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 32px 28px;
  box-shadow: var(--shadow);
}
.auth-brand { text-align: center; margin-bottom: 22px; }
.auth-logo { font-size: 40px; }
.auth-brand h1 { margin: 6px 0 2px; font-size: 26px; letter-spacing: -0.5px; }
.auth-tagline { margin: 0; color: var(--muted); font-size: 14px; }
.auth-tabs {
  display: flex; gap: 4px; background: var(--surface-2);
  padding: 4px; border-radius: 10px; margin-bottom: 18px;
}
.auth-tab {
  flex: 1; border: none; background: transparent; color: var(--muted);
  padding: 9px; border-radius: 7px; font-weight: 600; font-size: 14px;
}
.auth-tab.active { background: var(--surface); color: var(--fg); box-shadow: 0 1px 3px rgba(0,0,0,.08); }
.auth-form { display: flex; flex-direction: column; gap: 14px; }
.auth-form label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
.auth-form input {
  border: 1px solid var(--border); background: var(--bg); color: var(--fg);
  padding: 11px 12px; border-radius: 9px; font-size: 15px;
}
.auth-form input:focus { outline: 2px solid var(--primary); outline-offset: 0; border-color: transparent; }
.auth-error { color: var(--danger); font-size: 13px; min-height: 18px; margin: 0; }
.auth-note { color: var(--muted); font-size: 13px; text-align: center; min-height: 16px; }
.auth-switch-lang { text-align: center; color: var(--faint); font-size: 12px; margin-top: 14px; }

/* ============ App shell ============ */
.app-screen { display: flex; flex-direction: column; min-height: 100vh; }

.topbar {
  display: flex; align-items: center; gap: 16px;
  padding: 10px 18px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 20;
}
.brand { font-weight: 700; font-size: 18px; letter-spacing: -0.3px; white-space: nowrap; }
.brand-logo { font-size: 18px; }
.topbar-stats { display: flex; gap: 10px; flex: 1; overflow: hidden; }
.stat-chip {
  display: flex; flex-direction: column; line-height: 1.15;
  padding: 4px 12px; background: var(--surface-2); border-radius: 9px;
}
.stat-val { font-weight: 700; font-size: 15px; }
.stat-lbl { font-size: 11px; color: var(--muted); }
.topbar-right { display: flex; align-items: center; gap: 2px; }

/* ============ Grid ============ */
.grid-wrap { flex: 1; display: flex; flex-direction: column; padding: 18px 22px 40px; gap: 12px; }
.grid-wrap.dim { filter: blur(3px); pointer-events: none; opacity: .5; }
.grid-hint { color: var(--muted); font-size: 13px; }
.grid-scroll { overflow: auto; touch-action: pan-x pan-y; }
.grid-sizer { transform-origin: 0 0; }
.grid-inner { display: inline-flex; flex-direction: column; gap: var(--cell-gap); user-select: none; transform-origin: 0 0; will-change: transform; }
.grid-row { display: flex; align-items: center; gap: var(--cell-gap); }
.age-label {
  width: var(--age-label-w, 26px); text-align: right; font-size: 11px; color: var(--faint);
  flex: 0 0 var(--age-label-w, 26px); padding-right: 6px;
}
.cells { display: flex; gap: var(--cell-gap); }
.grid-colhead { margin-bottom: 2px; }
.colhead-cell {
  width: var(--cell); flex: 0 0 var(--cell);
  font-size: 10px; line-height: 1; color: var(--faint);
  text-align: center; overflow: visible; white-space: nowrap;
}
/* Kleine tussenruimte na elke 4 weken (groepeert het raster per ~maand) */
.group-gap { margin-left: var(--group-gap, 5px); }

/* 'Passend'-knop (alleen mobiel, zichtbaar als er is ingezoomd) */
.fit-btn {
  position: fixed; z-index: 70;
  right: max(14px, env(safe-area-inset-right));
  bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  border: 1px solid var(--border); background: var(--surface); color: var(--fg);
  padding: 10px 16px; border-radius: 999px; font-size: 14px; font-weight: 600;
  box-shadow: var(--shadow); display: inline-flex; align-items: center; gap: 6px;
}
.fit-btn::before { content: '⤢'; font-size: 15px; }
.cell {
  width: var(--cell); height: var(--cell);
  border-radius: 3px;
  background: var(--cell-empty);
  position: relative;
  transition: transform .06s;
}
.cell.past { background: var(--cell-past); }
.cell.has-period { }
.cell.today { outline: 2px solid var(--today); outline-offset: 1px; z-index: 2; }
.cell:hover { transform: scale(1.35); z-index: 3; box-shadow: 0 0 0 1px var(--fg); }
.cell.selecting { outline: 2px solid var(--primary); outline-offset: 0; z-index: 4; }
/* Hover-highlight vanuit de legenda: dim de rest, licht de bijbehorende weken op */
.grid-inner.has-highlight .cell { opacity: .25; transition: opacity .1s; }
.grid-inner.has-highlight .cell.hl {
  opacity: 1;
  outline: 1px solid var(--fg);
  outline-offset: 0;
  box-shadow: 0 0 5px 1px color-mix(in srgb, var(--fg) 45%, transparent);
  z-index: 3;
}
.note-dot {
  position: absolute; inset: 0; margin: auto;
  width: 6px; height: 6px; border-radius: 50%;
  box-shadow: 0 0 0 1px var(--surface);
}

/* ============ Legend ============ */
.legend { display: flex; flex-wrap: wrap; gap: 22px; padding-top: 6px; }
.legend-section { min-width: 140px; }
.legend-section h3 { margin: 0 0 8px; font-size: 12px; text-transform: uppercase; letter-spacing: .5px; color: var(--muted); }
.legend-item {
  display: flex; align-items: center; gap: 8px;
  border: none; background: none; color: var(--fg);
  padding: 4px 6px; border-radius: 7px; font-size: 13px; width: 100%; text-align: left;
}
.legend-item:hover { background: var(--surface-2); }
.legend-item.off { opacity: .4; text-decoration: line-through; }
.legend-item.solo { background: color-mix(in srgb, var(--primary) 16%, transparent); font-weight: 600; }
.solo-badge {
  font-size: 9px; text-transform: uppercase; letter-spacing: .5px;
  background: var(--primary); color: var(--primary-fg);
  padding: 1px 5px; border-radius: 5px; margin-left: auto;
}
.legend-item.static { cursor: default; }
.legend-item.static:hover { background: none; }
.legend-empty { color: var(--faint); font-size: 13px; margin: 0; }
.swatch { width: 14px; height: 14px; border-radius: 3px; flex: 0 0 14px; }
.swatch.round { border-radius: 50%; }
.today-swatch { background: transparent; outline: 2px solid var(--today); outline-offset: -1px; }
.legend-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ============ Onboarding ============ */
.onboarding { position: fixed; inset: 0; z-index: 30; display: grid; place-items: center; padding: 24px; }
.onboarding-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 18px;
  padding: 30px; width: 100%; max-width: 380px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 14px;
}
.onboarding-card h2 { margin: 0; }
.onboarding-card p { margin: 0; color: var(--muted); font-size: 14px; }
.onboarding-card label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
.onboarding-card input {
  border: 1px solid var(--border); background: var(--bg); color: var(--fg);
  padding: 10px 12px; border-radius: 9px; font-size: 15px;
}

/* ============ Drawer ============ */
.drawer-overlay { position: fixed; inset: 0; background: rgba(10,12,20,.4); z-index: 40; }
.drawer {
  position: fixed; top: 0; right: 0; height: 100%; width: 380px; max-width: 92vw;
  background: var(--surface); border-left: 1px solid var(--border);
  z-index: 50; display: flex; flex-direction: column;
  box-shadow: var(--shadow); animation: slideIn .18s ease;
}
@keyframes slideIn { from { transform: translateX(20px); opacity: .6; } to { transform: none; opacity: 1; } }
.drawer-head {
  display: flex; align-items: center; justify-content: space-between;
  /* Safe-area bovenin zodat de titel niet onder de notch/Dynamic Island valt */
  padding: calc(16px + env(safe-area-inset-top, 0px)) 18px 16px;
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; background: var(--surface); z-index: 1;
}
.drawer-head h2 { margin: 0; font-size: 17px; }
.drawer-body { padding: 18px; overflow-y: auto; display: flex; flex-direction: column; gap: 14px; }

.drawer-section { border-top: 1px solid var(--border); padding-top: 14px; display: flex; flex-direction: column; gap: 8px; }
.drawer-section h3 { margin: 0 0 4px; font-size: 12px; text-transform: uppercase; letter-spacing: .5px; color: var(--muted); }
.drawer-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 6px; }
.drawer-actions .btn { flex: 1; min-width: 90px; }

.muted { color: var(--muted); font-size: 14px; }
.hint { color: var(--faint); font-size: 12px; }

/* Detail head */
.detail-head { }
.detail-date { font-size: 20px; font-weight: 700; }
.detail-sub { color: var(--muted); font-size: 13px; margin-top: 2px; }

/* Forms */
.form { display: flex; flex-direction: column; gap: 14px; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
.field > span:first-child { font-weight: 500; }
.field input, .field textarea, .field select {
  border: 1px solid var(--border); background: var(--bg); color: var(--fg);
  padding: 10px 12px; border-radius: 9px; font-size: 15px; font-family: inherit;
}
.field input:focus, .field textarea:focus, .field select:focus { outline: 2px solid var(--primary); border-color: transparent; }
.field textarea { resize: vertical; }

/* Color picker */
.color-picker { display: flex; flex-wrap: wrap; gap: 7px; align-items: center; }
.color-swatch {
  width: 26px; height: 26px; border-radius: 7px; border: 2px solid transparent;
  padding: 0; transition: transform .08s;
}
.color-swatch:hover { transform: scale(1.12); }
.color-swatch.sel { border-color: var(--fg); box-shadow: 0 0 0 2px var(--surface); }
.color-native { width: 34px; height: 30px; padding: 0; border: 1px solid var(--border); border-radius: 7px; background: var(--bg); }

/* Lists */
.panel-list { display: flex; flex-direction: column; gap: 8px; }
.list-row {
  display: flex; align-items: center; gap: 10px; width: 100%; text-align: left;
  border: 1px solid var(--border); background: var(--surface); color: var(--fg);
  padding: 10px 12px; border-radius: 10px; font-size: 14px;
}
.list-row:hover { background: var(--surface-2); }
.list-info { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.list-title { font-weight: 600; }
.list-sub { font-size: 12px; color: var(--muted); }

.note-card {
  display: flex; gap: 10px; width: 100%; text-align: left;
  border: 1px solid var(--border); background: var(--surface); color: var(--fg);
  padding: 10px 12px; border-radius: 10px;
}
.note-card:hover { background: var(--surface-2); }
.note-bar { width: 4px; border-radius: 3px; flex: 0 0 4px; }
.note-body { font-size: 13px; color: var(--muted); margin-top: 4px; white-space: pre-wrap; }

/* Stats */
.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stat-box { background: var(--surface-2); border-radius: 11px; padding: 14px; }
.stat-box-val { font-size: 22px; font-weight: 700; }
.stat-box-lbl { font-size: 12px; color: var(--muted); margin-top: 2px; }
.progress { height: 10px; background: var(--surface-2); border-radius: 6px; overflow: hidden; }
.progress-fill { height: 100%; background: var(--primary); border-radius: 6px; transition: width .4s; }
.until-age { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; font-size: 14px; color: var(--muted); }
.until-age input { width: 80px; border: 1px solid var(--border); background: var(--bg); color: var(--fg); padding: 8px 10px; border-radius: 8px; }
.until-out { color: var(--fg); }

/* ============ Tooltip ============ */
.cell-tip {
  position: fixed; z-index: 60; pointer-events: none;
  background: var(--fg); color: var(--bg);
  padding: 7px 10px; border-radius: 8px; font-size: 12.5px; line-height: 1.5;
  max-width: 260px; box-shadow: var(--shadow);
}

/* ============ Toasts ============ */
.toast-host { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 80; display: flex; flex-direction: column; gap: 8px; align-items: center; }
.toast {
  background: var(--fg); color: var(--bg);
  padding: 10px 18px; border-radius: 10px; font-size: 14px; font-weight: 500;
  opacity: 0; transform: translateY(10px); transition: opacity .25s, transform .25s;
  box-shadow: var(--shadow);
}
.toast.show { opacity: 1; transform: none; }
.toast-err { background: var(--danger); color: #fff; }

/* ============ Update-popup (nieuwe versie) ============ */
.update-popup {
  position: fixed; left: 0; right: 0; z-index: 90;
  bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  display: flex; justify-content: center; padding: 0 12px;
  animation: slideUp .25s ease;
}
@keyframes slideUp { from { transform: translateY(20px); opacity: 0; } to { transform: none; opacity: 1; } }
.update-card {
  display: flex; align-items: center; gap: 12px;
  width: 100%; max-width: 440px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; padding: 12px 14px; box-shadow: var(--shadow);
}
.update-ico { font-size: 22px; }
.update-text { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.update-text strong { font-size: 14px; }
.update-text span { font-size: 12px; color: var(--muted); }
.update-refresh { flex: 0 0 auto; }
.update-dismiss { flex: 0 0 auto; }

/* ============ Responsive ============ */
@media (max-width: 760px) {
  .topbar-stats { display: none; }
  .grid-wrap { padding: 12px; }
  :root { --cell: 12px; --cell-gap: 2px; --group-gap: 4px; }

  /* Veilige marges (notch) + compactere topbar */
  .topbar {
    padding: calc(8px + env(safe-area-inset-top, 0px)) 10px 8px;
    gap: 8px;
  }
  .topbar-right { gap: 0; }
  .icon-btn { width: 34px; height: 34px; font-size: 16px; }
  .brand { font-size: 16px; }

  /* Fit: grid vangt de scroll niet -> de PAGINA scrolt normaal */
  .grid-scroll { overflow: hidden; touch-action: pan-y; }
  /* Ingezoomd: grid wordt een vaste viewport die je pant/zoomt */
  .grid-scroll.zoomed {
    position: fixed; left: 0; right: 0; bottom: 0; /* top wordt via JS gezet */
    overflow: auto; touch-action: pan-x pan-y;
    background: var(--bg); z-index: 25; padding: 4px;
  }

  /* Drawer full-screen op mobiel */
  .drawer { width: 100%; max-width: 100%; border-left: none; }
  .drawer-body { padding-bottom: calc(24px + env(safe-area-inset-bottom, 0px)); }

  /* Legenda onder elkaar */
  .legend { gap: 16px; }
  .legend-section { min-width: 45%; }

  .grid-wrap { padding-left: max(12px, env(safe-area-inset-left)); padding-right: max(12px, env(safe-area-inset-right)); }
}

@media (max-width: 400px) {
  :root { --cell: 10px; --cell-gap: 2px; --group-gap: 3px; }
  .age-label { width: 20px; flex-basis: 20px; font-size: 10px; }
}

/* In geïnstalleerde (standalone) modus: extra ademruimte bovenaan voor de statusbalk */
@media (display-mode: standalone) {
  .topbar { padding-top: calc(10px + env(safe-area-inset-top, 0px)); }
}
