:root {
  --bg: #f4f5f9;
  --surface: #ffffff;
  --surface-2: #f0f1f6;
  --text: #1a1b2e;
  --text-dim: #6b6f80;
  --border: #e5e7ef;
  --primary: #6366f1;
  --primary-dim: #eef0ff;
  --danger: #ef4444;
  --success: #22c55e;
  --warn: #f59e0b;
  --shadow: 0 4px 20px rgba(20,20,50,.08);
  --radius: 16px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}
:root[data-theme="dark"] {
  --bg: #0f1020;
  --surface: #1a1b2e;
  --surface-2: #23253c;
  --text: #eef0f7;
  --text-dim: #9a9fb5;
  --border: #2c2e47;
  --primary: #818cf8;
  --primary-dim: #26284a;
  --shadow: 0 4px 24px rgba(0,0,0,.35);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1020; --surface: #1a1b2e; --surface-2: #23253c;
    --text: #eef0f7; --text-dim: #9a9fb5; --border: #2c2e47;
    --primary: #818cf8; --primary-dim: #26284a;
    --shadow: 0 4px 24px rgba(0,0,0,.35);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; padding: 0; height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg); color: var(--text);
  overscroll-behavior-y: none;
}
body { -webkit-font-smoothing: antialiased; }
.hidden { display: none !important; }
button { font-family: inherit; cursor: pointer; border: none; background: none; color: inherit; }
input { font-family: inherit; }

/* Splash */
.splash {
  position: fixed; inset: 0; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 12px;
  background: var(--primary); color: #fff; z-index: 100;
}
.splash-logo { font-size: 56px; animation: pop .5s ease; }
.splash-text { font-size: 22px; font-weight: 700; letter-spacing: .5px; }
@keyframes pop { from { transform: scale(.6); opacity: 0 } to { transform: scale(1); opacity: 1 } }

/* Auth */
.auth {
  position: fixed; inset: 0; display: flex; align-items: center; justify-content: center;
  padding: 24px; background: var(--bg);
}
.auth-card {
  width: 100%; max-width: 380px; background: var(--surface);
  border-radius: 24px; padding: 32px 24px; box-shadow: var(--shadow); text-align: center;
}
.auth-logo { font-size: 44px; }
.auth h1 { margin: 8px 0 4px; font-size: 28px; }
.auth-sub { color: var(--text-dim); font-size: 14px; margin: 0 0 24px; line-height: 1.5; }
.btn {
  width: 100%; padding: 14px; border-radius: 12px; font-size: 15px; font-weight: 600;
  display: flex; align-items: center; justify-content: center; gap: 10px;
  transition: transform .08s ease, opacity .15s;
}
.btn:active { transform: scale(.98); }
.btn-google { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.btn-primary { background: var(--primary); color: #fff; }
.auth-divider { display: flex; align-items: center; gap: 12px; margin: 18px 0; color: var(--text-dim); font-size: 13px; }
.auth-divider::before, .auth-divider::after { content: ""; flex: 1; height: 1px; background: var(--border); }
.auth-form { display: flex; flex-direction: column; gap: 10px; }
.auth-form input {
  padding: 14px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text); font-size: 15px;
}
.auth-toggle { margin-top: 16px; font-size: 14px; color: var(--text-dim); }
.linkbtn { color: var(--primary); font-weight: 600; font-size: 14px; }
.auth-error { color: var(--danger); font-size: 13px; min-height: 18px; margin: 10px 0 0; }
.config-warn { font-size: 12px; color: var(--warn); background: rgba(245,158,11,.1); padding: 10px; border-radius: 10px; margin-top: 14px; }
.config-warn code { background: rgba(0,0,0,.1); padding: 1px 5px; border-radius: 4px; }

/* App shell */
.app { display: flex; flex-direction: column; height: 100dvh; }
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(var(--safe-top) + 12px) 18px 12px;
  background: var(--bg); position: sticky; top: 0; z-index: 20;
}
.topbar-title { font-size: 22px; font-weight: 800; }
.topbar-actions { display: flex; align-items: center; gap: 12px; }
.sync-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--success); box-shadow: 0 0 0 3px rgba(34,197,94,.2); }
.sync-dot.offline { background: var(--warn); box-shadow: 0 0 0 3px rgba(245,158,11,.2); }
.icon-btn { font-size: 22px; padding: 4px; border-radius: 10px; color: var(--text); min-width: 36px; }
.avatar {
  width: 34px; height: 34px; border-radius: 50%; background: var(--primary); color: #fff;
  display: inline-flex; align-items: center; justify-content: center; font-weight: 700; font-size: 15px;
}
.avatar-lg { width: 48px; height: 48px; font-size: 20px; }

