/* Habit Tracker — design tokens (spec §4.9) + Phase 2 login screen.
   Later phases (grid, dashboard, popup) build on these same variables. */

:root {
  /* Surfaces — off-white, never pure white. Less glare over a long session. */
  --bg:            #F7F8FA;
  --surface:       #FFFFFF;
  --surface-sunk:  #F1F5F9;
  --border:        #E5E7EB;
  --border-strong: #CBD5E1;

  /* Text */
  --text:          #0F172A;
  --text-muted:    #64748B;
  --text-faint:    #94A3B8;

  /* Checkbox states (used from Phase 4 onward) */
  --done-bg:       #22C55E;
  --done-fg:       #FFFFFF;
  --done-hover:    #16A34A;
  --missed-bg:     #FEE2E2;
  --missed-border: #FCA5A5;
  --upcoming-bg:   #F1F5F9;
  --weekend-band:  #EEF2F7;

  /* Accent */
  --accent:        #6366F1;
  --accent-soft:   #EEF2FF;

  /* Progress bands */
  --bar-track:     #E5E7EB;
  --bar-low:       #F59E0B;
  --bar-mid:       #3B82F6;
  --bar-high:      #22C55E;

  /* Feedback */
  --danger:        #DC2626;
  --danger-soft:   #FEF2F2;
  --success:       #16A34A;

  /* Type */
  --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;

  /* Geometry */
  --radius:      10px;
  --radius-cell:  7px;
  --cell:        32px;
  --gap:          4px;
  --shadow-card: 0 1px 2px rgba(15,23,42,.04), 0 4px 12px rgba(15,23,42,.05);
}

*, *::before, *::after { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
}

button, input { font-family: inherit; }

/* ── Login screen ─────────────────────────────────────────────────────── */

.auth-shell {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.auth-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-card);
  padding: 36px 32px;
  text-align: center;
}

.auth-mark {
  font-size: 28px;
  margin-bottom: 4px;
}

.auth-title {
  font-size: 20px;
  font-weight: 700;
  margin: 0 0 4px;
}

.auth-sub {
  font-size: 14px;
  color: var(--text-muted);
  margin: 0 0 24px;
}

.auth-field {
  display: block;
  text-align: left;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 6px;
}

.auth-input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  font-size: 15px;
  color: var(--text);
  background: var(--surface);
  margin-bottom: 14px;
}

.auth-input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
  border-color: var(--accent);
}

