:root {
  color-scheme: light;
  --surface: #fcfcfb;
  --page: #f9f9f7;
  --ink: #0b0b0b;
  --ink-secondary: #52514e;
  --ink-muted: #898781;
  --gridline: #e1e0d9;
  --baseline: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);

  --accent: #2a78d6;
  --accent-hover: #1c5cab;
  --good: #0ca30c;
  --good-text: #006300;
  --critical: #d03b3b;
  --warning: #fab219;

  --radius: 10px;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --surface: #1a1a19;
    --page: #0d0d0d;
    --ink: #ffffff;
    --ink-secondary: #c3c2b7;
    --ink-muted: #898781;
    --gridline: #2c2c2a;
    --baseline: #383835;
    --border: rgba(255, 255, 255, 0.10);

    --accent: #3987e5;
    --accent-hover: #5598e7;
    --good: #0ca30c;
    --good-text: #0ca30c;
    --critical: #e66767;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --surface: #1a1a19;
  --page: #0d0d0d;
  --ink: #ffffff;
  --ink-secondary: #c3c2b7;
  --ink-muted: #898781;
  --gridline: #2c2c2a;
  --baseline: #383835;
  --border: rgba(255, 255, 255, 0.10);

  --accent: #3987e5;
  --accent-hover: #5598e7;
  --good: #0ca30c;
  --good-text: #0ca30c;
  --critical: #e66767;
}

* { box-sizing: border-box; }

html, body {
  max-width: 100%;
  overflow-x: hidden;
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--page);
  color: var(--ink);
  font-size: 15px;
  line-height: 1.5;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.container {
  max-width: 960px;
  margin: 0 auto;
  padding: 24px 20px 64px;
  padding-left: max(20px, env(safe-area-inset-left));
  padding-right: max(20px, env(safe-area-inset-right));
}

.auth-container {
  max-width: 400px;
  margin: 64px auto;
  padding: 0 20px;
}

/* Topbar */
.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar-inner {
  max-width: 960px;
  margin: 0 auto;
  padding: 0 20px;
  padding-left: max(20px, env(safe-area-inset-left));
  padding-right: max(20px, env(safe-area-inset-right));
  display: flex;
  align-items: center;
  gap: 24px;
  height: 56px;
  position: relative;
}
.brand { font-weight: 700; color: var(--ink); font-size: 17px; flex-shrink: 0; }
.brand:hover { text-decoration: none; }

.hamburger {
  display: none;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 4px;
  width: 36px;
  height: 36px;
  margin-left: auto;
  background: none;
  border: none;
  border-radius: 8px;
  padding: 8px;
  cursor: pointer;
  flex-shrink: 0;
}
.hamburger span { display: block; width: 100%; height: 2px; background: var(--ink); border-radius: 2px; }
.hamburger:hover { background: var(--page); }

.menu { display: flex; align-items: center; gap: 24px; flex: 1; min-width: 0; }
.nav { display: flex; gap: 18px; flex: 1; min-width: 0; }
.nav a { color: var(--ink-secondary); font-weight: 500; padding: 6px 0; white-space: nowrap; }
.nav a.active, .nav a:hover { color: var(--ink); text-decoration: none; }
.nav-right { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }
.user-name { color: var(--ink-muted); font-size: 13px; }

/* Cards */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 20px;
}
.card h2 { margin-top: 0; font-size: 18px; }
.card-sub { color: var(--ink-muted); font-size: 13px; margin-top: -8px; margin-bottom: 16px; }

h1 { font-size: 22px; }

/* Forms */
label { display: block; font-weight: 600; font-size: 13px; margin-bottom: 6px; color: var(--ink-secondary); }
input[type=text], input[type=email], input[type=password], input[type=number], input[type=date], input[type=color], select, textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--baseline);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  font-size: 16px;
  margin-bottom: 14px;
  font-family: inherit;
}
input[type=color] { padding: 4px; height: 40px; cursor: pointer; }
.field-row { display: flex; gap: 12px; }
.field-row > * { flex: 1; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 18px;
  border-radius: 8px;
  border: none;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  font-family: inherit;
}
.btn:hover { background: var(--accent-hover); text-decoration: none; }
.btn-block { width: 100%; }
.btn-ghost { background: transparent; color: var(--ink-secondary); border: 1px solid var(--border); }
.btn-ghost:hover { background: var(--page); color: var(--ink); }
.btn-sm { padding: 6px 12px; font-size: 13px; }
.btn-danger { background: var(--critical); }

