:root,
:root[data-theme="dark"] {
  --bg: #0e1116;
  --surface: #161b22;
  --surface-2: #1f2630;
  --border: #2a313c;
  --text: #e6edf3;
  --text-dim: #8b949e;
  --accent: #4f8cff;
  --accent-strong: #6aa1ff;
  --danger: #f87171;
  --good: #4ade80;
  --warn: #fbbf24;
  --orange: #fb923c;
  --radius: 14px;
  --shadow: 0 6px 24px rgba(0,0,0,.35);
  color-scheme: dark;
}

:root[data-theme="light"] {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f1f3f5;
  --border: #e2e6eb;
  --text: #1a1d23;
  --text-dim: #6b7280;
  --accent: #2563eb;
  --accent-strong: #3b82f6;
  --danger: #dc2626;
  --good: #16a34a;
  --warn: #ca8a04;
  --orange: #ea580c;
  --shadow: 0 6px 24px rgba(15, 23, 42, .08);
  color-scheme: light;
}

* { box-sizing: border-box; }

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

input, button, textarea, select {
  font-family: inherit;
  color: inherit;
  /* 16px is the iOS no-zoom-on-focus threshold. Never let inputs inherit a smaller
     parent size from .modal label / .profile-card label (which use 13px captions). */
  font-size: 16px;
}

button {
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: 10px;
  padding: 12px 16px;
  min-height: 44px;
  transition: background .12s ease, transform .12s ease, border-color .12s ease;
}

/* Hover styles only on devices with a real cursor — prevents the "stuck dark"
   appearance after tapping on touch screens. */
@media (hover: hover) {
  button:hover { background: var(--border); }
  button.primary:hover { background: var(--accent-strong); }
}

button:active { transform: scale(.98); }

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
  font-weight: 600;
}

button.ghost { background: transparent; }

input[type="text"],
input[type="email"],
input[type="password"],
input[type="number"],
input[type="time"],
input[type="date"],
textarea, select {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
  font-size: 16px;
  line-height: 1.3;
  outline: none;
  min-height: 48px;
}
textarea { min-height: 64px; resize: vertical; }
select { min-height: 48px; padding-right: 36px; }

input:focus, textarea:focus, select:focus {
  border-color: var(--accent);
}

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

#app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

@media (min-width: 600px) {
  #app {
    max-width: 480px;
    margin: 0 auto;
    border-left: 1px solid var(--border);
    border-right: 1px solid var(--border);
    background: var(--bg);
    box-shadow: var(--shadow);
  }
}

/* ------- Auth screens ------- */
.auth-screen {
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: calc(32px + env(safe-area-inset-top, 0px)) calc(20px + env(safe-area-inset-right, 0px)) calc(32px + env(safe-area-inset-bottom, 0px)) calc(20px + env(safe-area-inset-left, 0px));
  max-width: 420px;
  margin: 0 auto;
  width: 100%;
  gap: 14px;
  flex: 1;
}
.auth-screen h1 { margin: 0 0 4px; font-size: 28px; }
.auth-screen p.muted { color: var(--text-dim); margin: 0 0 20px; }
.auth-screen .switch { text-align: center; color: var(--text-dim); margin-top: 8px; }
.auth-screen .error {
  background: rgba(248,113,113,.1);
  border: 1px solid rgba(248,113,113,.3);
  color: var(--danger);
  padding: 10px 12px;
  border-radius: 10px;
  font-size: 14px;
}
.auth-screen .google-btn {
  display: flex; align-items: center; justify-content: center;
  gap: 10px;
  width: 100%;
  margin-top: 4px;
}
.auth-screen .google-btn .g-icon { width: 18px; height: 18px; flex: 0 0 auto; }
.auth-screen .divider {
  display: flex; align-items: center; gap: 10px;
  color: var(--text-dim); font-size: 12px;
  text-transform: uppercase; letter-spacing: 0.08em;
  margin: 6px 0;
}
.auth-screen .divider::before,
.auth-screen .divider::after {
  content: ''; flex: 1; height: 1px; background: var(--border);
}

