/* ========== Design Tokens ========== */
:root {
  --bg: #ffffff;
  --bg-secondary: #f7f7f7;
  --bg-hover: #f0f0f0;
  --text: #222222;
  --text-secondary: #717171;
  --text-tertiary: #b0b0b0;
  --border: #ebebeb;
  --accent: #222222;
  --accent-soft: #f0f0f0;
  --green: #008a05;
  --green-bg: #e8f5e9;
  --gold: #c77d00;
  --gold-bg: #fff8e1;
  --red: #c13515;
  --red-bg: #fce4e4;
  --radius: 12px;
  --radius-full: 999px;
  --transition: 0.15s ease;
  --max-width: 520px;
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
}

[data-theme="dark"] {
  --bg: #121212;
  --bg-secondary: #1e1e1e;
  --bg-hover: #2a2a2a;
  --text: #f0f0f0;
  --text-secondary: #a0a0a0;
  --text-tertiary: #555;
  --border: #2a2a2a;
  --accent: #f0f0f0;
  --accent-soft: #2a2a2a;
  --green: #4caf50;
  --green-bg: #1b3a1b;
  --gold: #ffb300;
  --gold-bg: #332b00;
  --red: #ef5350;
  --red-bg: #3a1515;
}

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

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  min-height: 100dvh;
}

/* ========== App ========== */
.app {
  max-width: var(--max-width);
  margin: 0 auto;
  padding: env(safe-area-inset-top) 20px env(safe-area-inset-bottom);
}

/* ========== Header ========== */
.header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  padding: 20px 0 4px;
  position: sticky;
  top: 0;
  padding-top: calc(env(safe-area-inset-top) + 20px);
  margin-top: calc(-1 * env(safe-area-inset-top));
  background: var(--bg);
  z-index: 10;
}

.header__title {
  font-size: 32px;
  font-weight: 800;
  letter-spacing: -1px;
  line-height: 1.1;
}

.header__date {
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  padding: 0;
  margin-top: 2px;
}

.header__date:hover { color: var(--text); }

.header__actions { display: flex; gap: 4px; margin-top: 4px; }

/* ========== Icon Button ========== */
.icon-btn {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 10px;
  border-radius: var(--radius-full);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--transition);
}

.icon-btn:hover { background: var(--bg-secondary); }
.icon-btn:active { background: var(--bg-hover); }

[data-theme="light"] .theme-icon-dark { display: none; }
[data-theme="dark"] .theme-icon-light { display: none; }

/* ========== Date Strip ========== */
.date-nav {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 4px 0;
}

.date-nav__arrow {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: none;
  background: none;
  cursor: pointer;
  color: var(--text-tertiary);
  flex-shrink: 0;
  transition: background var(--transition), color var(--transition);
}

.date-nav__arrow:hover {
  background: var(--bg-secondary);
  color: var(--text);
}

.date-nav__arrow:disabled {
  opacity: 0.3;
  cursor: default;
}

.date-strip {
  display: flex;
  flex: 1;
  gap: 4px;
  padding: 8px 0;
  overflow-x: auto;
  scrollbar-width: none;
  -ms-overflow-style: none;
  justify-content: center;
}

.date-strip::-webkit-scrollbar { display: none; }

.date-strip__day {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 8px 12px;
  border-radius: var(--radius);
  border: none;
  background: none;
  cursor: pointer;
  min-width: 48px;
  transition: background var(--transition);
}

.date-strip__day:hover { background: var(--bg-secondary); }

.date-strip__day-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-tertiary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.date-strip__day-num {
  font-size: 16px;
  font-weight: 600;
  color: var(--text-secondary);
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
}

.date-strip__day.active .date-strip__day-num {
  background: var(--accent);
  color: var(--bg);
}

.date-strip__day.active .date-strip__day-label {
  color: var(--text);
}

.date-strip__day.has-data .date-strip__day-num::after {
  content: '';
  position: absolute;
  bottom: 2px;
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: var(--green);
}

.date-strip__day .date-strip__day-num { position: relative; }