.auth-button {
  width: 100%;
  padding: 11px 12px;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: #FFFFFF;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

.auth-button:hover { background: #4F46E5; }
.auth-button:disabled { opacity: .6; cursor: default; }

.auth-message {
  margin-top: 16px;
  font-size: 13.5px;
  line-height: 1.5;
  padding: 10px 12px;
  border-radius: 8px;
}

.auth-message.info {
  background: var(--accent-soft);
  color: #3730A3;
}

.auth-message.error {
  background: var(--danger-soft);
  color: var(--danger);
}

/* ── Signed-in shell (placeholder until Phase 4) ─────────────────────── */

.app-shell { display: none; }
.app-shell.visible { display: block; }

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.app-brand {
  font-weight: 700;
  font-size: 16px;
}

.app-user {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 13.5px;
  color: var(--text-muted);
}

.app-signout {
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  padding: 6px 12px;
  border-radius: 7px;
  font-size: 13px;
  cursor: pointer;
}

.app-signout:hover { background: var(--surface-sunk); }

.hidden { display: none !important; }

/* ── Dashboard cards — §4.7, §4.8 ─────────────────────────────────────── */

.dashboard-root { padding: 20px 24px 4px; }

.dash-loading, .dash-empty {
  padding: 24px;
  text-align: center;
  color: var(--text-muted);
  font-size: 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.dash-cards {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 12px;
}
@media (max-width: 900px) {
  .dash-cards { grid-template-columns: repeat(3, 1fr); }
}
@media (max-width: 560px) {
  .dash-cards { grid-template-columns: repeat(2, 1fr); }
}

.dash-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-card);
  padding: 14px 16px;
  min-height: 92px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.dash-card-muted { background: var(--surface-sunk); }

.dash-label {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .05em;
  text-transform: uppercase;
  color: var(--text-faint);
}
.dash-value {
  font-size: 22px;
  font-weight: 700;
  color: var(--text);
  line-height: 1.2;
}
.dash-value-name {
  font-size: 15px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.dash-sub { font-size: 12px; color: var(--text-muted); }
.dash-sub-block { margin-top: 6px; line-height: 1.4; }

.dash-mini-track {
  margin-top: 4px;
  height: 5px;
  border-radius: 3px;
  background: var(--bar-track);
  overflow: hidden;
}
.dash-mini-fill { height: 100%; border-radius: 3px; }
.dash-mini-fill.bar-low  { background: var(--bar-low); }
.dash-mini-fill.bar-mid  { background: var(--bar-mid); }
.dash-mini-fill.bar-high { background: var(--bar-high); }

/* Compared card: week/month deltas + the 12-month year strip */
.dash-card-compared { gap: 6px; }
.dash-compared-row { display: flex; gap: 10px; }
.dash-delta {
  font-size: 13px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.dash-delta-up { color: var(--success); }
.dash-delta-down { color: var(--danger); }
.dash-delta-flat { color: var(--text-faint); }

.dash-year-strip {
  display: flex;
  align-items: flex-end;
  gap: 3px;
  height: 28px;
  margin-top: 4px;
}
.dash-year-bar-track {
  flex: 1;
  height: 100%;
  display: flex;
  align-items: flex-end;
  border-radius: 2px;
}
.dash-year-bar-track.is-current { background: var(--accent-soft); }
.dash-year-bar {
  width: 100%;
  border-radius: 2px 2px 0 0;
  min-height: 2px;
}
.dash-year-bar.bar-low  { background: var(--bar-low); }
.dash-year-bar.bar-mid  { background: var(--bar-mid); }
.dash-year-bar.bar-high { background: var(--bar-high); }
.dash-year-bar.empty    { background: var(--border); }

/* ── Grid toolbar (month/year/today) — §4.2 ──────────────────────────── */

.grid-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 24px;
}

.grid-toolbar-selects {
  display: flex;
  gap: 8px;
  align-items: center;
}

.app-select {
  padding: 7px 10px;
  border: 1px solid var(--border-strong);
  border-radius: 7px;
  background: var(--surface);
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
}

.today-btn {
  padding: 7px 14px;
  border: 1px solid var(--border-strong);
  border-radius: 7px;
  background: var(--surface);
  color: var(--text);
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
}

.today-btn:hover { background: var(--surface-sunk); }

/* ── The grid itself — §4.1, §4.3-§4.5 ───────────────────────────────── */

.grid-scroll {
  overflow-x: auto;
  padding: 0 24px;
}

.grid-table {
  display: grid;
  grid-auto-rows: auto;
  width: max-content;
  border-top: 1px solid var(--border);
}

/* Sticky name column (left) and progress column (right). Every row contributes one of
   each, so position:sticky lines them up perfectly without any row-level markup. */
.name-col, .progress-col {
  position: sticky;
  background: var(--surface);
  z-index: 2;
}
.name-col { left: 0; border-right: 1px solid var(--border); }
.progress-col { right: 0; border-left: 1px solid var(--border); }

.header-cell { height: 100%; }

/* Week-number row (§4.3) */
.week-label {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .03em;
  color: var(--text-faint);
  background: var(--surface-sunk);
  border-bottom: 1px solid var(--border);
  padding: 6px 0;
}

/* Date row (§4.4) + shared day-column wrapper used by every row, header included — this
   is what makes the weekend tint one continuous band instead of a per-cell stripe. */
.day-col {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2px;
}
.day-col.weekend { background: var(--weekend-band); }

.date-col {
  flex-direction: column;
  padding: 6px 0 8px;
  border-bottom: 1px solid var(--border);
  cursor: default;
}
.date-num { font-size: 13px; font-weight: 600; color: var(--text); }
.date-wd { font-size: 10px; font-weight: 600; text-transform: uppercase; color: var(--text-faint); margin-top: 1px; }

/* Habit name cell (§4.5 left block) */
.row-name-col {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border-strong, var(--border));
  min-height: 56px;
}

.drag-handle {
  cursor: grab;
  color: var(--text-faint);
  font-size: 16px;
  line-height: 1;
  user-select: none;
}
.drag-handle:active { cursor: grabbing; }

.row-name-text { flex: 1; min-width: 0; cursor: pointer; border-radius: 6px; padding: 2px 4px; margin: -2px -4px; }
.row-name-text:hover { background: var(--surface-sunk); }
.row-name {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.row-goal { font-size: 12px; color: var(--text-muted); margin-top: 1px; }

.row-menu-btn {
  border: none;
  background: none;
  color: var(--text-faint);
  font-size: 16px;
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 6px;
  line-height: 1;
}
.row-menu-btn:hover { background: var(--surface-sunk); color: var(--text); }

/* Checkbox cells — the four states (§4.5) */
.day-cell {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-cell);
  border: 1px solid transparent;
  background: var(--upcoming-bg);
  color: var(--done-fg);
  font-size: 15px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  padding: 0;
}
.day-cell:hover { filter: brightness(0.97); }

.day-cell.is-missed {
  background: var(--missed-bg);
  border-color: var(--missed-border);
}
.day-cell.is-done {
  background: var(--done-bg);
}
.day-cell.is-done:hover { background: var(--done-hover); }

/* A daily habit with a frequency ("4x per day") that's been started but not finished for
   the day yet — e.g. 2 of 4 glasses of water. Shows the count instead of a checkmark. */
.day-cell.is-partial {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
  font-size: 13px;
}

.day-cell.is-today {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* Progress cell (§4.5 right block, §4.6 bar) */
.progress-col {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 4px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border-strong, var(--border));
  min-height: 56px;
}

.progress-track {
  height: 6px;
  border-radius: 3px;
  background: var(--bar-track);
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  border-radius: 3px;
  transition: width .2s ease;
}
.progress-fill.bar-low  { background: var(--bar-low); }
.progress-fill.bar-mid  { background: var(--bar-mid); }
.progress-fill.bar-high { background: var(--bar-high); }

.progress-pct { font-size: 13px; font-weight: 700; color: var(--text); }
.progress-detail { font-size: 11px; color: var(--text-muted); }

/* Add-habit button + legend */
.add-habit-btn {
  margin: 16px 24px;
  padding: 9px 16px;
  border: 1px dashed var(--border-strong);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.add-habit-btn:hover { background: var(--surface-sunk); }

.grid-legend {
  padding: 0 24px 32px;
  font-size: 12px;
  color: var(--text-faint);
}

/* ── Habit menu popover ───────────────────────────────────────────────── */

.habit-menu {
  position: absolute;
  z-index: 50;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow-card);
  padding: 4px;
  min-width: 140px;
}
.habit-menu button {
  display: block;
  width: 100%;
  text-align: left;
  padding: 8px 10px;
  border: none;
  background: none;
  font-size: 13.5px;
  color: var(--text);
  border-radius: 6px;
  cursor: pointer;
}
.habit-menu button:hover { background: var(--surface-sunk); }
.habit-menu button.danger { color: var(--danger); }

/* ── Add/Edit habit modal ─────────────────────────────────────────────── */

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, .4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 20px;
}
.modal-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-card);
  padding: 28px;
}
.modal-title { font-size: 18px; font-weight: 700; margin: 0 0 18px; }
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 6px;
}
.modal-actions .auth-button { width: auto; padding: 9px 18px; }
.modal-actions .app-signout { padding: 9px 14px; }

