:root {
  --bg: #fafafa;
  --surface: #ffffff;
  --text: #18181b;
  --muted: #71717a;
  --faint: #a1a1aa;
  --border: #e8e8eb;
  --border-strong: #d4d4d8;
  --accent: #15a36e;
  --accent-soft: #e6f6ef;
  --good: #15a36e;
  --good-soft: #e6f6ef;
  --bad: #e2533f;
  --bad-soft: #fcebe7;
  --shadow: 0 1px 3px rgba(0,0,0,.06), 0 8px 24px rgba(0,0,0,.06);
  --radius: 14px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}
.hidden { display: none !important; }

/* ===== Buttons ===== */
.btn {
  appearance: none;
  border: none;
  border-radius: 10px;
  padding: 12px 18px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  transition: background .15s, opacity .15s, transform .05s;
}
.btn:active { transform: scale(.98); }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: #128a5d; }
.btn-outline { background: var(--surface); color: var(--text); border: 1px solid var(--border-strong); }
.btn-ghost { background: transparent; color: var(--accent); }
.btn-danger-ghost { background: transparent; color: var(--bad); }
.btn-google {
  width: 100%;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border-strong);
  display: flex; align-items: center; justify-content: center; gap: 10px;
}

/* ===== Auth ===== */
.auth-screen {
  min-height: 100dvh;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 24px;
}
.auth-card {
  width: 100%; max-width: 360px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px 24px;
  box-shadow: var(--shadow);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark { display: inline-flex; }
.brand h1 { font-size: 26px; margin: 0; letter-spacing: -.02em; }
.auth-sub { color: var(--muted); font-size: 14px; margin: 12px 0 22px; }
.auth-divider { display: flex; align-items: center; gap: 12px; color: var(--faint); font-size: 13px; margin: 18px 0; }
.auth-divider::before, .auth-divider::after { content: ""; flex: 1; height: 1px; background: var(--border); }
.auth-form { display: flex; flex-direction: column; gap: 10px; }
.auth-error { color: var(--bad); font-size: 13px; min-height: 0; }
.auth-error:not(:empty) { min-height: 18px; }
.auth-foot { color: var(--faint); font-size: 12px; margin-top: 22px; }

.text-input {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  font-size: 16px;
  font-family: inherit;
  background: var(--surface);
  color: var(--text);
}
.text-input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea.text-input { resize: vertical; }

/* ===== Topbar ===== */
.app { min-height: 100dvh; display: flex; flex-direction: column; padding-bottom: env(safe-area-inset-bottom); }
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px; padding-top: calc(12px + env(safe-area-inset-top));
  background: rgba(250,250,250,.85);
  backdrop-filter: saturate(180%) blur(10px);
  border-bottom: 1px solid var(--border);
}
.topbar-brand { display: flex; align-items: center; gap: 8px; }
.topbar-title { font-weight: 700; font-size: 18px; letter-spacing: -.02em; }
.icon-btn { background: none; border: none; padding: 0; cursor: pointer; }
.avatar {
  width: 34px; height: 34px; border-radius: 50%;
  background: var(--accent); color: #fff;
  display: inline-flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 15px; text-transform: uppercase;
}
.user-menu {
  position: absolute; right: 14px; top: 58px; z-index: 30;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  box-shadow: var(--shadow); padding: 8px; min-width: 200px;
}
.user-menu-email { font-size: 13px; color: var(--muted); padding: 8px 10px; word-break: break-all; }
.user-menu-item { width: 100%; text-align: left; background: none; border: none; padding: 10px; font-size: 15px; border-radius: 8px; cursor: pointer; color: var(--text); }
.user-menu-item:hover { background: var(--bg); }
.user-menu-ver { font-size: 11px; color: var(--faint); padding: 6px 10px 2px; }

/* ===== Period tabs ===== */
.period-tabs {
  display: flex; gap: 4px;
  margin: 16px auto 0; max-width: 520px; width: calc(100% - 32px);
  background: #f0f0f1; border-radius: 12px; padding: 4px;
}
.period-tab {
  flex: 1; border: none; background: transparent; padding: 9px 0;
  font-size: 14px; font-weight: 600; color: var(--muted);
  border-radius: 9px; cursor: pointer; font-family: inherit;
  transition: background .15s, color .15s;
}
.period-tab.is-active { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.08); }

/* ===== Range nav ===== */
.range-nav {
  display: flex; align-items: center; justify-content: center; gap: 12px;
  margin: 14px auto 8px; max-width: 520px;
}
.nav-arrow {
  width: 36px; height: 36px; border-radius: 50%;
  border: 1px solid var(--border); background: var(--surface);
  font-size: 20px; line-height: 1; color: var(--text); cursor: pointer;
}
.nav-arrow:disabled { opacity: .35; cursor: default; }
.range-label { font-size: 14px; font-weight: 600; color: var(--muted); min-width: 140px; text-align: center; }

