/* === CSS Variables === */
:root {
  --bg: #f5f5f5;
  --card-bg: #ffffff;
  --text: #333333;
  --text-muted: #666666;
  --primary: #4CAF50;
  --primary-dark: #388E3C;
  --danger: #f44336;
  --border: #e0e0e0;
  --shadow: 0 2px 8px rgba(0,0,0,0.1);
  --radius: 8px;
}

.dark-mode {
  --bg: #1a1a2e;
  --card-bg: #16213e;
  --text: #eaeaea;
  --text-muted: #a0a0a0;
  --border: #2a2a4a;
  --shadow: 0 2px 8px rgba(0,0,0,0.3);
}

/* === Base Styles === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
  transition: background 0.3s, color 0.3s;
}

.app {
  max-width: 600px;
  margin: 0 auto;
  padding: 16px;
}

/* === Header === */
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 0;
  margin-bottom: 16px;
}

.header h1 { font-size: 1.5rem; font-weight: 700; }

/* === Cards === */
.card {
  background: var(--card-bg);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}

.card h2 { font-size: 1rem; margin-bottom: 12px; color: var(--text-muted); }

/* === Buttons === */
.btn-primary {
  background: var(--primary);
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: var(--radius);
  cursor: pointer;
  font-weight: 600;
  transition: background 0.2s;
}

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

.btn-secondary {
  background: var(--border);
  color: var(--text);
  border: none;
  padding: 8px 16px;
  border-radius: var(--radius);
  cursor: pointer;
}

.btn-danger {
  background: var(--danger);
  color: white;
  border: none;
  padding: 6px 12px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 0.85rem;
}

.icon-btn {
  background: transparent;
  border: none;
  font-size: 1.25rem;
  cursor: pointer;
  padding: 8px;
  color: var(--text);
  border-radius: var(--radius);
}

.icon-btn:hover { background: var(--border); }

/* === Form === */
.form-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.form-row input, .form-row select {
  flex: 1;
  min-width: 120px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 1rem;
  background: var(--card-bg);
  color: var(--text);
}

.form-row input:focus, .form-row select:focus {
  outline: none;
  border-color: var(--primary);
}

/* === Stats === */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  text-align: center;
}

.stat-value {
  display: block;
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--primary);
}

.stat-label { font-size: 0.75rem; color: var(--text-muted); }

/* === Habits List === */
.habits-section h2 { margin-bottom: 12px; }

.habits-list { display: flex; flex-direction: column; gap: 12px; }

.empty-state {
  text-align: center;
  padding: 32px;
  color: var(--text-muted);
  background: var(--card-bg);
  border-radius: var(--radius);
}

.habit-card {
  background: var(--card-bg);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}

.habit-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 12px;
}

.habit-info h3 { font-size: 1.1rem; margin-bottom: 4px; }

.habit-meta {
  font-size: 0.8rem;
  color: var(--text-muted);
  display: flex;
  gap: 12px;
}

.habit-actions { display: flex; gap: 8px; }

.streak { color: var(--primary); font-weight: 600; }

/* === Smart Recovery System === */
.grace-days {
  color: #2196F3;
  font-weight: 500;
  font-size: 0.75rem;
}

.consistency {
  color: #9C27B0;
  font-weight: 500;
}

.recovery-banner {
  background: linear-gradient(135deg, #ff9800 0%, #f57c00 100%);
  color: white;
  padding: 12px 16px;
  border-radius: var(--radius);
  margin: 12px 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  font-size: 0.9rem;
}

.recovery-banner.active {
  background: linear-gradient(135deg, #2196F3 0%, #1976D2 100%);
}

.recovery-banner.success {
  background: linear-gradient(135deg, #4CAF50 0%, #388E3C 100%);
  animation: success-pulse 0.5s ease;
}

@keyframes success-pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.02); }
}

.btn-recovery {
  background: white;
  color: #f57c00;
  border: none;
  padding: 8px 16px;
  border-radius: var(--radius);
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.2s, box-shadow 0.2s;
}

.btn-recovery:hover {
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(0,0,0,0.2);
}

.challenge-timer {
  background: rgba(255,255,255,0.2);
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 0.8rem;
  font-weight: 600;
}

.habit-meta {
  font-size: 0.8rem;
  color: var(--text-muted);
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  align-items: center;
}

/* === Progress Bar === */
.progress-container {
  margin: 12px 0;
}

.progress-bar {
  height: 8px;
  background: var(--border);
  border-radius: 4px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: var(--primary);
  border-radius: 4px;
  transition: width 0.3s ease;
}

.progress-text {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 4px;
}

/* === Main Calendar === */
.main-calendar {
  margin-bottom: 16px;
}

.main-calendar .calendar-day {
  flex-direction: column;
  gap: 2px;
  padding: 4px;
  cursor: default;
}

.main-calendar .day-num {
  font-size: 0.8rem;
}

.main-calendar .day-dots {
  font-size: 0.65rem;
  background: var(--primary);
  color: white;
  border-radius: 50%;
  width: 16px;
  height: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
}

