/* ---------- Themes ----------
   Each theme sets the same tokens. Selectors are plain [data-theme] so the
   theme previews in Settings can show any theme regardless of the active one. */
:root, [data-theme="light"] {
  --bg: #f4f5f9; --surface: #ffffff; --surface-2: #eef0f5; --fg: #15171c; --muted: #646b78; --line: #e2e5ec;
  --accent: #5865f2; --accent-fg: #ffffff; --danger: #d93f45;
  --shadow: 0 1px 2px rgb(16 24 40 / .05), 0 1px 3px rgb(16 24 40 / .06);
  --shadow-lg: 0 12px 32px rgb(16 24 40 / .18);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    --bg: #131418; --surface: #1c1d23; --surface-2: #262830; --fg: #eceef3; --muted: #9a9fab; --line: #2c2f37;
    --accent: #7480f7; --accent-fg: #ffffff; --danger: #f06b70;
    --shadow: 0 1px 2px rgb(0 0 0 / .3); --shadow-lg: 0 16px 40px rgb(0 0 0 / .5);
    color-scheme: dark;
  }
}
[data-theme="dark"] {
  --bg: #131418; --surface: #1c1d23; --surface-2: #262830; --fg: #eceef3; --muted: #9a9fab; --line: #2c2f37;
  --accent: #7480f7; --accent-fg: #ffffff; --danger: #f06b70;
  --shadow: 0 1px 2px rgb(0 0 0 / .3); --shadow-lg: 0 16px 40px rgb(0 0 0 / .5);
  color-scheme: dark;
}
[data-theme="midnight"] {
  --bg: #000000; --surface: #0e0e12; --surface-2: #19191f; --fg: #f3f3f6; --muted: #8d8d99; --line: #23232b;
  --accent: #a78bfa; --accent-fg: #160b2e; --danger: #f87171;
  --shadow: none; --shadow-lg: 0 16px 40px rgb(0 0 0 / .7);
  color-scheme: dark;
}
[data-theme="sunset"] {
  --bg: #fff6f0; --surface: #ffffff; --surface-2: #fdebe1; --fg: #2b1911; --muted: #866b5f; --line: #f2dbcf;
  --accent: #c2410c; --accent-fg: #ffffff; --danger: #be123c;
  --shadow: 0 1px 2px rgb(120 53 15 / .06), 0 1px 3px rgb(120 53 15 / .08);
  --shadow-lg: 0 12px 32px rgb(120 53 15 / .2);
  color-scheme: light;
}
[data-theme="forest"] {
  --bg: #0d1512; --surface: #141f1a; --surface-2: #1d2b25; --fg: #e5efe9; --muted: #8ea69a; --line: #22332b;
  --accent: #34d399; --accent-fg: #052e1f; --danger: #fb7185;
  --shadow: 0 1px 2px rgb(0 0 0 / .3); --shadow-lg: 0 16px 40px rgb(0 0 0 / .5);
  color-scheme: dark;
}
:root { --accent-soft: color-mix(in srgb, var(--accent) 14%, transparent); --radius: 14px; }

/* ---------- Base ---------- */
* { box-sizing: border-box; }
[hidden] { display: none !important; } /* author display rules would otherwise beat the hidden attribute */
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0 auto; max-width: 760px; padding: 16px 20px 32px;
  font: 15px/1.45 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg); color: var(--fg); -webkit-font-smoothing: antialiased;
  transition: background-color .2s, color .2s;
}
h1, h2, h3 { line-height: 1.2; letter-spacing: -.01em; }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.muted { color: var(--muted); }
.spacer { flex: 1; }
ul { list-style: none; margin: 0; padding: 0; }

.i { width: 20px; height: 20px; flex: none; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 40px; padding: 0 16px;
  border: 1px solid transparent; border-radius: 10px; font-weight: 600; cursor: pointer; text-decoration: none;
  transition: background-color .15s, border-color .15s, opacity .15s, transform .05s;
}
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: default; }
.btn .i { width: 18px; height: 18px; }
.btn.primary { background: var(--accent); color: var(--accent-fg); }
.btn.secondary { background: var(--surface); border-color: var(--line); box-shadow: var(--shadow); }
.btn.ghost { background: transparent; color: var(--fg); }
.btn.danger-ghost { background: transparent; color: var(--danger); padding: 0 10px; }
.btn.icon-only { width: 40px; padding: 0; }
.btn.small { min-height: 32px; padding: 0 12px; font-size: 14px; border-color: var(--line); }
.btn.wide { width: 100%; }
@media (hover: hover) {
  .btn.primary:hover:not(:disabled) { background: color-mix(in srgb, var(--accent) 88%, var(--fg)); }
  .btn.secondary:hover:not(:disabled), .btn.ghost:hover:not(:disabled) { background: var(--surface-2); }
  .btn.danger-ghost:hover { background: color-mix(in srgb, var(--danger) 12%, transparent); }
}

