:root {
  color-scheme: dark;
  --bg: #151824;
  --surface: #1d2131;
  --surface-2: #262b3e;
  --line: #333a52;
  --text: #e9ebf3;
  --muted: #9aa1b8;
  --accent: #a9b8ff;
  --accent-ink: #151824;
  --good: #8fd16a;
  --bad: #ef8a7a;
  --danger: #ef6f6f;
  --radius: 16px;
  --shadow: 0 10px 30px rgb(0 0 0 / 0.35);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: light) {
  :root {
    color-scheme: light;
    --bg: #f6f4ef;
    --surface: #ffffff;
    --surface-2: #efede6;
    --line: #dcd8cc;
    --text: #22242c;
    --muted: #6b6f7c;
    --accent: #4f5fc9;
    --accent-ink: #ffffff;
    --good: #3f8f2a;
    --bad: #c4513e;
    --danger: #c43c3c;
    --shadow: 0 10px 30px rgb(30 30 50 / 0.12);
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 var(--font);
  min-height: 100dvh;
  padding-bottom: calc(76px + env(safe-area-inset-bottom));
  -webkit-tap-highlight-color: transparent;
}

h1, h2, h3 { line-height: 1.2; margin: 0; }
h2 { font-size: 1.35rem; font-weight: 650; }
h3 { font-size: 0.8rem; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); margin: 22px 0 10px; }
p { margin: 0 0 10px; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }

/* ---------- layout */

.top {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 16px 6px;
  max-width: 680px; margin: 0 auto;
}
.brand { display: flex; align-items: center; gap: 10px; color: var(--text); text-decoration: none; font-weight: 650; letter-spacing: 0.01em; }

main { max-width: 680px; margin: 0 auto; padding: 8px 16px 24px; outline: none; }

.section-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 8px 0 4px; }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
  margin: 14px 0;
}

/* ---------- tab bar */

.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(5, 1fr);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabs a {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 9px 0 10px; color: var(--muted); text-decoration: none; font-size: 0.72rem;
}
.tabs a .ico { font-size: 1.15rem; line-height: 1.2; }
.tabs a[aria-current="page"] { color: var(--accent); }

@media (min-width: 760px) {
  .tabs { left: 50%; right: auto; transform: translateX(-50%); width: 680px; bottom: 12px; border: 1px solid var(--line); border-radius: 16px; padding-bottom: 0; }
  body { padding-bottom: 96px; }
}

/* ---------- controls */

button, .button {
  font: inherit; color: var(--text); background: var(--surface-2);
  border: 1px solid var(--line); border-radius: 12px; padding: 10px 16px;
  cursor: pointer; min-height: 44px;
}
button:hover { border-color: var(--muted); }
button:focus-visible, a:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
button.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; font-weight: 600; }
button.ghost { background: transparent; }
button.danger { color: var(--danger); background: transparent; border-color: color-mix(in srgb, var(--danger) 45%, transparent); }
button.small { min-height: 34px; padding: 6px 12px; font-size: 0.85rem; }
button.big { width: 100%; min-height: 54px; font-size: 1.1rem; border-radius: 14px; }
button:disabled { opacity: 0.5; cursor: default; }

input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface-2);
  border: 1px solid var(--line); border-radius: 10px; padding: 9px 12px; width: 100%;
  min-height: 44px;
}
textarea { min-height: 80px; resize: vertical; }
label.field { display: block; margin: 0 0 14px; }
label.field > span, .field-label { display: block; font-size: 0.85rem; color: var(--muted); margin-bottom: 6px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  border-radius: 999px; padding: 8px 14px; min-height: 40px; background: var(--surface-2);
}
.chip[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); border-color: transparent; font-weight: 600; }

