/* ── Variables ──────────────────────────────────────────────────────────────── */
:root {
  --accent:        #007AFF;
  --accent-dark:   #0051C7;
  --green:         #34C759;
  --red:           #FF3B30;
  --bg:            #F2F2F7;
  --card-bg:       #FFFFFF;
  --header-bg:     rgba(255, 255, 255, 0.85);
  --text:          #1C1C1E;
  --text-secondary:#6E6E73;
  --separator:     #E5E5EA;
  --dot-done:      #34C759;
  --dot-empty:     #E5E5EA;
  --radius:        14px;
  --sheet-radius:  20px;

  --safe-top:    env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left:   env(safe-area-inset-left, 0px);
  --safe-right:  env(safe-area-inset-right, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg:            #000000;
    --card-bg:       #1C1C1E;
    --header-bg:     rgba(28, 28, 30, 0.85);
    --text:          #FFFFFF;
    --text-secondary:#8E8E93;
    --separator:     #2C2C2E;
    --dot-empty:     #2C2C2E;
  }
}

/* ── Reset ──────────────────────────────────────────────────────────────────── */
*, *::before, *::after {
  box-sizing: border-box;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

html {
  overscroll-behavior: none;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Helvetica Neue', sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
}

/* ── App shell ──────────────────────────────────────────────────────────────── */
#app {
  /* Dynamic viewport height: avoids Safari address-bar overflow */
  min-height: 100vh;
  min-height: -webkit-fill-available;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
}

/* ── Header ─────────────────────────────────────────────────────────────────── */
.app-header {
  position: sticky;
  top: 0;
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding-top: calc(var(--safe-top) + 12px);
  padding-bottom: 12px;
  padding-left: 20px;
  padding-right: 16px;
  background: var(--header-bg);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 0.5px solid var(--separator);
}

.header-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

/* ── Today progress ─────────────────────────────────────────────────────────── */
.today-bar {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.today-bar[hidden] { display: none; }

.today-bar-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
}

.today-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
}
.today-label[hidden] { display: none; }
.today-bar.complete > .today-bar-row .today-label { color: var(--green); }

.today-date {
  font-size: 12px;
  color: var(--text-secondary);
  opacity: 0.8;
}

.today-track {
  height: 6px;
  border-radius: 3px;
  background: var(--separator);
  overflow: hidden;
}
.today-track[hidden] { display: none; }

.today-fill {
  height: 100%;
  width: 0;
  border-radius: 3px;
  background: var(--accent);
  transition: width 0.35s cubic-bezier(0.2, 0.8, 0.3, 1), background 0.3s ease;
}
.today-bar.complete > .today-track .today-fill { background: var(--green); }

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.app-title {
  margin: 0;
  font-size: 28px;
  font-weight: 700;
  letter-spacing: -0.5px;
}

.icon-btn[hidden] { display: none; }

.icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  transition: opacity 0.15s ease, transform 0.1s ease;
}
.icon-btn:active {
  opacity: 0.7;
  transform: scale(0.92);
}
.icon-btn.icon-btn-ghost {
  background: var(--separator);
  color: var(--text-secondary);
}
.icon-btn.icon-btn-ghost.active {
  background: var(--accent);
  color: #fff;
}