/* ------- App shell ------- */
.shell {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 100vh;
}
.shell-header {
  position: sticky;
  top: 0;
  z-index: 5;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(14px + env(safe-area-inset-top, 0px)) calc(16px + env(safe-area-inset-right, 0px)) 10px calc(16px + env(safe-area-inset-left, 0px));
  gap: 12px;
}
main#view { flex: 1; }
.burger {
  width: 44px; height: 44px;
  display: grid; place-items: center;
  padding: 0;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 10px;
  flex: 0 0 auto;
}
.burger svg { width: 22px; height: 22px; }

/* Arcade-style HUD: level / streak / points + chunky progress bar. */
.hud {
  flex: 1;
  cursor: pointer;
  display: flex; flex-direction: column; gap: 6px;
  margin-left: 4px;
}
.hud-row {
  display: flex; align-items: baseline; justify-content: space-between;
  gap: 8px;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}
.hud-level {
  font-weight: 700; font-size: 12px;
  color: var(--text-dim);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 2px 8px;
}
.hud-streak {
  font-size: 13px;
  color: var(--orange);
  font-weight: 700;
}
.hud-points {
  font-weight: 800; font-size: 18px;
  color: var(--text);
}
.hud-points small { font-size: 10px; color: var(--text-dim); margin-left: 2px; }
.hud-bar {
  position: relative;
  height: 10px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
}
.hud-bar > i {
  display: block; height: 100%;
  background: linear-gradient(90deg, var(--accent), var(--accent-strong));
  box-shadow: 0 0 12px color-mix(in srgb, var(--accent) 60%, transparent);
  transition: width .35s ease;
}
.avatar {
  width: 38px; height: 38px;
  border-radius: 50%;
  background: linear-gradient(135deg, #4f8cff, #6aa1ff);
  color: white; font-weight: 600;
  display: grid; place-items: center;
  font-size: 14px;
  background-size: cover;
  background-position: center;
}
.avatar.avatar-img { background-color: transparent; }

.tabs {
  position: sticky;
  bottom: 0;
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: space-around;
  padding: 6px 0 calc(6px + env(safe-area-inset-bottom));
  z-index: 4;
}
.tabs button {
  border: 0;
  background: transparent;
  color: var(--text-dim);
  padding: 8px 12px;
  border-radius: 10px;
  min-height: 44px;
  display: flex; align-items: center; justify-content: center;
  flex: 1;
}
.tabs button:hover { background: transparent; }
.tabs button.active { color: var(--accent); }
.tabs button svg {
  width: 26px;
  height: 26px;
  display: block;
}

/* ------- Cards / lists ------- */
.section { padding: 4px 16px 14px; }
.section h2 {
  margin: 16px 0 10px;
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-dim);
}

.empty {
  text-align: center;
  color: var(--text-dim);
  padding: 28px 16px;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
}

