:root{
  --radius-lg: 16px;
  --radius-md: 11px;
  --radius-sm: 8px;
  --font-display: 'Fraunces', serif;
  --font-body: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --font-mono: 'JetBrains Mono', ui-monospace, monospace;
  --sidebar-w: 264px;
}

/* ============ LIGHT THEME (Claude-like cream) ============ */
html[data-theme="light"]{
  --bg: #faf7f2;
  --bg-raised: #f3efe6;
  --panel: #ffffff;
  --panel-2: #ece5d6;
  --border: #e4dcc9;
  --border-soft: #ece5d6;
  --text: #30291f;
  --text-dim: #7d7364;
  --text-faint: #a89c88;
  --accent: #c15f3c;
  --accent-2: #a84f30;
  --accent-soft: rgba(193,95,60,0.10);
  --accent-border: rgba(193,95,60,0.35);
  --danger: #b7402c;
  --danger-bg: rgba(183,64,44,0.08);
  --ok: #4a8f5c;
  --user-bubble: #efe6d5;
  --shadow: rgba(60,45,20,0.10);
}

/* ============ DARK THEME (near-black) ============ */
html[data-theme="dark"]{
  --bg: #16140f;
  --bg-raised: #1c1a15;
  --panel: #211f19;
  --panel-2: #2a271f;
  --border: #383429;
  --border-soft: #2a2720;
  --text: #f0ece2;
  --text-dim: #9c9284;
  --text-faint: #6b6355;
  --accent: #d97a53;
  --accent-2: #c26640;
  --accent-soft: rgba(217,122,83,0.14);
  --accent-border: rgba(217,122,83,0.35);
  --danger: #e28068;
  --danger-bg: rgba(226,128,104,0.1);
  --ok: #7fb787;
  --user-bubble: #2a271f;
  --shadow: rgba(0,0,0,0.4);
}

*{ box-sizing: border-box; }
::selection{ background: var(--accent-soft); }

html, body{
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
  transition: background 0.2s ease, color 0.2s ease;
}

button{ font-family: inherit; }
textarea{ font-family: inherit; }

.shell{
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  height: 100dvh;
  position: relative;
}

/* ================= SIDEBAR ================= */
.sidebar{
  background: var(--bg-raised);
  border-right: 1px solid var(--border-soft);
  display: flex;
  flex-direction: column;
  min-width: 0;
  transition: transform 0.25s ease, background 0.2s ease, border-color 0.2s ease;
}

.sidebar-head{ padding: 16px 14px 10px; }

.brand{
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 4px 6px 14px;
}

.mark{
  width: 25px;
  height: 25px;
  border-radius: 7px;
  flex-shrink: 0;
  background: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 12px;
}

.brand-text{
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 16px;
  letter-spacing: -0.01em;
}

.new-chat-btn{
  width: 100%;
  display: flex;
  align-items: center;
  gap: 9px;
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 9px 13px;
  border-radius: var(--radius-md);
  font-size: 13.5px;
  font-weight: 500;
  cursor: pointer;
  transition: 0.15s ease;
}
.new-chat-btn:hover{ border-color: var(--accent); }
.new-chat-btn svg{ flex-shrink: 0; }

.sidebar-section-label{
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--text-faint);
  padding: 16px 20px 6px;
  font-weight: 600;
}

