:root {
  --bg: #f6f5f2; --surface: #fff; --surface2: #eeede8; --text: #1c1c1e; --muted: #6e6e73; --faint: #a1a1a6;
  --line: rgba(0, 0, 0, .08); --accent: #4a58c8; --accent-ink: #fff; --accent-soft: rgba(74, 88, 200, .12);
  --good: #23865a; --warn: #b87a0c; --bad: #cf3f35; --good-soft: rgba(35, 134, 90, .12);
  --warn-soft: rgba(201, 135, 18, .14); --bad-soft: rgba(207, 63, 53, .12);
  --shadow: 0 1px 2px rgba(0, 0, 0, .05), 0 0 0 1px rgba(0, 0, 0, .03);
  --r: 16px; --gut: 16px;
  --sl: env(safe-area-inset-left, 0px); --sr: env(safe-area-inset-right, 0px);
  --st: env(safe-area-inset-top, 0px); --sb: env(safe-area-inset-bottom, 0px);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1013; --surface: #1b1c21; --surface2: #26282e; --text: #f2f2f5; --muted: #a0a0a8; --faint: #6c6c75;
    --line: rgba(255, 255, 255, .08); --accent: #8e99ff; --accent-ink: #10121c; --accent-soft: rgba(142, 153, 255, .16);
    --good: #4cc38a; --warn: #e6a93e; --bad: #ff6b5e; --good-soft: rgba(76, 195, 138, .14);
    --warn-soft: rgba(230, 169, 62, .16); --bad-soft: rgba(255, 107, 94, .14); --shadow: 0 0 0 1px rgba(255, 255, 255, .04);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; overscroll-behavior: none; }