/* ---------- Form fields ---------- */
input:not([type=radio]), select, textarea {
  width: 100%; min-height: 42px; padding: 9px 12px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--surface); transition: border-color .15s, box-shadow .15s;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea { resize: vertical; }
.field { display: block; margin: 0 0 14px; font-size: 13px; font-weight: 600; color: var(--muted); }
.field > input, .field > select, .field > textarea { margin-top: 6px; font-size: 15px; font-weight: 400; color: var(--fg); }
fieldset.field { border: 0; padding: 0; min-width: 0; }
fieldset.field legend { padding: 0; margin-bottom: 6px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.grid-3 { display: grid; grid-template-columns: 1.4fr 1fr 1fr; gap: 10px; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-end; align-items: center; margin-top: 4px; }
.actions.start { justify-content: flex-start; }

.segmented { display: flex; padding: 4px; gap: 4px; border-radius: 12px; background: var(--surface-2); }
.segmented label { flex: 1; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented span {
  display: block; padding: 7px 8px; border-radius: 8px; text-align: center; font-size: 14px; font-weight: 600;
  color: var(--muted); cursor: pointer; transition: background-color .15s, color .15s;
}
.segmented input:checked + span { background: var(--surface); color: var(--fg); box-shadow: var(--shadow); }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 1px; }

/* ---------- Layout ---------- */
.topbar { display: flex; align-items: center; gap: 10px; margin-bottom: 14px; }
.brand-logo { border-radius: 9px; }
.brand { font-weight: 700; font-size: 18px; letter-spacing: -.02em; }
.user { display: flex; align-items: center; gap: 8px; font-weight: 500; color: var(--muted); }
.user img { border-radius: 50%; }

nav {
  display: flex; gap: 4px; padding: 4px; margin-bottom: 18px; border-radius: 12px;
  background: var(--surface-2);
}
nav button {
  flex: 1; display: flex; align-items: center; justify-content: center; gap: 8px; min-height: 38px; padding: 0 12px;
  border: 0; border-radius: 9px; background: transparent; color: var(--muted); font-weight: 600; cursor: pointer;
}
nav button .i { width: 18px; height: 18px; }
nav button[aria-selected=true] { background: var(--surface); color: var(--fg); box-shadow: var(--shadow); }

.card {
  margin: 0 0 14px; padding: 18px; border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--surface); box-shadow: var(--shadow);
}
.card-title { display: flex; align-items: center; gap: 10px; margin: 0 0 14px; font-size: 16px; }
.card-title .i { color: var(--accent); }
.card p { margin: 0 0 12px; font-size: 14px; }
.tag { margin-left: 4px; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); font-size: 12px; font-weight: 600; }
.warn { display: flex; gap: 8px; align-items: flex-start; padding: 10px 12px; border-radius: 10px;
  background: color-mix(in srgb, var(--danger) 10%, transparent); color: var(--danger); }
.warn .i { width: 18px; height: 18px; margin-top: 1px; }
.section-title { margin: 22px 0 10px; font-size: 15px; color: var(--muted); font-weight: 600; }

/* ---------- Login ---------- */
#login { min-height: 100vh; min-height: 100dvh; display: grid; place-items: center; margin: -16px -20px -32px; padding: 20px; }
.login-card { width: min(380px, 100%); padding: 32px 24px; text-align: center; border: 1px solid var(--line);
  border-radius: 20px; background: var(--surface); box-shadow: var(--shadow-lg); }
.login-card .logo { border-radius: 22%; box-shadow: var(--shadow-lg); }
.login-card h1 { margin: 18px 0 6px; font-size: 26px; }
.login-card p { margin: 0 0 22px; }

/* ---------- List ---------- */
.toolbar { display: flex; gap: 8px; margin-bottom: 10px; }
.search { position: relative; flex: 1; }
.search > .i { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); width: 18px; height: 18px; color: var(--muted); pointer-events: none; }
.search input { padding-left: 38px; padding-right: 36px; }
.search kbd { position: absolute; right: 10px; top: 50%; transform: translateY(-50%); padding: 1px 6px; border: 1px solid var(--line);
  border-radius: 6px; font: 12px ui-monospace, monospace; color: var(--muted); }
.search input:focus ~ kbd, .search input:not(:placeholder-shown) ~ kbd { display: none; }