/* ── Habit detail popup — §4.6 ────────────────────────────────────────── */

.popup-card { max-width: 460px; }

.popup-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: 18px;
}
.popup-header .modal-title { margin: 0; }
.popup-goal { font-size: 13px; color: var(--text-muted); margin-top: 2px; }

.popup-close {
  border: none;
  background: none;
  font-size: 22px;
  line-height: 1;
  color: var(--text-faint);
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 6px;
}
.popup-close:hover { background: var(--surface-sunk); color: var(--text); }

.popup-controls {
  display: flex;
  gap: 14px;
  margin-bottom: 18px;
}
.popup-field {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
}
.popup-field select {
  padding: 8px 10px;
  border: 1px solid var(--border-strong);
  border-radius: 7px;
  background: var(--surface);
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
}

.popup-body { min-height: 160px; }

.popup-sparkline {
  display: flex;
  align-items: flex-end;
  gap: 3px;
  height: 64px;
  margin-bottom: 14px;
  padding: 4px;
  background: var(--surface-sunk);
  border-radius: 8px;
}
.popup-bar-track {
  flex: 1;
  height: 100%;
  display: flex;
  align-items: flex-end;
}
.popup-bar {
  width: 100%;
  border-radius: 2px 2px 0 0;
  min-height: 2px;
}
.popup-bar.bar-low  { background: var(--bar-low); }
.popup-bar.bar-mid  { background: var(--bar-mid); }
.popup-bar.bar-high { background: var(--bar-high); }