.chat-list{
  flex: 1;
  overflow-y: auto;
  padding: 2px 10px 10px;
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.chat-item{
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  color: var(--text-dim);
  font-size: 13px;
  position: relative;
  transition: 0.12s ease;
}
.chat-item:hover{ background: var(--panel); color: var(--text); }
.chat-item.active{ background: var(--panel-2); color: var(--text); }
.chat-item .title{ flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item .del{
  opacity: 0;
  background: none;
  border: none;
  color: var(--text-faint);
  cursor: pointer;
  padding: 3px;
  border-radius: 5px;
  display: flex;
}
.chat-item:hover .del{ opacity: 1; }
.chat-item .del:hover{ color: var(--danger); background: var(--danger-bg); }

.empty-history{
  padding: 12px 16px;
  font-size: 12.5px;
  color: var(--text-faint);
  line-height: 1.6;
}

.sidebar-foot{
  border-top: 1px solid var(--border-soft);
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.foot-row{
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  background: none;
  border: none;
  color: var(--text-dim);
  padding: 8px 9px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  cursor: pointer;
  transition: 0.12s ease;
  text-align: left;
}
.foot-row:hover{ background: var(--panel); color: var(--text); }

.theme-toggle{ display: flex; gap: 6px; margin-bottom: 2px; }
.theme-btn{
  flex: 1;
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text-dim);
  padding: 7px 0;
  border-radius: var(--radius-sm);
  font-size: 12px;
  cursor: pointer;
  transition: 0.15s ease;
}
.theme-btn:hover{ color: var(--text); }
.theme-btn.active{ border-color: var(--accent-border); color: var(--text); background: var(--accent-soft); }

.key-dot{ width: 6px; height: 6px; border-radius: 50%; background: var(--text-faint); margin-left: auto; flex-shrink: 0; }
.key-dot.set{ background: var(--ok); }

.user-chip{ display: flex; align-items: center; gap: 9px; padding: 7px 9px; }
.user-avatar{
  width: 24px; height: 24px; border-radius: 50%;
  background: var(--accent);
  color: #fff; display: flex; align-items: center; justify-content: center;
  font-size: 10.5px; font-weight: 700; flex-shrink: 0;
}
.user-name{ font-size: 13px; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.logout-btn{
  background: none; border: none; color: var(--text-faint);
  cursor: pointer; padding: 4px; border-radius: 5px; flex-shrink: 0; display: flex;
}
.logout-btn:hover{ color: var(--danger); }

/* ================= MAIN ================= */
.main{ display: flex; flex-direction: column; min-width: 0; height: 100dvh; position: relative; }

header{
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 13px 20px;
  border-bottom: 1px solid var(--border-soft);
  background: var(--bg);
  transition: background 0.2s ease, border-color 0.2s ease;
}

.hamburger{ display: none; background: none; border: none; color: var(--text-dim); cursor: pointer; padding: 4px; }

.header-title{
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 14.5px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  flex: 1;
}

.model-pill{
  display: flex; align-items: center; gap: 6px;
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text-dim);
  font-size: 12px;
  padding: 6px 11px;
  border-radius: 999px;
  cursor: default;
  white-space: nowrap;
}
.model-pill .dot{ width: 6px; height: 6px; border-radius: 50%; background: var(--accent); flex-shrink: 0; }

/* ================= CHAT ================= */
.chat{ flex: 1; overflow-y: auto; padding: 26px 0 8px; }
.chat-inner{
  max-width: 700px; margin: 0 auto; padding: 0 24px;
  display: flex; flex-direction: column; gap: 24px;
}

.welcome{ margin: auto; padding-top: 8vh; text-align: center; max-width: 500px; }
.welcome .mark{ width: 46px; height: 46px; border-radius: 12px; font-size: 18px; margin: 0 auto 20px; }
.welcome h1{
  font-family: var(--font-display);
  font-size: 25px; font-weight: 600; margin: 0 0 8px; letter-spacing: -0.01em;
}
.welcome p{ color: var(--text-dim); font-size: 14px; line-height: 1.6; margin: 0 0 24px; }

.suggestions{ display: grid; grid-template-columns: 1fr 1fr; gap: 9px; }
.suggestion-chip{
  text-align: left;
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text-dim);
  padding: 11px 13px;
  border-radius: var(--radius-md);
  font-size: 12.5px; line-height: 1.5;
  cursor: pointer;
  transition: 0.15s ease;
}
.suggestion-chip:hover{ border-color: var(--accent); color: var(--text); }
.suggestion-chip b{ display: block; color: var(--text); font-size: 13px; margin-bottom: 2px; font-weight: 600; }

.msg{ display: flex; gap: 12px; align-items: flex-start; animation: rise 0.2s ease; }
@keyframes rise{ from{ opacity: 0; transform: translateY(5px);} to{ opacity: 1; transform: translateY(0);} }

.avatar{
  width: 26px; height: 26px; border-radius: 7px; flex-shrink: 0; margin-top: 2px;
  display: flex; align-items: center; justify-content: center;
  font-size: 11px; font-weight: 700;
}
.msg.bot .avatar{ background: var(--accent); color: #fff; }
.msg.user .avatar{ background: var(--panel-2); border: 1px solid var(--border); color: var(--text-dim); }

.msg-col{ display: flex; flex-direction: column; gap: 5px; flex: 1; min-width: 0; }
.msg.user{ flex-direction: row-reverse; }
.msg.user .msg-col{ align-items: flex-end; }

.bubble{
  font-size: 14.5px;
  line-height: 1.65;
  word-wrap: break-word;
  max-width: 100%;
}
.msg.bot .bubble{
  padding: 2px 0;
  color: var(--text);
}
.msg.user .bubble{
  background: var(--user-bubble);
  border-radius: var(--radius-md);
  padding: 10px 14px;
  max-width: 480px;
}
.bubble.error{
  background: var(--danger-bg);
  border: 1px solid var(--danger);
  color: var(--danger);
  border-radius: var(--radius-md);
  padding: 10px 14px;
}
.bubble p{ margin: 0 0 10px; }
.bubble p:last-child{ margin-bottom: 0; }
.bubble ul, .bubble ol{ margin: 6px 0 10px; padding-left: 22px; }
.bubble li{ margin-bottom: 4px; }
.bubble a{ color: var(--accent); }
.bubble strong{ color: var(--text); font-weight: 600; }
.bubble code{
  font-family: var(--font-mono);
  background: var(--panel-2);
  padding: 2px 5px; border-radius: 4px; font-size: 0.88em;
}

.code-block{
  margin: 8px 0 12px;
  border-radius: var(--radius-sm);
  overflow: hidden;
  border: 1px solid var(--border);
  background: var(--panel);
}
.code-head{
  display: flex; align-items: center; justify-content: space-between;
  padding: 7px 12px;
  background: var(--bg-raised);
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-faint);
  border-bottom: 1px solid var(--border);
}
.code-copy{
  background: none; border: none; color: var(--text-faint);
  cursor: pointer; font-size: 11px;
  display: flex; align-items: center; gap: 4px; padding: 2px 4px;
}
.code-copy:hover{ color: var(--text); }
.code-block pre{
  margin: 0; padding: 13px 15px; overflow-x: auto;
  font-family: var(--font-mono); font-size: 12.5px; line-height: 1.6;
  color: var(--text);
}

.msg-actions{ display: flex; gap: 4px; opacity: 0; transition: opacity 0.15s ease; margin-top: 2px; }
.msg:hover .msg-actions{ opacity: 1; }
.msg-actions button{
  background: none; border: none; color: var(--text-faint);
  cursor: pointer; padding: 4px; border-radius: 5px; display: flex;
}
.msg-actions button:hover{ color: var(--text); background: var(--panel); }

.typing-dots{ display: flex; gap: 4px; padding: 4px 2px; }
.typing-dots span{
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--text-dim);
  animation: blink 1.3s infinite ease-in-out;
}
.typing-dots span:nth-child(2){ animation-delay: 0.2s; }
.typing-dots span:nth-child(3){ animation-delay: 0.4s; }
@keyframes blink{ 0%,80%,100%{ opacity:0.25; transform:scale(0.85);} 40%{ opacity:1; transform:scale(1);} }

