:root {
  --primary: #4f46e5;
  --primary-dark: #4338ca;
  --primary-light: #ede9fe;
  --success: #16a34a;
  --success-bg: #dcfce7;
  --danger: #dc2626;
  --danger-bg: #fee2e2;
  --warning: #ea580c;
  --bg: #f8fafc;
  --card-bg: #ffffff;
  --text: #0f172a;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --cell-empty: #e2e8f0;
  --cell-done: #22c55e;
  --radius: 14px;
  --radius-sm: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 4px 12px rgba(0,0,0,0.05);
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Hiragino Sans', sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.5;
}

/* ── Header ────────────────────────────────────── */
.app-header {
  background: var(--primary);
  padding: 0.875rem 1rem;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: 0 2px 8px rgba(79,70,229,0.35);
}

.header-inner {
  max-width: 680px;
  margin: 0 auto;
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.5rem;
}

.header-inner h1 {
  color: #fff;
  font-size: 1.2rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  white-space: nowrap;
}

.today-label {
  color: rgba(255,255,255,0.85);
  font-size: 0.8125rem;
  white-space: nowrap;
}

/* ── Main ──────────────────────────────────────── */
.app-main {
  max-width: 680px;
  margin: 0 auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

/* ── Add section ───────────────────────────────── */
.add-row {
  display: flex;
  gap: 0.5rem;
}

.habit-input {
  flex: 1;
  min-width: 0;
  padding: 0.75rem 1rem;
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 1rem;
  color: var(--text);
  background: var(--card-bg);
  outline: none;
  transition: border-color 0.18s;
  -webkit-appearance: none;
}

.habit-input::placeholder {
  color: #94a3b8;
}

.habit-input:focus {
  border-color: var(--primary);
}

.btn {
  padding: 0.75rem 1.125rem;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.18s, transform 0.1s;
  min-height: 44px;
  white-space: nowrap;
}

.btn:active {
  transform: scale(0.97);
}

.btn-primary {
  background: var(--primary);
  color: #fff;
}

.btn-primary:hover {
  background: var(--primary-dark);
}

/* ── Overall stats ─────────────────────────────── */
.overall-stats:empty {
  display: none;
}

.stats-card {
  background: var(--card-bg);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
  box-shadow: var(--shadow);
  display: flex;
  gap: 0;
}

.stat-item {
  flex: 1;
  text-align: center;
  padding: 0 0.5rem;
}

.stat-item + .stat-item {
  border-left: 1px solid var(--border);
}

.stat-value {
  font-size: 1.75rem;
  font-weight: 800;
  color: var(--primary);
  line-height: 1.1;
}

.stat-sub {
  font-size: 1rem;
  font-weight: 500;
  color: var(--text-muted);
}

.stat-label {
  font-size: 0.6875rem;
  color: var(--text-muted);
  margin-top: 0.25rem;
  font-weight: 500;
  letter-spacing: 0.02em;
}

/* ── Habits list ───────────────────────────────── */
.habits-list {
  display: flex;
  flex-direction: column;
  gap: 0.875rem;
}

.empty-message {
  text-align: center;
  color: var(--text-muted);
  padding: 2.5rem 1rem;
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  font-size: 0.9375rem;
}

/* ── Habit card ────────────────────────────────── */
.habit-card {
  background: var(--card-bg);
  border-radius: var(--radius);
  padding: 1rem;
  box-shadow: var(--shadow);
}

.habit-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.625rem;
}

.habit-name {
  font-size: 1.0625rem;
  font-weight: 700;
  word-break: break-word;
}

.delete-btn {
  flex-shrink: 0;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1.25rem;
  line-height: 1;
  cursor: pointer;
  padding: 0.25rem 0.5rem;
  border-radius: var(--radius-sm);
  transition: background 0.15s, color 0.15s;
  min-height: 36px;
  min-width: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.delete-btn:hover {
  background: var(--danger-bg);
  color: var(--danger);
}

/* ── Habit meta: streak + rates ────────────────── */
.habit-meta {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin-bottom: 0.875rem;
}

.streak {
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--text-muted);
}

.streak.active {
  color: var(--warning);
}

.rate-badge {
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0.2rem 0.55rem;
  border-radius: 99px;
  background: var(--bg);
  color: var(--text-muted);
  border: 1px solid var(--border);
}

/* ── Activity grid ─────────────────────────────── */
.grid-wrapper {
  margin-bottom: 0.875rem;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.grid-inner {
  min-width: 0;
}

.day-labels {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 3px;
  margin-bottom: 3px;
}

.day-label {
  text-align: center;
  font-size: 0.6rem;
  color: var(--text-muted);
  font-weight: 600;
  letter-spacing: 0.02em;
}

.activity-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 3px;
}

.grid-cell {
  aspect-ratio: 1 / 1;
  border-radius: 4px;
  background: var(--cell-empty);
  cursor: pointer;
  transition: transform 0.12s, opacity 0.12s;
}

.grid-cell.done {
  background: var(--cell-done);
}

.grid-cell.today {
  outline: 2px solid var(--primary);
  outline-offset: 1px;
}

.grid-cell.future {
  background: var(--cell-empty);
  opacity: 0.35;
  cursor: default;
  pointer-events: none;
}

.grid-cell:not(.future):hover {
  transform: scale(1.2);
  opacity: 0.8;
  z-index: 1;
  position: relative;
}

/* ── Check button ──────────────────────────────── */
.check-btn {
  width: 100%;
  min-height: 44px;
  padding: 0.625rem 1rem;
  border: 2px solid var(--success);
  border-radius: var(--radius-sm);
  background: var(--card-bg);
  color: var(--success);
  font-size: 0.9375rem;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.18s, color 0.18s, transform 0.1s;
}

.check-btn:hover:not(.checked) {
  background: var(--success-bg);
}

.check-btn.checked {
  background: var(--success);
  color: #fff;
  border-color: var(--success);
}

.check-btn:active {
  transform: scale(0.98);
}

/* ── Responsive ────────────────────────────────── */
@media (max-width: 400px) {
  .header-inner h1 {
    font-size: 1.05rem;
  }

  .today-label {
    font-size: 0.75rem;
  }

  .stat-value {
    font-size: 1.4rem;
  }

  .habit-card {
    padding: 0.875rem 0.75rem;
  }
}
