/* ===== Budsjett – design system =====
   Struktur: reset -> temaer (CSS-variabler) -> tokens -> base -> komponenter -> skjermer -> verktøy
   Kun transform/opacity animeres (ytelse). Ease-out ved inn, ease-in-out ved bevegelse, aldri ease-in. */

/* ---------- Reset ---------- */
*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}
button { font: inherit; color: inherit; background: none; border: none; cursor: pointer; }
/* Synlig fokusring for tastaturbrukere - kun ved :focus-visible, ikke ved museklikk. */
button:focus-visible, a:focus-visible, [tabindex]:focus-visible {
  outline: none; box-shadow: 0 0 0 3px var(--ring); border-radius: var(--radius-s);
}
input, select, textarea { font: inherit; color: inherit; }
a { color: inherit; text-decoration: none; }
ul { margin: 0; padding: 0; list-style: none; }
h1, h2, h3, p { margin: 0; }
svg { display: block; }

/* ---------- Temaer ---------- */
/* Hver variabel: bg/surface/text/muted/divider = flate og tekst. accent/accentdeep/ring = merkevarefarge + gradientdybde + fokusring.
   oncard-* = tekst oppå accent-flater (balansekort, FAB). */
:root[data-theme="rosa"] {
  --bg: #FBF4F3; --surface: #FFFFFF; --text: #2B2320; --muted: #8A7B76; --divider: #EDE1DF;
  --accent: #E99AA0; --accentdeep: #C85F68; --ring: #F3C2C6;
  --oncard-text: #FFFFFF; --oncard-muted: rgba(255,255,255,.74); --oncard-chip: rgba(255,255,255,.16);
  --danger: #C64B4B; --positive: #4C8C5C;
}
:root[data-theme="nordmarka"] {
  --bg: #F3F6EF; --surface: #FFFFFF; --text: #243324; --muted: #74836C; --divider: #E2E8DB;
  --accent: #5E7A52; --accentdeep: #3F5536; --ring: #BFD1AE;
  --oncard-text: #FFFFFF; --oncard-muted: rgba(255,255,255,.74); --oncard-chip: rgba(255,255,255,.16);
  --danger: #C64B4B; --positive: #3F5536;
}
:root[data-theme="midnatt"] {
  --bg: #16172B; --surface: #202244; --text: #EDEDF5; --muted: #9B9DC2; --divider: #2E3058;
  --accent: #5457A0; --accentdeep: #3B3D6B; --ring: #4A4D82;
  --oncard-text: #FFFFFF; --oncard-muted: rgba(255,255,255,.74); --oncard-chip: rgba(255,255,255,.16);
  --danger: #E07A7A; --positive: #7FD19B;
}
:root[data-theme="hvit"] {
  --bg: #FAFAFA; --surface: #FFFFFF; --text: #1A1A1A; --muted: #8A8A8A; --divider: #EBEBEB;
  --accent: #2B2B2B; --accentdeep: #000000; --ring: #CFCFCF;
  --oncard-text: #FFFFFF; --oncard-muted: rgba(255,255,255,.72); --oncard-chip: rgba(255,255,255,.14);
  --danger: #C64B4B; --positive: #3F8C4C;
}
:root[data-theme="host"] {
  --bg: #FBF2EA; --surface: #FFFFFF; --text: #332318; --muted: #8C7363; --divider: #EEE0D3;
  --accent: #C06B35; --accentdeep: #8C4A22; --ring: #E7B98F;
  --oncard-text: #FFFFFF; --oncard-muted: rgba(255,255,255,.74); --oncard-chip: rgba(255,255,255,.16);
  --danger: #C64B4B; --positive: #5E8C3F;
}
:root[data-theme="himmel"] {
  --bg: #F0F6FA; --surface: #FFFFFF; --text: #1C2E36; --muted: #74909C; --divider: #DCEAF0;
  --accent: #4FA3C7; --accentdeep: #2E7495; --ring: #AEDCEE;
  --oncard-text: #FFFFFF; --oncard-muted: rgba(255,255,255,.74); --oncard-chip: rgba(255,255,255,.16);
  --danger: #C64B4B; --positive: #3F8C4C;
}

