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

:root {
  --accent:   #0d9488;
  --accent-d: #0f766e;
  --bg:       #f1f5f9;
  --surface:  #ffffff;
  --border:   #e2e8f0;
  --text:     #1e293b;
  --muted:    #64748b;
  --danger:   #ef4444;
  --radius:   14px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100dvh;
  overflow: hidden;
}

/* ── Screens ── */
.screen {
  display: none;
  flex-direction: column;
  height: 100dvh;
}
.screen.active { display: flex; }

/* ── Header ── */
header {
  background: var(--accent);
  color: #fff;
  padding: 14px 16px;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}
header span {
  flex: 1;
  font-size: 1.1rem;
  font-weight: 600;
  text-align: center;
}
.header-btn {
  background: none;
  border: none;
  color: #fff;
  font-size: 1.3rem;
  cursor: pointer;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  flex-shrink: 0;
}
.header-btn:active { background: rgba(255,255,255,.2); }
.header-spacer { width: 36px; flex-shrink: 0; }

/* ── Main scroll area ── */
main {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  padding-bottom: 24px;
}
main.with-tabs { padding-bottom: 80px; }

/* ── Bottom tabs ── */
.tabs {
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.tab-btn,
.type-tab-btn,
.habit-tab-btn {
  flex: 1;
  border: none;
  background: none;
  padding: 10px 4px 14px;
  font-size: .75rem;
  color: var(--muted);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
}
.tab-btn .icon,
.type-tab-btn .icon { font-size: 1.4rem; }
.tab-btn.active,
.type-tab-btn.active,
.habit-tab-btn.active { color: var(--accent); font-weight: 600; }

/* ── Cards ── */
.card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
  box-shadow: 0 1px 4px rgba(0,0,0,.07);
}

/* ── Type cards (home) ── */
.type-card {
  background: var(--surface);
  border-radius: var(--radius);
  margin-bottom: 12px;
  box-shadow: 0 1px 4px rgba(0,0,0,.07);
  display: flex;
  align-items: center;
  overflow: hidden;
}
.type-card-body {
  flex: 1;
  padding: 18px 16px;
  cursor: pointer;
}
.type-card-body:active { background: #f8fafc; }
.type-card-name { font-size: 1.1rem; font-weight: 600; }
.type-card-meta { font-size: .82rem; color: var(--muted); margin-top: 3px; }
.type-card-arrow {
  color: var(--muted);
  font-size: 1.1rem;
  padding: 0 4px 0 0;
}
.type-edit-btn {
  border: none;
  background: none;
  font-size: 1.2rem;
  padding: 18px 16px;
  cursor: pointer;
  color: var(--muted);
  border-left: 1px solid var(--border);
}
.type-edit-btn:active { background: #f8fafc; }

/* ── FAB ── */
.fab-wrap {
  padding: 12px 16px 28px;
  background: var(--bg);
  flex-shrink: 0;
}
.fab {
  display: block;
  width: 100%;
  padding: 14px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(13,148,136,.35);
}
.fab:active { opacity: .85; }

/* ── Form ── */
.form-group { margin-bottom: 14px; }
label {
  display: block;
  font-size: .85rem;
  font-weight: 500;
  color: var(--muted);
  margin-bottom: 6px;
}
input[type="text"],
input[type="number"],
input[type="datetime-local"],
select,
textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  font-size: 1rem;
  color: var(--text);
  background: var(--surface);
  -webkit-appearance: none;
  appearance: none;
  transition: border-color .15s;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
}
textarea { resize: vertical; min-height: 72px; }

select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%2364748b' stroke-width='1.5' fill='none' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 36px;
}

.form-check {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-direction: row;
}
.form-check input[type="checkbox"] {
  width: 20px;
  height: 20px;
  margin: 0;
  accent-color: var(--accent);
}
.form-check label { margin: 0; }

