/* ============ TOKENS ============ */
:root{
  --bg: #0c0b0a;
  --bg-soft: #131211;
  --surface: #1a1917;
  --surface-hover: #232120;
  --border: #262422;
  --border-strong: #3a3634;
  --text: #ebe8e3;
  --text-dim: #a8a29e;
  --text-muted: #6b6966;
  --accent: #d4b483;
  --accent-soft: rgba(212,180,131,.08);
  --danger: #d97768;
  --danger-soft: rgba(217,119,104,.1);
  --r-sm: 6px;
  --r-md: 10px;
  --r-lg: 14px;
  --r-full: 999px;
  --fs: 15px;
  --ease: cubic-bezier(.32,.72,0,1);
  --ease-out: cubic-bezier(.16,1,.3,1);
}
[data-theme="light"]{
  --bg: #faf8f5;
  --bg-soft: #f5f2ee;
  --surface: #f0ece6;
  --surface-hover: #e8e3db;
  --border: #e0dad1;
  --border-strong: #c9c1b5;
  --text: #1a1816;
  --text-dim: #57534e;
  --text-muted: #928c84;
  --accent: #9a7b4f;
  --accent-soft: rgba(154,123,79,.08);
  --danger: #b94a3a;
  --danger-soft: rgba(185,74,58,.08);
}

/* ============ RESET ============ */
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0;-webkit-tap-highlight-color:transparent}
html,body{
  height:100%;overflow:hidden;
  font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Inter,system-ui,sans-serif;
  background:var(--bg);color:var(--text);
  font-size:var(--fs);line-height:1.6;
  letter-spacing:-.011em;
  -webkit-font-smoothing:antialiased;
  -moz-osx-font-smoothing:grayscale;
  text-rendering:optimizeLegibility;
}
button{font:inherit;color:inherit;background:none;border:none;cursor:pointer;-webkit-user-select:none;user-select:none}
input,textarea,select{
  font:inherit;color:inherit;background:var(--bg-soft);
  border:1px solid var(--border);border-radius:var(--r-md);
  padding:10px 12px;width:100%;outline:none;
  transition:border-color .18s var(--ease),background .18s var(--ease);
}
input::placeholder,textarea::placeholder{color:var(--text-muted)}
input:focus,textarea:focus,select:focus{border-color:var(--border-strong);background:var(--surface)}
textarea{resize:none;font-family:inherit;line-height:1.55}
a{color:var(--accent);text-decoration:none}
a:hover{text-decoration:underline;text-underline-offset:2px}
.hidden{display:none!important}
select{
  cursor:pointer;-webkit-appearance:none;appearance:none;
  background-image:url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='10' viewBox='0 0 24 24' fill='none' stroke='%236b6966' stroke-width='2.4' stroke-linecap='round'><polyline points='6 9 12 15 18 9'/></svg>");
  background-repeat:no-repeat;background-position:right 12px center;padding-right:32px;
}
svg{display:block;flex-shrink:0}

/* ============ SCREENS ============ */
.screen{position:fixed;inset:0;overflow:hidden}
#appScreen{display:flex;flex-direction:column;height:100dvh}

/* ============ AUTH ============ */
#authScreen{display:flex;align-items:center;justify-content:center;padding:24px;overflow-y:auto}
.auth-card{
  width:100%;max-width:380px;
  background:var(--bg-soft);border:1px solid var(--border);
  border-radius:var(--r-lg);padding:32px 28px;
  animation:authIn .5s var(--ease-out);
}
@keyframes authIn{from{opacity:0;transform:translateY(8px)}to{opacity:1;transform:none}}
.auth-brand{display:flex;align-items:center;gap:10px;margin-bottom:28px}
.auth-brand-mark{
  width:32px;height:32px;border-radius:var(--r-sm);
  background:var(--surface);border:1px solid var(--border);
  display:flex;align-items:center;justify-content:center;color:var(--text);
}
.auth-brand-text{font-size:15px;font-weight:600;letter-spacing:-.02em}
.auth-title{font-size:22px;font-weight:600;letter-spacing:-.03em;margin-bottom:6px}
.auth-sub{font-size:13px;color:var(--text-dim);margin-bottom:24px}
.auth-tabs{display:flex;gap:24px;margin-bottom:24px;border-bottom:1px solid var(--border)}
.auth-tab{
  padding:0 0 12px;font-size:13px;font-weight:500;
  color:var(--text-muted);position:relative;transition:color .2s var(--ease);
}
.auth-tab.active{color:var(--text)}
.auth-tab.active::after{
  content:'';position:absolute;bottom:-1px;left:0;right:0;
  height:1.5px;background:var(--text);border-radius:1px;
}
.auth-form{display:flex;flex-direction:column;gap:14px}
.field{display:flex;flex-direction:column;gap:6px}
.field label{font-size:12px;color:var(--text-dim);font-weight:500}
.auth-error{color:var(--danger);font-size:12.5px;min-height:16px;line-height:1.4}
.btn-cta{
  background:var(--text);color:var(--bg);
  font-weight:500;padding:11px;border-radius:var(--r-md);font-size:14px;
  transition:opacity .18s var(--ease),transform .1s var(--ease);
}
.btn-cta:hover{opacity:.9}
.btn-cta:active{transform:scale(.985)}
.auth-foot{text-align:center;font-size:11.5px;color:var(--text-muted);margin-top:20px;line-height:1.5}