.segmented { display: flex; gap: 6px; flex-wrap: wrap; }
.segmented label { flex: 1 1 0; min-width: 0; }
.segmented input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.segmented span {
  display: flex; align-items: center; justify-content: center; text-align: center;
  min-height: 42px; padding: 6px 8px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface-2); cursor: pointer; font-size: 0.92rem;
}
.segmented input:checked + span { background: var(--accent); color: var(--accent-ink); border-color: transparent; font-weight: 600; }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.segmented input:disabled + span { opacity: 0.55; cursor: not-allowed; }

.swatches { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.swatches label { position: relative; }
.swatches input[type=radio] { position: absolute; opacity: 0; }
.swatches span { display: block; width: 32px; height: 32px; border-radius: 50%; border: 3px solid transparent; cursor: pointer; }
.swatches input:checked + span { border-color: var(--text); }
.swatches input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

.row { display: flex; gap: 10px; align-items: center; }
.row.end { justify-content: flex-end; }
.row.wrap { flex-wrap: wrap; }
.spacer { flex: 1; }

.dot { width: 10px; height: 10px; border-radius: 50%; flex: none; display: inline-block; }

.badge { font-size: 0.72rem; padding: 2px 8px; border-radius: 999px; border: 1px solid var(--line); color: var(--muted); white-space: nowrap; }
.badge.good { color: var(--good); border-color: color-mix(in srgb, var(--good) 45%, transparent); }
.badge.bad { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 45%, transparent); }

/* ---------- meditate */

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin: 14px 0; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 12px; text-align: center; }
.stat b { display: block; font-size: 1.5rem; font-weight: 650; }
.stat span { font-size: 0.78rem; color: var(--muted); }

.session { text-align: center; padding: 10px 0 0; }
.ring { position: relative; width: min(78vw, 320px); aspect-ratio: 1; margin: 12px auto 18px; }
.ring svg { width: 100%; height: 100%; transform: rotate(-90deg); }
.ring circle { fill: none; stroke-width: 6; }
.ring .track { stroke: var(--surface-2); }
.ring .progress { stroke: var(--accent); stroke-linecap: round; transition: stroke-dashoffset 0.5s linear; }
.ring-text { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; }
.ring-text .time { font-size: clamp(2.6rem, 13vw, 4rem); font-weight: 300; font-variant-numeric: tabular-nums; letter-spacing: 0.02em; }
.ring-text .sub { color: var(--muted); font-size: 0.9rem; }
.session.paused .progress { stroke: var(--muted); }
.session .controls { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; max-width: 420px; margin: 0 auto; }
.session .controls .primary { grid-column: 1 / -1; }
.breathe { animation: breathe 10s ease-in-out infinite; }
@keyframes breathe { 0%, 100% { opacity: 0.55; } 50% { opacity: 1; } }
@media (prefers-reduced-motion: reduce) { .breathe { animation: none; } .ring .progress { transition: none; } }

/* ---------- log */