.popup-rows {
  max-height: 220px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  margin-bottom: 14px;
}
.popup-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  font-size: 13px;
  border-bottom: 1px solid var(--border-soft, var(--border));
}
.popup-row:last-child { border-bottom: none; }
.popup-row-label { flex: 0 0 64px; font-weight: 600; color: var(--text); }
.popup-row-detail { flex: 1; color: var(--text-muted); font-variant-numeric: tabular-nums; }
.popup-row-pct { font-weight: 700; color: var(--text); font-variant-numeric: tabular-nums; }

.popup-summary { font-size: 13.5px; color: var(--text-muted); text-align: center; }
.popup-summary strong { color: var(--text); }

/* ── Toast (failed save) ──────────────────────────────────────────────── */

.grid-toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(8px);
  background: var(--text);
  color: #FFFFFF;
  padding: 10px 18px;
  border-radius: 8px;
  font-size: 13.5px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .15s ease, transform .15s ease;
  z-index: 200;
}
.grid-toast.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ── API keys panel — §5.13, §9 Phase 8 ───────────────────────────────── */

.keys-card { max-width: 440px; }

.keys-intro { font-size: 13.5px; color: var(--text-muted); margin: 0 0 16px; line-height: 1.5; }

.keys-list { display: flex; flex-direction: column; gap: 8px; max-height: 320px; overflow-y: auto; }

.key-row {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  position: relative;
}
.key-row-revoked { opacity: .55; }

.key-row-main { display: flex; align-items: baseline; gap: 8px; margin-bottom: 2px; }
.key-label { font-weight: 600; font-size: 13.5px; color: var(--text); }
.key-prefix {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  color: var(--text-faint);
}

.key-row-meta { font-size: 12px; color: var(--text-muted); }

.key-revoke {
  position: absolute;
  top: 10px;
  right: 10px;
  border: none;
  background: none;
  color: var(--danger);
  font-size: 12px;
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 6px;
}
.key-revoke:hover { background: var(--danger-soft); }

.key-reveal {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 13px;
  background: var(--surface-sunk);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 14px;
  word-break: break-all;
  user-select: all;
  margin-bottom: 4px;
}