/* ===== Grid ===== */
.grid-wrap {
  flex: 1;
  overflow: auto;
  margin: 0 auto; width: 100%; max-width: 760px;
  padding: 0 8px 12px;
  -webkit-overflow-scrolling: touch;
}
.grid { border-collapse: separate; border-spacing: 0; width: 100%; }
.grid th, .grid td { padding: 0; }

.grid thead th {
  position: sticky; top: 0; z-index: 5;
  background: var(--bg);
}
.col-date {
  position: sticky; left: 0; z-index: 6;
  background: var(--bg);
  min-width: 76px; width: 76px;
}
thead .col-date { z-index: 7; }

.hdr-emoji {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 8px 4px 10px; cursor: pointer;
}
.hdr-emoji .e { font-size: 22px; line-height: 1; }
.hdr-emoji .pol { width: 6px; height: 6px; border-radius: 50%; }
.hdr-emoji .pol.good { background: var(--good); }
.hdr-emoji .pol.bad { background: var(--bad); }
.hdr-add {
  font-size: 22px; color: var(--accent); cursor: pointer;
  width: 44px; text-align: center; padding: 8px 0; font-weight: 400;
}

.row-date {
  font-size: 12px; font-weight: 600; color: var(--muted);
  padding: 0 10px 0 4px; text-align: left; white-space: nowrap;
  border-top: 1px solid var(--border);
  height: 52px;
}
.row-date .dnum { font-size: 15px; color: var(--text); font-weight: 700; display: block; }
tr.is-today .row-date .dnum { color: var(--accent); }
tr.is-today .row-date::before {
  content: ""; position: absolute; left: 0; width: 3px; height: 36px; margin-top: 8px;
  background: var(--accent); border-radius: 0 3px 3px 0;
}
.row-date { position: relative; }