/* ------- Day view ------- */
.day-nav {
  display: flex; align-items: center; justify-content: space-between;
  padding: 6px 8px 4px;
}
.day-nav button { width: 44px; font-size: 22px; padding: 6px 12px; }
.day-title { text-align: center; }
.dt-day { font-weight: 700; font-size: 17px; }
.dt-iso { font-size: 12px; }
.score-row {
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  padding: 10px 0 14px;
  gap: 10px;
}
.score-circle { position: relative; }
.score-circle .score-inner {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  pointer-events: none;
}
.score-photo {
  border-radius: 50%;
  background-size: cover;
  background-position: center;
  background-color: var(--surface-2);
}
.score-photo-text {
  display: grid; place-items: center;
  background: linear-gradient(135deg, #4f8cff, #6aa1ff);
  color: white; font-weight: 700; font-size: 32px;
}
.score-text-inside { display: flex; flex-direction: column; align-items: center; }
.score-num { font-size: 38px; font-weight: 800; }
.score-label { color: var(--text-dim); font-size: 13px; }
.score-below { text-align: center; }
.score-below-num {
  font-size: 28px; font-weight: 800; color: var(--text);
}
.score-below-num small { font-size: 14px; font-weight: 600; color: var(--text-dim); }
.score-below-label { color: var(--text-dim); font-size: 13px; margin-top: 2px; }

.bars { display: flex; flex-direction: column; gap: 8px; }

.habit-bar {
  position: relative;
  padding: 0; margin: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--accent);
  border-radius: 12px;
  text-align: left;
  cursor: pointer;
  height: 56px;
  overflow: hidden;
  transition: transform .08s ease, background .12s ease;
}
.habit-bar:hover { background: var(--surface-2); }
.habit-bar.readonly { cursor: default; opacity: .85; }
.hb-fill {
  position: absolute; inset: 0 auto 0 0;
  background: rgba(79,140,255,0.18);
  transition: width .35s ease;
  z-index: 0;
}
.hb-content {
  position: relative; z-index: 1;
  display: flex; align-items: center; justify-content: space-between;
  height: 100%; padding: 8px 14px;
  gap: 12px;
}
.hb-name { font-weight: 600; line-height: 1.15; }
.hb-meta { font-size: 12px; color: var(--text-dim); margin-top: 2px; }
.hb-right { display: flex; align-items: center; gap: 10px; }
.hb-points { font-weight: 700; color: var(--accent-strong); }
.hb-ic { font-size: 14px; line-height: 1; }
.hb-ic.ok  { color: var(--good);   }
.hb-ic.bad { color: var(--danger); }
.hb-ic.mid { color: var(--warn);   }
.hb-ic.dim { color: var(--text-dim); }

/* ------- Completion modal extras ------- */
.completion .bigprogress { display: flex; flex-direction: column; gap: 6px; }
.bp-bar {
  height: 14px; background: var(--surface-2); border-radius: 7px; overflow: hidden;
}
.bp-bar > i { display: block; height: 100%; background: var(--accent); transition: width .3s ease; }
.bp-numbers { font-weight: 600; color: var(--text-dim); font-size: 14px; }
.counter { display: grid; grid-template-columns: 60px 1fr 60px; gap: 8px; align-items: stretch; }
.counter button { font-size: 22px; }
.small { font-size: 12px; }

/* ------- Calendar ------- */
.cal-month {
  margin: 14px 0 22px;
}
.cal-month h3 {
  margin: 0 0 6px; font-size: 16px; color: var(--text);
}
.cal-weekdays {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
  font-size: 11px;
  color: var(--text-dim);
  text-align: center;
  padding: 0 2px 4px;
}
.cal-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
}
.cal-cell {
  background: transparent;
  border: 0;
  border-radius: 8px;
  aspect-ratio: 1;
  padding: 0;
  position: relative;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  color: var(--text);
  cursor: pointer;
}
.cal-cell.empty { cursor: default; pointer-events: none; }
.cal-cell:hover:not(.empty) { background: var(--surface-2); }
.cal-cell.today { box-shadow: inset 0 0 0 2px var(--accent); }
.cal-num { font-size: 11px; color: var(--text-dim); line-height: 1; }
.cal-dot { width: 10px; height: 10px; border-radius: 50%; margin-top: 4px; transition: opacity .2s ease; }

/* ------- Habit list ------- */
.section-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; margin: 12px 0 8px;
}
.section-head h2 { margin: 0; }
.cat {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-dim);
  margin: 18px 0 8px;
}
.habit-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 8px; }
.habit-row {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 14px;
  display: flex; align-items: center; justify-content: space-between;
  cursor: pointer;
  transition: background .12s ease;
}
.habit-row:hover { background: var(--surface-2); }
.hr-name { font-weight: 600; }
.hr-meta { font-size: 12px; }
.hr-points { font-weight: 700; color: var(--accent-strong); }
.muted { color: var(--text-dim); font-weight: normal; }