/* ============ APP HEADER ============ */
.app-header{
  display:flex;align-items:center;justify-content:space-between;
  padding:12px 16px;padding-top:calc(12px + env(safe-area-inset-top));
  border-bottom:1px solid var(--border);background:var(--bg);
  position:relative;z-index:10;
}
.header-left{display:flex;align-items:center;gap:10px}
.brand-mark{
  width:26px;height:26px;border-radius:var(--r-sm);
  background:var(--surface);border:1px solid var(--border);
  display:flex;align-items:center;justify-content:center;color:var(--text);
}
.brand-text{font-size:13.5px;font-weight:600;letter-spacing:-.015em}
.header-right{display:flex;align-items:center;gap:2px}
.icon-btn{
  width:34px;height:34px;border-radius:var(--r-sm);
  display:flex;align-items:center;justify-content:center;color:var(--text-dim);
  transition:background .18s var(--ease),color .18s var(--ease);
}
.icon-btn:hover{background:var(--surface);color:var(--text)}
.icon-btn:active{transform:scale(.94)}

/* ============ APP MAIN / VIEWS ============ */
.app-main{flex:1;overflow:hidden;position:relative}
.view{
  position:absolute;inset:0;overflow-y:auto;-webkit-overflow-scrolling:touch;
  opacity:0;pointer-events:none;transition:opacity .2s var(--ease);
}
.view.active{opacity:1;pointer-events:auto}
#view-chat{display:flex;flex-direction:column;overflow:hidden}

/* ============ CHAT ============ */
.chat-container{display:flex;flex-direction:column;height:100%}
.chat-messages{
  flex:1;overflow-y:auto;padding:24px 20px 20px;
  display:flex;flex-direction:column;gap:24px;
  -webkit-overflow-scrolling:touch;
}
.welcome{padding:12px 4px;margin:auto 0;animation:welcomeIn .4s var(--ease-out)}
@keyframes welcomeIn{from{opacity:0;transform:translateY(6px)}to{opacity:1;transform:none}}
.welcome-eyebrow{
  font-size:11px;font-weight:600;text-transform:uppercase;
  letter-spacing:.12em;color:var(--text-muted);margin-bottom:14px;
}
.welcome h2{font-size:26px;font-weight:600;letter-spacing:-.03em;line-height:1.2;margin-bottom:10px}
.welcome p{color:var(--text-dim);font-size:14px;margin-bottom:28px;max-width:420px}
.suggestions{display:grid;grid-template-columns:1fr 1fr;gap:8px;max-width:520px}
.suggestion{
  display:flex;align-items:flex-start;gap:10px;padding:12px 14px;text-align:left;
  background:var(--bg-soft);border:1px solid var(--border);border-radius:var(--r-md);
  transition:background .18s var(--ease),border-color .18s var(--ease);
}
.suggestion:hover{background:var(--surface);border-color:var(--border-strong)}
.suggestion:active{transform:scale(.99)}
.suggestion-icon{color:var(--text-dim);margin-top:2px}
.suggestion-text{font-size:13px;color:var(--text-dim);line-height:1.45;font-weight:500}

/* Messages */
.message{animation:msgIn .25s var(--ease-out)}
@keyframes msgIn{from{opacity:0;transform:translateY(4px)}to{opacity:1;transform:none}}
.message.user{display:flex;justify-content:flex-end;padding-left:48px}
.message.user .msg-body{
  background:var(--surface);border:1px solid var(--border);
  border-radius:var(--r-lg);padding:11px 15px;max-width:100%;
  font-size:14.5px;line-height:1.55;overflow-wrap:break-word;
}
.message.assistant{padding-right:32px}
.message.assistant .msg-body{
  max-width:100%;font-size:15px;line-height:1.7;
  overflow-wrap:break-word;word-break:break-word;
}

