:root {
  color-scheme: dark light;
  --bg: #0e0f12;
  --bg-elev: #16181d;
  --bg-card: #1a1d23;
  --bg-input: #1c1f25;
  --border: #262a31;
  --border-strong: #353a44;
  --text: #f1f3f5;
  --text-muted: #a9b0bb;
  --text-faint: #6f7681;
  --accent: #4f8cff;
  --accent-dim: rgba(79, 140, 255, 0.16);
  --good: #34c77b;
  --good-bg: rgba(52, 199, 123, 0.14);
  --warn: #ffc24a;
  --warn-bg: rgba(255, 194, 74, 0.16);
  --bad: #ff6b6b;
  --bad-bg: rgba(255, 107, 107, 0.16);
  --neutral: #61676f;
  --neutral-bg: rgba(110, 117, 130, 0.16);
  --radius-lg: 18px;
  --radius-md: 12px;
  --radius-sm: 8px;
  --shadow-1: 0 4px 18px rgba(0,0,0,0.35);
  --tap-min: 44px;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f5f7;
    --bg-elev: #ffffff;
    --bg-card: #ffffff;
    --bg-input: #f4f5f7;
    --border: #e1e3e8;
    --border-strong: #cdd1d8;
    --text: #15171b;
    --text-muted: #5b626c;
    --text-faint: #8a909a;
    --shadow-1: 0 4px 20px rgba(15,17,21,0.08);
    --good-bg: rgba(31, 168, 96, 0.16);
    --warn-bg: rgba(217, 154, 18, 0.18);
    --bad-bg: rgba(230, 58, 58, 0.14);
    --neutral-bg: rgba(110, 117, 130, 0.1);
    --accent-dim: rgba(58, 124, 240, 0.12);
  }
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 16px;
  line-height: 1.45;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

body {
  padding-bottom: env(safe-area-inset-bottom);
}

#app {
  min-height: 100vh;
}

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

.loading-screen {
  min-height: 100vh;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 16px;
  color: var(--text-muted);
}
.spinner {
  width: 32px; height: 32px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 800ms linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.auth-screen {
  min-height: 100vh;
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
}
.auth-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 28px 24px;
  max-width: 420px;
  width: 100%;
  box-shadow: var(--shadow-1);
}
.auth-card h1 { margin: 0 0 6px; font-size: 24px; }
.auth-card label { display: block; font-size: 13px; color: var(--text-muted); margin: 16px 0 6px; }
.auth-card input[type="email"],
.auth-card input[type="password"],
.auth-card input[type="text"] {
  width: 100%;
  padding: 13px 14px;
  font-size: 16px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  color: var(--text);
}
.auth-card input:focus { outline: none; border-color: var(--accent); }
.auth-card .checkbox { display: flex; align-items: center; gap: 10px; margin-top: 14px; color: var(--text-muted); font-size: 14px; }
.auth-card .checkbox input { width: 18px; height: 18px; accent-color: var(--accent); }