.form-error { color: var(--critical); font-size: 13px; margin: -8px 0 14px; }
.form-links { text-align: center; margin-top: 16px; font-size: 13px; color: var(--ink-muted); }

/* Flash */
.flash { padding: 12px 16px; border-radius: 8px; margin-bottom: 16px; font-size: 14px; }
.flash-success { background: rgba(12,163,12,0.12); color: var(--good-text); }
.flash-error { background: rgba(208,59,59,0.12); color: var(--critical); }
.flash-info { background: rgba(42,120,214,0.12); color: var(--accent); }

/* Habit checklist */
.habit-list { list-style: none; margin: 0; padding: 0; }
.habit-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid var(--gridline);
}
.habit-row:last-child { border-bottom: none; }
.habit-dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
.habit-name { flex: 1; font-weight: 500; }
.habit-actions { display: flex; gap: 6px; }
.check-btn {
  width: 42px;
  height: 42px;
  border-radius: 8px;
  border: 1px solid var(--baseline);
  background: var(--surface);
  color: var(--ink-muted);
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.check-btn[data-kind="done"].active { background: var(--good); border-color: var(--good); color: #fff; }
.check-btn[data-kind="failed"].active { background: var(--critical); border-color: var(--critical); color: #fff; }
.check-btn:hover { border-color: var(--ink-muted); }

.section-title { font-size: 13px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--ink-muted); margin: 28px 0 10px; }
.section-title:first-child { margin-top: 0; }

.overdue-banner {
  background: rgba(250, 178, 25, 0.14);
  border: 1px solid var(--warning);
  border-radius: 8px;
  padding: 10px 14px;
  margin-bottom: 16px;
  font-size: 13px;
  color: var(--ink-secondary);
}

/* Metrics quick-entry */
.metric-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 12px; }
.metric-item label { margin-bottom: 4px; }
.metric-unit { color: var(--ink-muted); font-weight: 400; }

/* Photos */
.photo-grid { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 8px; }
.photo-thumb { position: relative; width: 96px; height: 96px; border-radius: 8px; overflow: hidden; border: 1px solid var(--border); }
.photo-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.photo-thumb .del { position: absolute; top: 2px; right: 2px; background: rgba(0,0,0,0.55); color: #fff; border: none; border-radius: 6px; width: 22px; height: 22px; cursor: pointer; font-size: 12px; }
.photo-upload-btn { width: 96px; height: 96px; border: 1px dashed var(--baseline); border-radius: 8px; display: flex; align-items: center; justify-content: center; color: var(--ink-muted); cursor: pointer; font-size: 24px; }

/* Habits/metrics management list */
.manage-list { list-style: none; margin: 0; padding: 0; }
.manage-row { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 12px; padding: 10px 0; border-bottom: 1px solid var(--gridline); }
.manage-row:last-child { border-bottom: none; }
.manage-row .meta { color: var(--ink-muted); font-size: 13px; }
.manage-edit-form { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin: 0; flex: 1; min-width: 180px; }
.manage-action-form { margin: 0; }
.manage-name-input { margin: 0; max-width: 220px; flex: 1; min-width: 100px; }
.manage-unit-input { margin: 0; max-width: 80px; width: 80px; }
.manage-color-input { margin: 0; width: 40px; padding: 2px; flex-shrink: 0; }
.badge-archived { font-size: 11px; padding: 2px 8px; border-radius: 999px; background: var(--gridline); color: var(--ink-muted); }

/* Reports */
.filter-bar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 20px; }
#custom-range { flex-wrap: wrap; }
#custom-range input[type=date] { width: auto; flex: 1; min-width: 130px; }
.filter-bar .presets { display: flex; flex-wrap: wrap; gap: 6px; }
.filter-bar .presets button { padding: 7px 12px; border-radius: 999px; border: 1px solid var(--baseline); background: var(--surface); color: var(--ink-secondary); font-size: 13px; cursor: pointer; font-family: inherit; }
.filter-bar .presets button.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.checklist { display: flex; flex-wrap: wrap; gap: 8px; }
.checklist label { display: flex; align-items: center; gap: 6px; font-weight: 500; font-size: 13px; color: var(--ink); background: var(--page); border: 1px solid var(--border); padding: 6px 10px; border-radius: 999px; cursor: pointer; margin: 0; }
.checklist input { margin: 0; width: auto; }
.chart-wrap { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; margin-bottom: 20px; }
.chart-legend { display: flex; flex-wrap: wrap; gap: 14px; margin-top: 10px; font-size: 13px; color: var(--ink-secondary); }
.chart-legend .swatch { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 6px; vertical-align: middle; }
.table-scroll { overflow-x: auto; }
.stat-table { width: 100%; min-width: 460px; border-collapse: collapse; font-size: 13px; }
.stat-table th, .stat-table td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--gridline); }
.stat-table th { color: var(--ink-muted); font-weight: 600; }
.empty-state { color: var(--ink-muted); font-size: 14px; padding: 24px 0; text-align: center; }

