:root {
  --ocean: #1A5F8A; --lagon: #3BA8C4; --ecume: #7ECFE0; --sunset: #E26B3A; --soleil: #F5C842;
  --sable: #F5F0E8; --white: #FFFFFF; --text: #1A2433; --muted: #4A5568;
  --bg: #FFFFFF; --panel: #F5F0E8; --line: rgba(26, 95, 138, 0.12); --bubble-me: #1A5F8A; --bubble-me-text: #fff;
  --bubble-bot: #FFFFFF; --hover: #EAF6FB; --shadow: 0 4px 24px rgba(26, 95, 138, 0.08);
  --safe-top: env(safe-area-inset-top, 0px); --safe-bottom: env(safe-area-inset-bottom, 0px);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0F1B26; --panel: #13222F; --text: #E6EEF4; --muted: #9AB0C0; --line: rgba(126, 207, 224, 0.14);
    --bubble-me: #1F6E9E; --bubble-bot: #182B3A; --hover: #1A3142; --white: #182B3A;
    --shadow: 0 4px 24px rgba(0, 0, 0, 0.3);
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: 'DM Sans', system-ui, sans-serif; font-size: 17px; line-height: 1.55; color: var(--text);
  background: var(--bg); -webkit-font-smoothing: antialiased; overscroll-behavior: none;
}
button { font: inherit; color: inherit; cursor: pointer; }
h1, h2 { font-family: 'Syne', sans-serif; color: var(--ocean); margin: 0; }
@media (prefers-color-scheme: dark) { h1, h2 { color: var(--ecume); } }
[hidden] { display: none !important; }
.muted { color: var(--muted); font-size: 15px; }

