:root {
  --bg: #0c0d13;
  --bg-grad1: #14152080;
  --surface: #16171f;
  --surface-2: #1d1f2b;
  --border: #2a2c3a;
  --text: #e8e9f0;
  --text-dim: #9497a8;
  --accent: #7c5cff;
  --accent-2: #ff5c8a;
  --accent-3: #38bdf8;
  --user-grad: linear-gradient(135deg, #7c5cff, #a855f7);
  --ai-bubble: #1d1f2b;
  --err: #3a1c22;
  --err-border: #7f2d3a;
  --shadow: 0 8px 30px rgba(0,0,0,.4);
  --font: 'Vazirmatn', 'Space Grotesk', sans-serif;
  --mono: 'JetBrains Mono', monospace;
}
[data-theme="light"] {
  --bg: #f4f5fa;
  --bg-grad1: #ffffff80;
  --surface: #ffffff;
  --surface-2: #eef0f7;
  --border: #dde0ec;
  --text: #1a1c26;
  --text-dim: #6b6e80;
  --ai-bubble: #eef0f7;
  --err: #fde8ec;
  --err-border: #f5b5c0;
  --shadow: 0 8px 30px rgba(80,80,120,.12);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: var(--font);
  background:
    radial-gradient(1200px 600px at 80% -10%, #7c5cff22, transparent 60%),
    radial-gradient(900px 500px at 0% 100%, #ff5c8a18, transparent 55%),
    var(--bg);
  color: var(--text);
  overflow: hidden;
  transition: background .4s, color .3s;
}
#root { height: 100vh; height: 100dvh; }

/* ---------- shell ---------- */
.app-shell {
  display: grid;
  grid-template-columns: 280px 1fr;
  height: 100%;
}
@media (max-width: 820px) {
  .app-shell { grid-template-columns: 1fr; }
}

/* ---------- sidebar ---------- */
.sidebar {
  background: var(--surface);
  border-inline-end: 1px solid var(--border);
  display: flex; flex-direction: column;
  padding: 16px 14px;
  gap: 12px;
  padding-top: max(16px, env(safe-area-inset-top));
}
.side-head { display: flex; justify-content: space-between; align-items: center; }
.brand-mini { font-weight: 700; font-size: 15px; letter-spacing: .5px; color: var(--text-dim); }
.new-chat {
  background: var(--user-grad);
  color: #fff; border: none; border-radius: 12px;
  padding: 11px; font-family: var(--font); font-weight: 700; font-size: 14px;
  cursor: pointer; transition: transform .15s, box-shadow .2s;
  box-shadow: 0 4px 16px #7c5cff44;
}
.new-chat:hover { transform: translateY(-1px); box-shadow: 0 6px 22px #7c5cff66; }
.convo-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 6px; }
.empty-side { color: var(--text-dim); font-size: 13px; text-align: center; margin-top: 20px; }
.convo-item {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 12px; border-radius: 10px; cursor: pointer;
  border: 1px solid transparent; transition: background .15s, border .15s;
  gap: 8px;
}
.convo-item:hover { background: var(--surface-2); }
.convo-item.active {
  background: var(--surface-2);
  border-color: var(--accent);
  box-shadow: inset 3px 0 0 var(--accent);
}
.convo-title { font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.convo-del { background: none; border: none; cursor: pointer; opacity: 0; font-size: 13px; transition: opacity .15s; }
.convo-item:hover .convo-del { opacity: .7; }
.convo-del:hover { opacity: 1; }
.clear-all {
  background: none; border: 1px solid var(--border); color: var(--text-dim);
  border-radius: 10px; padding: 9px; font-family: var(--font); font-size: 13px; cursor: pointer;
  transition: color .15s, border .15s;
}
.clear-all:hover { color: var(--accent-2); border-color: var(--accent-2); }

@media (max-width: 820px) {
  .sidebar {
    position: fixed; inset: 0 auto 0 0; width: 82%; max-width: 300px; z-index: 40;
    transform: translateX(-105%); transition: transform .28s cubic-bezier(.4,0,.2,1);
  }
  .sidebar.open { transform: translateX(0); box-shadow: var(--shadow); }
}
.backdrop {
  position: fixed; inset: 0; background: #0009; z-index: 35;
  backdrop-filter: blur(2px); animation: fade .2s;
}

/* ---------- main ---------- */
.main-col { display: flex; flex-direction: column; height: 100%; min-width: 0; }
.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 18px; padding-top: max(12px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--border);
  background: linear-gradient(90deg, #7c5cff10, transparent 40%);
  backdrop-filter: blur(8px);
}
.brand { display: flex; align-items: center; gap: 10px; flex: 1; }
.logo-dot {
  width: 34px; height: 34px; display: grid; place-items: center;
  background: var(--user-grad); border-radius: 10px; font-size: 17px;
  box-shadow: 0 3px 12px #7c5cff55;
}
.brand-title { font-size: 17px; font-weight: 700; letter-spacing: .3px; line-height: 1; }
.brand-sub { font-size: 11px; color: var(--text-dim); font-family: var(--mono); }
.top-actions { display: flex; align-items: center; gap: 8px; }

.tok-badge {
  display: flex; flex-direction: column; align-items: center;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 10px; padding: 4px 12px; line-height: 1.2;
  transition: box-shadow .3s;
}
.tok-badge.pulse { box-shadow: 0 0 0 3px #7c5cff55; }
.tok-label { font-size: 9px; color: var(--text-dim); }
.tok-val { font-size: 13px; font-weight: 700; font-family: var(--mono); color: var(--accent-3); }

.icon-btn {
  width: 38px; height: 38px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text); cursor: pointer; font-size: 16px;
  display: grid; place-items: center; transition: background .15s, transform .12s, border .15s;
}
.icon-btn:hover { background: var(--surface); border-color: var(--accent); transform: translateY(-1px); }
.only-mobile { display: none; }
@media (max-width: 820px) { .only-mobile { display: grid; } }

/* ---------- chat area ---------- */
.chat-area { flex: 1; overflow-y: auto; padding: 22px 18px; scroll-behavior: smooth; }
.msg-list { max-width: 820px; margin: 0 auto; display: flex; flex-direction: column; gap: 18px; }

.msg-row { display: flex; gap: 10px; align-items: flex-start; animation: rise .35s cubic-bezier(.2,.8,.2,1); }
.msg-row.right { flex-direction: row-reverse; }
.avatar {
  width: 32px; height: 32px; border-radius: 9px; flex-shrink: 0;
  display: grid; place-items: center; font-size: 15px;
  background: var(--user-grad); box-shadow: 0 2px 8px #7c5cff44;
}
.msg-wrap { max-width: 78%; display: flex; flex-direction: column; }
.msg-row.right .msg-wrap { align-items: flex-end; }

.bubble {
  padding: 12px 15px; border-radius: 15px; font-size: 14.5px; line-height: 1.7;
  white-space: pre-wrap; word-wrap: break-word; box-shadow: var(--shadow);
}
.bubble.user { background: var(--user-grad); color: #fff; border-end-end-radius: 4px; }
.bubble.ai { background: var(--ai-bubble); border: 1px solid var(--border); border-end-start-radius: 4px; }
.bubble.err { background: var(--err); border: 1px solid var(--err-border); color: #ffb3c0; }
.cursor { animation: blink 1s steps(2) infinite; color: var(--accent); }

.msg-actions { display: flex; gap: 6px; margin-top: 5px; align-items: center; opacity: .55; transition: opacity .15s; }
.msg-row:hover .msg-actions { opacity: 1; }
.act {
  background: none; border: none; color: var(--text-dim); font-size: 11px;
  cursor: pointer; font-family: var(--font); padding: 2px 4px; border-radius: 6px;
  transition: color .15s, background .15s;
}
.act:hover { color: var(--accent-3); background: var(--surface-2); }
.act.danger:hover { color: var(--accent-2); }
.tok-mini { font-size: 10px; color: var(--text-dim); font-family: var(--mono); }

.bubble.typing { display: flex; gap: 5px; padding: 15px; }
.dot { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); animation: bounce 1.2s infinite; }
.dot:nth-child(2) { animation-delay: .2s; }
.dot:nth-child(3) { animation-delay: .4s; }

/* ---------- empty state ---------- */
.empty-state {
  height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center; gap: 8px; animation: fade .5s;
}
.empty-emoji { font-size: 54px; filter: drop-shadow(0 4px 16px #ff5c8a55); animation: float 3s ease-in-out infinite; }
.empty-state h2 { font-size: 22px; font-weight: 700; }
.empty-state p { color: var(--text-dim); font-size: 14px; }
.sample-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 18px; max-width: 520px; }
@media (max-width: 560px) { .sample-grid { grid-template-columns: 1fr; } }
.sample-chip {
  background: var(--surface); border: 1px solid var(--border); color: var(--text);
  border-radius: 12px; padding: 12px 14px; font-family: var(--font); font-size: 13px;
  cursor: pointer; text-align: start; transition: border .15s, transform .12s, background .15s;
}
.sample-chip:hover { border-color: var(--accent); transform: translateY(-2px); background: var(--surface-2); }

/* ---------- input dock ---------- */
.input-dock {
  border-top: 1px solid var(--border); padding: 12px 18px;
  padding-bottom: max(12px, env(safe-area-inset-bottom));
  background: linear-gradient(0deg, var(--surface), transparent);
}
.input-wrap {
  max-width: 820px; margin: 0 auto; display: flex; gap: 10px; align-items: flex-end;
  background: var(--surface); border: 1px solid var(--border); border-radius: 16px;
  padding: 8px; transition: border .2s, box-shadow .2s;
}
.input-wrap:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px #7c5cff33; }
.chat-input {
  flex: 1; background: none; border: none; outline: none; resize: none;
  color: var(--text); font-family: var(--font); font-size: 14.5px; line-height: 1.6;
  padding: 8px 10px; max-height: 160px;
}
.chat-input::placeholder { color: var(--text-dim); }
.send-btn {
  width: 42px; height: 42px; flex-shrink: 0; border: none; border-radius: 12px;
  background: var(--user-grad); color: #fff; font-size: 17px; cursor: pointer;
  display: grid; place-items: center; transition: transform .12s, opacity .2s;
  box-shadow: 0 3px 12px #7c5cff55;
}
.send-btn:hover:not(:disabled) { transform: scale(1.06); }
.send-btn:disabled { opacity: .4; cursor: not-allowed; }
.dock-meta {
  max-width: 820px; margin: 6px auto 0; display: flex; justify-content: space-between;
  font-size: 11px; color: var(--text-dim); font-family: var(--mono);
}
.remix-link { color: var(--text-dim); text-decoration: none; }
.remix-link:hover { color: var(--accent); }

.spinner {
  width: 16px; height: 16px; border: 2px solid #fff5; border-top-color: #fff;
  border-radius: 50%; animation: spin .7s linear infinite;
}

/* ---------- modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: #000a; backdrop-filter: blur(4px);
  display: grid; place-items: center; z-index: 60; padding: 16px; animation: fade .2s;
}
.modal {
  background: var(--surface); border: 1px solid var(--border); border-radius: 18px;
  width: 100%; max-width: 460px; max-height: 90vh; overflow-y: auto;
  box-shadow: var(--shadow); animation: pop .25s cubic-bezier(.2,.8,.2,1);
}
.modal-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 18px 20px; border-bottom: 1px solid var(--border);
}
.modal-head h2 { font-size: 18px; font-weight: 700; }
.modal-body { padding: 18px 20px; display: flex; flex-direction: column; gap: 16px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field-label { font-size: 12px; color: var(--text-dim); font-weight: 500; }
.inp {
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text);
  border-radius: 10px; padding: 10px 12px; font-family: var(--font); font-size: 13.5px;
  outline: none; transition: border .15s; width: 100%;
}
textarea.inp { font-family: var(--mono); font-size: 12.5px; resize: vertical; }
.inp:focus { border-color: var(--accent); }
.key-row { display: flex; gap: 8px; }
.mini-btn {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px;
  padding: 0 12px; cursor: pointer; font-size: 15px; color: var(--text);
}
.range { width: 100%; accent-color: var(--accent); }
.toggle-row { display: flex; justify-content: space-between; align-items: center; font-size: 13px; }
.switch {
  width: 46px; height: 26px; border-radius: 20px; border: none; background: var(--surface-2);
  border: 1px solid var(--border); cursor: pointer; position: relative; transition: background .2s;
  flex-shrink: 0;
}
.switch.on { background: var(--accent); border-color: var(--accent); }
.knob {
  position: absolute; top: 2px; inset-inline-start: 2px; width: 20px; height: 20px;
  background: #fff; border-radius: 50%; transition: transform .22s;
}
.switch.on .knob { transform: translateX(-20px); }
[dir="ltr"] .switch.on .knob { transform: translateX(20px); }
.hint { font-size: 11.5px; color: var(--text-dim); line-height: 1.6; background: var(--surface-2); padding: 10px; border-radius: 10px; }
.modal-foot {
  display: flex; justify-content: flex-end; gap: 10px; padding: 16px 20px;
  border-top: 1px solid var(--border);
}
.btn {
  border: none; border-radius: 10px; padding: 10px 20px; cursor: pointer;
  font-family: var(--font); font-weight: 600; font-size: 13.5px; transition: transform .12s, opacity .2s;
}
.btn.ghost { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.btn.primary { background: var(--user-grad); color: #fff; box-shadow: 0 3px 12px #7c5cff55; }
.btn:hover { transform: translateY(-1px); }

/* ---------- animations ---------- */
@keyframes rise { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: none; } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes pop { from { opacity: 0; transform: scale(.94); } to { opacity: 1; transform: scale(1); } }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes blink { 50% { opacity: 0; } }
@keyframes bounce { 0%,60%,100% { transform: translateY(0); opacity: .5; } 30% { transform: translateY(-6px); opacity: 1; } }
@keyframes float { 0%,100% { transform: translateY(0); } 50% { transform: translateY(-10px); } }

/* scrollbars */
::-webkit-scrollbar { width: 8px; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 8px; }
::-webkit-scrollbar-thumb:hover { background: var(--accent); }