.ai-summary-card .ai-summary-output { margin-bottom: 14px; line-height: 1.6; }
.ai-summary-card .ai-summary-output p:first-child { margin-top: 0; }
.ai-summary-note { margin-top: 10px; margin-bottom: 0; }

.day-nav { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 16px; }
.day-nav .date-label { font-weight: 600; font-size: 16px; order: -1; flex-basis: 100%; text-align: center; }
@media (min-width: 480px) {
  .day-nav .date-label { order: 0; flex-basis: auto; }
}

/* Calendar (week view) */
.calendar-scroll { overflow-x: auto; }
.calendar-table { border-collapse: collapse; width: 100%; min-width: 640px; }
.calendar-table th, .calendar-table td { padding: 6px; text-align: center; border-bottom: 1px solid var(--gridline); }
.calendar-table .row-head {
  text-align: left;
  white-space: nowrap;
  padding-right: 14px;
  font-weight: 500;
  font-size: 13px;
  position: sticky;
  left: 0;
  background: var(--surface);
}
.day-head a {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  color: var(--ink-secondary);
  text-decoration: none;
  position: relative;
  padding: 8px 4px;
  margin: -8px -4px;
  border-radius: 8px;
}
.day-head a:hover, .day-head a:active { color: var(--ink); background: var(--page); text-decoration: none; }
.day-head.is-today a { color: var(--accent); font-weight: 700; }
.day-head.is-future a { color: var(--ink-muted); opacity: 0.5; }
.day-head .dow { font-size: 11px; text-transform: uppercase; }
.day-head .dnum { font-size: 15px; }
.incomplete-dot { position: absolute; top: -3px; right: -9px; width: 7px; height: 7px; border-radius: 50%; background: var(--warning); }
.group-row td { background: var(--page); color: var(--ink-muted); font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em; text-align: left; padding: 6px 8px; border-bottom: none; }
.cal-habit-cell {
  width: 32px;
  height: 32px;
  border-radius: 6px;
  border: 1px solid var(--baseline);
  background: var(--surface);
  cursor: pointer;
  font-size: 14px;
  color: var(--ink-muted);
}
.cal-habit-cell.status-done { background: var(--good); border-color: var(--good); color: #fff; }
.cal-habit-cell.status-failed { background: var(--critical); border-color: var(--critical); color: #fff; }
.cal-habit-cell:disabled { opacity: 0.35; cursor: not-allowed; }
.cal-metric-cell { width: 64px; margin: 0 auto; padding: 6px; text-align: center; }

@media (max-width: 640px) {
  .container { padding-top: 16px; }

  .hamburger { display: flex; }

  .menu {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    flex-direction: column;
    align-items: stretch;
    gap: 4px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 8px max(20px, env(safe-area-inset-left)) 16px;
    box-shadow: 0 12px 24px rgba(11, 11, 11, 0.12);
    z-index: 30;
  }
  .menu.open { display: flex; }

  .nav { flex-direction: column; gap: 0; }
  .nav a { padding: 12px 4px; border-bottom: 1px solid var(--gridline); }

  .nav-right {
    justify-content: space-between;
    padding-top: 10px;
  }

  .field-row { flex-direction: column; }

  .card { padding: 16px; }

  .metric-grid { grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); }
}

@media (max-width: 380px) {
  .filter-bar .presets button { padding: 6px 9px; font-size: 12px; }
}