/* ── Buttons ── */
.btn {
  display: block;
  width: 100%;
  padding: 13px;
  border: none;
  border-radius: 10px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity .15s;
  text-align: center;
}
.btn:active { opacity: .8; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-danger  { background: var(--danger); color: #fff; }
.btn-outline {
  background: none;
  color: var(--accent);
  border: 1.5px solid var(--accent);
}
.btn-ghost {
  background: var(--bg);
  color: var(--text);
}

/* ── Section title ── */
.section-title {
  font-size: .8rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .06em;
  margin: 20px 0 8px;
}

/* ── Field list (settings) ── */
.field-item {
  background: var(--surface);
  border-radius: 10px;
  padding: 12px 14px;
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  box-shadow: 0 1px 3px rgba(0,0,0,.05);
}
.field-item-info { flex: 1; }
.field-item-label { font-weight: 500; }
.field-item-meta { font-size: .8rem; color: var(--muted); margin-top: 2px; }
.field-item-actions { display: flex; gap: 4px; }
.icon-btn {
  border: none;
  background: none;
  font-size: 1.1rem;
  padding: 6px;
  cursor: pointer;
  border-radius: 6px;
  color: var(--muted);
}
.icon-btn:active { background: var(--border); }

/* ── History records ── */
.record-item {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 12px 14px;
  margin-bottom: 8px;
  box-shadow: 0 1px 3px rgba(0,0,0,.06);
  display: flex;
  align-items: flex-start;
  gap: 8px;
}
.record-meta { font-size: .8rem; color: var(--muted); margin-top: 3px; }
.record-data { font-size: .95rem; margin-top: 4px; }
.record-data span { margin-right: 10px; }
.record-notes { font-size: .82rem; color: var(--muted); margin-top: 4px; font-style: italic; }
.delete-btn {
  border: none;
  background: none;
  color: var(--muted);
  font-size: 1rem;
  cursor: pointer;
  padding: 4px;
  flex-shrink: 0;
}
.delete-btn:active { color: var(--danger); }

/* ── History 工具列 ── */
.history-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 12px;
}
.date-filter {
  display: flex;
  gap: 6px;
}
.filter-chip {
  padding: 6px 12px;
  border: 1.5px solid var(--border);
  border-radius: 16px;
  font-size: .82rem;
  background: var(--surface);
  cursor: pointer;
  transition: all .15s;
}
.filter-chip.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}
.export-btn {
  padding: 6px 12px;
  border: 1.5px solid var(--border);
  border-radius: 16px;
  font-size: .82rem;
  background: var(--surface);
  cursor: pointer;
  white-space: nowrap;
  color: var(--muted);
}
.export-btn:active { background: var(--border); }

/* ── 統計摘要卡片 ── */
.stats-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
  box-shadow: 0 1px 3px rgba(0,0,0,.06);
}
.stats-title {
  font-size: .8rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .06em;
  margin-bottom: 12px;
}
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
  gap: 12px;
}
.stat-item { text-align: center; }
.stat-value { font-size: 1.3rem; font-weight: 700; color: var(--accent); }
.stat-label { font-size: .75rem; color: var(--muted); margin-top: 2px; }

/* ── Charts ── */
.chart-wrap {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
  box-shadow: 0 1px 3px rgba(0,0,0,.06);
}
.chart-title {
  font-size: .9rem;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 12px;
}

/* ── Empty state ── */
.empty {
  text-align: center;
  padding: 48px 0;
  color: var(--muted);
  font-size: .95rem;
  line-height: 1.7;
}

/* ── Modal (bottom sheet) ── */
.modal { display: none; position: fixed; inset: 0; z-index: 50; }
.modal.show { display: block; }
.modal-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,.4);
}
.modal-sheet {
  position: absolute;
  bottom: 0; left: 0; right: 0;
  background: var(--surface);
  border-radius: var(--radius) var(--radius) 0 0;
  padding: 20px 16px 36px;
  max-height: 85dvh;
  overflow-y: auto;
}
.modal-title {
  font-size: 1rem;
  font-weight: 700;
  margin-bottom: 16px;
  text-align: center;
}
.modal-actions {
  display: flex;
  gap: 10px;
  margin-top: 16px;
}
.modal-actions .btn { flex: 1; }