.btn {
  width: 100%;
  min-height: var(--tap-min);
  padding: 13px 18px;
  background: var(--bg-card);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  font-weight: 600;
  font-size: 16px;
  cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
  gap: 8px;
  transition: opacity 120ms ease;
}
.btn:active { opacity: 0.85; }
.btn.primary { background: var(--accent); color: #fff; border-color: var(--accent); margin-top: 16px; }
.btn.danger { background: var(--bad); color: #fff; border-color: var(--bad); }
.btn.ghost { background: transparent; }
.btn.small { min-height: 36px; padding: 6px 12px; font-size: 13px; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-row { display: flex; gap: 10px; }
.btn-row .btn { width: auto; flex: 1; }

.error { color: var(--bad); font-size: 14px; margin-top: 12px; min-height: 18px; }

.shell { min-height: 100vh; }
.view {
  max-width: 640px;
  margin: 0 auto;
  padding: 20px 18px 96px;
}

.bottom-nav {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  background: var(--bg-elev);
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: space-around;
  padding: 6px 4px calc(env(safe-area-inset-bottom) + 6px);
  z-index: 50;
}
.nav-item {
  flex: 1; min-height: 54px;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px;
  background: transparent; border: 0;
  color: var(--text-faint);
  font-size: 11px;
  cursor: pointer;
  padding: 4px;
}
.nav-item .nav-icon { font-size: 18px; line-height: 1; }
.nav-item.active { color: var(--accent); }

.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 18px;
  margin-bottom: 14px;
}
.card.compact { padding: 14px; }
.card h2 { margin: 0 0 12px; font-size: 16px; color: var(--text); }
.card h3 { margin: 0 0 8px; font-size: 14px; color: var(--text-muted); font-weight: 500; }

.muted { color: var(--text-muted); font-size: 14px; }
.faint { color: var(--text-faint); font-size: 13px; }
.small { font-size: 13px; }
.center { text-align: center; }
.row { display: flex; gap: 12px; align-items: center; }
.row.between { justify-content: space-between; }
.row.wrap { flex-wrap: wrap; }
.grid { display: grid; gap: 12px; }
.grid.cols-2 { grid-template-columns: 1fr 1fr; }
.grid.cols-3 { grid-template-columns: 1fr 1fr 1fr; }

.kpi { display: flex; flex-direction: column; align-items: flex-start; gap: 4px; }
.kpi .value { font-size: 28px; font-weight: 700; letter-spacing: -0.02em; }
.kpi .label { font-size: 12px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.04em; }
.kpi .sub { font-size: 12px; color: var(--text-faint); }

.status-pill {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
}
.status-pill.good { background: var(--good-bg); color: var(--good); }
.status-pill.warn { background: var(--warn-bg); color: var(--warn); }
.status-pill.bad { background: var(--bad-bg); color: var(--bad); }
.status-pill.neutral { background: var(--neutral-bg); color: var(--text-muted); }

.progress {
  background: var(--bg-input);
  border-radius: 999px;
  height: 8px;
  overflow: hidden;
  position: relative;
}
.progress .bar {
  height: 100%;
  border-radius: 999px;
  background: var(--accent);
  transition: width 240ms ease;
}
.progress.good .bar { background: var(--good); }
.progress.warn .bar { background: var(--warn); }
.progress.bad .bar { background: var(--bad); }

.dashboard-hero {
  background: linear-gradient(140deg, rgba(79,140,255,0.18) 0%, transparent 60%), var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 22px 18px;
  margin-bottom: 14px;
}
.dashboard-hero h1 { margin: 0 0 4px; font-size: 26px; font-weight: 700; letter-spacing: -0.02em; }
.dashboard-hero .date { color: var(--text-muted); font-size: 14px; margin-bottom: 16px; }

.big-action {
  display: block; width: 100%;
  min-height: 56px;
  padding: 16px;
  border: 0;
  border-radius: 14px;
  background: var(--accent);
  color: #fff;
  font-size: 17px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: var(--shadow-1);
}
.big-action:active { transform: scale(0.99); }

.fav-buttons { display: grid; gap: 10px; grid-template-columns: 1fr 1fr; }
@media (max-width: 380px) { .fav-buttons { grid-template-columns: 1fr; } }
.fav-button {
  min-height: 78px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 12px 14px;
  text-align: left;
  cursor: pointer;
  display: flex; flex-direction: column; gap: 2px;
  color: var(--text);
  position: relative;
}
.fav-button:active { background: var(--bg-card); }
.fav-button .name { font-weight: 600; font-size: 15px; }
.fav-button .brand { color: var(--text-muted); font-size: 12px; }
.fav-button .mg { color: var(--text-faint); font-size: 12px; margin-top: 4px; }
.fav-button .swatch {
  position: absolute; top: 12px; right: 12px;
  width: 12px; height: 12px; border-radius: 50%;
  background: var(--border-strong);
}
.fav-button .swatch.red { background: #ff5555; }
.fav-button .swatch.blue { background: #4f8cff; }
.fav-button .swatch.green { background: #34c77b; }
.fav-button .swatch.yellow { background: #ffc24a; }
.fav-button .swatch.purple { background: #a26bff; }
.fav-button .swatch.black { background: #2a2e36; }
.fav-button .swatch.white { background: #ddd; }

.list { display: flex; flex-direction: column; gap: 0; }
.list-item {
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px;
}
.list-item:last-child { border-bottom: 0; }
.list-item .meta { color: var(--text-muted); font-size: 13px; }
.list-item .name { font-weight: 500; }

/* Forms */
.form-group { margin-bottom: 14px; }
.form-group label { display: block; font-size: 13px; color: var(--text-muted); margin-bottom: 6px; }
.form-group input, .form-group select, .form-group textarea {
  width: 100%;
  padding: 12px 14px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  color: var(--text);
  font-size: 16px;
  font-family: inherit;
}
.form-group textarea { min-height: 80px; resize: vertical; }
.form-group input:focus, .form-group select:focus, .form-group textarea:focus { outline: none; border-color: var(--accent); }
.form-row { display: flex; gap: 10px; }
.form-row > * { flex: 1; }

/* Chips */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  padding: 6px 12px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 13px;
  color: var(--text-muted);
  cursor: pointer;
}
.chip.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.chip.toggle { user-select: none; }

/* Range filter chips */
.range-chips { overflow-x: auto; padding-bottom: 4px; }
.range-chips::-webkit-scrollbar { display: none; }
.range-chips { -ms-overflow-style: none; scrollbar-width: none; }
.range-chips .chip { white-space: nowrap; flex-shrink: 0; }

/* Bottom sheet */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  z-index: 100;
  display: flex; align-items: flex-end; justify-content: center;
  animation: fadeIn 180ms ease;
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
.sheet {
  background: var(--bg-elev);
  border-radius: 20px 20px 0 0;
  padding: 20px 18px calc(20px + env(safe-area-inset-bottom));
  width: 100%;
  max-width: 640px;
  max-height: 88vh;
  overflow-y: auto;
  animation: slideUp 220ms ease;
}
@keyframes slideUp { from { transform: translateY(40px); } to { transform: translateY(0); } }
.sheet h2 { margin: 0 0 14px; font-size: 18px; }
.sheet .handle { width: 40px; height: 4px; background: var(--border-strong); border-radius: 2px; margin: -8px auto 14px; }
.sheet .close-btn { position: absolute; top: 14px; right: 14px; background: transparent; border: 0; color: var(--text-muted); font-size: 20px; cursor: pointer; }

/* Calendar */
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; }
.cal-weekdays { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; margin-bottom: 6px; }
.cal-weekdays div { text-align: center; color: var(--text-faint); font-size: 11px; padding: 4px 0; }
.cal-day {
  aspect-ratio: 1 / 1.05;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 4px 4px;
  display: flex; flex-direction: column; justify-content: space-between;
  font-size: 11px;
  position: relative;
  cursor: pointer;
}
.cal-day .dnum { color: var(--text-muted); font-size: 11px; font-weight: 500; }
.cal-day .count { font-size: 15px; font-weight: 700; color: var(--text); }
.cal-day.empty { opacity: 0.3; cursor: default; }
.cal-day.today { border-color: var(--accent); }
.cal-day.status-good { border-color: var(--good); background: var(--good-bg); }
.cal-day.status-warn { border-color: var(--warn); background: var(--warn-bg); }
.cal-day.status-bad { border-color: var(--bad); background: var(--bad-bg); }
.cal-day.has-late::after {
  content: ''; position: absolute; top: 4px; right: 4px;
  width: 6px; height: 6px; border-radius: 50%; background: var(--bad);
}

/* Charts */
.chart-wrap { position: relative; width: 100%; }
.chart-svg { display: block; width: 100%; height: auto; }
.chart-legend { display: flex; gap: 12px; flex-wrap: wrap; margin-top: 8px; font-size: 12px; color: var(--text-muted); }
.chart-legend .dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 6px; vertical-align: middle; }

.donut-list { margin-top: 10px; }
.donut-list .row { padding: 6px 0; border-bottom: 1px solid var(--border); font-size: 13px; }
.donut-list .row:last-child { border-bottom: 0; }

/* Toast */
.toast {
  position: fixed;
  left: 50%; bottom: calc(80px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(20px);
  background: var(--bg-elev);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 18px;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 200ms ease, transform 200ms ease;
  z-index: 200;
  max-width: 80%;
  text-align: center;
  box-shadow: var(--shadow-1);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); pointer-events: auto; }
.toast.good { border-color: var(--good); }
.toast.bad { border-color: var(--bad); }

/* Misc */
.divider { height: 1px; background: var(--border); margin: 14px 0; }
.spacer-sm { height: 8px; }
.spacer-md { height: 16px; }

.timer-card { text-align: center; padding: 30px 24px; }
.timer-card .time { font-size: 48px; font-weight: 700; letter-spacing: -0.03em; margin: 10px 0; }
.timer-card .muted { margin-bottom: 18px; }

.late-badge {
  display: inline-block;
  background: var(--bad-bg);
  color: var(--bad);
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 6px;
  margin-left: 6px;
}

.quote {
  font-style: italic;
  color: var(--text-muted);
  font-size: 14px;
  line-height: 1.5;
}

/* Bars */
.bar-list { display: flex; flex-direction: column; gap: 8px; }
.bar-row {
  display: grid;
  grid-template-columns: 60px 1fr 40px;
  align-items: center;
  gap: 8px;
  font-size: 12px;
}
.bar-row .label { color: var(--text-muted); }
.bar-row .track { background: var(--bg-input); border-radius: 4px; height: 10px; overflow: hidden; }
.bar-row .fill { height: 100%; background: var(--accent); border-radius: 4px; }
.bar-row .v { text-align: right; color: var(--text); font-weight: 600; }

.tiny-num { font-size: 11px; color: var(--text-faint); }

input[type="time"], input[type="date"], input[type="datetime-local"] { color-scheme: dark; }
@media (prefers-color-scheme: light) {
  input[type="time"], input[type="date"], input[type="datetime-local"] { color-scheme: light; }
}

.empty-state { text-align: center; padding: 28px 20px; color: var(--text-muted); }
.empty-state .em-icon { font-size: 32px; margin-bottom: 10px; color: var(--text-faint); }

.segmented {
  display: flex;
  background: var(--bg-input);
  border-radius: var(--radius-md);
  padding: 3px;
  gap: 2px;
}
.segmented button {
  flex: 1;
  background: transparent;
  border: 0;
  color: var(--text-muted);
  padding: 8px 10px;
  font-size: 13px;
  border-radius: calc(var(--radius-md) - 4px);
  cursor: pointer;
  font-weight: 500;
}
.segmented button.active { background: var(--bg-elev); color: var(--text); }

.delta-chip {
  display: inline-flex; align-items: center; gap: 3px;
  font-size: 12px; padding: 2px 8px; border-radius: 4px; font-weight: 600;
  background: var(--neutral-bg); color: var(--text-muted);
}
.delta-chip.good { background: var(--good-bg); color: var(--good); }
.delta-chip.bad { background: var(--bad-bg); color: var(--bad); }
