:root {
  --bg: #f5f6fa;
  --card: #ffffff;
  --text: #1f2333;
  --muted: #6b7280;
  --border: #e5e7eb;
  --primary: #4f46e5;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

:root[data-theme="dark"] {
  --bg: #0f1117;
  --card: #1a1d27;
  --text: #e6e8ee;
  --muted: #9aa1ad;
  --border: #2b303c;
  --primary: #7c79f0;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
}

* { box-sizing: border-box; }

html, body { background: var(--bg); }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

.app-header {
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding: 1rem 1rem 0;
  position: sticky;
  top: 0;
  z-index: 10;
}

.header-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}
.app-header h1 {
  margin: 0 0 0.75rem;
  font-size: 1.4rem;
}
.theme-toggle {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 50%;
  width: 2.2rem;
  height: 2.2rem;
  font-size: 1.1rem;
  cursor: pointer;
  margin-bottom: 0.75rem;
  line-height: 1;
}
.theme-toggle:hover { border-color: var(--primary); }

.tabs {
  display: flex;
  gap: 0.25rem;
  overflow-x: auto;
  scrollbar-width: none; /* Firefox */
}
.tabs::-webkit-scrollbar { display: none; } /* Chrome/Safari */

.tab {
  flex: 0 0 auto;
  white-space: nowrap;
  background: none;
  border: none;
  padding: 0.6rem 0.9rem;
  font-size: 0.95rem;
  cursor: pointer;
  color: var(--muted);
  border-bottom: 3px solid transparent;
}

.tab.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
  font-weight: 600;
}

main {
  max-width: 720px;
  margin: 0 auto;
  padding: 1.25rem 1rem 4rem;
}

.view { display: none; }
.view.active { display: block; }

.hidden { display: none !important; }

.empty {
  color: var(--muted);
  text-align: center;
  margin-top: 2rem;
}

/* Date bar */
.date-bar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}

.nav-btn {
  width: 2.2rem;
  height: 2.2rem;
  border: 1px solid var(--border);
  background: var(--card);
  border-radius: 8px;
  font-size: 1.3rem;
  line-height: 1;
  cursor: pointer;
  color: var(--text);
}

.date-bar input {
  flex: 1;
  padding: 0.5rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 1rem;
  background: var(--card);
  color: var(--text);
}

.link-btn {
  background: none;
  border: none;
  color: var(--primary);
  cursor: pointer;
  font-size: 0.9rem;
  white-space: nowrap;
}

.day-label {
  color: var(--muted);
  margin: 0.25rem 0 1rem;
  text-transform: capitalize;
}

/* Checklist (day view) */
.habit-checklist {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.habit-item {
  display: flex;
  align-items: center;
  gap: 0.8rem;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.9rem 1rem;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: transform 0.05s ease;
  user-select: none;
}

.habit-item:active { transform: scale(0.99); }

.habit-item .dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  flex-shrink: 0;
}

.habit-item .name { flex: 1; }

.checkbox {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 2px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  color: #fff;
  font-size: 0.95rem;
}

.habit-item.done {
  border-color: transparent;
}
.habit-item.done .name {
  color: var(--muted);
  text-decoration: line-through;
}

/* Month summary */
.month-stats {
  display: flex;
  gap: 0.75rem;
  margin: 1rem 0;
}

.stat-card {
  flex: 1;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.9rem;
  text-align: center;
  box-shadow: var(--shadow);
}
.stat-card .big {
  font-size: 1.6rem;
  font-weight: 700;
}
.stat-card .label {
  font-size: 0.8rem;
  color: var(--muted);
}

.summary-row {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.9rem 1rem;
  margin-bottom: 0.6rem;
  box-shadow: var(--shadow);
}

.summary-row .top {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin-bottom: 0.5rem;
}
.summary-row .dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
}
.summary-row .name { flex: 1; font-weight: 600; }
.summary-row .count { color: var(--muted); font-size: 0.9rem; }

