:root {
  --bg: var(--tg-theme-secondary-bg-color, #f2f2f7);
  --card: var(--tg-theme-section-bg-color, var(--tg-theme-bg-color, #ffffff));
  --text: var(--tg-theme-text-color, #0b0b0f);
  --hint: var(--tg-theme-hint-color, #8a8a93);
  --accent: var(--tg-theme-button-color, #2a78d6);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --link: var(--tg-theme-link-color, #2a78d6);
  --danger: var(--tg-theme-destructive-text-color, #e5484d);
  --sep: var(--tg-theme-section-separator-color, rgba(127, 127, 127, .2));
  --head: var(--tg-theme-section-header-text-color, var(--hint));
  --good: #2fa866;
  --warn: #d99a1e;
  --bad: #e5484d;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(0, 0, 0, .04), 0 6px 20px rgba(0, 0, 0, .05);
  --track: color-mix(in srgb, var(--hint) 22%, transparent);
}
@media (prefers-color-scheme: dark) {
  :root:not(.tg) {
    --bg: #0f0f10; --card: #1c1c1e; --text: #f0efec; --hint: #9a9aa2; --sep: rgba(255, 255, 255, .1);
  }
}
@media (prefers-color-scheme: dark) { :root:not(.tg) { --shadow: none; } }
:root.dark { --shadow: none; }

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body.locked { overflow: hidden; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body { font: 15px/1.4 -apple-system, system-ui, "Segoe UI", Roboto, sans-serif; -webkit-tap-highlight-color: transparent;
       -webkit-font-smoothing: antialiased; }
button { font: inherit; color: inherit; }
svg { display: block; flex: none; }
#app { max-width: 640px; margin: 0 auto; min-height: 100vh; padding-bottom: calc(76px + env(safe-area-inset-bottom)); }
#view { padding: 12px 16px 4px; }
#view.enter > * { animation: rise .22s ease both; }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
@media (prefers-reduced-motion: reduce) { #view.enter > * { animation: none; } }
.loading, .denied { padding: 40vh 16px 0; text-align: center; color: var(--hint); }
.denied b { display: block; color: var(--text); font-size: 18px; margin-bottom: 6px; }

/* ---------- bottom navigation ---------- */
#tabs { position: fixed; left: 0; right: 0; bottom: 0; display: flex; justify-content: center; z-index: 5;
        background: color-mix(in srgb, var(--card) 88%, transparent); border-top: 1px solid var(--sep);
        -webkit-backdrop-filter: saturate(180%) blur(18px); backdrop-filter: saturate(180%) blur(18px);
        padding: 6px 4px calc(6px + env(safe-area-inset-bottom)); }
#tabs button { flex: 1; max-width: 128px; border: 0; background: none; padding: 4px 2px 2px; color: var(--hint);
               display: flex; flex-direction: column; align-items: center; gap: 3px; font-size: 11px; font-weight: 500;
               cursor: pointer; transition: color .15s; }
#tabs button.on { color: var(--accent); }
#tabs button svg { width: 25px; height: 25px; }

/* ---------- headings, sections, grouped lists ---------- */
.ptitle { font-size: 28px; font-weight: 700; letter-spacing: -.01em; margin: 6px 4px 14px; }
.sec { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--head); margin: 22px 16px 7px; font-weight: 500; }
.sec:first-child { margin-top: 6px; }
.note { font-size: 13px; color: var(--hint); margin: 7px 16px 0; line-height: 1.35; }
.list { background: var(--card); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
.item { display: flex; align-items: center; gap: 12px; min-height: 52px; padding: 9px 14px 9px 16px; position: relative; }
.item + .item::before { content: ""; position: absolute; top: 0; right: 0; left: 58px; border-top: 1px solid var(--sep); }
.list.plain .item + .item::before { left: 16px; }
.item.tap { cursor: pointer; }
.item.tap:active { background: color-mix(in srgb, var(--hint) 12%, transparent); }
.ic { width: 30px; height: 30px; border-radius: 8px; background: var(--c, var(--accent)); color: #fff;
      display: grid; place-items: center; flex: none; }
.ic svg { width: 18px; height: 18px; }
.tx { flex: 1; min-width: 0; }
.tx .t { font-size: 16px; line-height: 1.3; }
.tx .d { font-size: 13px; color: var(--hint); margin-top: 1px; line-height: 1.3; }
.tx .t.clamp { display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; font-size: 15px; }
.mono { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: 12.5px; }
.ell { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chev { color: var(--hint); opacity: .6; }
.xbtn { border: 0; background: none; color: var(--hint); width: 34px; height: 34px; border-radius: 17px; display: grid;
        place-items: center; cursor: pointer; flex: none; margin-right: -6px; }
.xbtn:active { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); }
.xbtn svg { width: 18px; height: 18px; }

/* switch */
.sw { position: relative; width: 51px; height: 31px; flex: none; }
.sw input { position: absolute; inset: 0; width: 100%; height: 100%; margin: 0; opacity: 0; cursor: pointer; z-index: 1; }
.sw i { position: absolute; inset: 0; border-radius: 16px; background: var(--track); transition: background .2s; }
.sw i::after { content: ""; position: absolute; top: 2px; left: 2px; width: 27px; height: 27px; border-radius: 50%; background: #fff;
               box-shadow: 0 2px 6px rgba(0, 0, 0, .22); transition: transform .22s cubic-bezier(.3, .7, .4, 1); }
.sw input:checked + i { background: var(--accent); }
.sw input:checked + i::after { transform: translateX(20px); }
.sw input:focus-visible + i { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- home ---------- */
.top { display: flex; align-items: center; gap: 14px; padding: 16px; background: var(--card); border-radius: 20px; box-shadow: var(--shadow); }
.ava { width: 58px; height: 58px; border-radius: 50%; flex: none; display: grid; place-items: center; color: #fff;
       background: linear-gradient(140deg, #7cb7ff 0%, #a98bff 55%, #f19ad1 100%); }
.ava svg { width: 30px; height: 30px; }
.who { flex: 1; min-width: 0; }
.who .nm { font-size: 21px; font-weight: 700; letter-spacing: -.01em; }
.who .st { display: flex; align-items: center; gap: 6px; font-size: 14px; color: var(--hint); margin-top: 2px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--good); flex: none; }
.dot.busy { background: var(--warn); animation: pulse 1.2s ease-in-out infinite; }
.dot.off { background: var(--hint); }
@keyframes pulse { 50% { opacity: .35; } }
.stopbtn { border: 0; border-radius: 12px; padding: 9px 14px; font-weight: 600; cursor: pointer;
           background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); }

.tiles { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.tile { border: 0; text-align: left; background: var(--card); border-radius: var(--radius); padding: 14px; cursor: pointer;
        display: flex; flex-direction: column; gap: 10px; min-height: 104px; box-shadow: var(--shadow); position: relative; }
.tile:active { transform: scale(.98); }
.tile.wide { grid-column: span 2; min-height: 0; flex-direction: row; align-items: center; gap: 12px; }
.tile.wide small { margin-top: 0; margin-left: auto; }
.tile b { font-size: 15px; font-weight: 600; line-height: 1.25; }
.tile small { font-size: 12.5px; color: var(--hint); margin-top: -7px; line-height: 1.3; }
.tile .live { position: absolute; top: 14px; right: 14px; font-size: 11px; font-weight: 600; color: var(--good);
              display: flex; align-items: center; gap: 5px; }

.sleepcard { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px 16px; }
.sleepcard .hd { display: flex; align-items: center; gap: 12px; cursor: pointer; }
.sleepcard .big { font-size: 20px; font-weight: 700; margin-top: 1px; }
.sleepcard .btn { width: 100%; margin-top: 12px; }

.meter { padding: 11px 16px; }
.meter + .meter { border-top: 1px solid var(--sep); }
.meter .top2 { display: flex; justify-content: space-between; gap: 12px; font-size: 14px; }
.meter .top2 span:last-child { color: var(--hint); font-variant-numeric: tabular-nums; }
.meter .bar { margin: 7px 0 0; height: 6px; }
.meter .bar i.hot { background: var(--warn); }

.empty { text-align: center; padding: 30px 20px; color: var(--hint); background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); }
.empty svg { width: 34px; height: 34px; margin: 0 auto 10px; opacity: .55; }
.empty b { display: block; color: var(--text); font-weight: 600; margin-bottom: 4px; }

.addbox { display: flex; gap: 8px; }
.addbox textarea { min-height: 46px; height: 46px; resize: none; background: var(--card); }
.addbox .btn { flex: none; }
.gav { width: 38px; height: 38px; border-radius: 50%; display: grid; place-items: center; color: #fff; font-weight: 600; flex: none;
       background: var(--c, var(--hint)); font-size: 16px; }
.pill { border: 0; border-radius: 14px; padding: 6px 12px; font-size: 13px; font-weight: 600; cursor: pointer; flex: none;
        background: color-mix(in srgb, var(--accent) 12%, transparent); color: var(--accent); }
.pill.red { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); }
.okmark { width: 8px; height: 8px; border-radius: 50%; flex: none; background: var(--good); }
.okmark.no { background: var(--bad); }
.stamp { font-size: 12px; color: var(--hint); text-align: center; margin: 18px 0 4px; }

/* skeleton */
.skel { background: var(--card); border-radius: var(--radius); height: 86px; margin-bottom: 10px; position: relative; overflow: hidden; }
.skel::after { content: ""; position: absolute; inset: 0; transform: translateX(-100%);
               background: linear-gradient(90deg, transparent, color-mix(in srgb, var(--hint) 12%, transparent), transparent);
               animation: shimmer 1.2s infinite; }
@keyframes shimmer { to { transform: translateX(100%); } }

/* segmented control (sleep sub-tabs) */
.seg2 { display: flex; background: var(--track); border-radius: 10px; padding: 2px; margin: 0 0 12px; position: sticky; top: 8px; z-index: 3;
        -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px); }
.seg2 button { flex: 1; border: 0; background: none; border-radius: 8px; padding: 7px 4px; font-size: 14px; font-weight: 500; cursor: pointer; color: var(--text); }
.seg2 button.on { background: var(--card); font-weight: 600; box-shadow: 0 1px 4px rgba(0, 0, 0, .12); }

/* ---------- sleep tracker (cards, forms) ---------- */
.card { background: var(--card); border-radius: var(--radius); padding: 14px 16px; margin-bottom: 10px; box-shadow: var(--shadow); }
.card h3 { margin: 0 0 8px; font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--hint); }
.hero { text-align: center; padding: 20px 16px 16px; }
.hero .big { font-size: 26px; font-weight: 700; margin: 2px 0; }
.hero .sub { color: var(--hint); margin-bottom: 14px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; }
.row > * { flex: 1 1 auto; }
.btn { border: 0; border-radius: 12px; padding: 13px 14px; background: color-mix(in srgb, var(--accent) 12%, transparent);
       color: var(--accent); font-weight: 600; cursor: pointer; min-height: 46px; }
.btn.primary { background: var(--accent); color: var(--accent-text); font-size: 17px; }
.btn.ghost { background: none; color: var(--hint); font-weight: 500; }
.btn.danger { background: color-mix(in srgb, var(--danger) 12%, transparent); color: var(--danger); }
.btn:active { opacity: .7; }
.btn[disabled] { opacity: .45; }
.kv { display: flex; justify-content: space-between; gap: 12px; padding: 6px 0; border-bottom: 1px solid var(--sep); }
.kv:last-child { border-bottom: 0; }
.kv span:first-child { color: var(--hint); }
.kv span:last-child { text-align: right; font-weight: 500; }
.muted { color: var(--hint); font-size: 13px; }
.pre { white-space: pre-wrap; word-break: break-word; }
.bar { height: 8px; border-radius: 4px; background: var(--track); overflow: hidden; margin: 8px 0 4px; }
.bar i { display: block; height: 100%; border-radius: 4px; background: var(--accent); }
.badge { display: inline-block; min-width: 34px; padding: 2px 8px; border-radius: 10px; font-size: 13px; font-weight: 600; text-align: center;
         background: color-mix(in srgb, var(--hint) 18%, transparent); }
.badge.s85 { background: color-mix(in srgb, var(--good) 20%, transparent); color: var(--good); }
.badge.s70 { background: color-mix(in srgb, var(--accent) 18%, transparent); color: var(--accent); }
.badge.s55 { background: color-mix(in srgb, var(--warn) 22%, transparent); color: var(--warn); }
.badge.s0 { background: color-mix(in srgb, var(--bad) 18%, transparent); color: var(--bad); }
.tag { display: inline-block; font-size: 12px; color: var(--hint); border: 1px solid var(--sep); border-radius: 8px; padding: 0 6px; margin-left: 4px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { border: 1px solid var(--sep); background: none; border-radius: 18px; padding: 7px 12px; font-size: 14px; cursor: pointer; }
.chip.on { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.seg { display: flex; flex-wrap: wrap; gap: 6px; }
.seg button { flex: 1 1 0; min-width: 58px; border: 1px solid var(--sep); background: none; border-radius: 10px; padding: 9px 6px; font-size: 13px; cursor: pointer; }
.seg button.on { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.field { margin: 14px 0 0; }
.field > label { display: block; font-size: 13px; color: var(--hint); margin-bottom: 6px; }
input[type=time], input[type=date], textarea { width: 100%; font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--sep);
       border-radius: 10px; padding: 10px 12px; }
textarea { min-height: 80px; resize: vertical; }
input.search { width: 100%; font: inherit; color: var(--text); background: var(--card); border: 1px solid var(--sep); border-radius: 12px;
               padding: 11px 14px; margin-bottom: 10px; }
.times { display: flex; gap: 8px; }
.times > div { flex: 1; }

.night { display: flex; align-items: center; gap: 12px; padding: 11px 0; border-bottom: 1px solid var(--sep); cursor: pointer; }
.night:last-child { border-bottom: 0; }
.night .d { width: 64px; flex: none; }
.night .d b { display: block; }
.night .d span { font-size: 12px; color: var(--hint); }
.night .m { flex: 1; min-width: 0; }
.night .m div:last-child { font-size: 13px; color: var(--hint); }

.steps { list-style: none; margin: 6px 0 0; padding: 0; }
.steps li { display: flex; gap: 10px; padding: 5px 0; }
.steps li b { width: 46px; flex: none; font-variant-numeric: tabular-nums; }
.steps li.past { color: var(--hint); text-decoration: line-through; }
.steps li.next b { color: var(--accent); }
.dots { display: flex; gap: 6px; margin-top: 8px; }
.dots i { width: 14px; height: 14px; border-radius: 50%; background: var(--sep); }
.dots i.ok { background: var(--good); } .dots i.miss { background: var(--bad); } .dots i.sick { background: var(--warn); }
.dots i.today { outline: 2px solid var(--accent); outline-offset: 1px; }
.chart { width: 100%; border-radius: 10px; display: block; margin-top: 6px; }
table.cmp { width: 100%; border-collapse: collapse; font-size: 14px; }
table.cmp td, table.cmp th { padding: 6px 4px; border-bottom: 1px solid var(--sep); text-align: right; }
table.cmp td:first-child, table.cmp th:first-child { text-align: left; color: var(--hint); font-weight: 400; }

/* ---------- sheet and toast ---------- */
#sheet { position: fixed; inset: 0; background: var(--bg); z-index: 20; overflow-y: auto; padding: 12px 16px calc(90px + env(safe-area-inset-bottom)); }
#sheet .head { display: flex; align-items: center; justify-content: space-between; margin: 4px 4px 12px; }
#sheet .head b { font-size: 20px; }
#sheet .foot { position: fixed; left: 0; right: 0; bottom: 0; padding: 10px 16px calc(10px + env(safe-area-inset-bottom)); background: var(--bg);
               display: flex; gap: 8px; max-width: 640px; margin: 0 auto; }
#sheet .foot .btn { flex: 1; }
#toast { position: fixed; left: 12px; right: 12px; bottom: calc(80px + env(safe-area-inset-bottom)); max-width: 616px; margin: 0 auto;
         background: #222; color: #fff; border-radius: 12px; padding: 12px 14px; z-index: 30; display: flex; gap: 12px; align-items: center;
         box-shadow: 0 6px 24px rgba(0, 0, 0, .25); }
#toast.err { background: #8e1f1f; }
#toast span { flex: 1; font-size: 14px; white-space: pre-wrap; }
#toast button { border: 0; background: none; color: #8cc4ff; font-weight: 600; padding: 4px; }
