/* ============================================================
   Habit Tracker — Telegram Mini App
   Design tokens as CSS custom properties, themed via [data-theme]
   ============================================================ */

:root {
  --bg: #ffffff;
  --text: #0b0c0f;
  --muted: #9298a3;
  --border: #e8e9ec;
  --track: #e8e9ec;
  --btn-bg: #0b0c0f;
  --btn-text: #ffffff;
  --tip-bg: #f5f6f8;
  --toggle-off: #d9dbe0;
  --toggle-on: #0b0c0f;
  --knob: #ffffff;
  --icon-muted: #69707c;
  --page-bg: #e9eaec;
}

[data-theme="dark"] {
  --bg: #0f1012;
  --text: #f2f3f5;
  --muted: #8b8f98;
  --border: #2a2c31;
  --track: #26282d;
  --btn-bg: #f2f3f5;
  --btn-text: #0f1012;
  --tip-bg: #1c1e22;
  --toggle-off: #3a3d42;
  --toggle-on: #f2f3f5;
  --knob: #c7c9cd;
  --icon-muted: #8b95a3;
  --page-bg: #0b0c0f;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--page-bg);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

button {
  font-family: inherit;
  border: none;
  background: none;
  padding: 0;
  color: inherit;
}

#app {
  max-width: 520px;
  margin: 0 auto;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  position: relative;
  transition: background .2s ease, color .2s ease;
}

.screen {
  display: none;
  flex-direction: column;
  min-height: 100vh;
  animation: fadeIn .25s ease;
}
.screen.active { display: flex; }

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

.muted { color: var(--muted); }
.center-text { text-align: center; }

.divider {
  height: 1px;
  background: var(--border);
  margin: 4px 0 20px;
  flex: none;
}

/* ---------- topbar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: max(env(safe-area-inset-top), 24px) 20px 20px;
  flex: none;
}
.topbar-centered {
  justify-content: center;
  position: relative;
  padding: max(env(safe-area-inset-top), 22px) 20px 22px;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-title { font-size: 21px; font-weight: 700; letter-spacing: -0.3px; }
.topbar-actions { display: flex; align-items: center; gap: 18px; }
.topbar-title { font-size: 19px; font-weight: 700; }
.icon-btn { cursor: pointer; color: var(--text); display: flex; }
.back-btn { position: absolute; left: 20px; }

/* ---------- home: progress ---------- */
.progress-block { padding: 4px 20px 20px; flex: none; }
.progress-row { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; font-size: 14px; }
.progress-count { font-size: 15px; font-weight: 700; }
.progress-track { height: 6px; border-radius: 3px; background: var(--track); overflow: hidden; }
.progress-fill { height: 100%; border-radius: 3px; background: var(--btn-bg); transition: width .3s ease; }