.type-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.type-btn {
  display: flex; align-items: center; gap: 10px; text-align: left;
  padding: 12px 14px; min-height: 56px; border-radius: 14px;
  background: var(--surface); border: 1px solid var(--line);
  border-left: 5px solid var(--c, var(--accent));
}
.type-btn .name { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.type-btn:active { transform: scale(0.98); }

/* ---------- entries list */

.entries { list-style: none; margin: 0; padding: 0; }
.entries li { border-bottom: 1px solid var(--line); }
.entries li:last-child { border-bottom: 0; }
.entry {
  display: flex; align-items: center; gap: 12px; width: 100%; text-align: left;
  background: none; border: 0; border-radius: 0; padding: 12px 2px; min-height: 52px;
}
.entry .main { flex: 1; min-width: 0; }
.entry .name { overflow-wrap: anywhere; }
.entry .meta, .entry .note { display: block; }
.entry .meta { font-size: 0.82rem; color: var(--muted); }
.entry .note { font-size: 0.85rem; color: var(--muted); font-style: italic; overflow-wrap: anywhere; }
.entry time { color: var(--muted); font-size: 0.85rem; font-variant-numeric: tabular-nums; }
.day-label { position: sticky; top: 0; background: var(--bg); padding: 14px 0 6px; font-weight: 600; font-size: 0.9rem; z-index: 1; }

.empty { text-align: center; color: var(--muted); padding: 30px 10px; }

/* ---------- insights */

.grid-wrap { overflow-x: auto; margin: 0 -4px; padding: 0 4px; }
.heat { border-collapse: separate; border-spacing: 3px; font-size: 0.8rem; }
.heat th { font-weight: 500; text-align: left; white-space: nowrap; padding-right: 8px; color: var(--text); max-width: 150px; overflow: hidden; text-overflow: ellipsis; }
.heat thead th { color: var(--muted); font-size: 0.7rem; text-align: center; padding: 0; }
.heat td { width: 16px; height: 16px; border-radius: 4px; background: var(--surface-2); padding: 0; }
.heat td.on { background: var(--c); }

.pair { padding: 14px 0; border-bottom: 1px solid var(--line); }
.pair:last-child { border-bottom: 0; }
.pair .title { margin-bottom: 8px; }
.bar { display: grid; grid-template-columns: 74px 1fr 46px; gap: 8px; align-items: center; font-size: 0.82rem; color: var(--muted); margin: 4px 0; }
.bar .track { height: 10px; border-radius: 6px; background: var(--surface-2); overflow: hidden; }
.bar .fill { display: block; height: 100%; border-radius: 6px; background: var(--c, var(--accent)); }
.bar .fill.without { opacity: 0.45; }
.bar .pct { text-align: right; font-variant-numeric: tabular-nums; color: var(--text); }
.delta { font-weight: 600; font-size: 0.85rem; }
.delta.good { color: var(--good); }
.delta.bad { color: var(--bad); }

.notice { font-size: 0.85rem; color: var(--muted); border-left: 3px solid var(--line); padding: 4px 0 4px 12px; margin: 14px 0; }

/* ---------- settings */

.type-row { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.type-row:last-child { border-bottom: 0; }
.type-row .main { flex: 1; min-width: 0; }
.type-row .name { display: block; overflow-wrap: anywhere; }
.type-row .meta { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; margin-top: 2px; }
.type-row .actions { display: flex; gap: 6px; flex: none; }
.type-row.archived .name { color: var(--muted); text-decoration: line-through; }

/* ---------- sheet & toast */

dialog#sheet {
  width: min(560px, 100%); max-height: 92dvh; margin: auto auto 0; padding: 0;
  border: 1px solid var(--line); border-radius: 22px 22px 0 0;
  background: var(--surface); color: var(--text); box-shadow: var(--shadow);
}
@media (min-width: 600px) { dialog#sheet { margin: auto; border-radius: 22px; } }
dialog#sheet::backdrop { background: rgb(0 0 0 / 0.5); }
.sheet-body { padding: 20px 18px calc(18px + env(safe-area-inset-bottom)); }
.sheet-body h2 { display: flex; align-items: center; gap: 10px; margin-bottom: 16px; }
.sheet-actions { display: flex; gap: 10px; margin-top: 18px; flex-wrap: wrap; }
.sheet-actions .spacer { flex: 1; }

#toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(86px + env(safe-area-inset-bottom)); z-index: 20;
  width: max-content; max-width: calc(100% - 32px);
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  background: var(--text); color: var(--bg); padding: 10px 10px 10px 16px;
  border-radius: 14px; box-shadow: var(--shadow); font-size: 0.92rem;
}
#toast.error { background: var(--danger); color: #fff; }
#toast button { min-height: 34px; padding: 4px 12px; background: transparent; color: inherit; border-color: currentColor; font-size: 0.85rem; }
@media (min-width: 760px) { #toast { bottom: 100px; } }

.feel-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; }
.feel-grid button[aria-pressed="true"] { opacity: 0.6; }