.btn-primary { background: var(--sunset); color: #fff; border: 0; border-radius: 50px; padding: 12px 24px; font-weight: 500; font-size: 16px; }
.btn-primary:hover { opacity: .9; }
.btn-secondary { background: transparent; color: var(--ocean); border: 2px solid var(--ocean); border-radius: 50px; padding: 10px 22px; font-weight: 500; font-size: 16px; }
@media (prefers-color-scheme: dark) { .btn-secondary { color: var(--ecume); border-color: var(--ecume); } }
.btn-secondary.light { color: #fff; border-color: rgba(255,255,255,.7); }
.icon-btn { background: none; border: 0; width: 42px; height: 42px; border-radius: 50%; display: grid; place-items: center; flex: none; color: var(--ocean); }
.icon-btn svg { width: 24px; height: 24px; fill: currentColor; }
.icon-btn:hover { background: var(--hover); }
@media (prefers-color-scheme: dark) { .icon-btn { color: var(--ecume); } }
.tag { display: inline-block; background: #E8F5FA; color: var(--ocean); border: 0; border-radius: 50px; padding: 1px 10px; font-size: 13px; font-weight: 500; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
@media (prefers-color-scheme: dark) { .tag { background: #1A3A4E; color: var(--ecume); } }

/* auth pages */
body.auth { display: grid; place-items: center; min-height: 100%; background: linear-gradient(160deg, var(--bg) 0%, #EAF6FB 60%, var(--sable) 100%); padding: 16px; }
@media (prefers-color-scheme: dark) { body.auth { background: var(--bg); } }
.auth-card { background: var(--white); border-radius: 16px; padding: 32px 28px; width: 100%; max-width: 380px; box-shadow: var(--shadow); text-align: center; }
.auth-card h1 { font-size: 26px; margin: 12px 0 20px; }
.auth-card form { display: grid; gap: 16px; text-align: left; }
.auth-card label { display: grid; gap: 6px; font-weight: 500; font-size: 15px; }
.auth-card input { font: inherit; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg); color: var(--text); }
.err { color: var(--sunset); font-weight: 500; }

/* layout */
#app { display: grid; grid-template-columns: 320px minmax(0, 1fr); height: 100dvh; }
#side { min-width: 0; overflow: hidden; background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; padding-top: var(--safe-top); }
#chat { display: flex; flex-direction: column; min-height: 0; min-width: 0; padding-top: var(--safe-top); }
.side-head { display: flex; align-items: center; gap: 10px; padding: 12px 10px 8px 16px; position: relative; }
.side-head h1 { font-size: 22px; flex: 1; }
.side-head img { border-radius: 9px; }
.search { padding: 4px 12px 8px; }
.search input { width: 100%; font: inherit; font-size: 15px; padding: 9px 14px; border-radius: 50px; border: 1px solid var(--line); background: var(--bg); color: var(--text); }
.tree { flex: 1; overflow-y: auto; padding: 4px 8px 12px; -webkit-overflow-scrolling: touch; }
.side-foot { padding: 10px 12px calc(12px + var(--safe-bottom)); border-top: 1px solid var(--line); }
.side-foot .btn-primary { width: 100%; }

.proj { margin-top: 6px; }
.proj-head { display: flex; align-items: center; gap: 4px; width: 100%; background: none; border: 0; padding: 6px 8px; border-radius: 10px; text-align: left; font-weight: 500; font-size: 14px; color: var(--muted); }
.proj-head:hover { background: var(--hover); }
.proj-head .chev { width: 16px; height: 16px; transition: transform .15s; flex: none; }
.proj.closed .chev { transform: rotate(-90deg); }
.proj.closed .proj-body { display: none; }
.proj-head .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; text-transform: uppercase; letter-spacing: .04em; font-size: 12.5px; }
.proj-head .count { font-size: 12px; opacity: .7; }
.proj-head .add { opacity: 0; font-size: 20px; line-height: 1; padding: 0 6px; border-radius: 6px; }
.proj-head:hover .add, .proj-head:focus-within .add { opacity: .8; }
@media (hover: none) { .proj-head .add { opacity: .6; } }
.proj.sub { margin-left: 12px; }
.conv { display: block; width: 100%; text-align: left; background: none; border: 0; border-radius: 12px; padding: 8px 12px; margin: 1px 0; }
.conv:hover { background: var(--hover); }
.conv.active { background: var(--ocean); color: #fff; }
.conv .t { display: block; font-weight: 500; font-size: 15.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv .d { display: block; font-size: 12.5px; opacity: .65; }
.conv .dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--sunset); margin-right: 6px; vertical-align: middle; animation: pulse 1.2s infinite; }
.tree-empty { color: var(--muted); font-size: 14px; padding: 20px 12px; text-align: center; }

.menu { position: absolute; top: 52px; right: 10px; background: var(--white); border-radius: 14px; box-shadow: 0 12px 40px rgba(26,95,138,.18); padding: 6px; z-index: 30; min-width: 230px; border: 1px solid var(--line); }
.menu button { display: block; width: 100%; text-align: left; background: none; border: 0; padding: 10px 12px; border-radius: 10px; font-size: 15px; }
.menu button:hover { background: var(--hover); }

.chat-head { display: flex; align-items: center; gap: 6px; padding: 8px 8px 8px 12px; border-bottom: 1px solid var(--line); position: relative; background: var(--bg); }
.chat-head .back { display: none; }
.chat-title { flex: 1; min-width: 0; display: grid; gap: 2px; justify-items: start; }
.chat-title input { width: 100%; font-family: 'Syne', sans-serif; font-weight: 700; font-size: 18px; border: 0; background: none; color: var(--text); padding: 2px 0; outline: none; text-overflow: ellipsis; }
.messages { flex: 1; overflow-y: auto; padding: 20px 16px 12px; display: flex; flex-direction: column; gap: 10px; -webkit-overflow-scrolling: touch;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='240' height='60' viewBox='0 0 240 60'%3E%3Cpath d='M0,30 C40,10 80,50 120,30 C160,10 200,50 240,30' fill='none' stroke='%233BA8C4' stroke-width='1.2' opacity='0.08'/%3E%3C/svg%3E"); }
.empty { margin: auto; text-align: center; color: var(--muted); max-width: 320px; }
.empty img { border-radius: 16px; opacity: .9; }
.msg { max-width: min(720px, 86%); padding: 10px 14px; border-radius: 18px; word-wrap: break-word; overflow-wrap: anywhere; box-shadow: 0 1px 2px rgba(26,95,138,.08); }
.msg.user { align-self: flex-end; background: var(--bubble-me); color: var(--bubble-me-text); border-bottom-right-radius: 6px; white-space: pre-wrap; }
.msg.assistant { align-self: flex-start; background: var(--bubble-bot); border: 1px solid var(--line); border-bottom-left-radius: 6px; }
.msg.assistant p { margin: 0 0 .6em; } .msg.assistant p:last-child { margin-bottom: 0; }
.msg.assistant ul, .msg.assistant ol { margin: .3em 0 .6em; padding-left: 1.3em; }
.msg.assistant h3 { font-family: 'Syne', sans-serif; font-size: 17px; margin: .6em 0 .3em; color: var(--ocean); }
.msg.assistant a { color: var(--lagon); }
.msg code { font-family: ui-monospace, Menlo, monospace; font-size: 14px; background: rgba(59,168,196,.12); padding: 1px 5px; border-radius: 5px; }
.msg pre { background: rgba(26,36,51,.06); padding: 10px 12px; border-radius: 10px; overflow-x: auto; font-size: 14px; }
.msg pre code { background: none; padding: 0; }
.msg .time { display: block; font-size: 11.5px; opacity: .55; margin-top: 4px; text-align: right; }
.msg .speak { float: right; background: none; border: 0; opacity: .5; padding: 0 0 0 8px; font-size: 14px; }
.msg .speak:hover { opacity: 1; }
.status { align-self: flex-start; color: var(--muted); font-size: 14px; font-style: italic; padding: 2px 6px; display: flex; gap: 8px; align-items: center; }
.typing { display: inline-flex; gap: 4px; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--lagon); animation: bounce 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: .15s; } .typing i:nth-child(3) { animation-delay: .3s; }
@keyframes bounce { 0%, 60%, 100% { transform: translateY(0); opacity: .5; } 30% { transform: translateY(-5px); opacity: 1; } }
@keyframes pulse { 50% { opacity: .35; } }
.approval { align-self: stretch; background: #FEF9E7; border: 1px solid var(--soleil); border-radius: 16px; padding: 14px 16px; color: #1A2433; }
.approval b { display: block; margin-bottom: 6px; }
.approval code { display: block; white-space: pre-wrap; margin: 6px 0 10px; background: rgba(0,0,0,.05); padding: 8px; border-radius: 8px; font-size: 13.5px; }
.approval .row-btns { display: flex; gap: 8px; flex-wrap: wrap; }
.approval button { border-radius: 50px; padding: 8px 16px; border: 0; font-weight: 500; font-size: 15px; }
.approval .ok { background: var(--ocean); color: #fff; } .approval .ok2 { background: #E8F5FA; color: var(--ocean); } .approval .no { background: #FFF0E8; color: #C45A2A; }
.error-line { align-self: center; color: var(--sunset); font-size: 14px; }

.composer { display: flex; align-items: flex-end; gap: 4px; padding: 8px 10px calc(8px + var(--safe-bottom)); border-top: 1px solid var(--line); background: var(--bg); }
.composer textarea { flex: 1; resize: none; font: inherit; font-size: 17px; line-height: 1.4; padding: 10px 16px; border-radius: 22px; border: 1px solid var(--line); background: var(--panel); color: var(--text); max-height: 40vh; min-height: 44px; outline: none; }
.composer textarea:focus { border-color: var(--lagon); }
.composer .send { background: var(--sunset); color: #fff; }
.composer .send:hover { background: var(--sunset); opacity: .9; }
.composer .stop { background: var(--muted); color: #fff; }
.composer .mic.rec { background: var(--sunset); color: #fff; animation: pulse 1.2s infinite; }
.composer.disabled { opacity: .5; pointer-events: none; }

/* voice mode */
.voice { position: fixed; inset: 0; z-index: 50; background: radial-gradient(circle at 50% 40%, #3BA8C4 0%, #1A5F8A 55%, #0E3A57 100%); color: #fff;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 22px; padding: calc(24px + var(--safe-top)) 16px calc(24px + var(--safe-bottom)); text-align: center; }
.voice-project { position: absolute; top: calc(18px + var(--safe-top)); font-size: 14px; opacity: .8; }
.orb { width: 180px; height: 180px; border-radius: 50%; border: 0; background: rgba(255,255,255,.12); display: grid; place-items: center; transition: transform .1s; }
.orb span { width: 110px; height: 110px; border-radius: 50%; background: radial-gradient(circle at 35% 35%, #fff, var(--ecume) 45%, var(--lagon)); box-shadow: 0 0 60px rgba(126,207,224,.6); transition: transform .08s; }
.voice.thinking .orb span { animation: breathe 1.4s infinite ease-in-out; background: radial-gradient(circle at 35% 35%, #fff, var(--soleil) 50%, var(--sunset)); }
.voice.speaking .orb span { animation: talk .5s infinite alternate ease-in-out; }
.voice.muted .orb span { opacity: .35; }
@keyframes breathe { 50% { transform: scale(.82); } }
@keyframes talk { from { transform: scale(.92); } to { transform: scale(1.08); } }
.voice-state { font-family: 'Syne', sans-serif; font-weight: 700; font-size: 24px; margin: 0; }
.voice-heard { margin: 0; max-width: 560px; min-height: 3em; opacity: .85; font-size: 16px; }
.voice-actions { display: flex; gap: 12px; }

dialog { border: 0; border-radius: 18px; padding: 22px; width: min(440px, calc(100vw - 32px)); background: var(--white); color: var(--text); box-shadow: 0 20px 60px rgba(0,0,0,.25); }
dialog::backdrop { background: rgba(14, 30, 45, .45); }
dialog h2 { font-size: 20px; margin-bottom: 12px; }
.pick-list { max-height: 55vh; overflow-y: auto; margin-bottom: 14px; display: grid; gap: 2px; }
.pick-list button { text-align: left; background: none; border: 0; padding: 9px 12px; border-radius: 10px; font-size: 16px; }
.pick-list button:hover, .pick-list button.cur { background: var(--hover); }
.pick-list button.sub { padding-left: 28px; }
#voice-dlg select { width: 100%; font: inherit; padding: 10px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); color: var(--text); margin: 10px 0; }
.row { display: flex; gap: 12px; align-items: center; margin: 8px 0 16px; } .row input { flex: 1; }
.row-btns { display: flex; gap: 10px; justify-content: flex-end; }

/* phone */
@media (max-width: 760px) {
  #app { grid-template-columns: minmax(0, 1fr); }
  #app.show-list #chat { display: none; }
  #app.show-chat #side { display: none; }
  .chat-head .back { display: grid; }
  .msg { max-width: 90%; }
  .messages { padding: 14px 10px 10px; }
}