.chips { display: flex; gap: 6px; margin-bottom: 12px; overflow-x: auto; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chips button {
  flex: none; display: inline-flex; align-items: center; gap: 6px; min-height: 34px; padding: 0 12px;
  border: 1px solid var(--line); border-radius: 999px; background: var(--surface); color: var(--fg);
  font-size: 14px; font-weight: 500; cursor: pointer; white-space: nowrap;
}
.chips .count { padding: 0 6px; border-radius: 999px; background: var(--surface-2); color: var(--muted); font-size: 12px; font-weight: 600; }
.chips button[aria-pressed=true] { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); }
.chips button[aria-pressed=true] .count { background: color-mix(in srgb, var(--accent-fg) 20%, transparent); color: inherit; }

.entries { display: grid; gap: 8px; }
.entry {
  display: flex; gap: 12px; align-items: center; width: 100%; min-height: 62px; padding: 10px 12px; text-align: left;
  border: 1px solid var(--line); border-radius: 12px; background: var(--surface); box-shadow: var(--shadow); cursor: pointer;
  transition: border-color .15s, transform .1s;
}
@media (hover: hover) { .entry:hover { border-color: color-mix(in srgb, var(--accent) 50%, var(--line)); } }
.entry:active { transform: scale(.995); }
.entry.is-today { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 8%, var(--surface)); }
.who { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 1px; }
.who .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.who .date { font-size: 13px; color: var(--muted); }
.badge {
  flex: none; display: inline-flex; align-items: center; gap: 4px; padding: 3px 9px; border-radius: 999px;
  background: var(--surface-2); color: var(--muted); font-size: 12px; font-weight: 600; white-space: nowrap;
}
.badge .i { width: 14px; height: 14px; }
.badge.soon { background: var(--accent-soft); color: var(--accent); }
.badge.now { background: var(--accent); color: var(--accent-fg); }
.badge .age { opacity: .8; font-weight: 500; }