/* ------- Slide-in sheet (used for habit add/edit) ------- */
.sheet {
  position: fixed; inset: 0;
  background: var(--bg);
  z-index: 60;
  display: flex; flex-direction: column;
  transform: translateX(100%);
  transition: transform .25s ease;
  overflow: hidden;
}
.sheet.in { transform: translateX(0); }
.sheet-inner { display: flex; flex-direction: column; height: 100%; }
.sheet-head {
  display: flex; align-items: center; gap: 12px;
  padding: calc(8px + env(safe-area-inset-top, 0px)) 12px 8px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.sheet-head h2 { margin: 0; font-size: 17px; flex: 1; text-align: center; }
.sheet-head .sheet-back { width: 44px; padding: 0; display: grid; place-items: center; min-height: 44px; }
.sheet-head .sheet-back svg { width: 22px; height: 22px; }
.sheet-head .sheet-spacer { width: 44px; }
.sheet-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px 16px 32px;
  display: flex; flex-direction: column; gap: 12px;
  -webkit-overflow-scrolling: touch;
}
.sheet-body label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--text-dim); }
.sheet-body label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); }
.sheet-body .row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.sheet-foot {
  padding: 16px 16px calc(20px + env(safe-area-inset-bottom, 0px));
  border-top: 1px solid var(--border);
  background: var(--surface);
  box-shadow: 0 -4px 12px rgba(0,0,0,.04);
  display: flex; justify-content: space-between; gap: 8px;
}
.sheet-foot > div { display: flex; gap: 8px; }
.sheet-foot .danger { color: var(--danger); }

@media (min-width: 600px) {
  .sheet { left: auto; width: 480px; max-width: 100vw; right: 0; }
}

/* ------- Side menu drawer (burger) ------- */
.menu-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.35);
  z-index: 55;
  display: flex;
}
.menu-panel {
  width: min(320px, 86vw);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column; gap: 4px;
  padding: calc(20px + env(safe-area-inset-top, 0px)) 14px calc(20px + env(safe-area-inset-bottom, 0px));
  animation: menu-in .22s ease;
}
@keyframes menu-in { from { transform: translateX(-12px); opacity: 0 } to { transform: translateX(0); opacity: 1 } }
.menu-head { display: flex; align-items: center; gap: 12px; padding: 10px 6px 18px; }
.menu-avatar {
  width: 44px; height: 44px; border-radius: 50%;
  background: linear-gradient(135deg, #4f8cff, #6aa1ff);
  color: white; font-weight: 700; font-size: 15px;
  display: grid; place-items: center;
  overflow: hidden;
}
.menu-avatar img { width: 100%; height: 100%; object-fit: cover; }
.menu-name  { font-weight: 600; }
.menu-email { color: var(--text-dim); font-size: 13px; }
.menu-panel button {
  text-align: left;
  background: transparent;
  border: 0;
  padding: 12px 10px;
  border-radius: 10px;
}
.menu-panel button:hover { background: var(--surface-2); }
.menu-panel hr { border: 0; border-top: 1px solid var(--border); margin: 6px 0; }
.menu-theme { display: flex; gap: 6px; padding: 4px 6px; }
.menu-theme button {
  flex: 1;
  text-align: center;
  background: var(--surface-2);
  font-weight: 600;
  font-size: 13px;
  padding: 10px;
}
.menu-theme button.on {
  background: var(--accent);
  color: white;
}
.menu-logout { color: var(--danger); }

/* ------- Modal (legacy, still used by HabitCompletionModal) ------- */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.55);
  display: flex; align-items: flex-end; justify-content: center;
  z-index: 50;
  animation: fade-in .15s ease;
}
@media (min-width: 600px) {
  .modal-backdrop { align-items: center; }
}
@keyframes fade-in { from {opacity:0} to {opacity:1} }
.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px 18px 0 0;
  width: 100%;
  max-width: 480px;
  max-height: 92vh;
  display: flex; flex-direction: column;
  box-shadow: var(--shadow);
  animation: slide-up .18s ease;
}
@media (min-width: 600px) {
  .modal { border-radius: 18px; }
}
@keyframes slide-up { from {transform:translateY(20px); opacity:0} to {transform:translateY(0); opacity:1} }
.modal header {
  padding: 14px 16px; display:flex; align-items:center; justify-content:space-between;
  border-bottom: 1px solid var(--border);
}
.modal header h2 { margin: 0; font-size: 18px; }
.modal .form-grid {
  padding: 14px 16px; overflow-y: auto;
  display: flex; flex-direction: column; gap: 12px;
}
.modal .row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.modal label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--text-dim); }
.modal label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); }
.modal footer {
  padding: 12px 16px; display:flex; justify-content:space-between; gap:8px;
  border-top: 1px solid var(--border);
}
.modal footer > div { display: flex; gap: 8px; }
.modal .danger { color: var(--danger); }