.cell {
  border-top: 1px solid var(--border);
  border-left: 1px solid var(--border);
  height: 52px; min-width: 52px;
  text-align: center; vertical-align: middle;
  cursor: pointer; position: relative;
  transition: background .12s;
}
.cell:active { background: #f3f3f4; }
.cell-inner {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 30px; height: 30px; border-radius: 8px;
  font-size: 14px; font-weight: 700; padding: 0 6px;
}
.cell.filled.good .cell-inner { background: var(--good-soft); color: var(--good); }
.cell.filled.bad .cell-inner { background: var(--bad-soft); color: var(--bad); }
.cell.empty .cell-inner { color: var(--faint); font-weight: 400; }
.cell .dot {
  position: absolute; top: 7px; right: 7px;
  width: 6px; height: 6px; border-radius: 50%; background: var(--accent);
}
.cell.bad .dot { background: var(--bad); }

.empty-state { text-align: center; padding: 60px 24px; color: var(--muted); }
.empty-state p { margin-bottom: 16px; }

/* ===== Bottom bar ===== */
.bottom-bar {
  position: sticky; bottom: 0;
  display: flex; gap: 10px;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: rgba(250,250,250,.9);
  backdrop-filter: saturate(180%) blur(10px);
  border-top: 1px solid var(--border);
}
.bottom-bar .btn { flex: 1; }

/* ===== Overlays / sheets ===== */
.overlay {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(0,0,0,.35);
  display: flex; align-items: flex-end; justify-content: center;
  animation: fade .15s ease;
}
@keyframes fade { from { opacity: 0; } }
.sheet {
  width: 100%; max-width: 480px;
  background: var(--surface);
  border-radius: 18px 18px 0 0;
  padding: 18px 18px calc(20px + env(safe-area-inset-bottom));
  max-height: 90dvh; overflow-y: auto;
  animation: slideUp .2s ease;
}
.sheet-tall { min-height: 60dvh; }
@keyframes slideUp { from { transform: translateY(100%); } }
@media (min-width: 540px) {
  .overlay { align-items: center; }
  .sheet { border-radius: 18px; }
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.sheet-head h2 { font-size: 19px; margin: 0; letter-spacing: -.01em; }
.sheet-close { background: none; border: none; font-size: 28px; line-height: 1; color: var(--faint); cursor: pointer; padding: 0 4px; }

.field-label { display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin: 16px 0 8px; }
.field-label .optional { font-weight: 400; color: var(--faint); }
.emoji-preview { font-size: 20px; }

.toggle-row { display: flex; gap: 8px; }
.toggle-opt {
  flex: 1; border: 1px solid var(--border-strong); background: var(--surface);
  padding: 10px; border-radius: 10px; font-size: 14px; font-weight: 600;
  cursor: pointer; color: var(--text); font-family: inherit;
}
.toggle-opt.is-active { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.toggle-opt:disabled { cursor: default; opacity: .55; }
#habitPeriodToggle.is-locked .toggle-opt { opacity: .55; }
#habitPeriodToggle.is-locked .toggle-opt.is-active { opacity: 1; }

.field-hint { font-size: 12px; color: var(--faint); margin: 8px 0 0; line-height: 1.4; }

/* Keyboard focus for the interactive grid controls */
.cell:focus-visible, .hdr-emoji:focus-visible, .hdr-add:focus-visible {
  outline: 2px solid var(--accent); outline-offset: -2px; border-radius: 4px;
}

.emoji-grid {
  display: grid; grid-template-columns: repeat(8, 1fr); gap: 4px;
  max-height: 168px; overflow-y: auto;
  border: 1px solid var(--border); border-radius: 12px; padding: 8px;
}
.emoji-cell {
  aspect-ratio: 1; border: none; background: none; font-size: 22px;
  border-radius: 8px; cursor: pointer; display: flex; align-items: center; justify-content: center;
}
.emoji-cell.is-active { background: var(--accent-soft); }

.sheet-actions { display: flex; gap: 10px; margin-top: 22px; }
.sheet-actions .btn { flex: 1; }

/* ===== Cell modal ===== */
.cell-period { font-size: 14px; color: var(--muted); margin-bottom: 8px; }
.stepper { display: flex; align-items: center; justify-content: center; gap: 22px; margin: 18px 0 8px; }
.step-btn {
  width: 56px; height: 56px; border-radius: 50%;
  border: 1px solid var(--border-strong); background: var(--surface);
  font-size: 28px; line-height: 1; cursor: pointer; color: var(--text);
}
.step-btn:active { background: var(--bg); }
.step-value { font-size: 44px; font-weight: 800; min-width: 70px; text-align: center; letter-spacing: -.02em; }

.photo-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.photo-pick {
  display: inline-flex; align-items: center; gap: 6px;
  border: 1px dashed var(--border-strong); border-radius: 10px;
  padding: 10px 14px; font-size: 14px; font-weight: 600; color: var(--muted); cursor: pointer;
}
.photo-preview { position: relative; }
.photo-preview img { width: 72px; height: 72px; object-fit: cover; border-radius: 10px; display: block; }
.photo-remove {
  position: absolute; top: -8px; right: -8px;
  width: 22px; height: 22px; border-radius: 50%; border: none;
  background: var(--bad); color: #fff; font-size: 15px; line-height: 1; cursor: pointer;
}

/* ===== Stats ===== */
.stats-body { display: flex; flex-direction: column; gap: 14px; }
.stats-overview { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.stat-tile { background: var(--bg); border-radius: 12px; padding: 14px 10px; text-align: center; }
.stat-tile .num { font-size: 24px; font-weight: 800; letter-spacing: -.02em; }
.stat-tile .lbl { font-size: 11px; color: var(--muted); margin-top: 2px; }
.stats-section-title { font-size: 13px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin: 6px 0 -4px; }
.stat-card {
  border: 1px solid var(--border); border-radius: 14px; padding: 14px;
}
.stat-card-head { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.stat-card-head .e { font-size: 24px; }
.stat-card-head .nm { font-weight: 700; font-size: 15px; flex: 1; }
.pill { font-size: 11px; font-weight: 700; padding: 3px 8px; border-radius: 999px; }
.pill.good { background: var(--good-soft); color: var(--good); }
.pill.bad { background: var(--bad-soft); color: var(--bad); }
.stat-metrics { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 10px; }
.stat-metric { text-align: center; }
.stat-metric .v { font-size: 18px; font-weight: 800; }
.stat-metric .k { font-size: 11px; color: var(--muted); }
.bar-track { height: 8px; background: var(--bg); border-radius: 6px; overflow: hidden; }
.bar-fill { height: 100%; border-radius: 6px; }
.bar-fill.good { background: var(--good); }
.bar-fill.bad { background: var(--bad); }
.bar-caption { font-size: 11px; color: var(--muted); margin-top: 6px; }
.stats-empty { text-align: center; color: var(--muted); padding: 40px 0; }

/* ===== Toast ===== */
.toast {
  position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%);
  background: #18181b; color: #fff; padding: 10px 16px; border-radius: 10px;
  font-size: 14px; z-index: 80; box-shadow: var(--shadow);
  animation: fade .15s ease;
}