/* Content */
.content { flex: 1; overflow-y: auto; padding: 4px 16px 190px; -webkit-overflow-scrolling: touch; }
.section-h { font-size: 14px; color: var(--text-dim); margin: 22px 0 10px; font-weight: 700; }

/* Hero */
.hero { margin: 6px 0 16px; }
.hero-date { color: var(--text-dim); font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .5px; }
.hero-greeting { font-size: 26px; font-weight: 800; margin-top: 2px; }

/* Stat row */
.stat-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.stat {
  background: var(--surface); border-radius: var(--radius); padding: 14px 12px;
  box-shadow: var(--shadow); text-align: center;
}
.stat-num { font-size: 24px; font-weight: 800; }
.stat-label { font-size: 11px; color: var(--text-dim); margin-top: 2px; }

/* Lists / cards */
.list { display: flex; flex-direction: column; gap: 8px; }
.card {
  background: var(--surface); border-radius: var(--radius); padding: 14px;
  box-shadow: var(--shadow); display: flex; align-items: center; gap: 12px;
}
.card-main { flex: 1; min-width: 0; }
.card-title { font-size: 15px; font-weight: 600; line-height: 1.3; word-break: break-word; }
.card-title.done { text-decoration: line-through; color: var(--text-dim); }
.card-sub { font-size: 12px; color: var(--text-dim); margin-top: 3px; display: flex; gap: 8px; flex-wrap: wrap; }
.badge { font-size: 11px; padding: 2px 8px; border-radius: 20px; font-weight: 600; }
.badge.high { background: rgba(239,68,68,.14); color: var(--danger); }
.badge.med { background: rgba(245,158,11,.16); color: var(--warn); }
.badge.low { background: rgba(99,102,241,.14); color: var(--primary); }
.badge.time { background: var(--primary-dim); color: var(--primary); }
.badge.overdue { background: rgba(239,68,68,.14); color: var(--danger); }

.check {
  width: 24px; height: 24px; border-radius: 50%; border: 2px solid var(--border);
  flex-shrink: 0; display: flex; align-items: center; justify-content: center;
  font-size: 14px; color: #fff; transition: all .15s;
}
.check.on { background: var(--success); border-color: var(--success); }
.del { color: var(--text-dim); font-size: 18px; padding: 6px; }
.empty { color: var(--text-dim); font-size: 14px; text-align: center; padding: 20px; }

