:root {
  color-scheme: light dark;
  --bg: #f2f2f7; --card: #ffffff; --text: #1c1c1e; --muted: #6e6e73; --sep: rgba(60,60,67,.14);
  --blue: #007aff; --blue-press: #0062cc; --red: #ff3b30; --green: #34c759; --yellow: #ffd60a;
  --fill: rgba(120,120,128,.12); --fill2: rgba(120,120,128,.2); --fill3: rgba(120,120,128,.3);
  --deep: #3634a3; --core: #3e8ef7; --rem: #7ac8ff; --awake: #ff7d54; --asleep: #6d8fd6; --window: #5e5ce6;
  --grid: rgba(60,60,67,.14); --goal: rgba(52,199,89,.8); --avg: rgba(0,122,255,.55);
  --seg-on: #ffffff; --radius: 14px; --h: 40px; --hs: 32px;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display", "Helvetica Neue", Helvetica, Arial, sans-serif;
  --ease: cubic-bezier(.2,.8,.2,1);
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #000; --card: #1c1c1e; --text: #f5f5f7; --muted: #98989d; --sep: rgba(84,84,88,.6);
    --fill: rgba(120,120,128,.24); --fill2: rgba(120,120,128,.36); --fill3: rgba(120,120,128,.48);
    --deep: #5a58d6; --core: #4a9cff; --rem: #8ad4ff; --awake: #ff8a65; --window: #7d7aff; --grid: rgba(84,84,88,.5);
    --blue: #0a84ff; --blue-press: #409cff; --red: #ff453a; --green: #30d158; --seg-on: #636366; }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font-family: var(--font); font-size: 16px; line-height: 1.45; -webkit-font-smoothing: antialiased; }
a { color: var(--blue); text-decoration: none; transition: opacity .15s var(--ease); }
a:hover { opacity: .75; }
a:active { opacity: .5; }
code { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: .9em; }
:focus-visible { outline: 2px solid var(--blue); outline-offset: 2px; }
.wrap { max-width: 980px; margin: 0 auto; padding: 0 16px 48px; }

/* top bar */
.topbar { position: sticky; top: 0; z-index: 5; backdrop-filter: saturate(180%) blur(20px); -webkit-backdrop-filter: saturate(180%) blur(20px); background: color-mix(in srgb, var(--bg) 72%, transparent); border-bottom: 1px solid var(--sep); }
.bar { display: flex; align-items: center; justify-content: space-between; height: 52px; padding-bottom: 0; }
.brand { display: inline-flex; align-items: center; gap: 8px; font-weight: 600; color: var(--text); font-size: 17px; }
.brand .lemon { transition: transform .3s var(--ease); }
.brand:hover .lemon { transform: rotate(-12deg) scale(1.08); }
nav { display: flex; align-items: center; gap: 4px; }
nav form { display: contents; }
nav a, .navbtn { display: inline-flex; align-items: center; height: 32px; padding: 0 12px; border-radius: 8px; font: inherit; font-size: 16px; font-weight: 500; color: var(--blue); background: none; border: 0; cursor: pointer; line-height: 1; transition: background .15s var(--ease), opacity .15s var(--ease); opacity: 1; }
nav a:hover, .navbtn:hover { background: var(--fill); opacity: 1; }
nav a:active, .navbtn:active { background: var(--fill2); opacity: 1; }
nav a.on { color: var(--text); font-weight: 600; background: var(--fill); }
.badge { display: inline-flex; align-items: center; height: 32px; background: var(--fill); border-radius: 999px; padding: 0 12px; font-size: 13px; color: var(--muted); }

/* typography */
.title { font-size: 22px; font-weight: 700; margin: 0; letter-spacing: -.01em; }
.title.lg { font-size: 34px; letter-spacing: -.02em; margin: 24px 0 12px; }
h2 { font-size: 17px; font-weight: 600; margin: 0; }
.muted { color: var(--muted); }
.hint { color: var(--muted); font-size: 13px; margin: 10px 0 0; }
.footnote { color: var(--muted); font-size: 12px; text-align: center; margin: 20px 0 0; }
.b { font-weight: 600; }
.r { text-align: right; }

/* period bar */
.periodbar { display: flex; flex-direction: column; gap: 12px; padding: 18px 0 14px; }
.segmented { display: flex; background: var(--fill); border-radius: 9px; padding: 2px; width: fit-content; }
.segmented a { display: inline-flex; align-items: center; height: 28px; padding: 0 14px; border-radius: 7px; font-size: 13px; font-weight: 600; color: var(--text); opacity: 1; transition: background .15s var(--ease), transform .1s var(--ease); }
.segmented a:hover:not(.on) { background: color-mix(in srgb, var(--seg-on) 30%, transparent); }
.segmented a:active { transform: scale(.96); }
.segmented a.on { background: var(--seg-on); color: var(--text); box-shadow: 0 2px 6px rgba(0,0,0,.2), 0 0 0 .5px rgba(0,0,0,.06); }
.navdates { display: flex; align-items: center; gap: 6px; min-height: var(--h); }
.navdates .title { margin: 0 4px; }
.chev { display: inline-flex; align-items: center; justify-content: center; width: var(--hs); height: var(--hs); border-radius: 50%; font-size: 26px; line-height: 1; padding-bottom: 3px; color: var(--blue); font-weight: 400; opacity: 1; transition: background .15s var(--ease); }
.chev:hover { background: var(--fill); opacity: 1; }
.chev:active { background: var(--fill2); opacity: 1; }
.chev.off { color: var(--muted); opacity: .35; cursor: default; pointer-events: none; }
.pill { display: inline-flex; align-items: center; height: 28px; background: var(--blue); color: #fff; border-radius: 999px; padding: 0 12px; font-size: 13px; font-weight: 600; margin-left: 6px; opacity: 1; transition: filter .15s var(--ease), transform .1s var(--ease); }
.pill:hover { filter: brightness(1.1); opacity: 1; }
.pill:active { transform: scale(.96); opacity: 1; }
.pill.ghost { background: var(--fill); color: var(--text); }
.pill.ghost:hover { background: var(--fill2); filter: none; }
.controls { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.rangeform { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin: 0; }
form { margin: 0; }
.periodbar .rangeform input[type=date] { width: auto; flex: 0 0 auto; height: var(--hs); padding: 0 10px; font-size: 14px; }
.rangeform .dash { padding: 0 2px; }
.dash { color: var(--muted); }

/* cards & tiles */
.card { background: var(--card); border-radius: var(--radius); padding: 18px; margin: 12px 0; box-shadow: 0 0 0 .5px var(--sep); }
.card.ok { box-shadow: 0 0 0 1.5px var(--green); }
.card.empty { text-align: center; padding: 48px 18px; }
.card.dim ul, .card.dim p { opacity: .8; }
.cardhead { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.tiles { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
.tile { background: var(--card); border-radius: var(--radius); padding: 14px 16px; box-shadow: 0 0 0 .5px var(--sep); }
.tlabel { font-size: 13px; color: var(--muted); font-weight: 500; }
.tvalue { font-size: 26px; font-weight: 700; letter-spacing: -.02em; margin-top: 2px; font-variant-numeric: tabular-nums; }
.tsub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.stagegrid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-top: 12px; }
.stat { display: flex; gap: 10px; align-items: flex-start; }
.svalue { font-weight: 600; font-variant-numeric: tabular-nums; }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; margin-top: 5px; }
.legend { display: flex; gap: 12px; font-size: 12px; color: var(--muted); }
.legend span { display: inline-flex; align-items: center; gap: 5px; }
.legend .dot { margin-top: 0; width: 8px; height: 8px; }
.legend .legend { display: contents; }
.key { display: inline-block; width: 14px; height: 0; border-top: 2px dashed var(--goal); vertical-align: middle; margin-right: 2px; }
.key.avg { border-top-color: var(--avg); }
.summary ul { margin: 0; padding-left: 18px; }
.summary li { margin: 6px 0; }
.verdict { font-size: 13px; font-weight: 600; }

/* charts */
.chart { width: 100%; height: auto; display: block; overflow: visible; }
.chart .grid { stroke: var(--grid); stroke-width: 1; }
.chart .tick { fill: var(--muted); font-size: 11px; font-family: var(--font); pointer-events: none; }
.chart .goal { stroke: var(--goal); stroke-width: 1.5; stroke-dasharray: 4 4; }
.chart .avg { stroke: var(--avg); stroke-width: 1.5; stroke-dasharray: 3 5; }
.chart a { opacity: 1; outline: none; }
.chart .band { fill: var(--fill); opacity: 0; transition: opacity .15s var(--ease); }
.chart a.hov .band, .chart a:focus-visible .band { opacity: 1; }
.chart .seg { transition: opacity .18s var(--ease), filter .18s var(--ease), transform .18s var(--ease); }
.chart .val { opacity: 0; font-weight: 600; fill: var(--text); transition: opacity .15s var(--ease); }
.chart a.hov .val, .chart a:focus-visible .val { opacity: 1; }
.chart .seg.hl { filter: brightness(1.25) saturate(1.15); }
.bars a.hov .seg:not(.hl) { opacity: .55; }
.bars .seg { transform-box: fill-box; transform-origin: bottom; animation: grow .55s var(--ease) both; }
.window .seg { transform-box: fill-box; transform-origin: center; animation: grow .5s var(--ease) both; }
.window .guide { opacity: 0; transition: opacity .15s var(--ease); pointer-events: none; }
.window .guide.on { opacity: 1; }
.window .gl { stroke: var(--window); stroke-width: 1; stroke-dasharray: 3 3; }
.window .gt { fill: var(--window); font-weight: 600; paint-order: stroke; stroke: var(--card); stroke-width: 5px; stroke-linejoin: round; }
.window a.hov .avgline, .window a.hov ~ .avgline { opacity: .4; }
.hypno-line { fill: none; stroke: var(--fill2); stroke-width: 1.5; }
.hypno .seg { transform-box: fill-box; transform-origin: left; animation: growx .45s var(--ease) both; cursor: default; }
.hypno .seg.hl { filter: brightness(1.3) saturate(1.2); stroke: var(--text); stroke-width: 1; }
.hypno .rowlabel { transition: fill .15s var(--ease), font-weight .15s; }
.hypno .cursor { opacity: 0; transition: opacity .12s var(--ease); pointer-events: none; }
.hypno .cursor.on { opacity: 1; }
.hypno .cl { stroke: var(--text); stroke-width: 1; stroke-dasharray: 2 3; opacity: .6; }
.hypno .cb { fill: var(--text); }
.hypno .ct { fill: var(--card); font-size: 11px; font-weight: 600; font-family: var(--font); }
.stagebar { width: 100%; height: 12px; border-radius: 6px; overflow: hidden; display: block; }
.stagebar .seg { transition: opacity .18s var(--ease), filter .18s var(--ease); transform-box: fill-box; transform-origin: left; animation: growx .5s var(--ease) both; }
.stagebar .seg.hl { filter: brightness(1.25); }
.c-deep { fill: var(--deep); background: var(--deep); }
.c-core { fill: var(--core); background: var(--core); }
.c-rem { fill: var(--rem); background: var(--rem); }
.c-awake { fill: var(--awake); background: var(--awake); }
.c-asleep { fill: var(--asleep); background: var(--asleep); }
.c-inbed { fill: var(--window); background: var(--window); }
.c-window { fill: var(--window); opacity: .85; }
.chart .whisker { stroke: var(--window); stroke-width: 1.5; opacity: .45; }
.chart.dense .band { display: none; }
.chart.dense .seg { animation: none; }
h2 .sub { font-weight: 400; font-size: 13px; }
.chart .ghost { fill: none; stroke: var(--muted); stroke-width: 1.5; stroke-dasharray: 3 3; opacity: .7; }
.chart a.hov .ghost { opacity: 1; stroke: var(--text); }
tr.untracked td { color: var(--muted); }
tr.untracked td:first-child a { color: var(--muted); }
.window a.hov .c-window { opacity: 1; filter: brightness(1.15); }
/* stage focus: hovering a legend item or a segment dims the other stages in the same card */
.card[data-focus] .chart .seg:not(.hl), .card[data-focus] .stagebar .seg:not(.hl) { opacity: .28; }
.card[data-focus=deep] .c-deep, .card[data-focus=core] .c-core, .card[data-focus=rem] .c-rem, .card[data-focus=awake] .c-awake, .card[data-focus=asleep] .c-asleep { opacity: 1 !important; }
.card[data-focus] .legend [data-stage] { opacity: .45; }
.card[data-focus=deep] .legend [data-stage=deep], .card[data-focus=core] .legend [data-stage=core], .card[data-focus=rem] .legend [data-stage=rem], .card[data-focus=awake] .legend [data-stage=awake] { opacity: 1; }
.card[data-focus=deep] .hypno .row-deep .rowlabel, .card[data-focus=core] .hypno .row-core .rowlabel, .card[data-focus=rem] .hypno .row-rem .rowlabel, .card[data-focus=awake] .hypno .row-awake .rowlabel { fill: var(--text); font-weight: 600; }
.legend [data-stage] { cursor: default; transition: opacity .15s var(--ease); }
@keyframes grow { from { transform: scaleY(0); } to { transform: scaleY(1); } }
@keyframes growx { from { transform: scaleX(0); } to { transform: scaleX(1); } }
@media (prefers-reduced-motion: reduce) { .chart .seg, .stagebar .seg { animation: none; } }

/* tooltip */
.tip { position: fixed; z-index: 50; pointer-events: none; min-width: 150px; max-width: 280px; padding: 10px 12px; border-radius: 12px;
  background: color-mix(in srgb, var(--card) 88%, transparent); color: var(--text); box-shadow: 0 8px 24px rgba(0,0,0,.28), 0 0 0 .5px var(--sep);
  backdrop-filter: saturate(180%) blur(18px); -webkit-backdrop-filter: saturate(180%) blur(18px);
  opacity: 0; transform: translateY(4px) scale(.98); transition: opacity .14s var(--ease), transform .14s var(--ease); font-size: 13px; line-height: 1.35; }
.tip.show { opacity: 1; transform: none; }
.tip-t { font-weight: 700; font-size: 14px; }
.tip-s { color: var(--muted); font-size: 12px; margin-top: 1px; }
.tip-rows { margin-top: 8px; display: flex; flex-direction: column; gap: 3px; }
.tip-r { display: flex; align-items: center; gap: 7px; padding: 2px 6px; margin: 0 -6px; border-radius: 6px; transition: background .12s var(--ease); }
.tip-r.on { background: var(--fill); font-weight: 600; }
.tip-r .dot { margin: 0; width: 8px; height: 8px; flex: none; }
.tip-r .dot.none { background: var(--fill3); }
.tip-r .k { color: var(--muted); flex: 1; }
.tip-r.on .k { color: var(--text); }
.tip-r .v { font-variant-numeric: tabular-nums; font-weight: 600; }

/* tables */
.scroll { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: 14px; font-variant-numeric: tabular-nums; }
th { text-align: left; color: var(--muted); font-weight: 500; font-size: 12px; padding: 6px 8px; border-bottom: 1px solid var(--sep); }
td { padding: 9px 8px; border-bottom: 1px solid var(--sep); white-space: nowrap; }
tr:last-child td { border-bottom: 0; }
tbody tr { transition: background .12s var(--ease); }
tbody tr:hover { background: var(--fill); }
tr.we td:first-child a { color: var(--window); }
table.list td { padding: 10px 8px; }
td a { font-weight: 500; }

/* forms */
.stack { display: flex; flex-direction: column; gap: 12px; }
.row { display: flex; gap: 10px; align-items: flex-end; flex-wrap: wrap; margin-top: 10px; }
.stack .row { margin-top: 0; }
label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: var(--muted); font-weight: 500; flex: 1; min-width: 0; }
input, select, button { font: inherit; }
input:not([type=checkbox]):not([type=file]), select { height: var(--h); padding: 0 12px; border-radius: 10px; border: 1px solid var(--sep); background: var(--fill); color: var(--text); min-width: 0; width: 100%; transition: border-color .15s var(--ease), background .15s var(--ease); }
input:not([type=checkbox]):not([type=file]):hover, select:hover { background: var(--fill2); }
input:not([type=checkbox]):focus, select:focus { outline: none; border-color: var(--blue); box-shadow: 0 0 0 3px color-mix(in srgb, var(--blue) 30%, transparent); background: var(--fill); }
input[type=date] { font-variant-numeric: tabular-nums; }
input[type=file] { padding: 0; height: var(--h); line-height: var(--h); color: var(--muted); flex: 1; }
input[type=file]::file-selector-button { font: inherit; font-weight: 600; height: var(--h); padding: 0 14px; margin-right: 10px; border: 0; border-radius: 10px; background: var(--fill2); color: var(--text); cursor: pointer; transition: background .15s var(--ease); }
input[type=file]::file-selector-button:hover { background: var(--fill3); }
select { appearance: none; -webkit-appearance: none; padding-right: 32px; background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1.5l5 5 5-5' fill='none' stroke='%23888' stroke-width='1.8' stroke-linecap='round'/%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: right 12px center; }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; height: var(--h); font-weight: 600; background: var(--blue); color: #fff; border: 0; border-radius: 10px; padding: 0 18px; cursor: pointer; white-space: nowrap; transition: filter .15s var(--ease), transform .1s var(--ease), background .15s var(--ease); }
.btn:hover { filter: brightness(1.1); }
.btn:active { transform: scale(.97); filter: brightness(.92); }
.btn.small { height: var(--hs); padding: 0 14px; font-size: 13px; }
.btn.secondary { background: var(--fill2); color: var(--text); }
.btn.secondary:hover { background: var(--fill3); filter: none; }
.btn.danger { background: var(--red); }
.link { display: inline-flex; align-items: center; height: var(--hs); background: none; border: 0; color: var(--blue); font: inherit; font-weight: 500; cursor: pointer; padding: 0 8px; border-radius: 8px; transition: background .15s var(--ease); }
.link:hover { background: var(--fill); }
.link:active { background: var(--fill2); }
.link.danger { color: var(--red); }
.error { color: var(--red); margin: 0; }
.flash { border-radius: 10px; padding: 10px 14px; margin: 12px 0; }
.flash.ok { background: color-mix(in srgb, var(--green) 15%, transparent); color: var(--green); }
.flash.error { background: color-mix(in srgb, var(--red) 12%, transparent); }
.tokenbox { background: var(--fill); border-radius: 10px; padding: 12px; margin: 10px 0; }
.token { display: block; word-break: break-all; padding: 10px 12px; background: var(--fill); border-radius: 8px; user-select: all; }
.token:hover { background: var(--fill2); }

/* destructive confirmation + source tags */
.confirm { background: color-mix(in srgb, var(--red) 10%, transparent); border: 1px solid color-mix(in srgb, var(--red) 40%, transparent); border-radius: 12px; padding: 14px 16px; margin-bottom: 12px; }
.confirm p { margin: 0; }
.confirm .row { margin-top: 12px; }
.confirm .row form { display: contents; }
a.btn { opacity: 1; }
tr.sel td { background: color-mix(in srgb, var(--red) 8%, transparent); }
.tag { display: inline-flex; align-items: center; height: 22px; padding: 0 8px; border-radius: 6px; font-size: 12px; font-weight: 500; background: var(--fill); color: var(--muted); }
.tag.ok { background: color-mix(in srgb, var(--green) 18%, transparent); color: var(--green); }
.tag.tz { margin-left: 6px; height: 18px; font-size: 11px; background: color-mix(in srgb, var(--window) 18%, transparent); color: var(--window); }
a.link { opacity: 1; }

details.detected { margin: 10px 0; }
details.detected summary { cursor: pointer; font-weight: 500; color: var(--blue); list-style: none; }
details.detected summary::-webkit-details-marker { display: none; }
details.detected summary:hover { opacity: .8; }
details.detected[open] summary { margin-bottom: 6px; }

/* iOS-style switch */
.rangeopts { display: flex; flex-direction: column; gap: 10px; background: var(--fill); border-radius: 12px; padding: 12px; }
.switchrow { flex-direction: row; align-items: center; justify-content: space-between; gap: 12px; color: var(--text); font-size: 16px; cursor: pointer; }
.switch { appearance: none; -webkit-appearance: none; width: 51px; height: 31px; border-radius: 16px; background: var(--fill3); position: relative; margin: 0; cursor: pointer; flex: none; transition: background .2s var(--ease); }
.switch::before { content: ""; position: absolute; top: 2px; left: 2px; width: 27px; height: 27px; border-radius: 50%; background: #fff; box-shadow: 0 3px 8px rgba(0,0,0,.25), 0 1px 1px rgba(0,0,0,.1); transition: transform .2s var(--ease); }
.switch:checked { background: var(--green); }
.switch:checked::before { transform: translateX(20px); }
.switch:active::before { width: 31px; }
.rangeopts:has(.switch:checked) .dates { opacity: .35; pointer-events: none; }
.rangeopts .dates { transition: opacity .2s var(--ease); }

/* auth pages */
.auth { max-width: 360px; margin: 12vh auto 0; text-align: center; }
.auth h1 { font-size: 28px; margin: 0 0 6px; letter-spacing: -.02em; }
.auth .stack { margin-top: 20px; text-align: left; }
.hero { display: flex; justify-content: center; margin-bottom: 14px; }
.hero .lemon { filter: drop-shadow(0 6px 14px rgba(255,214,10,.35)); }

@media (max-width: 700px) {
  .tiles, .stagegrid { grid-template-columns: repeat(2, 1fr); }
  .tvalue { font-size: 22px; }
  nav a, .navbtn { padding: 0 8px; font-size: 14px; }
  .title.lg { font-size: 28px; }
  .segmented a { padding: 0 10px; }
}

/* security page */
.sessions { width: 100%; border-collapse: collapse; }
.sessions td { padding: 8px 6px; border-top: .5px solid var(--sep); vertical-align: top; }
.sessions .ua { color: var(--muted); font-size: 13px; word-break: break-word; }
.stat { display: flex; gap: 24px; flex-wrap: wrap; margin: 8px 0 14px; }
.stat div b { display: block; font-size: 17px; }
.stat div span { color: var(--muted); font-size: 13px; }
.small, p.small { font-size: 13px; }

/* upload progress (replaces the file row while an import runs) */
.progress { padding: 2px 0 4px; }
.progress .head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; margin-bottom: 8px; }
.progress .label { font-weight: 600; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.progress .pct { color: var(--muted); font-variant-numeric: tabular-nums; font-size: 14px; flex: none; }
.progress .bar { height: 20px; border-radius: 10px; background: var(--fill); overflow: hidden; }
.progress .fill { height: 100%; width: 0; background: var(--blue); border-radius: 10px; transition: width .3s var(--ease), background .3s var(--ease); }
.progress.busy .fill { background-image: linear-gradient(90deg, transparent 0, rgba(255,255,255,.22) 50%, transparent 100%); background-size: 200% 100%; animation: shimmer 1.6s linear infinite; }
.progress.ok .fill { background: var(--green); }
.progress.err .fill { background: var(--red); width: 100%; }
.progress .sub { color: var(--muted); font-size: 13px; margin-top: 8px; }
@keyframes shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }
[hidden] { display: none !important; }
.progress.ok .label, .progress.err .label { white-space: normal; }
