:root {
  --bg: #f6f5fb;
  --surface: #ffffff;
  --surface-2: #efedf8;
  --text: #1c1a2b;
  --muted: #6b6883;
  --border: #e2dff0;
  --accent: #5b4fd6;
  --accent-text: #ffffff;
  --accent-soft: #e9e6fb;
  --user-bubble: #5b4fd6;
  --user-text: #ffffff;
  --danger: #c2334d;
  --ok: #1f8a5b;
  --warn: #b7791f;
  --radius: 18px;
  --shadow: 0 1px 2px rgba(28, 26, 43, .06), 0 4px 16px rgba(28, 26, 43, .05);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --tabbar-h: 58px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f0e17;
    --surface: #1a1926;
    --surface-2: #232133;
    --text: #ecebf5;
    --muted: #9a97b0;
    --border: #2d2b40;
    --accent: #8f84ff;
    --accent-text: #0f0e17;
    --accent-soft: #2a2650;
    --user-bubble: #3d3690;
    --user-text: #f2f0ff;
    --danger: #ff6b85;
    --ok: #4cc38a;
    --warn: #f0b458;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
#app { height: 100dvh; display: flex; flex-direction: column; }
h1, h2, h3 { line-height: 1.2; margin: 0 0 .4em; }
h2 { font-size: 1.4rem; }
h3 { font-size: 1.02rem; margin-top: .2em; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.error { color: var(--danger); min-height: 1.4em; }

/* buttons & inputs */
.btn {
  appearance: none; border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: .6em 1em; border-radius: 12px; font: inherit; font-weight: 550; cursor: pointer;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: transparent; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); }
.btn.small { padding: .35em .7em; font-size: .88rem; }
.btn.wide { width: 100%; }
.btn.icon { width: 44px; height: 44px; padding: 0; display: grid; place-items: center; border-radius: 50%; flex: none; }
.btn:disabled { opacity: .5; cursor: default; }
input, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: .6em .8em; width: 100%; min-width: 0;
}
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.field { display: block; margin: .6em 0; text-align: left; }
.field span { display: block; font-size: .85rem; color: var(--muted); margin-bottom: .2em; }
.row { display: flex; gap: .5em; align-items: center; flex-wrap: wrap; margin-top: .6em; }
.row input { flex: 1; }
.stack { display: flex; flex-direction: column; gap: .5em; margin-top: .5em; }

/* topbar & tabbar */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 1em;
  padding: calc(env(safe-area-inset-top, 0px) + 10px) 16px 10px;
  background: var(--bg); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: .5em; font-weight: 700; font-size: 1.1rem; }
.topbar-sub { color: var(--muted); font-size: .85rem; text-align: right; }
main { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.view { flex: 1; min-height: 0; }
.page { overflow-y: auto; padding: 20px 16px calc(var(--tabbar-h) + var(--safe-bottom) + 24px); max-width: 720px; width: 100%; margin: 0 auto; }
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
  display: flex; justify-content: space-around;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
  padding-bottom: var(--safe-bottom); height: calc(var(--tabbar-h) + var(--safe-bottom));
}
.tabbar button {
  position: relative; flex: 1; border: 0; background: none; color: var(--muted); font: inherit; font-size: .72rem;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; cursor: pointer;
}
.tabbar svg { width: 24px; height: 24px; }
.tabbar button.active { color: var(--accent); }
.badge {
  position: absolute; top: 6px; left: calc(50% + 6px); background: var(--danger); color: #fff;
  font-size: .68rem; min-width: 18px; height: 18px; border-radius: 9px; padding: 0 5px; line-height: 18px;
}