/* Chips / segments */
.chips { display: flex; gap: 8px; overflow-x: auto; padding: 4px 0 14px; }
.chip {
  padding: 8px 16px; border-radius: 20px; background: var(--surface); font-size: 13px;
  font-weight: 600; white-space: nowrap; box-shadow: var(--shadow); color: var(--text-dim);
}
.chip.active { background: var(--primary); color: #fff; }
.seg { display: flex; background: var(--surface-2); border-radius: 12px; padding: 4px; margin-bottom: 14px; }
.seg-btn { flex: 1; padding: 9px; border-radius: 9px; font-size: 14px; font-weight: 600; color: var(--text-dim); }
.seg-btn.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

.group-h { font-size: 13px; font-weight: 700; color: var(--text-dim); margin: 16px 0 8px; }

/* Calendar */
.cal-head { display: flex; align-items: center; justify-content: space-between; margin: 6px 0 14px; }
.cal-month { font-size: 18px; font-weight: 800; }
.cal-dow { display: grid; grid-template-columns: repeat(7,1fr); text-align: center; color: var(--text-dim); font-size: 12px; font-weight: 700; margin-bottom: 6px; }
.cal-grid { display: grid; grid-template-columns: repeat(7,1fr); gap: 4px; }
.cal-cell {
  aspect-ratio: 1; border-radius: 12px; display: flex; flex-direction: column;
  align-items: center; justify-content: center; font-size: 14px; font-weight: 600;
  background: var(--surface); position: relative;
}
.cal-cell.muted { background: transparent; color: var(--text-dim); opacity: .45; }
.cal-cell.today { outline: 2px solid var(--primary); }
.cal-cell.sel { background: var(--primary); color: #fff; }
.cal-dot { position: absolute; bottom: 6px; width: 5px; height: 5px; border-radius: 50%; background: var(--primary); }
.cal-cell.sel .cal-dot { background: #fff; }

/* Notes */
.notes { display: flex; flex-direction: column; gap: 10px; }
.note {
  background: var(--surface); border-radius: var(--radius); padding: 12px 14px; box-shadow: var(--shadow);
}
.note textarea {
  width: 100%; border: none; background: transparent; color: var(--text); resize: none;
  font-size: 15px; line-height: 1.5; outline: none; min-height: 24px;
}
.note-foot { display: flex; justify-content: space-between; align-items: center; margin-top: 6px; }
.note-time { font-size: 11px; color: var(--text-dim); }
.add-inline {
  width: 100%; padding: 14px; border-radius: var(--radius); border: 2px dashed var(--border);
  color: var(--text-dim); font-weight: 600; font-size: 14px; margin-top: 10px; background: transparent;
}

/* Habits */
.habit-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px,1fr)); gap: 10px; }
.habit-card {
  background: var(--surface); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow);
}
.habit-name { font-size: 14px; font-weight: 700; margin-bottom: 8px; }
.habit-streak { font-size: 12px; color: var(--warn); font-weight: 700; }
.habit-week { display: flex; gap: 5px; margin-top: 8px; }
.habit-day {
  flex: 1; aspect-ratio: 1; border-radius: 8px; background: var(--surface-2);
  display: flex; align-items: center; justify-content: center; font-size: 10px; color: var(--text-dim);
}
.habit-day.on { background: var(--success); color: #fff; }
.habit-day.today { outline: 2px solid var(--primary); }

/* Quick add */
.quickadd {
  position: fixed; left: 0; right: 0; bottom: calc(64px + var(--safe-bottom));
  padding: 8px 12px; z-index: 25;
}
.qa-hint { font-size: 12px; color: var(--primary); font-weight: 600; padding: 0 8px 6px; min-height: 0; }
.qa-hint:empty { display: none; }
.qa-row {
  display: flex; align-items: center; gap: 8px; background: var(--surface);
  border-radius: 26px; padding: 6px 6px 6px 8px; box-shadow: 0 6px 30px rgba(20,20,50,.18);
  border: 1px solid var(--border);
}
.qa-mic { font-size: 20px; width: 40px; height: 40px; border-radius: 50%; flex-shrink: 0; }
.qa-mic.listening { background: var(--danger); animation: pulse 1.2s infinite; }
@keyframes pulse { 0%,100% { box-shadow: 0 0 0 0 rgba(239,68,68,.5) } 50% { box-shadow: 0 0 0 10px rgba(239,68,68,0) } }
.qa-input { flex: 1; border: none; background: transparent; color: var(--text); font-size: 15px; outline: none; min-width: 0; }
.qa-send { background: var(--primary); color: #fff; width: 40px; height: 40px; border-radius: 50%; font-size: 20px; flex-shrink: 0; }

/* Bottom nav */
.bottomnav {
  position: fixed; left: 0; right: 0; bottom: 0; height: calc(64px + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  display: grid; grid-template-columns: repeat(5,1fr);
  background: var(--surface); border-top: 1px solid var(--border); z-index: 24;
}
.nav-btn { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; color: var(--text-dim); }
.nav-btn span { font-size: 20px; }
.nav-btn label { font-size: 10px; font-weight: 600; }
.nav-btn.active { color: var(--primary); }

/* Sheet */
.sheet { position: fixed; inset: 0; z-index: 40; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.4); }
.sheet-panel {
  position: absolute; left: 0; right: 0; bottom: 0; background: var(--surface);
  border-radius: 24px 24px 0 0; padding: 20px 18px calc(24px + var(--safe-bottom));
  box-shadow: var(--shadow); animation: slideup .25s ease;
}
@keyframes slideup { from { transform: translateY(100%) } to { transform: translateY(0) } }
.sheet-user { display: flex; align-items: center; gap: 12px; padding-bottom: 16px; border-bottom: 1px solid var(--border); margin-bottom: 8px; }
.sheet-name { font-weight: 700; font-size: 16px; }
.sheet-email { font-size: 13px; color: var(--text-dim); }
.sheet-item {
  display: flex; align-items: center; gap: 12px; width: 100%; padding: 15px 8px;
  font-size: 15px; font-weight: 600; text-align: left; border-radius: 10px;
}
.sheet-item span { font-size: 18px; }
.sheet-item:active { background: var(--surface-2); }
.sheet-item.danger { color: var(--danger); }
.sheet-version { font-size: 11px; color: var(--text-dim); text-align: center; margin-top: 12px; }

/* Toast */
.toast {
  position: fixed; bottom: calc(140px + var(--safe-bottom)); left: 50%; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 12px 20px; border-radius: 30px;
  font-size: 14px; font-weight: 600; z-index: 60; box-shadow: var(--shadow);
  animation: toastin .2s ease; max-width: 90%; text-align: center;
}
@keyframes toastin { from { opacity: 0; transform: translate(-50%, 10px) } to { opacity: 1; transform: translate(-50%,0) } }

/* Desktop niceties */
@media (min-width: 720px) {
  .content, .quickadd, .bottomnav, .topbar { max-width: 560px; margin-left: auto; margin-right: auto; }
  .quickadd, .bottomnav { left: 50%; transform: translateX(-50%); }
}
