:root {
  --bg: #F7F2E7;
  --panel: #FFFFFF;
  --panel-2: #F1EAD8;
  --border: #E6DCC3;
  --text: #24312B;
  --text-dim: #7C8A80;
  --accent: #0F8F73;
  --accent-2: #0B6E58;
  --accent-light: #E4F5EE;
  --amber: #E8A93C;
  --amber-light: #FBEDD3;
  --green: #1FA05F;
  --red: #E2574C;
  --red-light: #FBE7E5;
  --btn-text: #fff;
}

/* Giao diện tối — nền xanh than, chữ/điểm nhấn vàng, đồng bộ với landing/hướng dẫn. */
:root[data-theme="dark"] {
  --bg: #0B0E14;
  --panel: #12151F;
  --panel-2: #171B29;
  --border: #252838;
  --text: #F1EEE6;
  --text-dim: #8B8F9C;
  --accent: #D4AF6A;
  --accent-2: #B8934E;
  --accent-light: #1F1B0E;
  --amber: #F0D9A8;
  --amber-light: #5C4C1C;
  --green: #4FAE8B;
  --red: #E2574C;
  --red-light: #2E1917;
  --btn-text: #1A1508;
}

* { box-sizing: border-box; -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  max-width: 100%;
  overflow-x: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
}

#app {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100dvh;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  padding-bottom: calc(76px + env(safe-area-inset-bottom));
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

header.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  padding: calc(14px + env(safe-area-inset-top)) 16px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

header.topbar h1 {
  font-size: 16px;
  margin: 0;
  font-weight: 700;
  color: var(--accent-2);
}

main {
  flex: 1;
  padding: 16px;
}

nav.tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--panel);
  border-top: 1px solid var(--border);
  display: flex;
  max-width: 480px;
  margin: 0 auto;
  z-index: 20;
  padding-bottom: env(safe-area-inset-bottom);
}

