/*
 * Calcobase Personal — Phase 1 web.
 *
 * Mobile-first, three breakpoints that matter: a phone browser at ~390px, a tablet
 * at 768px and a desktop at 1440px. The layout changes shape exactly once, at 768px:
 * below it the sections live in a bottom tab bar within thumb reach; above it they
 * move into the top bar and the content gets a maximum width so lines stay readable
 * on a wide screen. There is no third layout to maintain and no horizontal scroll at
 * any width.
 */

:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #16191d;
  --text-muted: #5b636e;
  --border: #dfe3e8;
  --accent: #1f6feb;
  --accent-text: #ffffff;
  --ok: #0f7b46;
  --ok-bg: #e3f5eb;
  --warn: #8a5a00;
  --warn-bg: #fdf1dc;
  --danger: #b3261e;
  --danger-bg: #fbe6e5;
  --info: #274b9c;
  --info-bg: #e7edfb;
  --radius: 12px;
  --shadow: 0 1px 2px rgb(16 24 40 / 6%), 0 4px 12px rgb(16 24 40 / 6%);
  --tabbar-height: 60px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101317;
    --surface: #181c22;
    --text: #e7eaee;
    --text-muted: #9aa3ae;
    --border: #2a3039;
    --accent: #4d8ef7;
    --accent-text: #0b1016;
    --ok: #6ee7a8;
    --ok-bg: #12301f;
    --warn: #f0c06a;
    --warn-bg: #33260d;
    --danger: #f2948d;
    --danger-bg: #3a1614;
    --info: #9dbcfb;
    --info-bg: #17223c;
    --shadow: none;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  overflow-x: hidden;
}

h1, h2, h3, h4 { margin: 0; line-height: 1.25; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.05rem; }
p { margin: 0 0 0.75rem; }
a { color: var(--accent); }
.muted { color: var(--text-muted); font-size: 0.9rem; }

.skip-link {
  position: absolute; left: -9999px; top: 0; z-index: 100;
  background: var(--surface); padding: 0.75rem 1rem; border-radius: var(--radius);
}
.skip-link:focus { left: 1rem; top: 1rem; }

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

/* ── Shell ─────────────────────────────────────────────────────────────────── */
.shell { min-height: 100dvh; display: flex; flex-direction: column; }

.topbar {
  display: flex; align-items: center; gap: 1rem;
  padding: 0.75rem 1rem;
  padding-top: calc(0.75rem + env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
.brand { display: flex; align-items: center; gap: 0.5rem; font-weight: 650; }
.brand-mark {
  display: grid; place-items: center;
  width: 28px; height: 28px; border-radius: 8px;
  background: var(--accent); color: var(--accent-text); font-weight: 700;
}
.brand-name { white-space: nowrap; }
.who { margin-left: auto; color: var(--text-muted); font-size: 0.9rem; max-width: 40%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.topnav { display: none; }

.main {
  flex: 1;
  width: 100%;
  padding: 1rem;
  padding-bottom: calc(var(--tabbar-height) + env(safe-area-inset-bottom) + 1rem);
}

.tabbar {
  position: fixed; inset: auto 0 0 0; z-index: 10;
  display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar-link {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  min-height: var(--tabbar-height);
  color: var(--text-muted); text-decoration: none; font-size: 0.72rem;
}
.nav-icon { display: inline-flex; align-items: center; justify-content: center; }
.nav-icon svg { width: 22px; height: 22px; }
.tabbar-link.active { color: var(--accent); font-weight: 600; }

/* ── Page ──────────────────────────────────────────────────────────────────── */
.page { display: flex; flex-direction: column; gap: 1rem; max-width: 960px; margin: 0 auto; }
.page-head { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; }
.page-action { margin-left: auto; }

.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 1rem; display: flex; flex-direction: column; gap: 0.75rem;
}
.card-title { font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-muted); }

.empty { text-align: center; color: var(--text-muted); padding: 1rem 0; display: flex; flex-direction: column; gap: 0.75rem; align-items: center; }
.empty p { margin: 0; }
.loading { color: var(--text-muted); padding: 2rem 0; text-align: center; }

/* ── Stats ─────────────────────────────────────────────────────────────────── */
.stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 0.75rem; }
.stat {
  display: flex; flex-direction: column; gap: 0.15rem; align-items: flex-start;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 0.9rem 1rem; cursor: pointer; text-align: left; font: inherit; color: inherit;
  box-shadow: var(--shadow);
}
.stat-value { font-size: 1.75rem; font-weight: 700; line-height: 1; }
.stat-label { font-size: 0.82rem; color: var(--text-muted); }
.stat-warn .stat-value { color: var(--warn); }
.stat-danger .stat-value { color: var(--danger); }

/* ── Lists ─────────────────────────────────────────────────────────────────── */
.list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 0.5rem; }
.list-item {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 0.75rem 0.9rem; display: flex; align-items: center; gap: 0.75rem; flex-wrap: wrap;
}
.list-item-alert { border-color: var(--danger); }
.list-button {
  all: unset; cursor: pointer; display: flex; align-items: center; gap: 0.75rem;
  width: 100%; box-sizing: border-box;
}
.list-main { display: flex; flex-direction: column; gap: 0.15rem; min-width: 0; flex: 1; }
.list-title { font-weight: 600; overflow-wrap: anywhere; }
.list-sub { color: var(--text-muted); font-size: 0.85rem; overflow-wrap: anywhere; }
.list-actions { display: flex; gap: 0.4rem; flex-wrap: wrap; margin-left: auto; }
/*
 * On a narrow phone the row's actions and its title compete for the same line, and
 * the title loses — "Insurance expires on 2026-10-03" wraps to four words a line
 * while three buttons sit beside it. Below 560px the actions take a line of their
 * own so the title gets the full width, which is the part a person is reading.
 */