.cols-label {
  font-size: 14px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

/* ── Habits list ────────────────────────────────────────────────────────────── */
.habits-list {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px;
  padding-bottom: calc(var(--safe-bottom) + 88px);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* ── Habit card ─────────────────────────────────────────────────────────────── */
.habit-card {
  background: var(--card-bg);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.06);
  transition: transform 0.1s ease, opacity 0.3s ease;
}
.habit-card:active { transform: scale(0.99); }

.habit-card.done-today { opacity: 0.55; }

.habit-header {
  display: flex;
  align-items: center;
  gap: 10px;
}

.habit-emoji {
  font-size: 26px;
  line-height: 1;
  flex-shrink: 0;
  width: 36px;
  text-align: center;
}

.habit-info {
  flex: 1;
  min-width: 0;
}

.habit-name {
  font-size: 17px;
  font-weight: 600;
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.habit-streak {
  font-size: 13px;
  color: var(--text-secondary);
  margin: 2px 0 0 0;
}

.habit-streak .streak-val {
  color: var(--habit-color, var(--accent));
  font-weight: 600;
}

.habit-streak .week-done {
  color: var(--habit-color, var(--green));
  font-weight: 600;
}

/* ── Weekly progress pips ───────────────────────────────────────────────────── */
.week-pips {
  display: inline-flex;
  gap: 3px;
  vertical-align: middle;
  margin-right: 2px;
}

.pip {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--dot-empty);
}
.pip.filled { background: var(--habit-color, var(--accent)); }

.freq-tag {
  font-size: 11px;
  font-weight: 500;
  color: var(--habit-color, var(--accent));
  opacity: 0.8;
}

.habit-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.toggle-btn {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  border: 2.5px solid var(--separator);
  background: transparent;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.1s ease;
  color: transparent;
}
.toggle-btn.done {
  background: var(--habit-color, var(--green));
  border-color: var(--habit-color, var(--green));
  color: #fff;
}
.toggle-btn:active { transform: scale(0.88); }

.checkmark {
  width: 20px;
  height: 20px;
}

.burst-particle {
  position: fixed;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  pointer-events: none;
  z-index: 999;
}

/* ── Completion grid (7×7, Mon-aligned weeks) ───────────────────────────────── */
.completion-grid {
  display: grid;
  grid-template-columns: repeat(7, 18px);
  gap: 5px;
  width: fit-content;
  /* Room for the today-dot outline and hover scale, which overflow the dots;
     negative margin cancels it out so the layout doesn't shift */
  padding: 8px;
  margin: -8px;
}

.day-dot {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
  border-radius: 50%;
  background: var(--dot-empty);
  transition: background 0.2s ease, transform 0.1s ease;
  position: relative;
  cursor: pointer;
}
.day-dot:hover { transform: scale(1.25); }
.day-dot:active { transform: scale(0.85); }
.day-dot.done {
  background: var(--habit-color, var(--dot-done));
}
.day-dot.today {
  outline: 2px solid var(--habit-color, var(--accent));
  outline-offset: 2px;
}
.day-dot.today.done {
  background: var(--habit-color, var(--dot-done));
  outline-color: var(--habit-color, var(--dot-done));
}
.day-dot.future {
  opacity: 0.35;
  pointer-events: none;
  cursor: default;
}

/* ── Loading & empty states ─────────────────────────────────────────────────── */
.loading-state {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 60px 0;
}

.spinner {
  width: 28px;
  height: 28px;
  border: 3px solid var(--separator);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.empty-state[hidden] { display: none; }
.login-state[hidden] { display: none; }

.login-state {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 40px;
}
.google-signin {
  margin-top: 24px;
  min-height: 44px;
}
.login-error {
  margin: 16px 0 0 0;
  font-size: 14px;
  color: var(--red);
}
.login-error[hidden] { display: none; }

.account-img {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: block;
}
.account-initial {
  font-size: 14px;
  font-weight: 600;
}

.empty-state {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 40px;
  pointer-events: none;
}
.empty-icon { font-size: 56px; margin-bottom: 16px; }
.empty-title {
  font-size: 20px;
  font-weight: 600;
  margin: 0 0 8px 0;
}
.empty-sub {
  font-size: 15px;
  color: var(--text-secondary);
  margin: 0;
}

/* ── Bottom sheet ───────────────────────────────────────────────────────────── */
.sheet {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: flex-end;
}
.sheet[hidden] { display: none; }

.sheet-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  animation: fadeIn 0.2s ease;
}

.sheet-content {
  position: relative;
  width: 100%;
  background: var(--card-bg);
  border-radius: var(--sheet-radius) var(--sheet-radius) 0 0;
  padding: 12px 20px 24px;
  padding-bottom: calc(var(--safe-bottom) + 24px);
  display: flex;
  flex-direction: column;
  gap: 14px;
  animation: sheetUp 0.28s ease;
}

.sheet-handle {
  width: 36px;
  height: 4px;
  border-radius: 2px;
  background: var(--separator);
  margin: 0 auto 4px;
}

.sheet-title {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}
@keyframes sheetUp {
  from { transform: translateY(60px); opacity: 0; }
  to   { transform: translateY(0);    opacity: 1; }
}

/* ── Emoji picker ───────────────────────────────────────────────────────────── */
.emoji-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.emoji-pick {
  width: 44px;
  height: 44px;
  border-radius: 10px;
  border: 2px solid transparent;
  background: var(--bg);
  font-size: 22px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: border-color 0.15s ease, transform 0.1s ease;
  padding: 0;
}
.emoji-pick:active { transform: scale(0.88); }
.emoji-pick.active { border-color: var(--accent); }

/* ── Inputs ─────────────────────────────────────────────────────────────────── */
input[type="text"] {
  font-size: 16px; /* must be 16px+ to prevent iOS auto-zoom */
  font-family: inherit;
  -webkit-appearance: none;
  appearance: none;
  border-radius: 10px;
  border: 1.5px solid var(--separator);
  padding: 12px 14px;
  background: var(--bg);
  color: var(--text);
  width: 100%;
  outline: none;
  transition: border-color 0.15s ease;
}
input[type="text"]:focus { border-color: var(--accent); }
input[type="text"]::placeholder { color: var(--text-secondary); }

/* ── Compact card ───────────────────────────────────────────────────────────── */
.habits-list.compact-list { gap: 6px; }

.habit-card--compact {
  flex-direction: row;
  align-items: center;
  padding: 4px 8px 4px 12px;
  gap: 0;
  border-radius: 11px;
}
.habit-card--compact .habit-edit-trigger {
  gap: 7px;
  flex: 1;
  min-width: 0;
}
.habit-card--compact .habit-emoji {
  font-size: 17px;
  width: 24px;
}
.habit-card--compact .habit-name {
  font-size: 14px;
}
.habit-card--compact .habit-actions { gap: 4px; }
.habit-card--compact .toggle-btn {
  width: 36px;
  height: 36px;
  border-width: 2px;
}
.habit-card--compact .checkmark {
  width: 15px;
  height: 15px;
}
.compact-streak {
  font-size: 12px;
  font-weight: 600;
  color: var(--habit-color, var(--accent));
  min-width: 20px;
  text-align: right;
  margin-right: 4px;
}

/* ── Grid view ──────────────────────────────────────────────────────────────── */
.habits-list.grid-list {
  display: grid;
  grid-template-columns: repeat(var(--grid-cols, 2), minmax(0, 1fr));
  gap: 10px;
  align-content: start;
}

@media (min-width: 640px) {
  .habits-list.grid-list { --grid-cols: 3; }
}

.habit-card--grid {
  padding: 10px 12px 12px;
  gap: 8px;
}

.grid-head {
  display: flex;
  align-items: center;
  gap: 4px;
}
.habit-card--grid .habit-edit-trigger {
  gap: 6px;
  flex: 1;
  min-width: 0;
}
.habit-card--grid .habit-emoji {
  font-size: 16px;
  width: 20px;
}
.habit-card--grid .habit-name {
  font-size: 13px;
  font-weight: 600;
}
.habit-card--grid .toggle-btn {
  width: 32px;
  height: 32px;
  border-width: 2px;
}
.habit-card--grid .checkmark {
  width: 13px;
  height: 13px;
}

.dot-matrix {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 5px;
}
.dot-matrix .day-dot {
  width: 100%;
  height: auto;
  aspect-ratio: 1;
}

.grid-foot {
  display: flex;
  justify-content: center;
}

/* ── Drag ───────────────────────────────────────────────────────────────────── */
.habit-card.holding {
  transform: scale(0.97);
  transition: transform 0.15s ease;
}

.habit-card.dragging {
  opacity: 0.55;
  box-shadow: 0 8px 28px rgba(0,0,0,0.18);
}

.drag-placeholder {
  border-radius: var(--radius);
  background: var(--separator);
  opacity: 0.5;
}

/* ── Habit edit trigger ──────────────────────────────────────────────────────── */
.habit-edit-trigger {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  min-width: 0;
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  color: inherit;
  font: inherit;
  text-align: left;
}

/* ── Buttons ────────────────────────────────────────────────────────────────── */
.sheet-actions {
  display: flex;
  gap: 10px;
}

.btn {
  flex: 1;
  height: 50px;
  border-radius: 12px;
  border: none;
  font-size: 16px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: opacity 0.15s ease, transform 0.1s ease;
}
.btn:active {
  opacity: 0.75;
  transform: scale(0.97);
}

.btn-primary {
  background: var(--accent);
  color: #fff;
}
.btn-secondary {
  background: var(--separator);
  color: var(--text);
}

/* ── Toast ──────────────────────────────────────────────────────────────────── */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--safe-bottom) + 24px);
  transform: translateX(-50%);
  z-index: 300;
  display: flex;
  align-items: center;
  gap: 14px;
  max-width: min(90vw, 420px);
  padding: 12px 18px;
  border-radius: 14px;
  background: var(--card-bg);
  color: var(--text);
  font-size: 14px;
  font-weight: 500;
  box-shadow: 0 6px 24px rgba(0,0,0,0.22);
  animation: toastUp 0.25s ease;
}
.toast[hidden] { display: none; }