nav.tabbar button {
  flex: 1;
  background: none;
  border: none;
  color: var(--text-dim);
  padding: 10px 4px 8px;
  font-size: 11px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

nav.tabbar button.active { color: var(--accent); }
nav.tabbar button svg { width: 22px; height: 22px; }

.card {
  background: var(--panel);
  border-radius: 14px;
  padding: 16px;
  margin-bottom: 14px;
  border: 1px solid var(--border);
  box-shadow: 0 1px 3px rgba(36,49,43,0.05);
}

.card h2 {
  font-size: 13px;
  margin: 0 0 10px;
  color: var(--text-dim);
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.progress-bar {
  height: 10px;
  background: var(--panel-2);
  border-radius: 6px;
  overflow: hidden;
  margin-top: 8px;
}
.progress-bar > div {
  height: 100%;
  background: linear-gradient(90deg, #F6D879 0%, #F2994A 55%, #D6473A 100%);
  transition: width 0.3s ease;
}

.progress-label {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
}
.progress-label .big { font-size: 26px; font-weight: 800; color: var(--accent-2); }

/* ---- Dải 7 ngày Thứ 2 - Chủ nhật ---- */
.week-strip-nav {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 14px;
}
.week-strip-nav button.week-arrow {
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  width: 30px;
  height: 44px;
  flex-shrink: 0;
  font-size: 15px;
}
.week-strip {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
}
.week-strip .day-cell {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 6px 2px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
}
.week-strip .day-cell .wd { font-size: 10px; color: var(--text-dim); font-weight: 600; }
.week-strip .day-cell .dn { font-size: 14px; font-weight: 700; }
.week-strip .day-cell.today { border-color: var(--accent); }
.week-strip .day-cell.today .dn { color: var(--accent-2); }
.week-strip .day-cell.selected { background: var(--accent); border-color: var(--accent); }
.week-strip .day-cell.selected .wd, .week-strip .day-cell.selected .dn { color: var(--btn-text); }
.week-strip .day-cell .dot { width: 4px; height: 4px; border-radius: 50%; background: var(--amber); margin-top: 1px; }
.week-strip .day-cell.selected .dot { background: #fff; }

.selected-date-label { font-size: 13px; color: var(--text-dim); margin: -6px 0 14px; text-align: center; }

.main-focus {
  background: linear-gradient(135deg, #F0806A, #E0483C);
  border-radius: 14px;
  padding: 14px 16px;
  margin-bottom: 14px;
}
.main-focus .label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; color: #FDE3DD; }
.main-focus .value { font-size: 17px; font-weight: 700; margin-top: 4px; color: #fff; }

/* ---- Thói quen: chấm 7 ngày ---- */
.habit-block { padding: 10px 0; border-bottom: 1px solid var(--border); }
.habit-block:last-child { border-bottom: none; }
.habit-block-top { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.habit-block-top .habit-name { font-size: 14px; font-weight: 600; }
.habit-block-top .habit-target { font-size: 11px; color: var(--text-dim); margin-top: 1px; }
.habit-dots { display: flex; justify-content: space-between; }
.habit-dot-col { display: flex; flex-direction: column; align-items: center; gap: 5px; }
.habit-dot { width: 22px; height: 22px; border-radius: 50%; background: var(--panel-2); border: 2px solid var(--border); }
.habit-dot.on { background: var(--accent); border-color: var(--accent); }
.habit-dot-col.today .habit-dot { border-color: #E0483C; }
.habit-dot-col.today .habit-dot.on { background: var(--accent); border-color: #E0483C; }
.hd-label { font-size: 9px; color: var(--text-dim); font-weight: 600; }

.task-item, .habit-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.task-item:last-child, .habit-item:last-child { border-bottom: none; }

.task-item {
  background: var(--amber-light);
  border-bottom: none;
  border-radius: 12px;
  padding: 10px 12px;
  margin-bottom: 8px;
}
.task-item:last-child { margin-bottom: 0; }

.checkbox {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 2px solid var(--border);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  color: var(--btn-text);
}
.checkbox.done { background: var(--accent); border-color: var(--accent); }

.task-body { flex: 1; min-width: 0; }
.task-title { font-size: 14px; font-weight: 500; }
.task-title.done { text-decoration: line-through; color: var(--text-dim); }
.task-meta { font-size: 12px; color: var(--text-dim); margin-top: 2px; display: flex; gap: 8px; align-items: center; }
.task-meta .time-chip { color: var(--accent-2); font-weight: 600; }

.rank-badge {
  font-size: 10px;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 999px;
  color: #7A4E0A;
  background: #FBE1A0;
}
.rank-1 { background: #E2574C; color: #fff; }

.icon-btn {
  background: none;
  border: none;
  color: var(--text-dim);
  padding: 6px;
  font-size: 16px;
}
.icon-btn.danger { color: var(--red); }

textarea, input, select {
  width: 100%;
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 16px; /* >=16px bắt buộc — dưới mức này Safari iPhone tự zoom trang khi focus vào ô nhập */
  font-family: inherit;
}
textarea { min-height: 70px; resize: vertical; }

.btn-primary {
  width: 100%;
  background: var(--accent-2);
  color: var(--btn-text);
  border: none;
  border-radius: 10px;
  padding: 12px;
  font-size: 15px;
  font-weight: 700;
  margin-top: 10px;
}
.btn-secondary {
  width: 100%;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px;
  font-size: 14px;
  font-weight: 600;
  margin-top: 8px;
}
.btn-add-row {
  width: 100%;
  background: var(--accent-light);
  color: var(--accent-2);
  border: 1px dashed var(--accent);
  border-radius: 10px;
  padding: 12px;
  font-size: 14px;
  font-weight: 700;
  margin-top: 10px;
}

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(36,49,43,0.45);
  display: flex;
  align-items: flex-end;
  z-index: 50;
}
.modal-sheet {
  background: var(--panel);
  width: 100%;
  max-width: 480px;
  margin: 0 auto;
  border-radius: 16px 16px 0 0;
  padding: 20px 16px calc(20px + env(safe-area-inset-bottom));
  max-height: 85vh;
  overflow-y: auto;
}
.modal-sheet h3 { margin: 0 0 14px; font-size: 16px; color: var(--accent-2); }
.field { margin-bottom: 12px; }
.field label { display: block; font-size: 12px; color: var(--text-dim); margin-bottom: 6px; }
.field-row { display: flex; gap: 10px; }
.field-row .field { flex: 1; }
.modal-actions { display: flex; gap: 10px; margin-top: 16px; }
.modal-actions .btn-secondary, .modal-actions .btn-primary { margin-top: 0; }

.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 14px; }
.stat-box { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 14px; }
.stat-box .num { font-size: 22px; font-weight: 800; color: var(--accent-2); }
.stat-box .label { font-size: 11px; color: var(--text-dim); margin-top: 2px; }

.week-bars { display: flex; align-items: flex-end; gap: 8px; height: 110px; margin-top: 24px; }
.week-bars .col { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 6px; height: 100%; justify-content: flex-end; }
.week-bars .bar { width: 100%; background: var(--panel-2); border-radius: 4px 4px 0 0; position: relative; min-height: 4px; }
.week-bars .bar .bar-fill { position: absolute; bottom: 0; left: 0; right: 0; background: linear-gradient(180deg, var(--accent), var(--accent-2)); border-radius: 4px 4px 0 0; }
.week-bars .bar-pct { position: absolute; bottom: 100%; left: 0; right: 0; text-align: center; font-size: 10px; font-weight: 700; color: var(--text-dim); margin-bottom: 3px; white-space: nowrap; }
.week-bars .day-label { font-size: 10px; color: var(--text-dim); }
.week-bars.month-bars { gap: 4px; }
.week-bars.month-bars .day-label { font-size: 9px; }
.week-bars.month-bars .bar-pct { font-size: 8px; }


.empty-state { text-align: center; padding: 30px 10px; color: var(--text-dim); font-size: 13px; }

.goal-card { margin-bottom: 12px; }
.goal-card .goal-title { font-weight: 700; font-size: 15px; }
.goal-card .goal-period { font-size: 11px; color: var(--accent-2); font-weight: 700; text-transform: uppercase; }
.action-item { display: flex; align-items: center; gap: 8px; padding: 6px 0; font-size: 13px; }

.device-pick-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--border); }
.device-pick-row:last-of-type { border-bottom: 0; }
.device-pick-name { font-size: 14px; font-weight: 700; }
.device-pick-sub { font-size: 12px; color: var(--text-dim); margin-top: 2px; }

.chip { display: inline-block; font-size: 10px; padding: 2px 8px; border-radius: 999px; background: var(--panel-2); color: var(--text-dim); }
.chip.warn { background: var(--amber-light); color: #8A5A0E; }
.chip.on { background: var(--accent-light); color: var(--accent-2); }

.toggle-row { display: flex; align-items: center; justify-content: space-between; padding: 8px 0; }
.toggle-row .toggle-label { font-size: 14px; }
.toggle-row .toggle-sub { font-size: 12px; color: var(--text-dim); margin-top: 2px; }
.switch { width: 44px; height: 26px; border-radius: 999px; background: var(--panel-2); border: 1px solid var(--border); position: relative; flex-shrink: 0; }
.switch.on { background: var(--accent); border-color: var(--accent); }
.switch .knob { position: absolute; top: 2px; left: 2px; width: 20px; height: 20px; border-radius: 50%; background: #fff; transition: left 0.15s ease; }
.switch.on .knob { left: 20px; }

/* ---- Màn hình đăng nhập / kích hoạt (chặn trước khi vào app) ---- */
.login-gate { padding-top: 30px; }
.login-brand { text-align: center; margin-bottom: 24px; }
.login-logo {
  width: 56px; height: 56px; margin: 0 auto 14px;
  border-radius: 16px; background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: var(--btn-text); font-size: 26px; font-weight: 800;
  display: flex; align-items: center; justify-content: center;
}
.login-brand h1 { font-size: 22px; margin: 0 0 6px; color: var(--accent-2); }
.login-brand p { font-size: 13px; color: var(--text-dim); margin: 0; line-height: 1.5; }

#fatal-overlay {
  position: fixed; inset: 0; background: var(--bg); color: var(--text);
  display: none; align-items: center; justify-content: center; flex-direction: column;
  gap: 14px; z-index: 999; padding: 24px; text-align: center;
}
#fatal-overlay button { padding: 12px 20px; border-radius: 10px; border: none; background: var(--accent-2); color: var(--btn-text); font-size: 14px; }

.admin-table { width: 100%; border-collapse: collapse; font-size: 13px; white-space: nowrap; }
.admin-table th, .admin-table td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border); }
.admin-table th { color: var(--text-dim); font-size: 11px; text-transform: uppercase; letter-spacing: 0.03em; }
.admin-table tr:last-child td { border-bottom: none; }