/* ── Toast ── */
.toast {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: #1e293b;
  color: #fff;
  padding: 10px 20px;
  border-radius: 20px;
  font-size: .9rem;
  opacity: 0;
  transition: all .25s;
  pointer-events: none;
  white-space: nowrap;
  z-index: 100;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ── Toggle 開關 ── */
.toggle-wrap {
  display: flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
  padding: 4px 0;
}
.toggle-input { display: none; }
.toggle-track {
  width: 50px;
  height: 28px;
  background: var(--border);
  border-radius: 14px;
  position: relative;
  transition: background .2s;
  flex-shrink: 0;
}
.toggle-thumb {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 22px;
  height: 22px;
  background: #fff;
  border-radius: 50%;
  box-shadow: 0 1px 3px rgba(0,0,0,.2);
  transition: left .2s;
}
.toggle-input:checked + .toggle-track { background: var(--accent); }
.toggle-input:checked + .toggle-track .toggle-thumb { left: 25px; }
.toggle-label-text { font-size: 1rem; color: var(--text); }

/* ── 登入畫面 ── */
.login-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
  padding: 32px 24px;
  background: var(--bg);
}
.login-title {
  font-size: 1.8rem;
  font-weight: 700;
  color: var(--accent);
  margin-bottom: 8px;
}
.login-sub {
  font-size: .95rem;
  color: var(--muted);
  margin-bottom: 32px;
}
.login-wrap form { width: 100%; max-width: 320px; }
.login-wrap .form-group { margin-bottom: 14px; }
.login-error {
  margin-top: 12px;
  font-size: .88rem;
  color: var(--danger);
  min-height: 20px;
}

/* ── Add type prompt (inline input) ── */
.add-type-input-wrap {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 12px;
  box-shadow: 0 1px 4px rgba(0,0,0,.07);
  display: flex;
  gap: 10px;
}
.add-type-input-wrap input { flex: 1; margin: 0; }
.add-type-input-wrap button {
  padding: 10px 16px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: .95rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

/* ── 習慣追蹤 ── */
.habit-today-date {
  font-size: .82rem;
  color: var(--muted);
  margin-bottom: 14px;
}

.habit-card {
  background: var(--surface);
  border-radius: var(--radius);
  margin-bottom: 10px;
  box-shadow: 0 1px 4px rgba(0,0,0,.07);
  display: flex;
  align-items: center;
  padding: 14px;
  gap: 14px;
  transition: background .15s;
}
.habit-card.done { background: #f0fdf4; }

.habit-check-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 2px solid var(--border);
  background: var(--surface);
  cursor: pointer;
  font-size: 1.2rem;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: all .15s;
  color: #fff;
}
.habit-card.done .habit-check-btn {
  background: var(--accent);
  border-color: var(--accent);
}

.habit-card-info { flex: 1; min-width: 0; }
.habit-card-name {
  font-weight: 600;
  font-size: .95rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.habit-card-desc {
  font-size: .78rem;
  color: var(--muted);
  margin-top: 2px;
}

.habit-streak-badge {
  font-size: .75rem;
  font-weight: 600;
  background: var(--bg);
  color: var(--muted);
  padding: 3px 10px;
  border-radius: 20px;
  white-space: nowrap;
  flex-shrink: 0;
}

/* ── Heatmap ── */
.habit-history-sel {
  display: block;
  width: 100%;
  padding: 10px 12px;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  font-size: .95rem;
  background: var(--surface);
  margin-bottom: 12px;
  -webkit-appearance: none;
  appearance: none;
}

.heatmap-grid {
  display: flex;
  gap: 3px;
  min-width: max-content;
}
.heatmap-col {
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.heatmap-cell {
  width: 14px;
  height: 14px;
  border-radius: 3px;
  background: var(--border);
  cursor: pointer;
  transition: opacity .15s;
}
.heatmap-cell.empty   { background: transparent; cursor: default; }
.heatmap-cell.filled  { background: var(--accent); }
.heatmap-cell.today   { outline: 2px solid #374151; outline-offset: 1px; }
.heatmap-cell:not(.empty):hover { opacity: .7; }

/* ── 習慣統計格 ── */
.habit-stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0;
  text-align: center;
  margin-top: 12px;
}
.habit-stats-grid .stat-item { padding: 8px 0; }
.habit-stats-grid .stat-value { font-size: 1.3rem; font-weight: 700; color: var(--accent); }
.habit-stats-grid .stat-label { font-size: .72rem; color: var(--muted); margin-top: 2px; }

/* ── 習慣色點 ── */
.habit-color-dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  flex-shrink: 0;
  margin-right: 4px;
}