.bar {
  height: 8px;
  background: var(--border);
  border-radius: 4px;
  overflow: hidden;
}
.bar > span {
  display: block;
  height: 100%;
  border-radius: 4px;
}

/* Synthèse view */
.habit-filter {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin: 1rem 0;
}
.filter-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.4rem 0.8rem;
  border-radius: 999px;
  border: 1.5px solid var(--border);
  background: var(--card);
  cursor: pointer;
  font-size: 0.9rem;
  user-select: none;
  color: var(--muted);
}
.filter-chip .chip-dot {
  width: 11px;
  height: 11px;
  border-radius: 50%;
}
.filter-chip.active {
  color: var(--text);
  font-weight: 600;
}

.chart-wrap {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  box-shadow: var(--shadow);
  position: relative;
  height: 360px;
}
.chart-wrap canvas { display: block; width: 100% !important; height: 100% !important; }
.chart-empty {
  text-align: center;
  color: var(--muted);
  padding: 2rem 0;
}

/* Chart mode switch */
.chart-modes {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  background: var(--card);
}
.mode-btn {
  border: none;
  background: var(--card);
  padding: 0.45rem 0.9rem;
  cursor: pointer;
  font-size: 0.9rem;
  color: var(--muted);
  border-right: 1px solid var(--border);
}
.mode-btn:last-child { border-right: none; }
.mode-btn.active { background: var(--primary); color: #fff; font-weight: 600; }

/* Days-of-week picker */
.days-picker {
  display: flex;
  gap: 0.3rem;
  flex-wrap: wrap;
}
.day-toggle {
  width: 2rem;
  height: 2rem;
  border-radius: 50%;
  border: 1.5px solid var(--border);
  background: var(--card);
  color: var(--muted);
  cursor: pointer;
  font-size: 0.8rem;
  padding: 0;
}
.day-toggle.on {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
  font-weight: 600;
}

/* Calendar view */
.habit-select {
  width: 100%;
  padding: 0.65rem 0.85rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 1rem;
  background: var(--card);
  color: var(--text);
  margin-bottom: 0.75rem;
  font-weight: 600;
}
.calendar-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1rem 1rem 1.25rem;
}
.cal-nav {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
.cal-month-label {
  flex: 1;
  text-align: center;
  font-weight: 700;
  font-size: 1.05rem;
  text-transform: capitalize;
}

/* Barre de progression du mois */
.cal-stats {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin: 0.9rem 0 0.4rem;
}
.cal-stats .cal-progress {
  flex: 1;
  height: 8px;
  background: var(--bg);
  border-radius: 4px;
  overflow: hidden;
}
.cal-stats .cal-progress > span {
  display: block;
  height: 100%;
  border-radius: 4px;
  transition: width 0.25s ease;
}
.cal-stats .cal-stats-text {
  font-size: 0.85rem;
  color: var(--muted);
  white-space: nowrap;
}
.cal-stats .cal-stats-text b { color: var(--text); }

.cal-weekdays {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 6px;
  margin-top: 0.6rem;
}
.calendar-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  grid-auto-rows: 3rem;
  gap: 6px;
  margin-top: 6px;
}
.cal-head {
  text-align: center;
  font-size: 0.72rem;
  letter-spacing: 0.04em;
  color: var(--muted);
  font-weight: 700;
  text-transform: uppercase;
  padding-bottom: 0.2rem;
}
.cal-cell {
  height: 100%;
  border-radius: 10px;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  font-size: 0.95rem;
  color: var(--text);
  cursor: pointer;
  user-select: none;
  border: 1.5px solid transparent;
  transition: transform 0.06s ease, box-shadow 0.15s ease, background 0.15s ease;
}
.cal-cell:hover:not(.empty) {
  box-shadow: 0 0 0 2px var(--border);
}
.cal-cell:active:not(.empty) { transform: scale(0.93); }
.cal-cell .cal-num { line-height: 1; }
.cal-cell .cal-dot {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: transparent;
}
.cal-cell.empty { background: none; cursor: default; }
.cal-cell.off {
  background: transparent;
  color: #c2c7d0;
}
/* Jour prévu (non fait) : petit point coloré sous le numéro */
.cal-cell.scheduled .cal-dot { background: currentColor; }
.cal-cell.scheduled { color: var(--text); }
/* Jour réalisé : pastille pleine à la couleur de l'habitude */
.cal-cell.done {
  color: #fff;
  font-weight: 700;
  border-color: transparent;
}
.cal-cell.done .cal-dot { background: rgba(255, 255, 255, 0.85); }
.cal-cell.today { border-color: var(--primary); }
/* Jour hors période de validité de l'habitude (non cliquable) */
.cal-cell.out { opacity: 0.35; cursor: default; }
.cal-cell.out:hover { box-shadow: none; }
.cal-cell.out:active { transform: none; }
.cal-cell.out:not(.done) { background: repeating-linear-gradient(
  45deg, var(--bg), var(--bg) 4px, var(--border) 4px, var(--border) 5px); }