.date-strip__day.today:not(.active) .date-strip__day-num {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* ========== Tabs ========== */
.tabs {
  display: flex;
  gap: 8px;
  padding: 4px 0 16px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 16px;
}

.tab {
  padding: 8px 16px;
  border: none;
  background: none;
  border-radius: var(--radius-full);
  font-size: 14px;
  font-weight: 600;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all var(--transition);
}

.tab:hover { background: var(--bg-secondary); color: var(--text); }

.tab.active {
  background: var(--accent);
  color: var(--bg);
}

/* ========== Views ========== */
.view { animation: fadeIn 0.2s ease; }
.hidden { display: none !important; }

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

/* ========== Habit Cards ========== */
.habits-list {
  display: flex;
  flex-direction: column;
}

.habit-card {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 16px 0;
  border-bottom: 1px solid var(--border);
  cursor: default;
  transition: background var(--transition);
}

.habit-card:last-child { border-bottom: none; }

.habit-card__icon {
  font-size: 28px;
  flex-shrink: 0;
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius);
  background: var(--bg-secondary);
}

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

.habit-card__name {
  font-size: 16px;
  font-weight: 600;
  line-height: 1.3;
}

.habit-card__meta {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 2px;
  font-size: 13px;
  color: var(--text-secondary);
}

.habit-card__streak {
  color: var(--gold);
  font-weight: 600;
}

.habit-card__dots {
  display: flex;
  gap: 3px;
  align-items: center;
}

.dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--border);
}

.dot.filled { background: var(--green); }

.habit-card__action { flex-shrink: 0; }

/* Edit button */
.habit-card__edit {
  background: none;
  border: none;
  color: var(--text-tertiary);
  cursor: pointer;
  padding: 6px;
  border-radius: var(--radius-full);
  display: flex;
  opacity: 0;
  transition: opacity var(--transition);
}

.habit-card:hover .habit-card__edit { opacity: 1; }

@media (hover: none) {
  .habit-card__edit { opacity: 0.5; }
}

/* Check button */
.habit-check {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-full);
  border: 2px solid var(--border);
  background: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--transition);
  flex-shrink: 0;
}

.habit-check:hover { border-color: var(--green); }

.habit-check.checked {
  border-color: var(--green);
  background: var(--green);
}

.habit-check.checked svg { stroke: var(--bg); }
.habit-check svg { stroke: transparent; transition: stroke var(--transition); }

@keyframes pop {
  0% { transform: scale(1); }
  50% { transform: scale(1.15); }
  100% { transform: scale(1); }
}

.habit-check.checked { animation: pop 0.25s ease; }

/* Counter */
.counter-control {
  display: flex;
  align-items: center;
  gap: 10px;
}

.counter-btn {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-full);
  border: 1.5px solid var(--border);
  background: none;
  color: var(--text);
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--transition);
}

.counter-btn:hover { border-color: var(--text); }
.counter-btn:disabled { opacity: 0.2; cursor: not-allowed; }

.counter-value {
  font-size: 18px;
  font-weight: 700;
  min-width: 20px;
  text-align: center;
}

/* Bedtime */
.bedtime-btn {
  padding: 6px 14px;
  border-radius: var(--radius-full);
  border: 1.5px solid var(--border);
  background: none;
  color: var(--text-secondary);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
}

.bedtime-btn:hover { border-color: var(--text); color: var(--text); }
.bedtime-btn.has-value { border-color: var(--green); color: var(--green); background: var(--green-bg); }
.bedtime-btn.bonus { border-color: var(--gold); color: var(--gold); background: var(--gold-bg); }

/* Status badges */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: var(--radius-full);
  font-size: 12px;
  font-weight: 600;
}

.badge--green { background: var(--green-bg); color: var(--green); }
.badge--gold { background: var(--gold-bg); color: var(--gold); }
.badge--red { background: var(--red-bg); color: var(--red); }

/* Add habit button */
.add-habit-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  padding: 14px;
  margin-top: 8px;
  border: 1.5px dashed var(--border);
  border-radius: var(--radius);
  background: none;
  color: var(--text-secondary);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
}

.add-habit-btn:hover { border-color: var(--text-secondary); color: var(--text); background: var(--bg-secondary); }