/* ================= COMPOSER ================= */
.composer-wrap{ padding: 12px 24px 20px; }
.composer{ max-width: 700px; margin: 0 auto; }
.composer-inner{
  display: flex; align-items: flex-end; gap: 8px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 7px 7px 7px 17px;
  transition: 0.15s ease;
  box-shadow: 0 2px 10px var(--shadow);
}
.composer-inner:focus-within{ border-color: var(--accent); }
textarea#promptInput{
  flex: 1; background: transparent; border: none; outline: none; resize: none;
  color: var(--text); font-size: 14.5px; line-height: 1.5; max-height: 180px; padding: 8px 0;
}
textarea#promptInput::placeholder{ color: var(--text-faint); }

#sendBtn{
  width: 36px; height: 36px; border-radius: 10px; border: none;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  transition: 0.15s ease;
}
#sendBtn:hover{ background: var(--accent-2); }
#sendBtn:disabled{ background: var(--panel-2); color: var(--text-faint); cursor: not-allowed; }
#sendBtn.stop{ background: var(--danger); }

.composer-foot{ text-align: center; font-size: 11px; color: var(--text-faint); margin-top: 9px; }

/* ================= MODAL (shared) ================= */
.modal-overlay{
  position: fixed; inset: 0;
  background: rgba(20,15,8,0.45);
  backdrop-filter: blur(2px);
  display: none; align-items: center; justify-content: center;
  z-index: 100; padding: 20px;
}
.modal-overlay.open{ display: flex; }
.modal{
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  width: 100%; max-width: 420px;
  padding: 22px;
  box-shadow: 0 20px 50px var(--shadow);
}
.modal-head{ display: flex; align-items: center; justify-content: space-between; margin-bottom: 18px; }
.modal-head h3{ font-family: var(--font-display); font-size: 16.5px; margin: 0; font-weight: 600; }
.modal-close{ background: none; border: none; color: var(--text-dim); cursor: pointer; padding: 4px; border-radius: 6px; }
.modal-close:hover{ color: var(--text); background: var(--panel); }

