/* ============ Tokens ============ */
:root {
  --bg: #f2f2f7; --bg-0: rgba(242,242,247,0); --bg2: #e9e9ee; --bg3: #fff; --fg: #000; --fg2: #6c6c70; --sep: rgba(60,60,67,.16);
  --blue: #0a84ff; --red: #ff453a; --bubble-ai: #fff; --code: #f0f0f5;
  --glass: rgba(255,255,255,.62); --glass-strong: rgba(255,255,255,.78); --glass-line: rgba(0,0,0,.07);
  --shadow: 0 8px 28px rgba(0,0,0,.12), 0 1px 3px rgba(0,0,0,.06);
  --sat: env(safe-area-inset-top); --sab: env(safe-area-inset-bottom);
  --sal: env(safe-area-inset-left); --sar: env(safe-area-inset-right);
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", system-ui, sans-serif;
  --ease: cubic-bezier(.32,.72,0,1);
  --nav-h: 56px; --tab-h: 62px; --tab-bottom: max(calc(var(--sab) - 14px), 10px); --cmp-h: 60px;
  --stack: calc(var(--tab-h) + var(--tab-bottom));
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #000; --bg-0: rgba(0,0,0,0); --bg2: #1c1c1e; --bg3: #1c1c1e; --fg: #fff; --fg2: #8e8e93; --sep: rgba(84,84,88,.5);
    --bubble-ai: #1d1d20; --code: #121214;
    --glass: rgba(44,44,48,.55); --glass-strong: rgba(36,36,40,.78); --glass-line: rgba(255,255,255,.12);
    --shadow: 0 10px 30px rgba(0,0,0,.55), 0 1px 2px rgba(0,0,0,.4); }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; -webkit-touch-callout: none; }
html, body {
  margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--fg);
  font: 17px/1.38 var(--font); -webkit-font-smoothing: antialiased; letter-spacing: -.01em;
  overscroll-behavior: none; -webkit-text-size-adjust: 100%;
  touch-action: manipulation; -webkit-user-select: none; user-select: none;
}
button, input, textarea, select { font: inherit; color: inherit; touch-action: manipulation; letter-spacing: inherit; }
button { background: none; border: 0; padding: 0; cursor: pointer; }
input, textarea, select { font-size: 16px; -webkit-user-select: text; user-select: text; outline: none; border: 0; background: transparent; }
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
[hidden] { display: none !important; }
.glass { background: var(--glass); backdrop-filter: blur(28px) saturate(1.9); -webkit-backdrop-filter: blur(28px) saturate(1.9);
  box-shadow: inset 0 0 0 .5px var(--glass-line), var(--shadow); }

/* #app fills the *physical* screen (incl. under status bar & home indicator); JS only resizes it for the keyboard */
#app { position: fixed; left: 0; top: 0; width: 100%; height: 100%; height: 100lvh; min-height: 100%; overflow: hidden; background: var(--bg); }
.view { position: absolute; inset: 0; display: none; flex-direction: column; background: var(--bg); }
.view.active { display: flex; animation: fade .3s var(--ease); }
@keyframes fade { from { opacity: 0; transform: translateY(8px); } }
.kb .tabbar { transform: translateY(140%); opacity: 0; pointer-events: none; }
.kb { --stack: 0px; }

/* navbar */
.navbar { flex: none; display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: calc(var(--sat) + 8px) calc(14px + var(--sar)) 8px calc(14px + var(--sal)); min-height: calc(var(--sat) + var(--nav-h)); z-index: 5; }
#view-chat .navbar { position: absolute; top: 0; left: 0; right: 0; background: linear-gradient(to bottom, var(--bg) 62%, var(--bg-0)); padding-bottom: 22px; pointer-events: none; }
#view-chat .navbar > * { pointer-events: auto; }
.navbar.large { align-items: flex-end; padding-bottom: 6px; }
.large-title { margin: 0; font-size: 34px; font-weight: 800; letter-spacing: -.02em; padding-left: 4px; }
.nav-btn { flex: none; width: 42px; height: 42px; display: grid; place-items: center; color: var(--fg); border-radius: 21px;
  background: var(--glass); backdrop-filter: blur(28px) saturate(1.9); -webkit-backdrop-filter: blur(28px) saturate(1.9);
  box-shadow: inset 0 0 0 .5px var(--glass-line), var(--shadow); transition: transform .25s var(--ease), opacity .15s; }
