:root {
  --background: #f5f5f7;
  --surface: #ffffff;
  --ink: #1d1d1f;
  --muted: rgba(0, 0, 0, 0.56);
  --tertiary: rgba(0, 0, 0, 0.4);
  --line: rgba(0, 0, 0, 0.12);
  --blue: #0071e3;
  --blue-dark: #0066cc;
  --danger: #b42318;
  --font: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html { background: var(--background); }

body {
  margin: 0;
  min-width: 280px;
  overflow-x: clip;
  background: var(--background);
  color: var(--ink);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

button, input, textarea { font: inherit; }
button { color: inherit; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.app-shell {
  width: min(100%, 460px);
  min-height: 100dvh;
  margin: 0 auto;
  overflow-x: clip;
  padding: calc(var(--safe-top) + 24px) 24px calc(96px + var(--safe-bottom));
  background: var(--background);
}

.topbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: 54px;
}

.topbar h1 {
  margin: 0;
  font-size: clamp(32px, 9vw, 40px);
  line-height: 1.08;
  letter-spacing: -1.2px;
  font-weight: 700;
}

.topbar-subtitle {
  margin: 18px 0 0;
  color: var(--muted);
  font-size: 17px;
  line-height: 1.4;
  letter-spacing: -0.28px;
}

.icon-button {
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  margin: -6px -8px 0 0;
  border: 0;
  background: transparent;
  border-radius: 50%;
  cursor: pointer;
}

.icon-button svg {
  width: 30px;
  height: 30px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon-button:focus-visible, button:focus-visible, input:focus-visible, textarea:focus-visible, .file-button:focus-within {
  outline: 3px solid rgba(0, 113, 227, 0.28);
  outline-offset: 2px;
}

.view { display: none; }
.view.is-active { display: block; animation: view-in 180ms ease-out; }

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

.section-kicker {
  margin: 0 0 12px;
  font-size: 28px;
  line-height: 1.14;
  letter-spacing: -0.5px;
  font-weight: 650;
}

.streak-value {
  margin: 0;
  font-size: clamp(52px, 15vw, 72px);
  line-height: 1.04;
  letter-spacing: -3px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  text-wrap: nowrap;
}

.support-line {
  margin: 20px 0 0;
  color: var(--muted);
  font-size: 19px;
  line-height: 1.42;
  letter-spacing: -0.32px;
}

.stats-rail {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0;
  margin: 48px 0 38px;
}

.stats-rail div { min-width: 0; padding: 0 16px; }
.stats-rail div:first-child { padding-left: 0; }
.stats-rail div:last-child { padding-right: 0; }
.stats-rail div + div { border-left: 1px solid var(--line); }

.stats-rail dt {
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 15px;
  line-height: 1.3;
}

.stats-rail dd {
  margin: 0;
  color: var(--blue);
  font-size: clamp(24px, 7vw, 30px);
  line-height: 1.12;
  letter-spacing: -0.7px;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.primary-actions { display: grid; gap: 8px; }

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: 52px;
  padding: 12px 18px;
  border-radius: 8px;
  font-size: 17px;
  line-height: 1.2;
  font-weight: 550;
  letter-spacing: -0.2px;
  cursor: pointer;
}

.button-primary { border: 1px solid var(--blue); background: var(--blue); color: #fff; }
.button-primary:active { background: var(--blue-dark); transform: scale(0.99); }
.button-secondary { border: 1px solid var(--line); background: var(--surface); color: var(--blue); }

.text-button, .small-link {
  min-height: 44px;
  border: 0;
  background: transparent;
  color: var(--blue);
  cursor: pointer;
}

.text-button { font-size: 17px; }
.small-link { padding: 0; font-size: 14px; }

.today-section { margin-top: 46px; }
.section-heading-row, .goal-label-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 16px;
}

.section-heading-row h3, .goal-label-row h3, .chart-section h3, .insight-section h3, .settings-group h3 {
  margin: 0;
  font-size: 24px;
  line-height: 1.2;
  letter-spacing: -0.45px;
  font-weight: 650;
}

.event-list { margin-top: 16px; }

.event-row {
  display: grid;
  grid-template-columns: 72px 1fr auto;
  align-items: center;
  gap: 12px;
  min-height: 58px;
  border-bottom: 1px solid var(--line);
  font-size: 16px;
}

.event-time { font-variant-numeric: tabular-nums; }
.event-meta { color: var(--muted); text-align: right; }
.empty-state { padding: 22px 0; color: var(--muted); font-size: 16px; line-height: 1.5; }

.goal-section { margin-top: 44px; }
.goal-label-row span { color: var(--muted); font-size: 15px; }
.progress-track { height: 8px; margin-top: 16px; overflow: hidden; border-radius: 8px; background: #e2e2e5; }
.progress-track span { display: block; width: 0; height: 100%; border-radius: inherit; background: var(--blue); transition: width 300ms ease; }
.goal-section > p { margin: 10px 0 0; color: var(--muted); font-size: 14px; font-variant-numeric: tabular-nums; }

.view-header { margin-bottom: 30px; }
.view-header h2 { margin: 0; font-size: 40px; line-height: 1.08; letter-spacing: -1.2px; }
.view-header p { margin: 12px 0 0; color: var(--muted); font-size: 16px; line-height: 1.5; }

.segmented-control, .strength-control {
  display: grid;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
}

.segmented-control { grid-template-columns: repeat(3, 1fr); margin-bottom: 24px; }
.segmented-control button { min-height: 44px; border: 0; border-right: 1px solid var(--line); background: transparent; color: var(--muted); cursor: pointer; }
.segmented-control button:last-child { border-right: 0; }
.segmented-control button.is-selected { background: var(--blue); color: #fff; }

.history-day { margin-bottom: 30px; }
.history-day h3 { margin: 0 0 8px; color: var(--muted); font-size: 14px; font-weight: 550; }
.history-item { display: grid; grid-template-columns: 56px 1fr auto; gap: 12px; align-items: center; min-height: 62px; border-bottom: 1px solid var(--line); }
.history-item time { font-variant-numeric: tabular-nums; }
.history-item strong { font-size: 16px; font-weight: 560; }
.history-item span { color: var(--muted); font-size: 14px; text-align: right; }

.summary-list { margin: 0; }
.summary-list div { display: flex; align-items: baseline; justify-content: space-between; gap: 20px; min-height: 62px; padding: 16px 0; border-bottom: 1px solid var(--line); }
.summary-list dt { color: var(--muted); font-size: 16px; }
.summary-list dd { margin: 0; font-size: 20px; font-weight: 620; letter-spacing: -0.3px; text-align: right; }

.chart-section { margin-top: 44px; }
.chart-section .section-heading-row span { color: var(--muted); font-size: 14px; }
.bar-chart { display: grid; grid-template-columns: repeat(7, 1fr); align-items: end; gap: 10px; height: 190px; margin-top: 24px; padding-top: 12px; border-bottom: 1px solid var(--line); }
.bar-column { display: grid; grid-template-rows: 1fr auto; align-items: end; gap: 10px; height: 100%; text-align: center; color: var(--muted); font-size: 12px; }
.bar-column span { display: block; width: 100%; min-height: 4px; border-radius: 5px 5px 0 0; background: var(--blue); }
.bar-column em { font-style: normal; }

.insight-section { margin-top: 38px; padding: 24px 0; border-top: 1px solid var(--line); }
.insight-section p { margin: 12px 0 0; color: var(--muted); font-size: 16px; line-height: 1.55; }

.settings-form { display: grid; gap: 0; }
.settings-form > label, .setup-panel label { display: grid; gap: 8px; padding: 16px 0; border-bottom: 1px solid var(--line); }
.settings-form > label > span:first-child, .setup-panel label > span:first-child { color: var(--muted); font-size: 14px; }
.settings-form input, .setup-panel input, textarea { width: 100%; border: 0; outline: 0; background: transparent; color: var(--ink); }
.settings-form input, .setup-panel input { min-height: 34px; font-size: 18px; }
.settings-form .button { margin-top: 22px; }

.input-with-unit { display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 8px; }
.input-with-unit em { color: var(--muted); font-style: normal; }

.settings-group { margin-top: 44px; padding-top: 26px; border-top: 1px solid var(--line); }
.settings-group p { margin: 10px 0 20px; color: var(--muted); font-size: 15px; line-height: 1.5; }
.settings-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.file-button { position: relative; }
.file-button input { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }
.install-steps { margin: 14px 0 0; padding-left: 24px; color: var(--muted); font-size: 15px; line-height: 1.7; }
.danger-zone { padding-bottom: 12px; }
.danger-button { min-height: 48px; padding: 0; border: 0; background: transparent; color: var(--danger); font-size: 16px; cursor: pointer; }

.tabbar {
  position: fixed;
  z-index: 10;
  left: 0;
  right: 0;
  bottom: 0;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  width: auto;
  max-width: 460px;
  margin: 0 auto;
  min-height: calc(66px + var(--safe-bottom));
  padding: 8px 12px var(--safe-bottom);
  border-top: 1px solid var(--line);
  background: rgba(245, 245, 247, 0.92);
  backdrop-filter: saturate(180%) blur(20px);
}

.tabbar button { min-height: 50px; border: 0; background: transparent; color: var(--muted); font-size: 13px; cursor: pointer; }
.tabbar button.is-active { color: var(--blue); font-weight: 620; }

.modal-layer {
  position: fixed;
  z-index: 30;
  inset: 0;
  display: none;
  align-items: flex-end;
  justify-content: center;
}
.modal-layer.is-open { display: flex; }
.modal-backdrop { position: absolute; inset: 0; border: 0; background: rgba(0, 0, 0, 0.44); }

.bottom-sheet {
  position: relative;
  z-index: 1;
  width: min(100%, 460px);
  max-height: calc(100dvh - var(--safe-top) - 12px);
  overflow: auto;
  padding: 14px 22px calc(24px + var(--safe-bottom));
  border-radius: 12px 12px 0 0;
  background: var(--surface);
  box-shadow: 0 -10px 30px rgba(0, 0, 0, 0.12);
  animation: sheet-in 220ms ease-out;
}

@keyframes sheet-in {
  from { transform: translateY(24px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

.sheet-handle { width: 44px; height: 5px; margin: 0 auto 26px; border-radius: 5px; background: #d2d2d7; }
.bottom-sheet h2 { margin: 0 0 26px; font-size: 32px; line-height: 1.1; letter-spacing: -0.8px; }
.bottom-sheet fieldset { margin: 0 0 26px; padding: 0; border: 0; }
.bottom-sheet legend, .textarea-label > span { display: block; margin-bottom: 12px; font-size: 17px; font-weight: 560; }
.strength-control { grid-template-columns: repeat(5, 1fr); }
.strength-control label, .trigger-control label { position: relative; cursor: pointer; }
.strength-control input, .trigger-control input { position: absolute; opacity: 0; pointer-events: none; }
.strength-control span { display: grid; place-items: center; min-height: 48px; border-right: 1px solid var(--line); font-size: 18px; }
.strength-control label:last-child span { border-right: 0; }
.strength-control input:checked + span { background: var(--blue); color: #fff; }

.trigger-control { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.trigger-control span { display: grid; place-items: center; min-height: 46px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); font-size: 14px; }
.trigger-control input:checked + span { border-color: var(--blue); background: var(--blue); color: #fff; }

.textarea-label { position: relative; display: block; margin-bottom: 24px; }
.textarea-label textarea { min-height: 112px; resize: none; padding: 14px 14px 34px; border: 1px solid var(--line); border-radius: 8px; font-size: 16px; line-height: 1.5; }
.textarea-label small { position: absolute; right: 12px; bottom: 10px; color: var(--tertiary); }
.bottom-sheet .button + .button { margin-top: 10px; }
.sheet-support { margin: 16px 0 0; color: var(--muted); font-size: 14px; text-align: center; }
.bottom-sheet-compact { padding-bottom: calc(30px + var(--safe-bottom)); }
.sheet-copy { margin: -8px 0 24px; color: var(--muted); font-size: 16px; line-height: 1.55; }
.sheet-cancel { width: 100%; margin-top: 4px; }

.setup-layer { align-items: stretch; background: var(--background); overflow: auto; }
.setup-layer.is-open { display: block; }
.setup-panel { width: min(100%, 460px); min-height: 100dvh; margin: 0 auto; padding: calc(var(--safe-top) + 52px) 24px calc(var(--safe-bottom) + 32px); }
.setup-mark { width: 58px; height: 58px; margin-bottom: 42px; border-radius: 50%; background: var(--blue); }
.setup-mark span { display: block; width: 18px; height: 30px; margin: 0 auto; padding-top: 12px; border-right: 4px solid #fff; border-bottom: 4px solid #fff; transform: rotate(42deg) translate(-2px, 4px); }
.setup-panel h2 { margin: 0; font-size: 42px; line-height: 1.06; letter-spacing: -1.4px; }
.setup-panel > p { margin: 16px 0 30px; color: var(--muted); font-size: 17px; line-height: 1.55; }
.setup-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.setup-panel .button { margin-top: 28px; }

.toast {
  position: fixed;
  z-index: 60;
  left: 50%;
  bottom: calc(84px + var(--safe-bottom));
  max-width: calc(100% - 40px);
  padding: 11px 16px;
  transform: translate(-50%, 20px);
  border-radius: 8px;
  background: rgba(29, 29, 31, 0.92);
  color: #fff;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 180ms ease, transform 180ms ease;
}
.toast.is-visible { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 360px) {
  .app-shell { padding-left: 18px; padding-right: 18px; }
  .stats-rail div { padding: 0 10px; }
  .stats-rail dd { font-size: 22px; }
  .trigger-control { grid-template-columns: repeat(3, 1fr); }
  .setup-grid { grid-template-columns: 1fr; gap: 0; }
}

@media (min-width: 600px) {
  body { background: #e8e8eb; }
  .app-shell { box-shadow: 0 0 40px rgba(0, 0, 0, 0.08); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