/* Markdown */
.msg-body p{margin:0 0 14px}
.msg-body p:last-child{margin-bottom:0}
.msg-body h1,.msg-body h2,.msg-body h3,.msg-body h4{
  margin:24px 0 10px;line-height:1.3;font-weight:600;letter-spacing:-.02em;
}
.msg-body h1:first-child,.msg-body h2:first-child,
.msg-body h3:first-child,.msg-body h4:first-child{margin-top:0}
.msg-body h1{font-size:20px}
.msg-body h2{font-size:17px}
.msg-body h3{font-size:15.5px}
.msg-body h4{font-size:14.5px}
.msg-body ul,.msg-body ol{margin:8px 0 14px;padding-left:22px}
.msg-body li{margin:5px 0}
.msg-body li>p{margin:0}
.msg-body strong{font-weight:600;color:var(--text)}
.msg-body em{font-style:italic;color:var(--text)}
.msg-body code{
  background:var(--bg-soft);padding:1.5px 6px;border-radius:4px;
  font-family:"SF Mono",Menlo,Monaco,Consolas,monospace;
  font-size:.87em;color:var(--accent);border:1px solid var(--border);
}
.msg-body pre{
  background:var(--bg-soft);border:1px solid var(--border);
  padding:14px 16px;border-radius:var(--r-md);overflow-x:auto;
  margin:12px 0;font-size:13px;line-height:1.55;
}
[data-theme="light"] .msg-body pre{background:#1a1816;color:#ebe8e3;border-color:#2e2b28}
.msg-body pre code{background:none;padding:0;color:inherit;border:none;font-size:inherit}
.msg-body img{max-width:100%;border-radius:var(--r-md);margin:10px 0;border:1px solid var(--border)}
.msg-body blockquote{
  border-left:2px solid var(--border-strong);padding-left:14px;
  color:var(--text-dim);margin:12px 0;font-style:italic;
}
.msg-body a{color:var(--accent);text-decoration:underline;text-underline-offset:2px}
.msg-body table{border-collapse:collapse;width:100%;margin:12px 0;font-size:13.5px}
.msg-body th,.msg-body td{border-bottom:1px solid var(--border);padding:8px 12px;text-align:left}
.msg-body th{
  font-weight:600;font-size:12.5px;color:var(--text-dim);
  text-transform:uppercase;letter-spacing:.04em;
}
.msg-body hr{border:none;border-top:1px solid var(--border);margin:20px 0}

/* Actions */
.msg-actions{display:flex;gap:2px;margin-top:10px;opacity:0;transition:opacity .2s var(--ease)}
.message:hover .msg-actions,.message:focus-within .msg-actions{opacity:1}
.msg-action{
  display:inline-flex;align-items:center;gap:5px;
  font-size:11.5px;font-weight:500;padding:4px 8px;border-radius:var(--r-sm);
  color:var(--text-muted);
  transition:background .18s var(--ease),color .18s var(--ease);
}
.msg-action:hover{background:var(--surface);color:var(--text)}
.msg-action svg{width:12px;height:12px}
@media(hover:none){
  .msg-actions{opacity:.7}
  .message:focus-within .msg-actions{opacity:1}
}

/* Streaming caret */
.caret{
  display:inline-block;width:2px;height:1.1em;background:var(--text-dim);
  vertical-align:text-bottom;margin-left:1px;animation:blink 1s steps(2) infinite;
}
@keyframes blink{50%{opacity:0}}

/* ============ COMPOSER ============ */
.chat-input-area{
  padding:12px 16px;padding-bottom:calc(12px + env(safe-area-inset-bottom));
  border-top:1px solid var(--border);background:var(--bg);
}
.image-preview{position:relative;display:inline-block;margin-bottom:10px}
.image-preview img{
  height:64px;border-radius:var(--r-sm);border:1px solid var(--border);display:block;
}
.image-preview .remove-img{
  position:absolute;top:-5px;right:-5px;width:20px;height:20px;border-radius:50%;
  background:var(--text);color:var(--bg);
  display:flex;align-items:center;justify-content:center;
  border:2px solid var(--bg);
}
.composer{
  display:flex;gap:2px;align-items:flex-end;
  background:var(--bg-soft);border:1px solid var(--border);
  border-radius:var(--r-lg);padding:5px;
  transition:border-color .18s var(--ease);
}
.composer:focus-within{border-color:var(--border-strong)}
.composer textarea{
  flex:1;background:none;border:none;padding:9px 8px;
  max-height:140px;min-height:36px;font-size:15px;line-height:1.45;
  border-radius:0;
}
.composer textarea:focus{background:none;border:none}
.composer button{
  width:36px;height:36px;border-radius:var(--r-sm);
  display:flex;align-items:center;justify-content:center;color:var(--text-muted);
  transition:background .18s var(--ease),color .18s var(--ease),opacity .18s var(--ease);
}
.composer button:hover{background:var(--surface);color:var(--text)}
.composer .send-btn{background:var(--surface);color:var(--text-muted)}
.composer .send-btn.ready{background:var(--text);color:var(--bg)}
.composer .send-btn.ready:hover{opacity:.9;background:var(--text)}
.composer .send-btn.streaming{background:var(--danger-soft);color:var(--danger)}
.composer .send-btn.streaming:hover{background:var(--danger-soft)}

/* ============ PAGE HEADERS ============ */
.page-header{
  display:flex;align-items:center;justify-content:space-between;
  padding:20px 20px 16px;position:sticky;top:0;background:var(--bg);z-index:5;
}
.page-header h2{font-size:19px;font-weight:600;letter-spacing:-.025em}
.btn-text{
  font-size:12.5px;font-weight:500;color:var(--text-muted);
  padding:4px 8px;border-radius:var(--r-sm);
  transition:background .18s var(--ease),color .18s var(--ease);
}
.btn-text:hover{background:var(--surface);color:var(--text)}
.btn-text.danger:hover{background:var(--danger-soft);color:var(--danger)}

/* ============ LISTS ============ */
.list-container{padding:0 16px 24px;display:flex;flex-direction:column;gap:6px}
.list-item{
  background:var(--bg-soft);border:1px solid var(--border);
  border-radius:var(--r-md);padding:14px 16px;
  transition:background .18s var(--ease),border-color .18s var(--ease);
  cursor:pointer;
}
.list-item:hover{background:var(--surface);border-color:var(--border-strong)}
.list-item:active{transform:scale(.995)}
.list-item-head{
  display:flex;justify-content:space-between;align-items:flex-start;gap:12px;margin-bottom:4px;
}
.list-item-title{
  font-size:13.5px;font-weight:500;color:var(--text);
  overflow:hidden;text-overflow:ellipsis;
  display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical;line-height:1.4;
}
.list-item-time{font-size:11px;color:var(--text-muted);flex-shrink:0;white-space:nowrap}
.list-item-meta{font-size:11.5px;color:var(--text-muted)}
.list-item-actions{
  display:flex;gap:4px;margin-top:10px;padding-top:10px;border-top:1px solid var(--border);
}
.item-btn{
  display:inline-flex;align-items:center;gap:5px;
  font-size:11.5px;font-weight:500;
  padding:4px 10px;border-radius:var(--r-sm);color:var(--text-dim);
  border:1px solid var(--border);background:transparent;
  transition:all .18s var(--ease);
}
.item-btn:hover{background:var(--surface);color:var(--text);border-color:var(--border-strong)}
.item-btn.danger:hover{background:var(--danger-soft);color:var(--danger);border-color:transparent}
.item-btn svg{width:11px;height:11px}

/* Empty states */
.empty{text-align:center;padding:64px 24px;animation:msgIn .3s var(--ease-out)}
.empty-icon{
  width:40px;height:40px;margin:0 auto 16px;color:var(--text-muted);
  display:flex;align-items:center;justify-content:center;
}
.empty h3{font-size:14px;font-weight:600;color:var(--text-dim);margin-bottom:4px}
.empty p{font-size:12.5px;color:var(--text-muted);max-width:260px;margin:0 auto;line-height:1.5}

/* ============ SETTINGS ============ */
.settings-container{padding:0 16px 32px;display:flex;flex-direction:column;gap:24px}
.section-label{
  font-size:11px;font-weight:600;text-transform:uppercase;
  letter-spacing:.1em;color:var(--text-muted);padding:0 4px;
}
.settings-card{
  background:var(--bg-soft);border:1px solid var(--border);
  border-radius:var(--r-md);overflow:hidden;
}
.setting-row{
  padding:14px 16px;border-bottom:1px solid var(--border);
  display:flex;flex-direction:column;gap:8px;
}
.setting-row:last-child{border-bottom:none}
.setting-row.inline{flex-direction:row;align-items:center;justify-content:space-between}
.setting-info{flex:1;min-width:0;padding-right:12px}
.setting-label{font-size:13.5px;font-weight:500;color:var(--text);margin-bottom:2px}
.setting-hint{font-size:11.5px;color:var(--text-muted);line-height:1.4}
.setting-hint a{color:var(--text-dim);text-decoration:underline;text-underline-offset:2px}
.setting-hint a:hover{color:var(--accent)}
.setting-value{
  font-size:12px;font-weight:500;color:var(--text-dim);
  font-variant-numeric:tabular-nums;min-width:36px;text-align:right;
}
.setting-row select,
.setting-row input[type="text"],
.setting-row input[type="password"]{font-size:13px}

/* Range */
input[type="range"]{
  -webkit-appearance:none;appearance:none;width:100%;height:4px;
  background:var(--surface-hover);border-radius:2px;padding:0;border:none;
  margin:8px 0;box-shadow:none;
}
input[type="range"]:focus{background:var(--surface-hover);border:none}
input[type="range"]::-webkit-slider-runnable-track{
  height:4px;background:var(--surface-hover);border-radius:2px;
}
input[type="range"]::-webkit-slider-thumb{
  -webkit-appearance:none;appearance:none;
  width:14px;height:14px;border-radius:50%;
  background:var(--text);margin-top:-5px;border:none;cursor:pointer;
}
input[type="range"]::-moz-range-track{height:4px;background:var(--surface-hover);border-radius:2px}
input[type="range"]::-moz-range-thumb{
  width:14px;height:14px;border-radius:50%;background:var(--text);border:none;cursor:pointer;
}

.btn-block{
  width:100%;padding:12px;border-radius:var(--r-md);
  font-weight:500;font-size:14px;transition:all .18s var(--ease);
}
.btn-block.primary{background:var(--text);color:var(--bg)}
.btn-block.primary:hover{opacity:.9}
.btn-block.primary:active{transform:scale(.99)}
.btn-block.secondary{background:transparent;border:1px solid var(--border);color:var(--text)}
.btn-block.secondary:hover{background:var(--bg-soft);border-color:var(--border-strong)}
.btn-block.secondary:active{transform:scale(.99)}

/* ============ BOTTOM NAV ============ */
.app-nav{
  display:flex;justify-content:space-around;
  background:var(--bg);border-top:1px solid var(--border);
  padding:6px 4px calc(6px + env(safe-area-inset-bottom));
  position:relative;z-index:10;
}
.nav-btn{
  flex:1;display:flex;flex-direction:column;align-items:center;gap:3px;
  padding:6px 4px;color:var(--text-muted);border-radius:var(--r-sm);
  position:relative;transition:color .18s var(--ease);
}
.nav-btn svg{width:20px;height:20px;stroke-width:1.7}
.nav-btn span{font-size:10px;font-weight:500;letter-spacing:.01em}
.nav-btn.active{color:var(--text)}
.nav-btn.active svg{stroke-width:2}
.nav-btn.streaming::after{
  content:'';position:absolute;top:4px;right:calc(50% - 14px);
  width:5px;height:5px;border-radius:50%;background:var(--accent);
  animation:pulse 1.5s ease-in-out infinite;
}
@keyframes pulse{0%,100%{opacity:.3}50%{opacity:1}}

/* ============ TOAST ============ */
.toast{
  position:fixed;bottom:calc(78px + env(safe-area-inset-bottom));left:50%;
  transform:translateX(-50%) translateY(8px);
  background:var(--text);color:var(--bg);
  padding:9px 16px;border-radius:var(--r-full);
  font-size:12.5px;font-weight:500;
  opacity:0;pointer-events:none;transition:all .25s var(--ease);
  z-index:100;max-width:calc(100% - 32px);text-align:center;
}
.toast.show{opacity:1;transform:translateX(-50%) translateY(0)}

/* ============ SCROLLBAR ============ */
::-webkit-scrollbar{width:8px;height:8px}
::-webkit-scrollbar-track{background:transparent}
::-webkit-scrollbar-thumb{background:var(--surface-hover);border-radius:4px;border:2px solid var(--bg)}
::-webkit-scrollbar-thumb:hover{background:var(--border-strong)}

/* ============ RESPONSIVE ============ */
@media(min-width:768px){
  #appScreen{
    max-width:920px;margin:0 auto;
    border-left:1px solid var(--border);border-right:1px solid var(--border);
  }
  .chat-messages{padding:32px 32px 24px;gap:28px}
  .message.assistant{padding-right:56px}
  .message.user .msg-body{max-width:70%}
  .chat-input-area{padding:16px 32px;padding-bottom:calc(16px + env(safe-area-inset-bottom))}
  .list-container{padding:0 32px 32px}
  .settings-container{padding:0 32px 32px}
  .page-header{padding:24px 32px 16px}
  .app-nav{padding-bottom:8px}
  .suggestions{grid-template-columns:1fr 1fr}
}
@media(max-width:400px){
  .suggestions{grid-template-columns:1fr}
  .welcome h2{font-size:23px}
}