/* ---------- Tokens ---------- */
:root {
  --sp-1: 4px; --sp-2: 8px; --sp-3: 12px; --sp-4: 16px; --sp-5: 24px; --sp-6: 32px; --sp-7: 48px;
  --radius-s: 10px; --radius-m: 16px; --radius-l: 22px; --radius-full: 999px;
  --fs-display: 2.1rem; --fs-h1: 1.375rem; --fs-h2: 1.0625rem; --fs-body: .9375rem; --fs-small: .8125rem; --fs-tiny: .6875rem;
  --font-serif: ui-serif, Georgia, "Times New Roman", serif;
  --ease-out: cubic-bezier(.23,1,.32,1);
  --ease-in-out: cubic-bezier(.77,0,.175,1);
  --shadow-card: 0 1px 2px rgba(0,0,0,.04), 0 8px 24px rgba(0,0,0,.06);
}
:root[data-theme="midnatt"] { --shadow-card: 0 1px 2px rgba(0,0,0,.3), 0 8px 24px rgba(0,0,0,.4); }

/* ---------- Layout ---------- */
.app-root { min-height: 100%; display: flex; flex-direction: column; }
/* Tabbaren er posisjonert fast (se .tabbar), så den ligger alltid mot bunnen av skjermen
   uavhengig av hvor høyt innholdet er - derfor må innholdet ha nok bunnplass til å ikke havne under den. */
.screen-area { flex: 1; display: flex; flex-direction: column; min-height: 0; padding-bottom: calc(66px + env(safe-area-inset-bottom)); }
.screen {
  flex: 1;
  padding: calc(env(safe-area-inset-top) + var(--sp-5)) var(--sp-4) var(--sp-7);
  max-width: 480px;
  margin: 0 auto;
  width: 100%;
}
.screen--auth { display: flex; flex-direction: column; justify-content: center; min-height: 100vh; padding-top: var(--sp-6); }

.topbar { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: var(--sp-5); }
.topbar h1 { font-size: var(--fs-h1); font-weight: 600; }
.topbar .topbar__action { color: var(--muted); font-size: var(--fs-small); }

/* ---------- Typografi ---------- */
.display { font-family: var(--font-serif); font-size: var(--fs-display); font-weight: 600; letter-spacing: -.01em; }
.h2 { font-size: var(--fs-h2); font-weight: 600; }
.muted { color: var(--muted); }
.small { font-size: var(--fs-small); }
.tiny { font-size: var(--fs-tiny); }

/* ---------- Knapper ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--sp-2);
  padding: var(--sp-3) var(--sp-5); border-radius: var(--radius-full);
  font-size: var(--fs-body); font-weight: 600;
  transition: transform 160ms var(--ease-out), opacity 160ms var(--ease-out), background-color 160ms var(--ease-out);
}
.btn:active { transform: scale(.97); }
.btn-primary { background: var(--accent); color: var(--oncard-text); width: 100%; }
.btn-primary:disabled { opacity: .5; }
.btn-ghost { background: var(--surface); color: var(--text); border: 1px solid var(--divider); }
.btn-text { color: var(--accentdeep); padding: var(--sp-2) var(--sp-3); }
.btn-danger-text { color: var(--danger); padding: var(--sp-2) var(--sp-3); }
.icon-btn {
  width: 40px; height: 40px; border-radius: var(--radius-full); display: flex; align-items: center; justify-content: center;
  background: var(--surface); transition: transform 160ms var(--ease-out);
  font-size: 1.1rem;
}
.icon-btn:active { transform: scale(.94); }

/* FAB */
.fab {
  position: fixed; right: var(--sp-5); bottom: calc(env(safe-area-inset-bottom) + 84px);
  width: 56px; height: 56px; border-radius: var(--radius-full);
  background: var(--accentdeep); color: var(--oncard-text);
  display: flex; align-items: center; justify-content: center; font-size: 1.6rem; line-height: 1;
  box-shadow: 0 8px 20px rgba(0,0,0,.18);
  transition: transform 160ms var(--ease-out);
}
.fab:active { transform: scale(.92); }