/* Légende */
.cal-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  margin-top: 1rem;
  font-size: 0.8rem;
  color: var(--muted);
}
.cal-legend span { display: inline-flex; align-items: center; gap: 0.4rem; }
.cal-legend .lg-swatch {
  width: 14px;
  height: 14px;
  border-radius: 5px;
  display: inline-block;
}
.cal-legend .lg-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
}

/* Manage view */
.habit-form {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  margin-bottom: 1.25rem;
}
.habit-form-row {
  display: flex;
  gap: 0.5rem;
}
.habit-form input[type="text"] {
  flex: 1;
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 1rem;
}
.habit-form input[type="color"] {
  width: 44px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--card);
  cursor: pointer;
}
.habit-form button {
  padding: 0 1.1rem;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  cursor: pointer;
}

.manage-list { list-style: none; margin: 0; padding: 0; }
.manage-item {
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.7rem 0.9rem;
  margin-bottom: 0.6rem;
  box-shadow: var(--shadow);
}
.manage-top {
  display: flex;
  align-items: center;
  gap: 0.7rem;
}
.habit-dates {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.6rem;
  font-size: 0.85rem;
  color: var(--muted);
}
.habit-dates label {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
}
.habit-dates input[type="date"] {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.3rem 0.4rem;
  font-size: 0.85rem;
  background: var(--card);
  color: var(--text);
}
.dates-hint { font-size: 0.78rem; color: var(--muted); }
.manage-item input[type="color"] {
  width: 34px;
  height: 34px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
}
.manage-item input[type="text"] {
  flex: 1;
  border: 1px solid transparent;
  background: transparent;
  font-size: 1rem;
  padding: 0.4rem;
  border-radius: 6px;
  color: var(--text);
}
.manage-item input[type="text"]:focus {
  border-color: var(--border);
  background: var(--bg);
  outline: none;
}
.icon-btn {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1.1rem;
  padding: 0.3rem 0.5rem;
  border-radius: 6px;
  color: var(--muted);
}
.icon-btn:hover { background: var(--bg); }
.icon-btn.danger:hover { color: #dc2626; }

/* --- Mobile --- */
@media (max-width: 560px) {
  main { padding: 1rem 0.75rem 4rem; }
  .app-header { padding: 0.85rem 0.75rem 0; }
  .app-header h1 { font-size: 1.2rem; }
  .tab { padding: 0.55rem 0.7rem; font-size: 0.9rem; }

  /* Stats du récap : 3 cartes qui passent à la ligne proprement */
  .month-stats { flex-wrap: wrap; gap: 0.5rem; }
  .stat-card { flex: 1 1 30%; padding: 0.6rem 0.4rem; }
  .stat-card .big { font-size: 1.3rem; }

  .chart-wrap { height: 300px; padding: 0.6rem; }
  .chart-modes { width: 100%; }
  .mode-btn { flex: 1; }

  .calendar-grid { grid-auto-rows: 2.6rem; gap: 4px; }
  .cal-cell { font-size: 0.85rem; border-radius: 8px; }

  .habit-dates { font-size: 0.8rem; }
}