.av { border-radius: 50%; flex: none; object-fit: cover; background: var(--surface-2); }
.av.initial { display: inline-grid; place-items: center; color: #fff; font-weight: 700; background: hsl(var(--hue) 55% 48%); }

.empty { display: grid; justify-items: center; gap: 8px; padding: 36px 16px; text-align: center; color: var(--muted);
  border: 1px dashed var(--line); border-radius: var(--radius); }
.empty .i { width: 32px; height: 32px; color: var(--accent); }

.pager { display: flex; align-items: center; justify-content: center; gap: 8px; margin-top: 12px; }
.pager #page-label { min-width: 72px; text-align: center; font-size: 14px; font-weight: 600; color: var(--muted); font-variant-numeric: tabular-nums; }
.pager .btn { border: 1px solid var(--line); background: var(--surface); }

/* ---------- Calendar ---------- */
.cal-nav { display: flex; align-items: center; gap: 4px; margin-bottom: 12px; }
.cal-nav h2 { flex: 1; margin: 0; text-align: center; font-size: 18px; }
.cal-nav #this-month { margin-left: 4px; }
.calendar { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 4px; touch-action: pan-y; }
.calendar .dow { padding-bottom: 4px; text-align: center; font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.calendar .day {
  display: flex; flex-direction: column; align-items: flex-start; gap: 4px; min-height: 72px; min-width: 0; padding: 6px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--surface); cursor: pointer; overflow: hidden;
  transition: border-color .15s, background-color .15s;
}
@media (hover: hover) { .calendar .day:hover { border-color: color-mix(in srgb, var(--accent) 50%, var(--line)); } }
.calendar .num { display: grid; place-items: center; min-width: 22px; height: 22px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.calendar .day.today .num { background: var(--accent); color: var(--accent-fg); }
.calendar .day[aria-pressed=true] { border-color: var(--accent); background: var(--accent-soft); }
.calendar .day-head { display: flex; align-items: center; justify-content: space-between; gap: 2px; width: 100%; }
.calendar .pics { display: flex; flex-wrap: wrap; gap: 2px; align-items: center; }
.calendar .pics .av { box-shadow: 0 0 0 1.5px var(--surface); }
.calendar .more { font-size: 11px; font-weight: 700; color: var(--accent); }
.day-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 4px; }

/* ---------- Theme picker ---------- */
.themes { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 10px; }
.theme-option { display: grid; gap: 6px; justify-items: center; font-size: 13px; font-weight: 600; cursor: pointer; }
.theme-option input { position: absolute; opacity: 0; pointer-events: none; }
.preview {
  position: relative; display: block; width: 100%; aspect-ratio: 4 / 3; overflow: hidden; border-radius: 12px;
  border: 2px solid var(--line); background: var(--bg); transition: border-color .15s;
}
.preview i { position: absolute; left: 12%; right: 12%; top: 18%; height: 34%; border-radius: 6px; background: var(--surface); box-shadow: var(--shadow); }
.preview b { position: absolute; left: 12%; width: 40%; bottom: 16%; height: 18%; border-radius: 999px; background: var(--accent); }
.preview.split { display: flex; }
.preview.split span { flex: 1; background: var(--bg); position: relative; }
.preview.split span::after { content: ""; position: absolute; left: 24%; right: 24%; top: 30%; height: 40%; border-radius: 6px; background: var(--accent); }
.theme-option input:checked + .preview { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.theme-option input:focus-visible + .preview { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Dialog ---------- */
dialog {
  width: min(480px, calc(100% - 32px)); padding: 20px; border: 1px solid var(--line); border-radius: 18px;
  background: var(--surface); color: var(--fg); box-shadow: var(--shadow-lg);
}
dialog::backdrop { background: rgb(0 0 0 / .45); backdrop-filter: blur(3px); }
dialog[open] { animation: pop .18s ease-out; }
@keyframes pop { from { opacity: 0; transform: translateY(8px) scale(.98); } }
.dialog-head { display: flex; align-items: center; justify-content: space-between; margin: -4px -6px 14px 0; }
.dialog-head h2 { margin: 0; font-size: 19px; }

/* ---------- Toast ---------- */
#toast {
  position: fixed; z-index: 30; left: 50%; bottom: 24px; transform: translate(-50%, 12px); max-width: calc(100% - 32px);
  padding: 10px 16px; border-radius: 999px; background: var(--fg); color: var(--bg); font-size: 14px; font-weight: 500;
  box-shadow: var(--shadow-lg); opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Motion ---------- */
#birthdays { touch-action: pan-y; } /* horizontal drags are handled in JS for paging */
.chips button { transition: background-color .15s, border-color .15s, color .15s, transform .1s; }
.chips button:active { transform: scale(.96); }
.entry, .card, .calendar .day { transition: background-color .2s, border-color .15s, transform .1s; }
.badge.now .i { animation: twinkle 2.4s ease-in-out infinite; transform-origin: center; }
@keyframes twinkle { 0%, 100% { transform: scale(1) rotate(0); } 50% { transform: scale(1.18) rotate(12deg); } }
nav button[aria-selected=true] .i { animation: bump .35s cubic-bezier(.3, 1.6, .5, 1); }
@keyframes bump { 40% { transform: translateY(-3px) scale(1.12); } }
dialog[open]::backdrop { animation: fade .2s ease-out; }
@keyframes fade { from { opacity: 0; } }
.theme-option .preview { transition: border-color .15s, box-shadow .15s, transform .15s; }
.theme-option:active .preview { transform: scale(.97); }
.calendar .day:active { transform: scale(.96); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; }
}

/* ---------- Phones ---------- */
/* Tab bar at the bottom within thumb reach, editor as a bottom sheet, roomier tap targets. */
@media (max-width: 600px) {
  body { padding: 10px 14px calc(80px + env(safe-area-inset-bottom)); }
  #login { margin: -10px -14px calc(-80px - env(safe-area-inset-bottom)); }
  .topbar { margin-bottom: 10px; }
  .user #username { display: none; }
  nav {
    position: fixed; z-index: 20; left: 0; right: 0; bottom: 0; margin: 0; gap: 0; border-radius: 0;
    padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
    background: color-mix(in srgb, var(--surface) 88%, transparent); backdrop-filter: blur(14px);
    border-top: 1px solid var(--line);
  }
  nav button { flex-direction: column; gap: 3px; min-height: 52px; font-size: 11px; background: none !important; box-shadow: none !important; }
  nav button .i { width: 22px; height: 22px; }
  nav button[aria-selected=true] { color: var(--accent); }
  #add span { display: none; }
  #add { width: 42px; padding: 0; }
  .btn { min-height: 44px; }
  .btn.icon-only { width: 44px; }
  .entry { min-height: 58px; padding: 8px 10px; }
  .calendar { gap: 3px; }
  .calendar .day { min-height: 58px; padding: 4px; gap: 3px; border-radius: 8px; }
  .calendar .dow { font-size: 10px; letter-spacing: 0; }
  .card { padding: 16px; }
  #toast { bottom: calc(84px + env(safe-area-inset-bottom)); }
  dialog {
    width: 100%; max-width: 100%; margin: auto 0 0; border-radius: 20px 20px 0 0; border-bottom: 0;
    padding-bottom: calc(20px + env(safe-area-inset-bottom));
  }
  @keyframes pop { from { transform: translateY(40px); opacity: .6; } }
}