/* ---------- Kort ---------- */
.card {
  background: var(--surface); border-radius: var(--radius-l); padding: var(--sp-5);
  box-shadow: var(--shadow-card);
}
.balance-card {
  background: linear-gradient(160deg, var(--accent), var(--accentdeep));
  color: var(--oncard-text); border-radius: var(--radius-l); padding: var(--sp-5);
  margin-bottom: var(--sp-5);
}
.balance-card .display { color: var(--oncard-text); }
.balance-card .label { color: var(--oncard-muted); font-size: var(--fs-small); }
.chip-row { display: flex; gap: var(--sp-2); margin-top: var(--sp-4); flex-wrap: wrap; }
.chip {
  background: var(--oncard-chip); color: var(--oncard-text); border-radius: var(--radius-full);
  padding: var(--sp-1) var(--sp-3); font-size: var(--fs-tiny); display: flex; align-items: center; gap: 6px;
}

/* Streak */
.streak-row { display: flex; align-items: center; gap: var(--sp-2); margin: var(--sp-5) 0; }
.streak-dots { display: flex; gap: 6px; }
.streak-dot { width: 8px; height: 8px; border-radius: var(--radius-full); background: var(--divider); }
.streak-dot--on { background: var(--accent); }

/* Liste / transaksjoner */
.section-title { font-size: var(--fs-small); color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; margin: var(--sp-5) 0 var(--sp-3); }
.list { display: flex; flex-direction: column; gap: var(--sp-2); }
.list-item {
  background: var(--surface); border-radius: var(--radius-m); padding: var(--sp-3) var(--sp-4);
  display: flex; align-items: center; gap: var(--sp-3);
  transition: transform 160ms var(--ease-out);
}
.list-item:active { transform: scale(.98); }
.list-item__emoji { width: 36px; height: 36px; border-radius: var(--radius-full); background: var(--bg); display: flex; align-items: center; justify-content: center; font-size: 1.1rem; flex-shrink: 0; }
.list-item__body { flex: 1; min-width: 0; }
.list-item__title { font-weight: 600; font-size: var(--fs-body); }
.list-item__sub { color: var(--muted); font-size: var(--fs-small); }
.list-item__amount { font-weight: 600; font-size: var(--fs-body); white-space: nowrap; }
.list-item__amount--negative { color: var(--text); }
.list-item__amount--positive { color: var(--positive); }
.list-item__drag { display: flex; flex-direction: column; gap: 2px; margin-left: var(--sp-2); }
.list-item__drag button { color: var(--muted); font-size: .85rem; padding: 2px; transition: transform 120ms var(--ease-out); }
.list-item__drag button:active { transform: scale(.85); }

.empty-state { text-align: center; padding: var(--sp-6) var(--sp-4); color: var(--muted); }
.empty-state .emoji { font-size: 2rem; display: block; margin-bottom: var(--sp-3); }

/* Fremdrift / donut */
.progress-track { height: 8px; border-radius: var(--radius-full); background: var(--divider); overflow: hidden; }
.progress-fill { height: 100%; border-radius: var(--radius-full); background: var(--accent); transition: width 300ms var(--ease-in-out); }
.progress-fill--over { background: var(--danger); }
.donut-wrap { display: flex; align-items: center; justify-content: center; margin: var(--sp-4) 0; position: relative; }
.donut-wrap .donut-center { position: absolute; text-align: center; }
.donut-wrap .donut-center .display { font-size: 1.5rem; }
.donut-seg { transition: stroke-dasharray 400ms var(--ease-in-out); }

/* Måned/år-bytter */
.segmented { display: flex; background: var(--surface); border-radius: var(--radius-full); padding: 4px; gap: 2px; }
.segmented button { flex: 1; padding: var(--sp-2) var(--sp-3); border-radius: var(--radius-full); font-size: var(--fs-small); font-weight: 600; color: var(--muted); transition: background-color 160ms var(--ease-out), color 160ms var(--ease-out); }
.segmented button[aria-selected="true"] { background: var(--accent); color: var(--oncard-text); }

.period-nav { display: flex; align-items: center; justify-content: space-between; margin: var(--sp-4) 0; }
.period-nav .period-label { font-weight: 600; }
.period-nav button { color: var(--muted); font-size: 1.1rem; padding: var(--sp-2); }
.period-nav button:active { transform: scale(.9); }