/* ========== Week View ========== */
.week-grid { display: flex; flex-direction: column; }

.week-habit {
  padding: 16px 0;
  border-bottom: 1px solid var(--border);
}

.week-habit:last-child { border-bottom: none; }

.week-habit__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.week-habit__name {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  font-size: 15px;
}

.week-habit__name span:first-child { font-size: 20px; }

.week-habit__goal {
  font-size: 13px;
  color: var(--text-secondary);
  font-weight: 600;
}

.week-habit__goal.met { color: var(--green); }

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

.week-day {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.week-day__label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-tertiary);
  text-transform: uppercase;
}

.week-day__marker {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--bg-secondary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 700;
  color: var(--text-tertiary);
  transition: all var(--transition);
}

.week-day__marker.completed { background: var(--green-bg); color: var(--green); }
.week-day__marker.bonus { background: var(--gold-bg); color: var(--gold); }
.week-day__marker.over { background: var(--red-bg); color: var(--red); }

.week-day__marker.today {
  box-shadow: 0 0 0 2px var(--accent);
}

.week-progress-bar {
  margin-top: 10px;
  height: 4px;
  background: var(--bg-secondary);
  border-radius: 2px;
  overflow: hidden;
}

.week-progress-bar__fill {
  height: 100%;
  background: var(--green);
  border-radius: 2px;
  transition: width 0.4s ease;
}

.week-progress-bar__fill.exceeded { background: var(--gold); }

/* ========== Stats ========== */
.stats-container { display: flex; flex-direction: column; }

.stat-card {
  padding: 20px 0;
  border-bottom: 1px solid var(--border);
}

.stat-card:last-child { border-bottom: none; }

.stat-card__title {
  font-size: 12px;
  font-weight: 700;
  color: var(--text-tertiary);
  text-transform: uppercase;
  letter-spacing: 0.8px;
  margin-bottom: 16px;
}

.stat-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 0;
}

.stat-row__label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 15px;
  font-weight: 500;
}

.stat-row__value {
  font-size: 14px;
  font-weight: 700;
}

.stat-row__value.streak { color: var(--gold); }

.stat-bar-chart {
  display: flex;
  align-items: flex-end;
  gap: 4px;
  height: 100px;
  padding-top: 8px;
}

.stat-bar {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  height: 100%;
  gap: 4px;
}

.stat-bar__fill {
  width: 100%;
  background: var(--accent);
  border-radius: 3px;
  min-height: 2px;
  transition: height 0.4s ease;
  opacity: 0.15;
}

.stat-bar__label { font-size: 10px; color: var(--text-tertiary); font-weight: 600; }
.stat-bar__value { font-size: 11px; color: var(--text-secondary); font-weight: 600; }

.stat-summary {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}

.stat-summary__item { text-align: center; }

.stat-summary__number {
  font-size: 28px;
  font-weight: 800;
  line-height: 1.2;
}

.stat-summary__number.green { color: var(--green); }
.stat-summary__number.gold { color: var(--gold); }

.stat-summary__label {
  font-size: 11px;
  color: var(--text-secondary);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}

/* ========== Modal ========== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 100;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  animation: fadeIn 0.15s ease;
}

@media (min-width: 640px) {
  .modal-overlay { align-items: center; }
}

.modal {
  background: var(--bg);
  border-radius: 20px 20px 0 0;
  width: 100%;
  max-width: var(--max-width);
  max-height: 90dvh;
  overflow-y: auto;
  animation: slideUp 0.25s ease;
}

@media (min-width: 640px) {
  .modal { border-radius: 20px; max-height: 80vh; }
}

.modal--sm { max-width: 400px; }

@keyframes slideUp {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

.modal__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 24px 0;
}

.modal__header h2 { font-size: 20px; font-weight: 700; }

.modal__body { padding: 20px 24px 28px; }

.modal__actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  margin-top: 24px;
}

/* ========== Forms ========== */
.form-group { margin-bottom: 20px; }

.form-label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

.form-input {
  width: 100%;
  padding: 12px 14px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  font-family: var(--font);
  transition: border-color var(--transition);
}

