:root {
  --bg: #f7f4ef;
  --surface: #ffffff;
  --border: #e8e2d9;
  --text: #2c2825;
  --text-muted: #7a7268;
  --primary: #5b8a72;
  --primary-light: #e8f2ec;
  --wake: #f0a04b;
  --wake-light: #fef3e2;
  --nap: #7b9fd4;
  --nap-light: #edf2fa;
  --night: #6b5b95;
  --night-light: #f0ecf7;
  --waking: #c97b84;
  --waking-light: #fceef0;
  --danger: #c45c5c;
  --radius: 14px;
  --shadow: 0 2px 12px rgba(44, 40, 37, 0.06);
  --font: "Segoe UI", system-ui, -apple-system, sans-serif;
}

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

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

.app {
  max-width: 720px;
  margin: 0 auto;
  padding: 24px 20px 48px;
}

.header-top {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.help-btn {
  margin-left: auto;
}

.header h1 {
  font-size: 1.6rem;
  font-weight: 700;
}

.badge {
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  background: var(--primary-light);
  color: var(--primary);
  padding: 3px 8px;
  border-radius: 6px;
}

.subtitle {
  color: var(--text-muted);
  font-size: 0.9rem;
  margin-top: 4px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-top: 16px;
  box-shadow: var(--shadow);
}

.card h2 {
  font-size: 1rem;
  font-weight: 600;
  margin-bottom: 14px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 14px;
}

.section-header h2 {
  margin-bottom: 0;
}

/* Timers */
.timers {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.timer-block {
  text-align: center;
  padding: 16px;
  border-radius: 10px;
  background: var(--wake-light);
  transition: background 0.25s ease;
}

.timer-block.mode-awake { background: var(--wake-light); }
.timer-block.mode-nap { background: var(--nap-light); }
.timer-block.mode-night { background: var(--night-light); }
.timer-block.mode-waking { background: var(--waking-light); }
.timer-block.mode-idle { background: var(--bg); }

.timer-mode {
  display: inline-block;
  font-size: 0.95rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  margin-bottom: 6px;
  padding: 4px 12px;
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.55);
}

.mode-awake .timer-mode { color: #9a5f10; }
.mode-nap .timer-mode { color: #3d5f8f; }
.mode-night .timer-mode { color: #4a3d6b; }
.mode-waking .timer-mode { color: #8f4a52; }
.mode-idle .timer-mode { color: var(--text-muted); }

.timer-label {
  display: block;
  font-size: 0.8rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.timer-value {
  display: block;
  font-size: 2.4rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  margin: 4px 0;
}

.timer-hint {
  font-size: 0.85rem;
  color: var(--text-muted);
}

.estimates {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.estimate {
  padding: 12px;
  background: var(--bg);
  border-radius: 10px;
  text-align: center;
}

.estimate-label {
  display: block;
  font-size: 0.75rem;
  color: var(--text-muted);
}

.estimate-value {
  display: block;
  font-size: 1.1rem;
  font-weight: 600;
  margin-top: 2px;
}

.hidden {
  display: none !important;
}

/* Buttons */
.actions-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 10px;
}

.btn {
  border: none;
  border-radius: 10px;
  padding: 12px 14px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.1s, opacity 0.15s;
  font-family: inherit;
}

.btn:active {
  transform: scale(0.97);
}

.btn-wake { background: var(--wake-light); color: #9a5f10; }
.btn-nap { background: var(--nap-light); color: #3d5f8f; }
.btn-night { background: var(--night-light); color: #4a3d6b; }
.btn-waking { background: var(--waking-light); color: #8f4a52; }
.btn-primary { background: var(--primary); color: white; }
.btn-secondary { background: var(--bg); color: var(--text); border: 1px solid var(--border); }
.btn-ghost { background: transparent; color: var(--text-muted); }
.btn-danger { background: #fce8e8; color: var(--danger); }
.btn-sm { padding: 6px 12px; font-size: 0.8rem; }

.nap-progress {
  margin-top: 12px;
  font-size: 0.85rem;
  color: var(--text-muted);
}

/* Forms */
.settings-form,
.backdate-form {
  display: grid;
  gap: 12px;
}

label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.85rem;
  font-weight: 500;
}

input,
select {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.95rem;
  font-family: inherit;
  background: var(--bg);
}

/* Timeline */
.timeline {
  position: relative;
  height: 48px;
  background: var(--bg);
  border-radius: 8px;
  overflow: hidden;
}

.timeline-bar {
  position: absolute;
  top: 0;
  height: 100%;
  border-radius: 4px;
  opacity: 0.85;
  min-width: 2px;
}

.timeline-bar.nap { background: var(--nap); }
.timeline-bar.night { background: var(--night); }
.timeline-bar.waking { background: var(--waking); }
.timeline-bar.activity { background: var(--wake); top: 28px; height: 16px; opacity: 0.5; }

.actions-grid .btn.hidden {
  display: none;
}

.backdate-note {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 12px;
}

.state-hint {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 12px;
}

.inline-error {
  min-height: 1.25em;
  margin-top: 10px;
  color: var(--danger);
  font-size: 0.82rem;
  font-weight: 600;
}

.inline-error:empty {
  display: none;
}

.stat-extra {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 2px;
}

.stats-quality {
  color: var(--text-muted);
  font-size: 0.8rem;
  line-height: 1.4;
}

.timeline-labels {
  display: flex;
  justify-content: space-between;
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 6px;
}

.timeline-empty {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 48px;
  color: var(--text-muted);
  font-size: 0.85rem;
}

/* Event list */
.event-list {
  list-style: none;
}

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

.event-item:last-child {
  border-bottom: none;
}

.event-type {
  font-weight: 600;
  font-size: 0.9rem;
}

.event-time {
  font-size: 0.85rem;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.event-note {
  font-size: 0.8rem;
  color: var(--text-muted);
  font-style: italic;
}

.event-controls {
  display: inline-flex;
  align-items: center;
}

.event-edit,
.event-delete {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 1.1rem;
  padding: 4px 8px;
  opacity: 0.5;
}

.event-edit:hover,
.event-delete:hover {
  opacity: 1;
}

.event-edit:hover { color: var(--primary); }
.event-delete:hover {
  color: var(--danger);
}

/* Stats */
.tab-bar {
  display: flex;
  gap: 4px;
  background: var(--bg);
  padding: 3px;
  border-radius: 8px;
}

.tab {
  border: none;
  background: transparent;
  padding: 6px 14px;
  border-radius: 6px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  color: var(--text-muted);
}

.tab.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 3px rgba(0,0,0,0.08);
}

.stats-grid {
  display: grid;
  gap: 14px;
}

.live-stats-card {
  padding: 16px;
  background: linear-gradient(135deg, var(--primary-light), var(--surface));
  border: 1px solid rgba(91, 138, 114, 0.35);
  border-left: 4px solid var(--primary);
  border-radius: 12px;
  box-shadow: 0 3px 14px rgba(91, 138, 114, 0.08);
}

.live-stats-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.live-stats-eyebrow {
  display: block;
  color: #365f4c;
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.live-stats-heading h3 {
  margin-top: 2px;
  font-size: 1.05rem;
}

.live-stats-date {
  margin-top: 2px;
  color: var(--text-muted);
  font-size: 0.78rem;
}

.live-stats-badge {
  flex: 0 0 auto;
  padding: 3px 8px;
  border-radius: 20px;
  background: #365f4c;
  color: white;
  font-size: 0.7rem;
  font-weight: 700;
}

.live-stats-metrics {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.live-stat-metric {
  min-width: 0;
  padding: 10px;
  background: rgba(255, 255, 255, 0.72);
  border: 1px solid rgba(232, 226, 217, 0.8);
  border-radius: 9px;
}

.live-stat-metric > span,
.live-stat-metric > small {
  display: block;
  color: var(--text-muted);
  font-size: 0.73rem;
}

.live-stat-metric > strong {
  display: block;
  margin: 2px 0;
  font-size: 1rem;
  font-variant-numeric: tabular-nums;
}

.live-night-status {
  margin-top: 10px;
  padding: 10px 12px;
  background: var(--night-light);
  border-radius: 9px;
  color: #4a3d6b;
}

.live-night-title {
  display: block;
  margin-bottom: 3px;
  font-size: 0.78rem;
  font-weight: 700;
}

.live-night-details {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 14px;
  font-size: 0.78rem;
}

.live-night-empty {
  font-size: 0.78rem;
}

.live-stats-note {
  margin-top: 10px;
  color: var(--text-muted);
  font-size: 0.75rem;
  line-height: 1.4;
}

.stat-card {
  padding: 14px;
  background: var(--bg);
  border-radius: 10px;
}

.stat-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 8px;
}

.stat-title {
  font-size: 0.85rem;
  font-weight: 600;
}

.stat-value {
  font-size: 1.2rem;
  font-weight: 700;
}

.stat-trend {
  font-size: 0.8rem;
  font-weight: 600;
}

.stat-trend.up { color: var(--primary); }
.stat-trend.down { color: var(--waking); }
.stat-trend.flat { color: var(--text-muted); }
.stat-trend.insufficient { color: var(--text-muted); font-weight: 400; }

.sparkline {
  width: 100%;
  height: 36px;
}

.sparkline polyline {
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.footer {
  margin-top: 24px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.modal.hidden {
  display: none;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(44, 40, 37, 0.4);
}

.modal-content {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  width: 100%;
  max-width: 400px;
  max-height: min(85vh, 640px);
  overflow-y: auto;
  box-shadow: 0 8px 32px rgba(44, 40, 37, 0.15);
}

.modal-content-wide {
  max-width: 520px;
}

.modal-content h2 {
  font-size: 1.1rem;
  margin-bottom: 8px;
}

.modal-desc {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 16px;
}

.modal-form {
  display: grid;
  gap: 16px;
}

.modal-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
}

.help-list {
  list-style: none;
  display: grid;
  gap: 12px;
  margin-bottom: 16px;
}

.help-list li {
  font-size: 0.88rem;
  line-height: 1.45;
  padding-left: 12px;
  border-left: 3px solid var(--border);
}

.help-list strong {
  color: var(--text);
}

.help-tip {
  font-size: 0.85rem;
  color: var(--text-muted);
  background: var(--bg);
  padding: 10px 12px;
  border-radius: 8px;
  margin-bottom: 16px;
}

@media (max-width: 480px) {
  .estimates {
    grid-template-columns: 1fr;
  }

  .actions-grid {
    grid-template-columns: 1fr 1fr;
  }

  .live-stats-card {
    padding: 14px;
  }

  .live-stats-metrics {
    grid-template-columns: 1fr;
  }
}