/* Kategori-badge m/sirkel */
.legend { display: flex; flex-wrap: wrap; gap: var(--sp-2) var(--sp-4); margin-top: var(--sp-3); }
.legend__item { display: flex; align-items: center; gap: 6px; font-size: var(--fs-small); color: var(--muted); }
.legend__dot { width: 8px; height: 8px; border-radius: var(--radius-full); }

/* ---------- Formelementer ---------- */
.field { margin-bottom: var(--sp-4); }
.field label { display: block; font-size: var(--fs-small); color: var(--muted); margin-bottom: var(--sp-2); font-weight: 600; }
.field input, .field select {
  width: 100%; background: var(--surface); border: 1px solid var(--divider); border-radius: var(--radius-s);
  padding: var(--sp-3) var(--sp-4); color: var(--text);
  /* 16px, ikke --fs-body (15px): under 16px zoomer iOS Safari inn ved fokus på feltet,
     og i en installert PWA sitter zoomen ofte fast etterpå i stedet for å gå tilbake. */
  font-size: max(16px, var(--fs-body));
  transition: border-color 160ms var(--ease-out), box-shadow 160ms var(--ease-out);
}
.field input:focus, .field select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--ring); }
/* iOS sin interne tidsvelger-tekst bryr seg ikke om padding/line-height, bare en fast boks-høyde
   med lik line-height holder den innenfor boksen. */
.field input[type="time"] {
  height: 46px;
  padding-top: 0;
  padding-bottom: 0;
  line-height: 46px;
}
.field__error { color: var(--danger); font-size: var(--fs-small); margin-top: var(--sp-2); }
.field--row { display: flex; gap: var(--sp-3); }
.field--row .field { flex: 1; }

.switch { position: relative; width: 48px; height: 28px; border-radius: var(--radius-full); background: var(--divider); flex-shrink: 0; transition: background-color 160ms var(--ease-out); }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: var(--radius-full); background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.2); transition: transform 200ms var(--ease-out); }
.switch[aria-checked="true"] { background: var(--accent); }
.switch[aria-checked="true"]::after { transform: translateX(20px); }
.switch-row { display: flex; align-items: center; justify-content: space-between; padding: var(--sp-3) 0; }

/* Emoji-velger */
.emoji-grid { display: grid; grid-template-columns: repeat(6, 1fr); gap: var(--sp-2); max-height: 220px; overflow-y: auto; margin-bottom: var(--sp-4); }
.emoji-btn { aspect-ratio: 1; display: flex; align-items: center; justify-content: center; font-size: 1.3rem; border-radius: var(--radius-s); background: var(--bg); transition: transform 120ms var(--ease-out), background-color 120ms var(--ease-out); }
.emoji-btn:active { transform: scale(.9); }
.emoji-btn[aria-selected="true"] { background: var(--ring); }
.emoji-picked { width: 48px; height: 48px; border-radius: var(--radius-full); background: var(--bg); display: flex; align-items: center; justify-content: center; font-size: 1.4rem; margin: 0 auto var(--sp-4); transition: transform 120ms var(--ease-out); }
.emoji-picked:active { transform: scale(.92); }

/* Fargevelger (temaer) */
.theme-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: var(--sp-3); margin: var(--sp-3) 0; }
.theme-swatch { display: flex; flex-direction: column; align-items: center; gap: var(--sp-2); padding: var(--sp-3); border-radius: var(--radius-m); transition: transform 120ms var(--ease-out); }
.theme-swatch:active { transform: scale(.95); }
.theme-swatch__dot { width: 40px; height: 40px; border-radius: var(--radius-full); border: 2px solid transparent; }
.theme-swatch[aria-selected="true"] .theme-swatch__dot { border-color: var(--text); }
.theme-swatch__label { font-size: var(--fs-tiny); color: var(--muted); }