.nav-btn svg { width: 21px; height: 21px; }
.nav-btn:active { transform: scale(.9); opacity: .75; }
.model-pill { display: flex; align-items: center; gap: 6px; max-width: 58%; height: 42px; padding: 0 14px 0 16px; border-radius: 21px; font-weight: 600; font-size: 16px;
  background: var(--glass); backdrop-filter: blur(28px) saturate(1.9); -webkit-backdrop-filter: blur(28px) saturate(1.9);
  box-shadow: inset 0 0 0 .5px var(--glass-line), var(--shadow); transition: transform .25s var(--ease); }
.model-pill:active { transform: scale(.95); }
.model-pill::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: #30d158; box-shadow: 0 0 8px #30d158; flex: none; }
.model-pill span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chev { width: 14px; height: 14px; stroke-width: 2.6; color: var(--fg2); flex: none; }

/* scroll */
.scroll { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior-y: contain; }
.scroll.pad { padding: 8px 16px calc(var(--stack) + 28px); background: var(--bg); }
#view-settings, #view-settings .navbar { background: var(--bg); }
@media (prefers-color-scheme: light) { #view-settings, #view-settings .scroll.pad, #view-settings .navbar { background: var(--bg); } }

/* messages */
#messages { padding: calc(var(--sat) + var(--nav-h) + 14px) 14px calc(var(--stack) + var(--cmp-h) + 22px); display: flex; flex-direction: column; gap: 4px; }
.kb #messages { padding-bottom: calc(var(--cmp-h) + 22px); }
.empty { margin: auto; text-align: center; color: var(--fg2); padding: 24px; display: flex; flex-direction: column; align-items: center; }
.empty .logo { width: 84px; height: 84px; border-radius: 28px; display: grid; place-items: center; font-size: 44px; margin-bottom: 6px;
  background: linear-gradient(145deg, #34d8a4, #0b7a60); box-shadow: 0 14px 40px rgba(30,200,150,.35), inset 0 1px 0 rgba(255,255,255,.35); }
.chips { display: flex; flex-direction: column; gap: 8px; margin-top: 22px; width: 100%; max-width: 320px; }
.chips button { padding: 12px 16px; border-radius: 16px; text-align: left; color: var(--fg); font-size: 15px;
  background: var(--glass); box-shadow: inset 0 0 0 .5px var(--glass-line); transition: transform .2s var(--ease); }
.chips button:active { transform: scale(.97); }
.empty h1 { margin: 6px 0 2px; color: var(--fg); font-size: 28px; }
.empty p { margin: 0; }
.msg { display: flex; flex-direction: column; max-width: 86%; animation: pop .28s var(--ease); }
@keyframes pop { from { opacity: 0; transform: translateY(10px) scale(.97); } }
.msg.user { align-self: flex-end; align-items: flex-end; margin-top: 6px; }
.msg.assistant { align-self: flex-start; max-width: 94%; margin-top: 6px; }
.bubble { padding: 10px 15px; border-radius: 22px; word-wrap: break-word; overflow-wrap: anywhere; -webkit-user-select: text; user-select: text; }
.user .bubble { background: linear-gradient(180deg, #2d9bff, #0a7aff); color: #fff; border-bottom-right-radius: 7px; box-shadow: 0 2px 10px rgba(10,132,255,.28); white-space: pre-wrap; }
.assistant .bubble { background: var(--bubble-ai); border-bottom-left-radius: 7px; box-shadow: inset 0 0 0 .5px var(--glass-line); }
.bubble p { margin: 0 0 .6em; } .bubble p:last-child { margin: 0; }
.bubble ul, .bubble ol { margin: .3em 0; padding-left: 1.3em; }
.bubble h1, .bubble h2, .bubble h3 { margin: .6em 0 .3em; font-size: 1.05em; }
.bubble a { color: var(--blue); }
.bubble code { font: 14px ui-monospace, SFMono-Regular, Menlo, monospace; background: var(--code); padding: 1px 5px; border-radius: 5px; }
.bubble pre { margin: .5em 0; background: var(--code); border-radius: 10px; padding: 10px 12px; overflow-x: auto; -webkit-overflow-scrolling: touch; position: relative; }
.bubble pre code { background: none; padding: 0; white-space: pre; }
.cursor::after { content: ""; display: inline-block; width: 8px; height: 8px; margin-left: 4px; border-radius: 50%; background: var(--fg2); animation: pulse 1s infinite; vertical-align: middle; }
@keyframes pulse { 50% { opacity: .2; transform: scale(.7); } }
.msg-actions { display: flex; gap: 14px; padding: 4px 6px 0; color: var(--fg2); }
.msg-actions button { padding: 4px; } .msg-actions svg { width: 17px; height: 17px; }
.msg-actions button:active { opacity: .4; }
.media { display: flex; flex-wrap: wrap; gap: 4px; justify-content: flex-end; margin-bottom: 4px; }
.media .thumb { width: 150px; height: 150px; border-radius: 14px; object-fit: cover; background: var(--bg2); position: relative; }
.media .vid { position: relative; }
.media .vid::after { content: "▶"; position: absolute; inset: 0; display: grid; place-items: center; color: #fff; font-size: 28px; text-shadow: 0 1px 6px rgba(0,0,0,.6); }
.err { color: var(--red); font-size: 14px; }

/* composer — floating glass capsule above the tab bar */
.composer-wrap { position: absolute; left: 0; right: 0; bottom: calc(var(--stack) + 10px); z-index: 6; padding: 0 calc(12px + var(--sar)) 0 calc(12px + var(--sal)); transition: bottom .3s var(--ease); }
.kb .composer-wrap { bottom: 8px; }
.attach-strip { display: flex; gap: 8px; overflow-x: auto; padding: 0 4px 10px; }
.attach-strip:empty { display: none; }
.chip { position: relative; flex: none; width: 68px; height: 68px; border-radius: 16px; overflow: hidden; animation: pop .3s var(--ease); box-shadow: var(--shadow); }
.chip img { width: 100%; height: 100%; object-fit: cover; }
.chip.video::after { content: "▶"; position: absolute; left: 6px; bottom: 4px; color: #fff; font-size: 12px; text-shadow: 0 1px 3px #000; }
.chip button { position: absolute; top: 4px; right: 4px; width: 22px; height: 22px; border-radius: 11px; background: rgba(0,0,0,.6); color: #fff; display: grid; place-items: center; }
.chip button svg { width: 12px; height: 12px; stroke-width: 3; }
.composer { display: flex; align-items: flex-end; gap: 8px; }
.input-box { flex: 1; display: flex; align-items: flex-end; min-height: 46px; border-radius: 23px; padding: 0 6px 0 18px;
  background: var(--glass-strong); backdrop-filter: blur(28px) saturate(1.9); -webkit-backdrop-filter: blur(28px) saturate(1.9);
  box-shadow: inset 0 0 0 .5px var(--glass-line), var(--shadow); }
#input { flex: 1; resize: none; max-height: 140px; padding: 12px 0; line-height: 22px; }
#input::placeholder { color: var(--fg2); }
.circle { flex: none; width: 36px; height: 36px; border-radius: 18px; display: grid; place-items: center; transition: transform .25s var(--ease), opacity .15s, background .2s; }
.circle:active { transform: scale(.86); }
.circle svg { width: 20px; height: 20px; }
.circle.ghost { width: 46px; height: 46px; border-radius: 23px; color: var(--fg); background: var(--glass-strong); backdrop-filter: blur(28px) saturate(1.9); -webkit-backdrop-filter: blur(28px) saturate(1.9); box-shadow: inset 0 0 0 .5px var(--glass-line), var(--shadow); }
.circle.ghost svg { width: 22px; height: 22px; }
.circle.small { width: 34px; height: 34px; margin: 6px 0; color: var(--fg2); }
.circle.small.rec { background: var(--red); color: #fff; animation: pulse 1.2s infinite; }
.circle.primary { background: linear-gradient(180deg, #3aa0ff, #0a74f0); color: #fff; width: 46px; height: 46px; border-radius: 23px; box-shadow: 0 6px 18px rgba(10,132,255,.45); animation: pop .3s var(--ease); }
.circle.primary svg { stroke-width: 2.6; width: 22px; height: 22px; }

/* tab bar — floating glass capsule with a sliding highlight */
.tabbar { position: absolute; left: 16px; right: 16px; bottom: var(--tab-bottom); height: var(--tab-h); margin: 0 var(--sal) 0 var(--sal); z-index: 10; display: flex; padding: 6px; border-radius: 34px;
  background: var(--glass-strong); backdrop-filter: blur(30px) saturate(2); -webkit-backdrop-filter: blur(30px) saturate(2);
  box-shadow: inset 0 0 0 .5px var(--glass-line), var(--shadow); transition: transform .35s var(--ease), opacity .25s; --i: 0; }
.tab-ind { position: absolute; top: 6px; bottom: 6px; left: 6px; width: calc((100% - 12px) / 3); border-radius: 28px; background: rgba(120,120,128,.2);
  transform: translateX(calc(var(--i) * 100%)); transition: transform .42s var(--ease); }
.tab { position: relative; flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; color: var(--fg2); font-size: 10.5px; font-weight: 600; letter-spacing: 0; transition: color .25s, transform .25s var(--ease); }
.tab svg { width: 25px; height: 25px; transition: transform .3s var(--ease); }
.tab.active { color: var(--blue); }
.tab.active svg { transform: scale(1.06); }
.tab:active { transform: scale(.93); }

/* chat list */
.search { flex: none; padding: 4px 16px 10px; }
.search input { width: 100%; height: 40px; border-radius: 14px; background: var(--bg2); padding: 0 14px; }
#chat-list { background: var(--bg); padding: 0 12px calc(var(--stack) + 24px); }
.row-wrap { position: relative; overflow: hidden; }
.chat-row { position: relative; background: var(--bg); display: block; width: 100%; text-align: left; padding: 12px 8px; transition: transform .28s var(--ease); }
.chat-row.drag { transition: none; }
.chat-row::after { content: ""; position: absolute; left: 8px; right: 0; bottom: 0; height: .5px; background: var(--sep); }
.chat-row .t { font-weight: 600; display: flex; justify-content: space-between; gap: 8px; }
.chat-row .t span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-row .d { color: var(--fg2); font-size: 14px; font-weight: 400; flex: none; }
.chat-row .p { color: var(--fg2); font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-row:active { background: var(--bg2); }
.del-btn { position: absolute; right: 0; border-radius: 14px; top: 0; bottom: 0; width: 88px; background: var(--red); color: #fff; font-weight: 600; }
.row-wrap.removing { animation: remove .3s var(--ease) forwards; }
@keyframes remove { to { height: 0; opacity: 0; } }
.none { text-align: center; color: var(--fg2); padding: 60px 20px; }

/* settings */
.group-title { margin: 20px 16px 6px; font-size: 13px; color: var(--fg2); text-transform: uppercase; }
.group { background: var(--bg3); border-radius: 12px; overflow: hidden; }
@media (prefers-color-scheme: light) { .group { background: #fff; } }
.group { border-radius: 16px; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 11px 16px; min-height: 44px; width: 100%; text-align: left; position: relative; }
.row + .row::before { content: ""; position: absolute; left: 16px; right: 0; top: 0; height: .5px; background: var(--sep); }
.row > span { flex: none; }
.row input:not([type=checkbox]):not([type=range]), .row select { flex: 1; min-width: 0; text-align: right; color: var(--fg2); }
.row.col { flex-direction: column; align-items: stretch; gap: 4px; }
.row textarea { width: 100%; resize: none; }
.row.link { color: var(--blue); justify-content: center; } .row.link:active { background: var(--bg2); }
.row.danger { color: var(--red); }
.foot { margin: 6px 16px 0; font-size: 13px; color: var(--fg2); }
.switch { appearance: none; -webkit-appearance: none; width: 51px; height: 31px; border-radius: 16px; background: rgba(120,120,128,.32); position: relative; transition: background .25s; flex: none; }
.switch::after { content: ""; position: absolute; top: 2px; left: 2px; width: 27px; height: 27px; border-radius: 50%; background: #fff; box-shadow: 0 2px 4px rgba(0,0,0,.3); transition: transform .25s var(--ease); }
.switch:checked { background: #34c759; } .switch:checked::after { transform: translateX(20px); }
input[type=range] { flex: 1; max-width: 150px; }

/* sheet */
.backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.4); opacity: 0; pointer-events: none; transition: opacity .3s var(--ease); z-index: 20; }
.backdrop.show { opacity: 1; pointer-events: auto; }
.sheet { position: absolute; left: 8px; right: 8px; bottom: 0; z-index: 21; max-height: 80%; display: flex; flex-direction: column;
  background: var(--glass-strong); backdrop-filter: blur(40px) saturate(2); -webkit-backdrop-filter: blur(40px) saturate(2); border-radius: 30px 30px 0 0; padding-bottom: calc(var(--sab) + 8px);
  transform: translateY(105%); transition: transform .38s var(--ease); }
.sheet.show { transform: none; }
.grabber { width: 36px; height: 5px; border-radius: 3px; background: var(--sep); margin: 8px auto 4px; flex: none; }
#sheet-body { overflow-y: auto; padding: 4px 12px 8px; }
.sheet h3 { margin: 6px 6px 8px; font-size: 13px; color: var(--fg2); text-transform: uppercase; font-weight: 500; }
.opt { display: flex; align-items: center; justify-content: space-between; width: 100%; padding: 15px 18px; background: rgba(120,120,128,.16); text-align: left; border-bottom: .5px solid var(--sep); }
.opts { border-radius: 18px; overflow: hidden; }
.opt:last-child { border-bottom: 0; } .opt:active { filter: brightness(.92); }
.opt .check { color: var(--blue); }
.opt.cancel { margin-top: 8px; justify-content: center; border-radius: 18px; font-weight: 600; color: var(--blue); }

/* voice */
.voice { position: absolute; inset: 0; z-index: 30; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 28px;
  background: radial-gradient(circle at 50% 40%, #12302b, #000 70%); color: #fff;
  opacity: 0; pointer-events: none; transform: scale(1.04); transition: opacity .35s var(--ease), transform .35s var(--ease); }
.voice.show { opacity: 1; pointer-events: auto; transform: none; }
.voice-close { position: absolute; top: calc(var(--sat) + 14px); right: 16px; width: 40px; height: 40px; border-radius: 20px; background: rgba(255,255,255,.14); display: grid; place-items: center; color: #fff; }
.orb-wrap { width: 220px; height: 220px; display: grid; place-items: center; }
.orb { width: 140px; height: 140px; border-radius: 50%; background: radial-gradient(circle at 35% 30%, #9ff5d8, #1fa37c 60%, #0b4d3c); box-shadow: 0 0 60px rgba(60,220,170,.45); transition: transform .12s linear, filter .3s; }
.orb.listening { animation: breathe 2.4s ease-in-out infinite; }
.orb.thinking { animation: spin 1.1s linear infinite; filter: hue-rotate(40deg); border-radius: 46% 54% 52% 48%; }
.orb.speaking { animation: talk .7s ease-in-out infinite; }
@keyframes breathe { 50% { transform: scale(1.12); } }
@keyframes spin { to { transform: rotate(360deg) scale(.9); } }
@keyframes talk { 0%,100% { transform: scale(1); } 30% { transform: scale(1.18); } 60% { transform: scale(.95); } }
.voice-status { font-size: 20px; font-weight: 600; }
.voice-text { max-width: 84%; min-height: 3.6em; text-align: center; color: rgba(255,255,255,.7); font-size: 16px; max-height: 30%; overflow: hidden; }

.toast { position: absolute; left: 50%; top: calc(var(--sat) + 12px); transform: translate(-50%, -140%); z-index: 40; max-width: 88%;
  background: rgba(40,40,44,.95); color: #fff; padding: 10px 16px; border-radius: 22px; font-size: 15px; transition: transform .4s var(--ease); pointer-events: none; }
.toast.show { transform: translate(-50%, 0); }

.secret { -webkit-text-security: disc; }