@media (max-width: 559px) {
  .list-actions { width: 100%; margin-left: 0; justify-content: flex-start; }
}
.tag-row { display: flex; gap: 0.35rem; flex-wrap: wrap; margin-top: 0.2rem; }

.mini-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 0.35rem; font-size: 0.9rem; }
.mini-list li { display: flex; align-items: center; gap: 0.4rem; flex-wrap: wrap; }

/* ── Badges ────────────────────────────────────────────────────────────────── */
.badge {
  display: inline-block; padding: 0.15rem 0.5rem; border-radius: 999px;
  font-size: 0.75rem; font-weight: 600; white-space: nowrap;
}
.badge-ok { background: var(--ok-bg); color: var(--ok); }
.badge-warn { background: var(--warn-bg); color: var(--warn); }
.badge-danger { background: var(--danger-bg); color: var(--danger); }
.badge-info { background: var(--info-bg); color: var(--info); }
.badge-muted { background: var(--bg); color: var(--text-muted); border: 1px solid var(--border); }

/* ── Buttons ───────────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 0.4rem;
  min-height: 44px; padding: 0 1rem; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  font: inherit; font-weight: 600; cursor: pointer; text-decoration: none;
}
.btn:disabled { opacity: 0.6; cursor: progress; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--accent); }
.btn-danger { background: var(--danger-bg); border-color: var(--danger-bg); color: var(--danger); }
.btn-danger-text { color: var(--danger); }
.btn-small { min-height: 36px; padding: 0 0.7rem; font-size: 0.85rem; }
.btn-block { width: 100%; }
.row-end { display: flex; gap: 0.5rem; justify-content: flex-end; flex-wrap: wrap; }

/* ── Forms ─────────────────────────────────────────────────────────────────── */
.form { display: flex; flex-direction: column; gap: 0.9rem; }
.field { display: flex; flex-direction: column; gap: 0.3rem; }
.field-label { font-size: 0.85rem; font-weight: 600; }
.field-hint { font-size: 0.8rem; color: var(--text-muted); }
.field-row { display: grid; grid-template-columns: 1fr; gap: 0.9rem; }

input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 10px;
  padding: 0.6rem 0.7rem; min-height: 44px; width: 100%;
  /* 16px keeps iOS Safari from zooming the page when a field takes focus. */
  font-size: 16px;
}
textarea { min-height: 88px; resize: vertical; }
input:disabled { background: var(--bg); color: var(--text-muted); }
input[type="file"] { padding: 0.5rem; }

.form-error { color: var(--danger); font-size: 0.9rem; margin: 0; }

.tabs { display: flex; gap: 0.25rem; background: var(--surface); border: 1px solid var(--border); border-radius: 999px; padding: 0.25rem; align-self: flex-start; }
.tab { border: 0; background: transparent; font: inherit; font-weight: 600; color: var(--text-muted); border-radius: 999px; padding: 0.45rem 1rem; cursor: pointer; min-height: 40px; }
.tab-active { background: var(--accent); color: var(--accent-text); }

