/*
 * Tripsplit — mobile first, one column, thumb-reachable actions.
 *
 * Colours come from a validated data-viz palette so the charts and the UI
 * share one set of hues. Dark mode is a selected set of steps for the dark
 * surface, not an inverted copy of the light one.
 *
 * The page is served under a CSP with `style-src 'self'`, so there are no
 * inline styles anywhere: chart geometry rides on SVG attributes and every
 * dynamic value arrives as a CSS custom property set through CSSOM.
 */

:root {
  color-scheme: light;

  --surface-0: #f4f3f0;
  --surface-1: #fcfcfb;
  --surface-2: #f0efec;
  --border: #dedcd6;
  --border-strong: #c3c0b6;

  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #78766f;

  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --series-5: #e87ba4;
  --series-6: #008300;
  --series-7: #4a3aa7;
  --series-8: #e34948;

  --positive: #2a78d6;
  --negative: #e34948;
  --neutral: #f0efec;

  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --danger: #c22c2b;

  --grid: #e6e4de;

  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 8px 24px rgb(0 0 0 / 8%);

  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, monospace;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;

    --surface-0: #121211;
    --surface-1: #1a1a19;
    --surface-2: #232322;
    --border: #383835;
    --border-strong: #4d4d49;

    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #94938a;

    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;
    --series-6: #008300;
    --series-7: #9085e9;
    --series-8: #e66767;

    --positive: #3987e5;
    --negative: #e66767;
    --neutral: #383835;

    --accent: #3987e5;
    --accent-ink: #06121f;
    --danger: #e66767;

    --grid: #2e2e2c;
    --shadow: 0 1px 2px rgb(0 0 0 / 40%), 0 8px 24px rgb(0 0 0 / 45%);
  }
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
}

body {
  background: var(--surface-0);
  color: var(--text-primary);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
  overflow-wrap: break-word;
}

.app {
  max-width: 720px;
  margin: 0 auto;
  padding: 0 16px calc(96px + env(safe-area-inset-bottom));
}

h1,
h2,
h3 {
  margin: 0;
  line-height: 1.2;
  font-weight: 650;
}

h1 { font-size: 1.4rem; }
h2 { font-size: 1.05rem; }
h3 { font-size: 0.92rem; }

p { margin: 0 0 12px; }

a { color: var(--accent); }

/* ---------------------------------------------------------------- *
 * Header
 * ---------------------------------------------------------------- */

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 18px 0 14px;
  position: sticky;
  top: 0;
  background: var(--surface-0);
  z-index: 20;
}

.topbar__text { min-width: 0; flex: 1; }
.topbar__title {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}
.topbar__title h1 {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.topbar__sub {
  color: var(--text-secondary);
  font-size: 0.82rem;
  margin-top: 2px;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.topbar__actions { display: flex; gap: 8px; flex-shrink: 0; }

/* ---------------------------------------------------------------- *
 * Buttons & controls
 * ---------------------------------------------------------------- */

.btn {
  appearance: none;
  border: 1px solid var(--border-strong);
  background: var(--surface-1);
  color: var(--text-primary);
  font: inherit;
  font-size: 0.9rem;
  font-weight: 550;
  padding: 9px 14px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  min-height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  text-decoration: none;
  transition: background 0.12s ease, border-color 0.12s ease;
}

.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(1px); }

.btn--primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}
.btn--primary:hover { filter: brightness(1.07); background: var(--accent); }

.btn--ghost { border-color: transparent; background: transparent; }
.btn--ghost:hover { background: var(--surface-2); }

.btn--danger { color: var(--danger); }
.btn--danger:hover { background: color-mix(in srgb, var(--danger) 12%, transparent); }

.btn--sm { min-height: 32px; padding: 5px 10px; font-size: 0.82rem; }
.btn--block { width: 100%; }
.btn--icon { padding: 8px; min-width: 40px; }

.btn:disabled { opacity: 0.5; cursor: not-allowed; }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 4px;
}

.field { margin-bottom: 14px; }
.field__label {
  display: block;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 5px;
}
.field__hint {
  font-size: 0.76rem;
  color: var(--text-muted);
  margin-top: 5px;
}
.field__hint--warn { color: var(--series-2); }

input[type="text"],
input[type="number"],
input[type="date"],
input[type="search"],
select,
textarea {
  width: 100%;
  font: inherit;
  font-size: 1rem;
  padding: 10px 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface-1);
  color: var(--text-primary);
  min-height: 44px;
}

textarea { min-height: 68px; resize: vertical; }

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

.row { display: flex; gap: 10px; }
.row > * { flex: 1; min-width: 0; }
.row--tight { gap: 8px; }
.row--amount > :first-child { flex: 2; }