/* chat */
.chat { display: flex; flex-direction: column; }
.messages {
  flex: 1; overflow-y: auto; padding: 16px 16px 8px; display: flex; flex-direction: column; gap: 10px;
  max-width: 760px; width: 100%; margin: 0 auto;
}
.msg { max-width: 86%; padding: 10px 14px; border-radius: var(--radius); overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; background: var(--user-bubble); color: var(--user-text); border-bottom-right-radius: 6px; white-space: pre-wrap; }
.msg.assistant { align-self: flex-start; background: var(--surface); border: 1px solid var(--border); border-bottom-left-radius: 6px; box-shadow: var(--shadow); }
.msg.assistant p { margin: 0 0 .6em; }
.msg.assistant p:last-child, .msg.assistant ul:last-child, .msg.assistant ol:last-child { margin-bottom: 0; }
.msg.assistant ul, .msg.assistant ol { margin: 0 0 .6em; padding-left: 1.3em; }
.msg.assistant code { background: var(--surface-2); padding: .1em .35em; border-radius: 6px; font-size: .9em; }
.msg.assistant pre { background: var(--surface-2); padding: .7em; border-radius: 10px; overflow-x: auto; }
.msg.assistant pre code { background: none; padding: 0; }
.msg.assistant a { color: var(--accent); }
.chip-row { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; }
.chip { font-size: .75rem; color: var(--muted); background: var(--surface-2); border-radius: 999px; padding: 2px 9px; }
.chip.live::after { content: " …"; }
.note { align-self: center; font-size: .8rem; color: var(--muted); background: var(--surface-2); padding: 4px 12px; border-radius: 999px; text-align: center; max-width: 90%; }
.note.routine { color: var(--accent); background: var(--accent-soft); }
.day-empty { margin: auto; text-align: center; color: var(--muted); max-width: 320px; }
.day-empty img { opacity: .9; margin-bottom: .6em; }
.typing { display: inline-flex; gap: 4px; }
.typing i { width: 6px; height: 6px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: .2s; } .typing i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }
.inline-approval { border: 1px dashed var(--accent); background: var(--accent-soft); border-radius: 12px; padding: 8px 10px; margin-top: 8px; font-size: .9rem; }
.inline-approval a { color: var(--accent); font-weight: 600; }
.msg-error { align-self: center; color: var(--danger); font-size: .88rem; }

.composer {
  display: flex; gap: 8px; align-items: flex-end; padding: 8px 12px;
  padding-bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 8px);
  max-width: 760px; width: 100%; margin: 0 auto;
}
.composer textarea { resize: none; max-height: 40vh; border-radius: 22px; padding: .65em 1em; line-height: 1.4; }

/* cards */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; margin: 14px 0; box-shadow: var(--shadow); }
.item { display: flex; gap: .6em; align-items: flex-start; justify-content: space-between; padding: .5em 0; border-top: 1px solid var(--border); }
.item:first-child { border-top: 0; }
.item .grow { flex: 1; min-width: 0; }
.approval { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow); }
.approval.focus { outline: 2px solid var(--accent); }
.approval .meta { font-size: .8rem; color: var(--muted); margin-bottom: .3em; }
.approval pre { white-space: pre-wrap; font-size: .82rem; background: var(--surface-2); padding: .6em; border-radius: 10px; margin: .5em 0 0; max-height: 240px; overflow: auto; }
.status { font-size: .75rem; font-weight: 600; padding: 1px 8px; border-radius: 999px; background: var(--surface-2); }
.status.ok { color: var(--ok); } .status.error { color: var(--danger); } .status.running { color: var(--warn); }

/* login */
.login { display: grid; place-items: center; padding: 24px 16px; }
.login-card { width: 100%; max-width: 360px; text-align: center; }
.login-logo { margin-bottom: .4em; }
.login-card details { margin-top: 1.4em; text-align: left; }
.login-card summary { cursor: pointer; color: var(--muted); text-align: center; }

/* ---------- Lyra avatar & stage ---------- */
.stage {
  display: flex; align-items: center; gap: 12px;
  padding: calc(env(safe-area-inset-top, 0px) + 8px) 16px 8px;
  border-bottom: 1px solid var(--border); background: var(--bg);
  max-width: 760px; width: 100%; margin: 0 auto; transition: padding .35s ease;
}
.stage.large { flex-direction: column; gap: 4px; padding-top: calc(env(safe-area-inset-top, 0px) + 18px); padding-bottom: 12px; border-bottom-color: transparent; }
.stage-text { display: flex; flex-direction: column; min-width: 0; }
.stage.large .stage-text { align-items: center; text-align: center; }
.stage-name { font-weight: 700; font-size: 1.05rem; }
.caption { color: var(--accent); font-size: .85rem; min-height: 1.3em; transition: opacity .2s; }
.caption:empty::before { content: attr(data-idle); color: var(--muted); }