.field{ margin-bottom: 16px; }
.field label{ display: block; font-size: 12px; color: var(--text-dim); margin-bottom: 7px; font-weight: 500; }
.field-row{ display: flex; gap: 8px; }
.field input[type="text"], .field input[type="password"], .field select{
  width: 100%;
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-family: var(--font-mono);
  outline: none;
}
#displayNameInput{ font-family: var(--font-body); }
.field select{ font-family: var(--font-body); cursor: pointer; }
.field input:focus, .field select:focus{ border-color: var(--accent); }
.field .small-btn{
  background: var(--panel); border: 1px solid var(--border); color: var(--text-dim);
  border-radius: var(--radius-sm); padding: 0 13px; cursor: pointer; flex-shrink: 0; font-size: 12.5px;
}
.field .small-btn:hover{ color: var(--text); }
.field-hint{ font-size: 11.5px; color: var(--text-faint); margin-top: 7px; line-height: 1.6; }
.field-hint a{ color: var(--accent); text-decoration: none; }
.field-hint a:hover{ text-decoration: underline; }

.modal-actions{ display: flex; gap: 10px; margin-top: 20px; }
.btn-primary{
  flex: 1;
  background: var(--accent);
  color: #fff; border: none;
  padding: 10px; border-radius: var(--radius-sm);
  font-weight: 600; font-size: 13.5px; cursor: pointer;
}
.btn-primary:hover{ background: var(--accent-2); }
.btn-ghost{
  background: none; border: 1px solid var(--border); color: var(--text-dim);
  padding: 10px 16px; border-radius: var(--radius-sm); font-size: 13.5px; cursor: pointer;
}
.btn-ghost:hover{ color: var(--danger); border-color: var(--danger); }

/* Login modal specific look */
.login-modal{ max-width: 370px; text-align: center; }
.login-modal .mark{ width: 42px; height: 42px; border-radius: 11px; font-size: 16px; margin: 4px auto 16px; }
.login-modal h3{ font-family: var(--font-display); font-size: 18.5px; margin: 0 0 6px; font-weight: 600; }
.login-modal p{ color: var(--text-dim); font-size: 13px; margin: 0 0 20px; line-height: 1.6; }
.login-modal .field{ text-align: left; }
.skip-link{
  display: block; text-align: center; margin-top: 12px;
  color: var(--text-faint); font-size: 12.5px; cursor: pointer; background: none; border: none;
  width: 100%;
}
.skip-link:hover{ color: var(--text-dim); }

/* ================= TOASTS ================= */
.toast-stack{
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  display: flex; flex-direction: column; gap: 8px; z-index: 200; pointer-events: none;
}
.toast{
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 9px 15px;
  border-radius: 999px;
  font-size: 12.5px;
  box-shadow: 0 6px 20px var(--shadow);
  animation: toast-in 0.2s ease;
}
@keyframes toast-in{ from{ opacity:0; transform: translateY(8px);} to{ opacity:1; transform: translateY(0);} }

/* Scrollbars */
.chat-list::-webkit-scrollbar, .chat::-webkit-scrollbar{ width: 6px; }
.chat-list::-webkit-scrollbar-thumb, .chat::-webkit-scrollbar-thumb{ background: var(--panel-2); border-radius: 8px; }
.chat-list::-webkit-scrollbar-track, .chat::-webkit-scrollbar-track{ background: transparent; }

.sidebar-overlay{ display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.4); z-index: 40; }

/* ================= RESPONSIVE ================= */
@media (max-width: 860px){
  .shell{ grid-template-columns: 1fr; }
  .sidebar{
    position: fixed; top: 0; left: 0; bottom: 0;
    width: var(--sidebar-w); z-index: 50;
    transform: translateX(-100%);
  }
  .sidebar.open{ transform: translateX(0); }
  .sidebar-overlay.open{ display: block; }
  .hamburger{ display: flex; }
  .suggestions{ grid-template-columns: 1fr; }
  .chat-inner{ padding: 0 14px; }
  .composer-wrap{ padding: 10px 12px 16px; }
  .msg.user .bubble{ max-width: 86%; }
}

@media (prefers-reduced-motion: reduce){
  *{ animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
}