.swatches { display: flex; gap: 8px; flex-wrap: wrap; }
.swatch {
  width: 30px; height: 30px;
  border-radius: 50%;
  padding: 0;
  border: 2px solid transparent;
}
.swatch.on { border-color: var(--text); transform: scale(1.08); }

/* ------- Weekday toggle ------- */
.weekday-toggle {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 6px;
}
.weekday-toggle button {
  padding: 10px 0;
  border-radius: 10px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-dim);
  font-weight: 600;
  font-size: 13px;
}
.weekday-toggle button.on {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

/* ------- Dashed "+ Add" tile ------- */
.add-tile {
  width: 100%;
  background: transparent;
  border: 1.5px dashed var(--border);
  color: var(--text-dim);
  border-radius: 12px;
  padding: 14px 16px;
  display: flex; align-items: center; justify-content: center;
  gap: 10px;
  font-weight: 500;
  transition: border-color .12s ease, color .12s ease, background .12s ease;
  cursor: pointer;
}
.add-tile:hover {
  border-color: var(--accent);
  color: var(--text);
  background: var(--surface);
}
.add-tile .plus {
  display: grid; place-items: center;
  width: 22px; height: 22px;
  border-radius: 50%;
  background: var(--surface-2);
  font-weight: 700;
}
.habit-list li:has(.add-tile) { list-style: none; }

/* ------- Profile page ------- */
.profile-section { display: flex; flex-direction: column; gap: 12px; }
.profile-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  display: flex; flex-direction: column; gap: 10px;
}
.profile-card h3 { margin: 0 0 4px; font-size: 14px; }
.profile-card label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--text-dim); }
.profile-card .row-actions { display: flex; justify-content: flex-end; }

.avatar-card { flex-direction: row; align-items: center; gap: 16px; }
.avatar-large {
  width: 76px; height: 76px;
  border-radius: 50%;
  background: linear-gradient(135deg, #4f8cff, #6aa1ff);
  color: white; font-weight: 700; font-size: 24px;
  display: grid; place-items: center;
  flex: 0 0 auto;
}
.avatar-actions { display: flex; flex-direction: column; gap: 6px; }

.theme-toggle { display: flex; gap: 8px; }
.theme-toggle button {
  flex: 1;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px;
  color: var(--text-dim);
  font-weight: 600;
}
.theme-toggle button.on {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

/* ------- Toast ------- */
.toast-host {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(96px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 8px;
  z-index: 20;
  pointer-events: none;
}
.toast {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 10px 16px;
  font-size: 14px;
  box-shadow: var(--shadow);
  animation: toast-in .2s ease;
}
@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}