.avatar { width: 56px; height: 56px; flex: none; transition: width .35s ease, height .35s ease; cursor: pointer; }
.stage.large .avatar { width: 136px; height: 136px; }
.lyra-svg { width: 100%; height: 100%; overflow: visible; display: block; }
.lyra-svg * { transform-box: fill-box; transform-origin: center; }
.lyra-svg .halo { animation: lyra-breathe 4.5s ease-in-out infinite; opacity: .75; transition: opacity .4s; }
.lyra-svg .body { animation: lyra-float 6s ease-in-out infinite; }
.lyra-svg .orbit { opacity: 0; transform-origin: 100px 100px; transform-box: view-box; transition: opacity .4s; }
.lyra-svg .eyes { transition: transform .45s cubic-bezier(.3,.7,.4,1); }
.lyra-svg .eye { transition: transform .12s ease; }
.lyra-svg .smile { transition: opacity .2s, transform .3s; }
.lyra-svg .mouth { opacity: 0; transition: opacity .15s; }
.lyra-svg .stars { transition: opacity .6s; }
.lyra-svg .vega { animation: lyra-twinkle 3.2s ease-in-out infinite; }
.avatar.blink .eye { transform: scaleY(.08); }

.avatar[data-state="listening"] .halo { opacity: 1; animation-duration: 2.4s; }
.avatar[data-state="listening"] .eye { transform: scale(1.12); }
.avatar[data-state="thinking"] .orbit { opacity: 1; animation: lyra-spin 5s linear infinite; }
.avatar[data-state="thinking"] .stars { opacity: .85; }
.avatar[data-state="working"] .orbit { opacity: 1; animation: lyra-spin 1.8s linear infinite; }
.avatar[data-state="working"] .halo { animation-duration: 1.6s; opacity: 1; }
.avatar[data-state="speaking"] .smile { opacity: 0; }
.avatar[data-state="speaking"] .mouth { opacity: 1; }
.avatar[data-state="speaking"] .halo { opacity: calc(.7 + var(--talk, 0) * .3); }
.avatar[data-state="waiting"] .halo { filter: hue-rotate(175deg) saturate(1.6); animation-duration: 1.8s; opacity: 1; }
.avatar[data-state="sad"] .eye { transform: scaleY(.55) translateY(3px); }
.avatar[data-state="sad"] .smile { transform: translateY(6px) scaleY(-1); }

@keyframes lyra-breathe { 0%, 100% { transform: scale(.9); } 50% { transform: scale(1.03); } }
@keyframes lyra-float { 0%, 100% { transform: translateY(1.5px); } 50% { transform: translateY(-2.5px); } }
@keyframes lyra-spin { to { transform: rotate(360deg); } }
@keyframes lyra-twinkle { 0%, 100% { opacity: .55; } 50% { opacity: 1; } }
@media (prefers-reduced-motion: reduce) {
  .lyra-svg .halo, .lyra-svg .body, .lyra-svg .vega, .avatar .orbit { animation: none !important; }
}
.avatar-hi { animation: lyra-hop .6s ease; }
@keyframes lyra-hop { 30% { transform: translateY(-6px) rotate(-4deg); } 60% { transform: translateY(0) rotate(3deg); } }

/* photorealistic avatar */
.avatar.photo { border-radius: 50%; padding: 3px; transition: width .35s ease, height .35s ease, box-shadow .4s ease; box-shadow: 0 0 0 2px var(--border); }
.photo-frame { position: relative; width: 100%; height: 100%; border-radius: 50%; overflow: hidden; background: var(--surface-2); }
.photo-frame img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; opacity: 0; transition: opacity .6s ease; }
.photo-frame img.on { opacity: 1; }
.avatar.photo[data-state="listening"] { box-shadow: 0 0 0 3px var(--accent); }
.avatar.photo[data-state="thinking"],
.avatar.photo[data-state="working"] { box-shadow: 0 0 0 3px var(--accent); animation: photo-ring 1.8s ease-in-out infinite; }
.avatar.photo[data-state="speaking"] { box-shadow: 0 0 0 3px var(--accent), 0 0 22px 2px color-mix(in srgb, var(--accent) 45%, transparent); }
.avatar.photo[data-state="waiting"] { box-shadow: 0 0 0 3px var(--warn); }
.avatar.photo[data-state="sad"] { box-shadow: 0 0 0 3px var(--danger); }
@keyframes photo-ring { 50% { box-shadow: 0 0 0 3px var(--accent), 0 0 18px 3px color-mix(in srgb, var(--accent) 40%, transparent); } }
@media (prefers-reduced-motion: reduce) { .avatar.photo { animation: none !important; } }

.msg.assistant.reminder { border-color: var(--accent); background: var(--accent-soft); }