/* ---------- Bunn-navigasjon ---------- */
.tabbar {
  /* fixed (ikke sticky) - sticky krevde at innholdet fylte hele viewporten for å havne nederst,
     fixed ligger alltid mot bunnen av skjermen uansett innholdshøyde. */
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 50;
  display: flex; background: var(--surface); border-top: 1px solid var(--divider);
  padding: var(--sp-2) var(--sp-2) calc(env(safe-area-inset-bottom) + var(--sp-2));
}
.tab {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: var(--sp-2); color: var(--muted); font-size: var(--fs-tiny); font-weight: 600;
  transition: color 160ms var(--ease-out), transform 160ms var(--ease-out);
}
.tab:active { transform: scale(.94); }
.tab .tab__icon { font-size: 1.25rem; }
.tab[aria-selected="true"] { color: var(--accentdeep); }

/* ---------- Bunnark (sheet) ---------- */
.sheet-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.4); opacity: 0;
  transition: opacity 200ms var(--ease-out); z-index: 50;
}
.sheet-backdrop[data-open="true"] { opacity: 1; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 51;
  background: var(--surface); border-radius: var(--radius-l) var(--radius-l) 0 0;
  padding: var(--sp-5) var(--sp-4) calc(env(safe-area-inset-bottom) + var(--sp-5));
  max-height: 86vh; overflow-y: auto;
  transform: translateY(100%);
  transition: transform 280ms var(--ease-drawer, cubic-bezier(.32,.72,0,1));
}
.sheet[data-open="true"] { transform: translateY(0); }
.sheet__handle { width: 36px; height: 4px; border-radius: var(--radius-full); background: var(--divider); margin: 0 auto var(--sp-4); }
.sheet__title { font-size: var(--fs-h2); font-weight: 600; margin-bottom: var(--sp-4); }

/* ---------- Toast ---------- */
.toast-root { position: fixed; top: calc(env(safe-area-inset-top) + var(--sp-3)); left: 0; right: 0; display: flex; flex-direction: column; align-items: center; gap: var(--sp-2); z-index: 60; pointer-events: none; }
.toast {
  background: var(--text); color: var(--bg); padding: var(--sp-3) var(--sp-5); border-radius: var(--radius-full);
  font-size: var(--fs-small); font-weight: 600; opacity: 0; transform: translateY(-8px) scale(.97);
  transition: opacity 200ms var(--ease-out), transform 200ms var(--ease-out);
}
.toast[data-show="true"] { opacity: 1; transform: translateY(0) scale(1); }
.toast--danger { background: var(--danger); color: #fff; }

/* ---------- Pockets (lommer) velger ---------- */
.pocket-scroll { display: flex; gap: var(--sp-2); overflow-x: auto; padding-bottom: var(--sp-2); margin-bottom: var(--sp-2); -webkit-overflow-scrolling: touch; }
.pocket-pill { flex-shrink: 0; display: flex; align-items: center; gap: 6px; padding: var(--sp-2) var(--sp-4); border-radius: var(--radius-full); background: var(--surface); font-size: var(--fs-small); font-weight: 600; color: var(--muted); transition: background-color 160ms var(--ease-out), color 160ms var(--ease-out); }
.pocket-pill[aria-selected="true"] { background: var(--accent); color: var(--oncard-text); }

/* ---------- Auth ---------- */
.auth-logo { font-size: 2.4rem; text-align: center; margin-bottom: var(--sp-3); }
.auth-title { font-family: var(--font-serif); font-size: 1.5rem; font-weight: 600; text-align: center; margin-bottom: var(--sp-2); }
.auth-sub { text-align: center; color: var(--muted); margin-bottom: var(--sp-6); }
.auth-switch { text-align: center; margin-top: var(--sp-5); color: var(--muted); font-size: var(--fs-small); }
.auth-switch button { color: var(--accentdeep); font-weight: 600; }

/* ---------- Verktøy ---------- */
.flex-between { display: flex; align-items: center; justify-content: space-between; }
.stack-sm > * + * { margin-top: var(--sp-2); }
.stack > * + * { margin-top: var(--sp-4); }
.center { text-align: center; }
.spinner { width: 20px; height: 20px; border-radius: 50%; border: 2px solid var(--divider); border-top-color: var(--accent); animation: spin 700ms linear infinite; margin: var(--sp-6) auto; }
@keyframes spin { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
  * { transition-duration: 1ms !important; animation-duration: 1ms !important; }
}
