:root {
  --indigo: #4f46e5;
  --indigo-600: #4338ca;
  --indigo-300: #a5b4fc;
  --warm: #f97316;
  --warm-soft: #fed7aa;
  --bg: #f6f5fb;
  --surface: #ffffff;
  --surface-2: #f1f0fb;
  --text: #20203a;
  --muted: #6b6b85;
  --border: #e4e2f2;
  --good: #16a34a;
  --good-soft: #dcfce7;
  --danger: #dc2626;
  --ring-track: #e7e5f7;
  --shadow: 0 6px 22px rgba(79, 70, 229, 0.10);
  --radius: 18px;
  --radius-sm: 12px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

[data-theme="dark"] {
  --indigo: #818cf8;
  --indigo-600: #6366f1;
  --indigo-300: #4f46e5;
  --warm: #fb923c;
  --warm-soft: #7c2d12;
  --bg: #14131f;
  --surface: #1e1c2e;
  --surface-2: #272540;
  --text: #ece9fb;
  --muted: #a6a2c4;
  --border: #322f4d;
  --good: #4ade80;
  --good-soft: #14331f;
  --danger: #f87171;
  --ring-track: #322f4d;
  --shadow: 0 6px 22px rgba(0, 0, 0, 0.4);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}

body {
  max-width: 680px;
  margin: 0 auto;
  min-height: 100vh;
  padding-bottom: calc(96px + var(--safe-bottom));
  overflow-x: hidden;
}

img, svg { max-width: 100%; }

h1, h2, h3 { margin: 0 0 6px; line-height: 1.25; }
h2 { font-size: 1.1rem; }
h3 { font-size: 1.05rem; }
p { margin: 0 0 8px; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }

.visually-hidden, .skip-link {
  position: absolute;
  width: 1px; height: 1px;
  margin: -1px; padding: 0; border: 0;
  clip: rect(0 0 0 0);
  overflow: hidden;
}
.skip-link:focus {
  position: fixed;
  top: 8px; left: 8px;
  width: auto; height: auto;
  margin: 0; padding: 10px 14px;
  clip: auto;
  background: var(--indigo);
  color: #fff;
  border-radius: var(--radius-sm);
  z-index: 100;
}

/* Topbar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(12px + var(--safe-top)) 16px 12px;
  background: linear-gradient(135deg, var(--indigo) 0%, var(--indigo-600) 100%);
  color: #fff;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-mark {
  display: grid;
  place-items: center;
  width: 40px; height: 40px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.18);
  color: #fff;
  flex-shrink: 0;
}
.brand-text { display: flex; flex-direction: column; min-width: 0; }
.brand-text strong { font-size: 1.05rem; }
.brand-text small { opacity: 0.85; font-size: 0.78rem; }

.icon-btn {
  display: grid;
  place-items: center;
  width: 42px; height: 42px;
  border: none;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.18);
  color: #fff;
  font-size: 1.2rem;
  cursor: pointer;
  flex-shrink: 0;
}
.icon-btn:hover { background: rgba(255, 255, 255, 0.28); }

/* Tabbar */
.tabbar {
  position: sticky;
  top: calc(64px + var(--safe-top));
  z-index: 15;
  display: flex;
  gap: 6px;
  padding: 10px 12px;
  background: var(--bg);
}
.tab-btn {
  flex: 1;
  padding: 10px 8px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted);
  font-size: 0.92rem;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s, color .15s;
}
.tab-btn.is-active {
  background: var(--indigo);
  color: #fff;
  border-color: var(--indigo);
}

main { padding: 6px 12px 0; }
.view { display: none; flex-direction: column; gap: 14px; }
.view.is-active { display: flex; }

/* Cards */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}

/* Timer hero */
.hero-card { text-align: center; }
.ring-wrap {
  position: relative;
  width: 220px;
  max-width: 70vw;
  margin: 8px auto 6px;
  aspect-ratio: 1 / 1;
}
.ring { width: 100%; height: 100%; transform: rotate(-90deg); }
.ring-bg { fill: none; stroke: var(--ring-track); stroke-width: 14; }
.ring-fg {
  fill: none;
  stroke: url(#noGrad);
  stroke: var(--indigo);
  stroke-width: 14;
  stroke-linecap: round;
  stroke-dasharray: 540;
  stroke-dashoffset: 0;
  transition: stroke-dashoffset .4s ease, stroke .3s;
}
.ring-fg.is-over { stroke: var(--warm); }
.ring-center {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.ring-time {
  font-size: 2.4rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.5px;
}
.ring-sub { color: var(--muted); font-size: 0.9rem; }

.timer-state {
  display: inline-block;
  margin: 4px auto 12px;
  padding: 5px 14px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  font-weight: 600;
  font-size: 0.85rem;
}
.timer-state.running { background: var(--good-soft); color: var(--good); }
.timer-state.over { background: var(--warm-soft); color: var(--warm); }

.btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
}

.btn {
  appearance: none;
  border: 1px solid transparent;
  border-radius: 999px;
  padding: 11px 18px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform .08s, background .15s, opacity .15s;
}
.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--indigo); color: #fff; }
.btn-primary:hover { background: var(--indigo-600); }
.btn-ghost {
  background: var(--surface-2);
  color: var(--text);
  border-color: var(--border);
}
.btn-ghost:hover { background: var(--border); }
.danger-text { color: var(--danger); }