.main-calendar .calendar-day.partial {
  background: rgba(76, 175, 80, 0.2);
}

.main-calendar .calendar-day.partial .day-dots {
  background: var(--primary);
}

.main-calendar .calendar-day.completed {
  background: var(--primary);
}

.main-calendar .calendar-day.completed .day-dots {
  background: rgba(255,255,255,0.3);
  color: white;
}

/* === Modal Calendar === */
.calendar-section {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 90%;
  max-width: 400px;
  z-index: 100;
  max-height: 90vh;
  overflow-y: auto;
}

.calendar-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.calendar-header h3 { font-size: 1rem; }

.calendar-weekdays {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
  text-align: center;
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-bottom: 8px;
}

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

.calendar-day {
  aspect-ratio: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
  border-radius: var(--radius);
  background: var(--bg);
}

.calendar-day.completed {
  background: var(--primary);
  color: white;
  font-weight: 600;
}

.calendar-day.today {
  border: 2px solid var(--primary);
}

.calendar-day.empty { background: transparent; }

#closeCalendar { width: 100%; margin-top: 16px; }

/* === Dark Mode Toggle === */
.icon-moon { display: none; }
.dark-mode .icon-sun { display: none; }
.dark-mode .icon-moon { display: inline; }

/* === Install Banner === */
.install-banner {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--card-bg);
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  box-shadow: 0 -2px 8px rgba(0,0,0,0.1);
  z-index: 50;
}

.install-banner span { flex: 1; font-size: 0.9rem; }

/* === Responsive === */
@media (max-width: 600px) {
  .app { padding: 12px; }
  .header h1 { font-size: 1.25rem; }
  .stats-grid { grid-template-columns: repeat(3, 1fr); gap: 8px; }
  .stat-value { font-size: 1.25rem; }
  .form-row { flex-direction: column; }
  .form-row input, .form-row select { width: 100%; }
  .habit-header { flex-direction: column; gap: 12px; }
  .habit-actions { width: 100%; justify-content: flex-end; }
}

/* === Info Section === */
.info-section {
  margin-top: 24px;
}

.info-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
  user-select: none;
}

.info-header h2 {
  margin: 0;
  font-size: 1rem;
  color: var(--text);
}

.info-toggle {
  font-size: 0.8rem;
  color: var(--text-muted);
  transition: transform 0.3s;
}

.info-toggle.collapsed {
  transform: rotate(-90deg);
}

.info-content {
  margin-top: 16px;
  overflow: hidden;
  transition: max-height 0.3s ease, opacity 0.3s ease;
}

.info-content.collapsed {
  max-height: 0;
  opacity: 0;
  margin-top: 0;
}

.info-tagline {
  font-size: 1.1rem;
  margin-bottom: 12px;
  color: var(--text);
}

.info-features {
  display: flex;
  flex-direction: column;
  gap: 16px;
  margin-top: 16px;
}

.info-feature {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  padding: 12px;
  background: var(--bg);
  border-radius: var(--radius);
}

.info-icon {
  font-size: 1.5rem;
  line-height: 1;
}

.info-feature strong {
  display: block;
  margin-bottom: 4px;
  color: var(--text);
}

.info-feature p {
  margin: 0;
  font-size: 0.85rem;
  color: var(--text-muted);
}

/* === Standout Section === */
.standout-section {
  background: linear-gradient(135deg, #ff6b35 0%, #f7931e 100%);
  color: white;
}

.standout-section .info-header h2 {
  color: white;
}

.standout-section .info-toggle {
  color: rgba(255,255,255,0.8);
}

.standout-section .info-tagline {
  color: white;
}

.standout-section .info-feature {
  background: rgba(255,255,255,0.15);
}

.standout-section .info-feature strong {
  color: white;
}

.standout-section .info-feature p {
  color: rgba(255,255,255,0.9);
}

/* === Core Loop Section === */
.core-loop-section {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.core-loop-section .info-header h2 {
  color: white;
}

.core-loop-section .info-toggle {
  color: rgba(255,255,255,0.8);
}

.core-loop-section .info-tagline {
  color: white;
}

.core-loop-steps {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 8px;
  margin: 20px 0;
  padding: 16px;
  background: rgba(255,255,255,0.1);
  border-radius: var(--radius);
}

.loop-step {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 12px 16px;
  background: rgba(255,255,255,0.2);
  border-radius: var(--radius);
}

.step-number {
  width: 24px;
  height: 24px;
  background: white;
  color: #764ba2;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.85rem;
}

.step-text {
  font-size: 0.8rem;
  font-weight: 500;
  white-space: nowrap;
}

.loop-arrow {
  color: rgba(255,255,255,0.6);
  font-size: 1.2rem;
}

.loop-summary {
  text-align: center;
  font-size: 0.9rem;
  color: rgba(255,255,255,0.9);
  margin-top: 12px;
  font-style: italic;
}

@media (max-width: 600px) {
  .core-loop-steps {
    gap: 6px;
  }
  .loop-step {
    padding: 8px 10px;
  }
  .loop-arrow {
    font-size: 1rem;
  }
}

/* === Overlay === */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 99;
}