/* ---------------------------------------------------------------- *
 * Cards & lists
 * ---------------------------------------------------------------- */

.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 14px;
}

.card__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}

.empty {
  text-align: center;
  padding: 36px 20px;
  color: var(--text-secondary);
  background: var(--surface-1);
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius);
}
.empty h2 { margin-bottom: 6px; }

.list { list-style: none; margin: 0; padding: 0; }

.list__item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}
.list__item:last-child { border-bottom: none; }

.list__body { flex: 1; min-width: 0; }
.list__title {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.list__meta {
  font-size: 0.8rem;
  color: var(--text-secondary);
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  align-items: center;
  margin-top: 2px;
}
.list__amount {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  text-align: right;
  white-space: nowrap;
}
.list__sub {
  font-size: 0.76rem;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.trip-row {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  font: inherit;
  color: inherit;
  padding: 14px 0;
  cursor: pointer;
}

/* ---------------------------------------------------------------- *
 * Avatars, chips, badges
 * ---------------------------------------------------------------- */

.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 0.78rem;
  font-weight: 650;
  color: #fff;
  flex-shrink: 0;
  background: var(--avatar-color, var(--series-1));
}
.avatar--sm { width: 24px; height: 24px; font-size: 0.66rem; }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border-radius: 999px;
  border: 1px solid var(--border-strong);
  background: var(--surface-1);
  color: var(--text-secondary);
  font-size: 0.85rem;
  font-weight: 550;
  cursor: pointer;
  min-height: 36px;
}
.chip[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}
.chip-group { display: flex; flex-wrap: wrap; gap: 8px; }

/* Categories are a long list; on a phone they scroll sideways instead of
   pushing the rest of the form off the screen. */
.chip-group--scroll {
  flex-wrap: nowrap;
  overflow-x: auto;
  padding-bottom: 4px;
  scrollbar-width: thin;
  -webkit-overflow-scrolling: touch;
}
.chip-group--scroll .chip { flex-shrink: 0; }

.badge {
  display: inline-block;
  font-size: 0.7rem;
  font-weight: 650;
  padding: 2px 7px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-secondary);
  border: 1px solid var(--border);
  white-space: nowrap;
}
.badge--rate { color: var(--series-2); }

.dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  display: inline-block;
  flex-shrink: 0;
  background: var(--dot-color, var(--series-1));
}

.pos { color: var(--positive); }
.neg { color: var(--negative); }
.muted { color: var(--text-muted); }
.tnum { font-variant-numeric: tabular-nums; }

/* ---------------------------------------------------------------- *
 * Tabs
 * ---------------------------------------------------------------- */

.tabs {
  display: flex;
  gap: 4px;
  background: var(--surface-2);
  padding: 4px;
  border-radius: 10px;
  margin-bottom: 16px;
  position: sticky;
  top: 68px;
  z-index: 15;
}

.tabs__btn {
  flex: 1;
  appearance: none;
  border: none;
  background: transparent;
  font: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-secondary);
  padding: 8px 4px;
  border-radius: 7px;
  cursor: pointer;
  min-height: 38px;
}
.tabs__btn[aria-selected="true"] {
  background: var(--surface-1);
  color: var(--text-primary);
  box-shadow: 0 1px 2px rgb(0 0 0 / 10%);
}

/* ---------------------------------------------------------------- *
 * Filters
 * ---------------------------------------------------------------- */

.filters { margin-bottom: 14px; }

.filters__bar { display: flex; gap: 8px; align-items: center; }
.filters__bar input[type="search"] { flex: 1; }

.filters__panel {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}

.filters__summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  font-size: 0.82rem;
  color: var(--text-secondary);
  margin-top: 10px;
}

/* ---------------------------------------------------------------- *
 * Charts
 * ---------------------------------------------------------------- */

.chart { margin-bottom: 6px; }

/* The SVG is drawn at the container's measured pixel width, so it must not be
   rescaled by CSS — that would stretch 11px type along with the geometry.
   max-width is a safety net for the frame between a resize and the repaint. */
.chart svg { display: block; max-width: 100%; overflow: visible; }

.chart__title { font-size: 0.92rem; font-weight: 650; margin-bottom: 2px; }
.chart__note { font-size: 0.78rem; color: var(--text-muted); margin-bottom: 12px; }

.mark { fill: var(--mark-color, var(--series-1)); }
.mark--pos { fill: var(--positive); }
.mark--neg { fill: var(--negative); }
.mark--muted { fill: var(--neutral); }
.mark:hover { filter: brightness(1.12); }