.hint { margin-top: 10px; }
.hint.over { color: var(--warm); font-weight: 600; }

/* Budget form */
.budget-form { display: flex; flex-direction: column; gap: 10px; }
.budget-form label { font-size: 0.85rem; color: var(--muted); }
.stepper {
  display: flex;
  align-items: stretch;
  gap: 8px;
}
.stepper input {
  flex: 1;
  text-align: center;
  font-size: 1.3rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.step-btn {
  width: 52px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  color: var(--indigo);
  font-size: 1.5rem;
  font-weight: 700;
  cursor: pointer;
}
.step-btn:hover { background: var(--border); }

input[type="number"], input[type="text"] {
  width: 100%;
  padding: 11px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  color: var(--text);
  font-size: 1rem;
  font-family: inherit;
}
input::placeholder { color: var(--muted); }

.chip-row, .suggest-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}
.chip {
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text);
  padding: 7px 13px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
}
.chip:hover { background: var(--indigo); color: #fff; border-color: var(--indigo); }

/* Streak */
.streak-card { text-align: center; background: linear-gradient(135deg, var(--surface) 0%, var(--surface-2) 100%); }
.streak-num {
  font-size: 3rem;
  font-weight: 800;
  color: var(--indigo);
  line-height: 1;
}
.streak-label { font-weight: 600; margin-top: 4px; }

/* Pakt */
.rule-form { margin-bottom: 10px; }
.rule-input-row { display: flex; gap: 8px; }
.rule-input-row input { flex: 1; min-width: 0; }
.rule-input-row .btn { white-space: nowrap; flex-shrink: 0; }

.pakt-head { display: flex; align-items: center; justify-content: space-between; }
.badge {
  background: var(--indigo);
  color: #fff;
  border-radius: 999px;
  padding: 2px 10px;
  font-size: 0.8rem;
  font-weight: 700;
}

.rule-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.rule-item {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px;
  background: var(--surface-2);
}
.rule-item.is-sealed { border-color: var(--good); background: var(--good-soft); }
.rule-text {
  font-weight: 600;
  margin-bottom: 10px;
  overflow-wrap: anywhere;
  word-break: break-word;
}
.rule-checks { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.check {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 11px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  user-select: none;
}
.check input { width: auto; }
.check.checked { background: var(--good); color: #fff; border-color: var(--good); }
.rule-del {
  margin-left: auto;
  border: none;
  background: transparent;
  color: var(--danger);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  padding: 6px 8px;
  border-radius: 8px;
}
.rule-del:hover { background: var(--surface); }

.empty { color: var(--muted); text-align: center; padding: 14px 6px; font-size: 0.9rem; }

.pakt-seal {
  display: flex;
  align-items: center;
  gap: 14px;
  border: 1px solid var(--good);
  background: var(--good-soft);
}
.seal-icon { font-size: 2.2rem; }

/* Verlauf */
.history-stats { display: flex; gap: 10px; }
.stat {
  flex: 1;
  text-align: center;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  padding: 12px 6px;
}
.stat-num { font-size: 1.6rem; font-weight: 800; color: var(--indigo); }
.stat-label { font-size: 0.78rem; color: var(--muted); }

.history-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.history-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
}
.history-dot {
  width: 14px; height: 14px;
  border-radius: 50%;
  flex-shrink: 0;
  background: var(--ring-track);
}
.history-dot.kept { background: var(--good); }
.history-dot.over { background: var(--warm); }
.history-info { flex: 1; min-width: 0; }
.history-date { font-weight: 600; font-size: 0.92rem; }
.history-bar {
  height: 7px;
  border-radius: 999px;
  background: var(--ring-track);
  margin-top: 6px;
  overflow: hidden;
}
.history-fill { height: 100%; background: var(--indigo); border-radius: 999px; }
.history-fill.over { background: var(--warm); }
.history-meta { font-size: 0.8rem; color: var(--muted); white-space: nowrap; flex-shrink: 0; }

/* Install button */
.install-btn {
  position: fixed;
  left: 50%;
  bottom: calc(16px + var(--safe-bottom));
  transform: translateX(-50%);
  z-index: 30;
  border: none;
  border-radius: 999px;
  padding: 12px 22px;
  background: var(--warm);
  color: #fff;
  font-weight: 700;
  font-size: 0.95rem;
  cursor: pointer;
  box-shadow: 0 8px 24px rgba(249, 115, 22, 0.4);
}

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(80px + var(--safe-bottom));
  transform: translateX(-50%) translateY(10px);
  z-index: 40;
  max-width: 90vw;
  padding: 12px 18px;
  border-radius: 999px;
  background: var(--text);
  color: var(--bg);
  font-size: 0.9rem;
  font-weight: 600;
  opacity: 0;
  transition: opacity .2s, transform .2s;
  pointer-events: none;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* Modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(20, 19, 31, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.modal {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 22px;
  max-width: 360px;
  width: 100%;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}
.modal h3 { margin-bottom: 8px; }
.modal p { color: var(--muted); }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 18px; }

/* Focus states */
:focus-visible {
  outline: 3px solid var(--indigo);
  outline-offset: 2px;
  border-radius: 6px;
}
.tab-btn:focus-visible, .btn:focus-visible, .icon-btn:focus-visible { outline-offset: 3px; }

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

@media (max-width: 360px) {
  .rule-input-row { flex-direction: column; }
  .rule-input-row .btn { width: 100%; }
  .ring-time { font-size: 2rem; }
}