/* ---------- home: habit list ---------- */
.habit-list { padding: 0 20px; flex: 1; }
.habit-card {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 19px 18px; border: 1px solid var(--border); border-radius: 16px;
  margin-bottom: 14px; cursor: pointer; -webkit-tap-highlight-color: transparent;
}
.habit-left { display: flex; align-items: center; gap: 16px; min-width: 0; }
.habit-check {
  flex: none; width: 30px; height: 30px; border-radius: 50%;
  border: 2px solid var(--border); background: transparent;
  display: flex; align-items: center; justify-content: center;
  transition: background .15s, border-color .15s;
}
.habit-check.done { border-color: var(--btn-bg); background: var(--btn-bg); }
.habit-check svg { display: none; }
.habit-check.done svg { display: block; stroke: var(--btn-text); }
.habit-name {
  font-size: 16px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.habit-streak { flex: none; display: flex; align-items: center; gap: 5px; color: var(--icon-muted); }
.habit-streak span { font-size: 15px; font-weight: 700; }
.habit-right { flex: none; display: flex; align-items: center; gap: 16px; }
.habit-reminder {
  flex: none; display: flex; align-items: center; justify-content: center;
  width: 30px; height: 30px; color: var(--muted); cursor: pointer;
}
.habit-reminder.active { color: var(--text); }
.habit-reminder:active { color: var(--text); }
.habit-delete {
  flex: none; display: flex; align-items: center; justify-content: center;
  width: 30px; height: 30px; color: var(--muted); cursor: pointer;
}
.habit-delete:active { color: var(--text); }

/* ---------- bottom bar / buttons ---------- */
.bottom-bar { padding: 16px 20px 28px; flex: none; }
.primary-btn {
  width: 100%; display: flex; align-items: center; justify-content: center; gap: 8px;
  background: var(--btn-bg); color: var(--btn-text); height: 54px; border-radius: 16px;
  font-size: 16px; font-weight: 700; cursor: pointer;
}
.secondary-btn {
  flex: 1; height: 52px; border-radius: 14px; border: 1px solid var(--border);
  font-size: 15px; font-weight: 700; cursor: pointer; background: var(--bg); color: var(--text);
}

/* ---------- stats ---------- */
.stats-grid { padding: 0 20px; display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.stat-card { border: 1px solid var(--border); border-radius: 16px; padding: 20px; }
.stat-icon {
  width: 42px; height: 42px; border-radius: 50%; background: var(--track);
  display: flex; align-items: center; justify-content: center; margin-bottom: 16px;
}
.stat-value { font-size: 28px; font-weight: 700; margin-bottom: 6px; }
.stat-label { font-size: 13px; color: var(--muted); }

.section-block { padding: 28px 20px 0; }
.section-title { font-size: 17px; font-weight: 700; margin-bottom: 14px; }

.week-chart {
  border: 1px solid var(--border); border-radius: 16px; height: 220px;
  display: flex; align-items: flex-end; justify-content: space-between; padding: 16px 18px 12px;
}
.week-bar-col { display: flex; flex-direction: column; align-items: center; gap: 8px; flex: 1; }
.week-bar { width: 16px; border-radius: 5px; background: var(--track); }
.week-bar-label { font-size: 12px; color: var(--muted); }

/* ---------- settings ---------- */
.setting-row {
  display: flex; align-items: center; justify-content: space-between;
  border: 1px solid var(--border); border-radius: 16px; padding: 16px 18px; margin-bottom: 28px;
}
.setting-left { display: flex; align-items: center; gap: 14px; }
.setting-icon {
  width: 42px; height: 42px; border-radius: 50%; background: var(--track);
  display: flex; align-items: center; justify-content: center; flex: none;
}
.setting-title { font-size: 16px; font-weight: 600; }
.setting-sub { font-size: 13px; color: var(--muted); margin-top: 2px; }

.toggle {
  cursor: pointer; width: 48px; height: 28px; border-radius: 14px;
  background: var(--toggle-off); position: relative; transition: background .2s; flex: none;
}
.toggle.on { background: var(--toggle-on); }
.toggle-knob {
  position: absolute; top: 2px; left: 2px; width: 24px; height: 24px; border-radius: 50%;
  background: var(--knob); box-shadow: 0 1px 3px rgba(0,0,0,0.25); transition: left .2s;
}
.toggle.on .toggle-knob { left: 22px; }

/* ---------- pomodoro ---------- */
.segmented { margin: 0 20px 24px; display: flex; background: var(--track); border-radius: 14px; padding: 4px; flex: none; }
.segment {
  flex: 1; text-align: center; padding: 10px 0; border-radius: 11px; font-size: 14px;
  font-weight: 700; cursor: pointer; color: var(--text); transition: background .15s, color .15s;
}
.segment.active { background: var(--btn-bg); color: var(--btn-text); }

.timer-wrap { display: flex; align-items: center; justify-content: center; padding: 12px 0 28px; flex: none; }
.timer-ring {
  width: 250px; height: 250px; border-radius: 50%; border: 2px solid var(--border);
  display: flex; align-items: center; justify-content: center; position: relative;
}
.timer-ring-fill {
  position: absolute; inset: 0; border-radius: 50%;
  background: conic-gradient(var(--text) 0%, transparent 0%);
  -webkit-mask: radial-gradient(farthest-side, transparent calc(100% - 4px), #000 calc(100% - 4px));
  mask: radial-gradient(farthest-side, transparent calc(100% - 4px), #000 calc(100% - 4px));
}
.timer-center { text-align: center; }
.timer-time { font-size: 44px; font-weight: 700; letter-spacing: -1px; font-variant-numeric: tabular-nums; }
.timer-mode { font-size: 14px; color: var(--muted); margin-top: 4px; }

.timer-controls { display: flex; align-items: center; justify-content: center; gap: 22px; padding-bottom: 28px; flex: none; }
.round-btn {
  width: 50px; height: 50px; border-radius: 50%; border: 1.5px solid var(--border);
  display: flex; align-items: center; justify-content: center; cursor: pointer; color: var(--text);
}
.round-btn-main {
  width: 64px; height: 64px; border: none; background: var(--btn-bg); color: var(--btn-text);
}

.sessions-block { text-align: center; padding-bottom: 20px; flex: none; }
.sessions-circles { display: flex; align-items: center; justify-content: center; gap: 14px; margin: 14px 0 16px; }
.session-circle {
  width: 38px; height: 38px; border-radius: 50%; background: var(--track); color: var(--muted);
  display: flex; align-items: center; justify-content: center; font-size: 14px; font-weight: 700;
  transition: background .2s, color .2s;
}
.session-circle.filled { background: var(--btn-bg); color: var(--btn-text); }
.total-flame { display: flex; align-items: center; justify-content: center; gap: 6px; color: var(--icon-muted); }
.total-flame span:first-of-type { font-size: 15px; font-weight: 700; color: var(--text); }
.total-flame .muted { font-size: 14px; }

.tip-box {
  margin: 0 20px 24px; background: var(--tip-bg); border-radius: 16px; padding: 16px 18px;
  display: flex; gap: 12px; align-items: flex-start; font-size: 14px; line-height: 1.5; flex: none;
}

/* ---------- add habit modal ---------- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.4);
  display: none; align-items: flex-end; justify-content: center; z-index: 100;
}
.modal-overlay.open { display: flex; }
.modal-sheet {
  width: 100%; max-width: 520px; background: var(--bg); border-radius: 24px 24px 0 0;
  padding: 24px 20px max(env(safe-area-inset-bottom), 28px); animation: fadeIn .2s ease;
}
.modal-handle { width: 36px; height: 4px; border-radius: 2px; background: var(--border); margin: 0 auto 20px; }
.modal-title { font-size: 18px; font-weight: 700; margin-bottom: 18px; }
.modal-input {
  width: 100%; height: 52px; border-radius: 14px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text); font-size: 16px; padding: 0 16px; margin-bottom: 18px; outline: none;
}
.modal-actions { display: flex; gap: 12px; }
.modal-actions .primary-btn, .modal-actions .secondary-btn { width: auto; flex: 1; }