body {
  background: var(--bg); color: var(--text); -webkit-text-size-adjust: 100%;
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, system-ui, sans-serif;
}
[hidden] { display: none !important; }
button, input, textarea, label.btn, label.round { font: inherit; color: inherit; }
button { background: none; border: 0; padding: 0; cursor: pointer; touch-action: manipulation; }
a { color: var(--accent); }
.ic { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.vh { position: fixed; left: -9999px; top: 0; width: 1px; height: 1px; opacity: 0; }

/* Login */
.login { position: fixed; inset: 0; display: grid; place-items: center; padding: 24px max(24px, var(--sl)); }
.login-card { width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: 12px; text-align: center; }
.login-mark { margin: 0 auto; border-radius: 16px; }
.login h1 { margin: 4px 0 0; font-size: 26px; }
.login p { margin: 0 0 8px; color: var(--muted); }
.field, .login input {
  width: 100%; min-height: 48px; border-radius: 12px; border: 1px solid var(--line); background: var(--surface);
  padding: 0 14px; font-size: 17px; outline: none;
}
.login input:focus, .field:focus { border-color: var(--accent); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 44px; padding: 0 18px;
  border-radius: 12px; font-weight: 600; background: var(--surface2); cursor: pointer; user-select: none; -webkit-user-select: none;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.warn { background: var(--warn); color: #fff; width: 100%; }
.btn:disabled { opacity: .5; }

/* App frame: height follows the visual viewport so inputs sit above the keyboard */
#app { position: fixed; left: 0; right: 0; top: var(--vvt, 0px); height: var(--vvh, 100%); display: flex; flex-direction: column; }
.views { flex: 1; position: relative; min-height: 0; }
.view { position: absolute; inset: 0; display: none; flex-direction: column; }
#app[data-tab="today"] #v-today, #app[data-tab="chat"] #v-chat { display: flex; }
.scroll { flex: 1; min-height: 0; overflow-y: auto; overflow-x: hidden; -webkit-overflow-scrolling: touch; overscroll-behavior-y: contain; position: relative; }

.bar {
  flex: none; display: flex; align-items: center; gap: 2px; min-height: 52px; background: var(--bg);
  padding: var(--st) calc(6px + var(--sr)) 0 calc(6px + var(--sl)); border-bottom: 1px solid var(--line); touch-action: none;
}
.bar-title { flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; min-height: 44px; font-weight: 600; font-size: 17px; }
.bar-title span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.bar-title small { font-size: 12px; font-weight: 500; color: var(--muted); min-height: 0; }
.bar-title small:empty { display: none; }
.bar-title small.accent { color: var(--accent); }
.icon-btn { width: 44px; height: 44px; display: grid; place-items: center; border-radius: 12px; color: var(--accent); }
.icon-btn:active { background: var(--accent-soft); }
.icon-btn:disabled { color: var(--faint); }
.spin .ic { animation: spin .9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.tabs {
  flex: none; display: flex; background: var(--surface); border-top: 1px solid var(--line);
  padding: 0 var(--sr) var(--sb) var(--sl); touch-action: none; user-select: none; -webkit-user-select: none;
}
.tabs button { flex: 1; height: 54px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; font-size: 11px; font-weight: 600; color: var(--faint); }
.tabs .ic { width: 24px; height: 24px; }
#app[data-tab="today"] .tabs [data-tab="today"], #app[data-tab="chat"] .tabs [data-tab="chat"] { color: var(--accent); }
body.kb .tabs { display: none; }

/* Today */
.today { padding: 12px calc(var(--gut) + var(--sr)) 28px calc(var(--gut) + var(--sl)); display: flex; flex-direction: column; gap: 12px; transition: opacity .2s; }
.today.stale { opacity: .55; }
.ptr { position: absolute; left: 0; right: 0; top: -40px; height: 40px; display: grid; place-items: center; font-size: 13px; color: var(--muted); }
.card { background: var(--surface); border-radius: var(--r); padding: 14px 16px; box-shadow: var(--shadow); }
.card h2 { margin: 0 0 8px; font-size: 12px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); display: flex; justify-content: space-between; gap: 8px; }
.card h2 span { font-weight: 600; letter-spacing: 0; text-transform: none; font-size: 12px; }
.empty { color: var(--muted); text-align: center; padding: 32px 12px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { display: inline-flex; align-items: center; gap: 6px; min-height: 34px; padding: 0 12px; border-radius: 999px; font-size: 14px; font-weight: 600; background: var(--surface2); color: var(--muted); }
.chip::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.chip.good { background: var(--good-soft); color: var(--good); }
.chip.warn { background: var(--warn-soft); color: var(--warn); }
.chip.bad { background: var(--bad-soft); color: var(--bad); }
label.chip { cursor: pointer; min-height: 44px; }

.proof-card { display: flex; flex-direction: column; gap: 10px; background: var(--warn-soft); box-shadow: none; }
.proof-card p { margin: 0; font-weight: 600; }

.hero { border-left: 5px solid var(--k, var(--accent)); }
.hero-top { display: flex; justify-content: space-between; gap: 8px; font-size: 13px; color: var(--muted); font-weight: 600; }
.hero-title { font-size: 26px; font-weight: 700; line-height: 1.15; margin: 4px 0 2px; overflow-wrap: anywhere; }
.hero-meta { color: var(--muted); }
.leave { margin-top: 10px; padding: 8px 12px; border-radius: 10px; background: var(--accent-soft); color: var(--accent); font-weight: 700; display: flex; justify-content: space-between; gap: 8px; }
.leave.late { background: var(--bad-soft); color: var(--bad); }
.leave span { font-weight: 500; }
.note { margin-top: 8px; font-size: 14px; color: var(--muted); }

.tl { display: flex; flex-direction: column; }
.tl-row { display: flex; gap: 12px; padding: 9px 0; border-top: 1px solid var(--line); }
.tl-row:first-child { border-top: 0; padding-top: 2px; }
.tl-time { flex: none; width: 46px; font-variant-numeric: tabular-nums; font-weight: 600; font-size: 15px; line-height: 1.25; }
.tl-time small { display: block; font-weight: 400; color: var(--muted); font-size: 12px; }
.tl-body { flex: 1; min-width: 0; border-left: 3px solid var(--k, var(--faint)); padding-left: 10px; }
.tl-title { font-weight: 600; overflow-wrap: anywhere; }
.tl-meta { font-size: 13px; color: var(--muted); }
.tl-row.past { opacity: .6; }
.tl-row.skip { opacity: .42; }
.tl-row.skip .tl-title { text-decoration: line-through; text-decoration-thickness: 1px; }
.tl-row.now .tl-time { color: var(--accent); }
.tag { display: inline-block; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; padding: 1px 6px; border-radius: 6px; background: var(--surface2); color: var(--muted); vertical-align: 2px; margin-left: 4px; }

.k-class { --k: #4a78d8; } .k-lab { --k: #8b5cf6; } .k-exam { --k: #e0453a; } .k-club { --k: #e0679b; }
.k-deadline { --k: #d4930f; } .k-gym { --k: #2fa56d; } .k-event { --k: #1a9fb0; } .k-other { --k: #9a9aa2; }
.k-work { --k: #4a58c8; } .k-study { --k: #4a78d8; } .k-reading { --k: #b6792a; } .k-social { --k: #e0679b; } .k-free { --k: #9a9aa2; }

.list { display: flex; flex-direction: column; }
.row { display: flex; align-items: center; gap: 12px; min-height: 44px; padding: 8px 0; border-top: 1px solid var(--line); text-decoration: none; color: inherit; }
.row:first-child { border-top: 0; padding-top: 0; }
.row-main { flex: 1; min-width: 0; }
.row-title { font-weight: 600; overflow-wrap: anywhere; }
.row-sub { font-size: 13px; color: var(--muted); }
.row-end { flex: none; text-align: right; font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.row-end b { display: block; font-size: 14px; color: var(--text); }
.row.soon .row-end b { color: var(--bad); }
.row.near .row-end b { color: var(--warn); }
.row.now-block .row-title { color: var(--accent); }
.row.done { opacity: .55; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--k, var(--faint)); flex: none; }
a.row:active { opacity: .6; }

.meal + .meal { margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--line); }
.meal-name { font-weight: 700; }
.meal-sugg { margin: 2px 0 6px; }
.meal-sugg p { margin: 0; }
.opt { display: flex; gap: 10px; font-size: 14px; padding: 4px 0; }
.opt-main { flex: 1; min-width: 0; }
.opt-main div { color: var(--muted); font-size: 13px; }
.opt-end { flex: none; text-align: right; font-variant-numeric: tabular-nums; color: var(--muted); }
.opt-end b { color: var(--text); }

.meter { margin: 4px 0 12px; }
.meter-top { display: flex; justify-content: space-between; gap: 8px; font-size: 14px; }
.meter-top b { font-variant-numeric: tabular-nums; }
.meter-top span { color: var(--muted); }
.track { position: relative; height: 10px; border-radius: 6px; background: var(--surface2); overflow: hidden; margin-top: 6px; }
.fill { position: absolute; left: 0; top: 0; bottom: 0; border-radius: 6px; background: var(--accent); }
.fill.over { background: var(--bad); }
.fill.ok { background: var(--good); }
.band { position: absolute; top: 0; bottom: 0; right: 0; background: var(--good-soft); border-left: 2px solid var(--good); }
.entries { font-size: 14px; margin-bottom: 10px; }
.entries summary { color: var(--muted); cursor: pointer; min-height: 32px; display: flex; align-items: center; }
.entry { display: flex; justify-content: space-between; gap: 10px; padding: 4px 0; }
.entry span:last-child { color: var(--muted); flex: none; font-variant-numeric: tabular-nums; }
.add { display: flex; gap: 8px; }
.add .field { flex: 1; min-width: 0; font-size: 16px; }

.week { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stat { background: var(--surface2); border-radius: 12px; padding: 8px 8px 8px 12px; display: flex; align-items: center; gap: 8px; min-width: 0; }
.stat-main { flex: 1; min-width: 0; }
.stat-label { font-size: 12px; color: var(--muted); font-weight: 600; }
.stat-val { font-size: 19px; font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.stat-val.done { color: var(--good); }
.plus { flex: none; min-width: 44px; height: 44px; padding: 0 8px; border-radius: 12px; background: var(--surface); color: var(--accent); font-weight: 700; font-size: 14px; box-shadow: var(--shadow); }
.plus:active { background: var(--accent-soft); }
.plus:disabled { opacity: .5; }
.foot { text-align: center; font-size: 12px; color: var(--faint); padding: 4px 0 8px; }
.foot.bad { color: var(--bad); }

.skel { height: 90px; border-radius: var(--r); background: var(--surface); opacity: .6; animation: pulse 1.2s ease-in-out infinite alternate; }
@keyframes pulse { to { opacity: .25; } }

/* Chat */
.msgs { padding: 8px calc(12px + var(--sr)) 8px calc(12px + var(--sl)); }
.msgs-top { text-align: center; font-size: 12px; color: var(--faint); min-height: 24px; padding: 4px 0; }
.msg { display: flex; flex-direction: column; align-items: flex-start; margin: 0 0 10px; }
.msg.me { align-items: flex-end; }
.bubble {
  max-width: 86%; padding: 9px 13px; border-radius: 18px; background: var(--surface); box-shadow: var(--shadow);
  overflow-wrap: anywhere; word-break: break-word; line-height: 1.38;
}
.bot .bubble { border-bottom-left-radius: 6px; }
.me .bubble { background: var(--accent); color: var(--accent-ink); border-bottom-right-radius: 6px; box-shadow: none; }
.me .bubble a { color: inherit; }
.bubble p { margin: 0 0 .5em; } .bubble p:last-child { margin-bottom: 0; }
.bubble p.h { font-weight: 700; }
.bubble ul, .bubble ol { margin: .2em 0 .5em; padding-left: 1.25em; } .bubble ul:last-child, .bubble ol:last-child { margin-bottom: 0; }
.bubble li { margin: .15em 0; }
.bubble code { font: 14px ui-monospace, Menlo, monospace; background: var(--surface2); padding: 0 4px; border-radius: 4px; }
.me .bubble code { background: rgba(255, 255, 255, .2); }
.bubble img { display: block; max-width: 100%; width: 220px; max-height: 280px; object-fit: cover; border-radius: 12px; margin: 2px 0 6px; }
.bubble img:only-child { margin-bottom: 2px; }
.meta { font-size: 11px; color: var(--faint); margin: 3px 8px 0; display: flex; gap: 6px; align-items: center; }
.src { font-weight: 700; text-transform: uppercase; letter-spacing: .05em; font-size: 10px; padding: 1px 5px; border-radius: 5px; background: var(--surface2); color: var(--muted); }
.msg.sys { align-items: center; }
.msg.sys .bubble { background: transparent; box-shadow: none; color: var(--muted); font-size: 14px; text-align: center; max-width: 92%; padding: 2px 8px; }
.msg.pending .bubble { opacity: .7; }
.msg.failed .bubble { background: var(--bad-soft); color: var(--text); }
.msg.failed .meta { color: var(--bad); font-weight: 600; }
.day { text-align: center; font-size: 12px; font-weight: 600; color: var(--faint); margin: 14px 0 10px; }
.typing { display: flex; gap: 5px; padding: 14px 16px; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--faint); animation: blink 1.2s infinite ease-in-out; }
.typing i:nth-child(2) { animation-delay: .15s; } .typing i:nth-child(3) { animation-delay: .3s; }
@keyframes blink { 0%, 80%, 100% { opacity: .3; transform: translateY(0); } 40% { opacity: 1; transform: translateY(-3px); } }

.proof-banner { flex: none; padding: 8px calc(12px + var(--sr)) 0 calc(12px + var(--sl)); }
.composer { flex: none; position: relative; padding: 8px calc(10px + var(--sr)) 8px calc(10px + var(--sl)); background: var(--bg); border-top: 1px solid var(--line); }
.comp-row { display: flex; align-items: flex-end; gap: 8px; }
#input {
  flex: 1; min-width: 0; min-height: 44px; max-height: 136px; resize: none; border-radius: 22px; border: 1px solid var(--line);
  background: var(--surface); padding: 11px 16px; font-size: 17px; line-height: 20px; outline: none; margin-bottom: 6px;
}
#input:focus { border-color: var(--accent); }
.round { width: 44px; height: 44px; flex: none; border-radius: 50%; display: grid; place-items: center; cursor: pointer; margin-bottom: 6px; }
.round.primary { background: var(--accent); color: var(--accent-ink); }
.round.ghost { color: var(--accent); }
.round.ghost:active { background: var(--accent-soft); }
.round.proof { background: var(--warn); color: #fff; }
.mic {
  width: 56px; height: 56px; flex: none; border-radius: 50%; display: grid; place-items: center; background: var(--accent); color: var(--accent-ink);
  touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; transition: transform .15s, background .15s;
}
.mic .ic { width: 26px; height: 26px; }
.mic.on { background: var(--bad); color: #fff; transform: scale(1.12); box-shadow: 0 0 0 8px var(--bad-soft); }
.mic.cancel { background: var(--faint); box-shadow: none; }
.rec {
  position: absolute; left: calc(10px + var(--sl)); right: calc(80px + var(--sr)); bottom: 14px; height: 44px; border-radius: 22px;
  background: var(--surface); box-shadow: var(--shadow); display: flex; align-items: center; gap: 8px; padding: 0 14px; font-size: 14px;
}
.rec-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--bad); animation: blink 1s infinite; flex: none; }
#rec-time { font-weight: 700; font-variant-numeric: tabular-nums; }
#rec-hint { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.rec.cancel #rec-hint { color: var(--bad); font-weight: 600; }
.attach { position: relative; display: inline-block; margin: 0 0 8px 52px; }
.attach img { display: block; height: 72px; max-width: 160px; object-fit: cover; border-radius: 12px; }
.attach button { position: absolute; top: -8px; right: -8px; width: 28px; height: 28px; border-radius: 50%; background: var(--text); color: var(--bg); font-size: 18px; line-height: 1; }

.toast {
  position: fixed; left: 50%; top: calc(64px + var(--st)); transform: translate(-50%, -12px); max-width: calc(100% - 32px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px; font-weight: 500;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 10; text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