.axis-line { stroke: var(--border-strong); stroke-width: 1; }
.grid-line { stroke: var(--grid); stroke-width: 1; }

.tick {
  font-size: 11px;
  fill: var(--text-muted);
  font-family: var(--font);
}
.mark-label {
  font-size: 11.5px;
  fill: var(--text-secondary);
  font-family: var(--font);
  font-variant-numeric: tabular-nums;
}
.mark-label--strong { fill: var(--text-primary); font-weight: 600; }

/* ---------------------------------------------------------------- *
 * Balances
 * ---------------------------------------------------------------- */

.settle-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.settle-row:last-child { border-bottom: none; }
.settle-row__text { flex: 1; min-width: 160px; }
.settle-arrow { color: var(--text-muted); }

/* ---------------------------------------------------------------- *
 * Sheet (modal)
 * ---------------------------------------------------------------- */

.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgb(0 0 0 / 45%);
  z-index: 40;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.sheet {
  background: var(--surface-1);
  width: 100%;
  max-width: 720px;
  max-height: 92vh;
  overflow-y: auto;
  border-radius: 16px 16px 0 0;
  padding: 20px 16px calc(24px + env(safe-area-inset-bottom));
  box-shadow: var(--shadow);
}

.sheet__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}

.sheet__actions {
  display: flex;
  gap: 10px;
  margin-top: 20px;
  position: sticky;
  bottom: 0;
  background: var(--surface-1);
  padding-top: 12px;
}
.sheet__actions .btn { flex: 1; }

@media (min-width: 560px) {
  .sheet-backdrop { align-items: center; }
  .sheet { border-radius: 16px; margin: 20px; max-height: 88vh; }
}

/* ---------------------------------------------------------------- *
 * Floating action button
 * ---------------------------------------------------------------- */

.fab-group {
  position: fixed;
  right: max(16px, calc(50vw - 344px));
  bottom: calc(20px + env(safe-area-inset-bottom));
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 10px;
}

.fab {
  border-radius: 999px;
  padding: 14px 22px;
  font-size: 0.95rem;
  box-shadow: var(--shadow);
  min-height: 52px;
}

.fab--secondary {
  background: var(--surface-1);
  padding: 14px;
  min-width: 52px;
  font-size: 1.15rem;
}

/* ---------------------------------------------------------------- *
 * Import
 * ---------------------------------------------------------------- */

.dropzone {
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius);
  padding: 18px;
  text-align: center;
  cursor: pointer;
  background: var(--surface-2);
  transition: border-color 0.12s ease, background 0.12s ease;
}
.dropzone:hover,
.dropzone--over {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 8%, var(--surface-2));
}
.dropzone__hint { margin: 0; color: var(--text-secondary); font-size: 0.88rem; }

.thumbs { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; }

.thumb { position: relative; width: 84px; height: 84px; }
.thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
}
.thumb__remove {
  position: absolute;
  top: -7px;
  right: -7px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: none;
  background: var(--text-primary);
  color: var(--surface-1);
  font-size: 0.7rem;
  cursor: pointer;
  line-height: 1;
}

.toggle {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  font-size: 0.88rem;
  cursor: pointer;
  margin-bottom: 12px;
}
.toggle input { width: 20px; height: 20px; min-height: 20px; flex-shrink: 0; margin-top: 1px; }
.toggle .field__hint { margin-top: 2px; }
.toggle--tight { margin: 0; }

.draft {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  margin-bottom: 12px;
  background: var(--surface-2);
}
.draft--off { opacity: 0.45; }

.draft__head { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.draft__head input { font-weight: 600; }

.draft .row { margin-bottom: 8px; }

.draft__currency {
  display: flex;
  align-items: center;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-secondary);
  flex: 0 0 auto;
}

/* Destructive action, separated from the save controls so it is never the
   button someone hits on the way to saving. */
.danger-zone {
  margin-top: 22px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}

.draft__meta {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
  font-size: 0.78rem;
  color: var(--text-muted);
}

/* ---------------------------------------------------------------- *
 * Toast & connection state
 * ---------------------------------------------------------------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(88px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--text-primary);
  color: var(--surface-1);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 0.86rem;
  font-weight: 550;
  z-index: 60;
  box-shadow: var(--shadow);
  max-width: calc(100vw - 32px);
  text-align: center;
}
.toast--error { background: var(--danger); color: #fff; }

.live {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 0.74rem;
  color: var(--text-muted);
}
.live__dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--series-3);
}
.live--off .live__dot { background: var(--text-muted); }

.skeleton {
  padding: 40px 0;
  text-align: center;
  color: var(--text-muted);
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

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