.form-input:focus { outline: none; border-color: var(--accent); }
.form-input--sm { width: 70px; text-align: center; }

.form-row { display: flex; align-items: center; gap: 10px; }
.form-hint { font-size: 14px; color: var(--text-secondary); }

.form-radio-group {
  display: flex;
  gap: 8px;
}

.form-radio {
  cursor: pointer;
}

.form-radio input { display: none; }

.form-radio span {
  display: block;
  padding: 8px 16px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-full);
  font-size: 14px;
  font-weight: 600;
  color: var(--text-secondary);
  transition: all var(--transition);
}

.form-radio input:checked + span {
  border-color: var(--accent);
  background: var(--accent);
  color: var(--bg);
}

.form-radio span:hover {
  border-color: var(--text-secondary);
}

/* Icon picker */
.icon-picker {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.icon-option {
  width: 42px;
  height: 42px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  background: none;
  font-size: 22px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--transition);
}

.icon-option:hover { border-color: var(--text-secondary); }
.icon-option.selected { border-color: var(--accent); background: var(--accent-soft); }

/* ========== Settings ========== */
.settings-section { margin-bottom: 28px; }
.settings-section:last-child { margin-bottom: 0; }
.settings-section h3 { font-size: 16px; font-weight: 700; margin-bottom: 12px; }

.goal-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}

.goal-item:last-child { border-bottom: none; }
.goal-item__label { display: flex; align-items: center; gap: 8px; font-size: 15px; font-weight: 500; }
.goal-item__control { display: flex; align-items: center; gap: 6px; }

.goal-input {
  width: 56px;
  padding: 6px 8px;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  font-weight: 600;
  text-align: center;
}

.goal-input:focus { outline: none; border-color: var(--accent); }
.goal-input--time { width: 96px; }
.goal-unit { font-size: 13px; color: var(--text-secondary); }

.reminder-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
}

.reminder-item:last-child { border-bottom: none; }
.reminder-item__label { font-size: 14px; font-weight: 500; }
.reminder-item__control { display: flex; align-items: center; gap: 8px; }

.reminder-time {
  width: 86px;
  padding: 4px 6px;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}

.reminder-time:focus { outline: none; border-color: var(--accent); }

/* Toggle */
.toggle { position: relative; width: 44px; height: 26px; }
.toggle input { opacity: 0; width: 0; height: 0; }

.toggle__slider {
  position: absolute;
  inset: 0;
  background: var(--border);
  border-radius: 13px;
  cursor: pointer;
  transition: background var(--transition);
}

.toggle__slider::before {
  content: '';
  position: absolute;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #fff;
  top: 3px;
  left: 3px;
  transition: transform var(--transition);
  box-shadow: 0 1px 3px rgba(0,0,0,0.15);
}

.toggle input:checked + .toggle__slider { background: var(--green); }
.toggle input:checked + .toggle__slider::before { transform: translateX(18px); }

/* ========== Buttons ========== */
.btn {
  padding: 10px 20px;
  border-radius: var(--radius-full);
  border: none;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
  font-family: var(--font);
}

.btn--primary { background: var(--accent); color: var(--bg); }
.btn--primary:hover { opacity: 0.85; }

.btn--outline {
  background: none;
  color: var(--text);
  border: 1.5px solid var(--border);
}

.btn--outline:hover { border-color: var(--text); }

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

.btn--danger:hover { opacity: 0.85; }

.data-actions { display: flex; gap: 10px; }

/* ========== Responsive ========== */
@media (min-width: 640px) {
  .app { padding: 0 24px 48px; }
  .header__title { font-size: 36px; }
  :root { --max-width: 560px; }
}

/* ========== Scrollbar ========== */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

::selection { background: var(--accent); color: var(--bg); }

/* ========== Sync Button ========== */
.sync-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 14px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-full);
  background: none;
  color: var(--text-secondary);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
  font-family: var(--font);
}

.sync-btn:hover { border-color: var(--text-secondary); color: var(--text); }
.sync-btn.signed-in { border-color: var(--green); color: var(--green); }
.sync-btn.signed-in:hover { background: var(--green-bg); }