.toast-action {
  border: none;
  background: none;
  color: var(--accent);
  font-size: 14px;
  font-weight: 700;
  font-family: inherit;
  padding: 4px;
  cursor: pointer;
  white-space: nowrap;
}

@keyframes toastUp {
  from { transform: translate(-50%, 12px); opacity: 0; }
  to   { transform: translate(-50%, 0);    opacity: 1; }
}

.btn-danger {
  border: none;
  background: none;
  color: var(--red);
  font-size: 15px;
  font-weight: 600;
  font-family: inherit;
  padding: 6px;
  cursor: pointer;
  transition: opacity 0.15s ease;
}
.btn-danger:active { opacity: 0.6; }

/* ── Emoji section ──────────────────────────────────────────────────────────── */
.emoji-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.emoji-input {
  width: 52px;
  height: 44px;
  border-radius: 10px;
  border: 2px solid var(--separator);
  background: var(--bg);
  font-size: 24px;
  text-align: center;
  padding: 0;
  cursor: text;
  font-family: inherit;
  color: var(--text);
  -webkit-appearance: none;
  appearance: none;
  outline: none;
  flex-shrink: 0;
}
.emoji-input:focus { border-color: var(--accent); }

/* ── Color picker ───────────────────────────────────────────────────────────── */
.color-row {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.color-pick {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 2.5px solid transparent;
  cursor: pointer;
  padding: 0;
  position: relative;
  transition: transform 0.1s ease;
  outline: none;
}
.color-pick:active { transform: scale(0.85); }
.color-pick.active { border-color: var(--text); }
.color-pick.active::after {
  content: '';
  position: absolute;
  inset: 2px;
  border-radius: 50%;
  border: 2px solid rgba(255,255,255,0.75);
}

/* ── Frequency selector ─────────────────────────────────────────────────────── */
.freq-row {
  display: flex;
  background: var(--bg);
  border-radius: 10px;
  padding: 3px;
  gap: 2px;
}

.freq-pick {
  flex: 1;
  padding: 7px 2px;
  border: none;
  border-radius: 7px;
  background: transparent;
  color: var(--text-secondary);
  font-size: 13px;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  transition: background 0.15s, color 0.15s, box-shadow 0.15s;
}
.freq-pick.active {
  background: var(--card-bg);
  color: var(--text);
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

/* ── Reduced motion ─────────────────────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