/* ── Auth ──────────────────────────────────────────────────────────────────── */
.auth { min-height: 100dvh; display: grid; place-items: center; padding: 1.25rem; }
.auth-card {
  width: 100%; max-width: 380px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 1.5rem; display: flex; flex-direction: column; gap: 1rem;
}
.auth-lead { color: var(--text-muted); margin: 0; }
.auth-form { display: flex; flex-direction: column; gap: 0.9rem; }
.policy-list { margin: 0; padding-left: 1.1rem; display: flex; flex-direction: column; gap: 0.4rem; }

/* ── Sheets / dialogs ──────────────────────────────────────────────────────── */
.backdrop {
  position: fixed; inset: 0; z-index: 50;
  background: rgb(6 10 16 / 55%);
  display: flex; align-items: flex-end; justify-content: center;
  padding: 0; overscroll-behavior: contain;
}
.sheet {
  background: var(--surface); width: 100%; max-height: 92dvh; overflow-y: auto;
  border-radius: var(--radius) var(--radius) 0 0;
  padding: 1.25rem;
  padding-bottom: calc(1.25rem + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 1rem;
}
.sheet-head { display: flex; align-items: center; gap: 1rem; }
.sheet-head h2 { flex: 1; }
.sheet-body { display: flex; flex-direction: column; gap: 1rem; }

.detail { display: flex; flex-direction: column; gap: 0.9rem; }
.detail-head { display: flex; align-items: center; gap: 0.75rem; flex-wrap: wrap; }
.detail-head h3 { overflow-wrap: anywhere; }
.detail-grid { display: grid; grid-template-columns: max-content 1fr; gap: 0.35rem 1rem; margin: 0; font-size: 0.92rem; }
.detail-grid dt { color: var(--text-muted); }
.detail-grid dd { margin: 0; overflow-wrap: anywhere; }
.detail-notes { white-space: pre-wrap; overflow-wrap: anywhere; background: var(--bg); padding: 0.6rem 0.7rem; border-radius: 10px; }
.detail-actions { margin-top: 0.25rem; }

/* ── Toasts ────────────────────────────────────────────────────────────────── */
.toast-host {
  position: fixed; z-index: 60; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--tabbar-height) + env(safe-area-inset-bottom) + 0.75rem);
  width: min(560px, calc(100vw - 2rem));
}
.toast {
  display: flex; align-items: center; gap: 0.75rem;
  background: var(--surface); border: 1px solid var(--border); border-left-width: 4px;
  border-radius: 10px; box-shadow: var(--shadow); padding: 0.7rem 0.9rem;
}
.toast-ok { border-left-color: var(--ok); }
.toast-error { border-left-color: var(--danger); }
.toast span { flex: 1; }
.toast-close { border: 0; background: transparent; color: var(--accent); font: inherit; font-weight: 600; cursor: pointer; min-height: 32px; }

/* ── Tablet and up: sections move into the top bar ─────────────────────────── */
@media (min-width: 768px) {
  .topnav { display: flex; gap: 0.25rem; margin-left: 0.5rem; }
  .topnav-link {
    display: flex; align-items: center; gap: 0.4rem;
    padding: 0.5rem 0.8rem; border-radius: 999px;
    color: var(--text-muted); text-decoration: none; font-weight: 600; font-size: 0.92rem;
    min-height: 40px;
  }
  .topnav-link.active { background: var(--info-bg); color: var(--info); }
  .topnav-link .nav-icon svg { width: 18px; height: 18px; }
  .tabbar { display: none; }
  .main { padding: 1.5rem; padding-bottom: 2rem; }
  .toast-host { bottom: 1.25rem; }
  .stats { grid-template-columns: repeat(4, 1fr); }
  .field-row { grid-template-columns: 1fr 1fr; }
  .backdrop { align-items: center; padding: 1.5rem; }
  .sheet { border-radius: var(--radius); max-width: 560px; max-height: 85dvh; padding-bottom: 1.25rem; }
  .sheet-wide { max-width: 620px; }
}

/* ── Desktop: cap the reading width, keep everything centred ───────────────── */
@media (min-width: 1200px) {
  .topbar { padding-inline: 2rem; }
  .main { padding-inline: 2rem; }
  .page { max-width: 1080px; }
}

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