:root {
  --bg: #0c0d11;
  --sidebar: #0a0b0e;
  --bg-elev: #131519;
  --surface: #1a1d24;
  --surface-2: #21252e;
  --border: #282c36;
  --border-soft: #1e222a;
  --text: #edeef2;
  --text-dim: #c3c7d1;
  --muted: #8a8f9c;
  --accent: #d3835f;
  --accent-2: #c06c48;
  --accent-soft: rgba(211, 131, 95, 0.14);
  --code-bg: #0f1116;
  --mono: "SF Mono", ui-monospace, "JetBrains Mono", Menlo, Consolas, monospace;
  --font: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --sidebar-w: 268px;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font-family: var(--font); font-size: 15.5px; line-height: 1.7;
  -webkit-font-smoothing: antialiased; text-rendering: optimizeLegibility;
}

#app { display: flex; height: 100vh; overflow: hidden; }
.icon-btn {
  background: transparent; border: none; color: var(--muted); cursor: pointer;
  width: 34px; height: 34px; border-radius: 9px; font-size: 16px;
  display: grid; place-items: center; transition: all 0.15s;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

/* ---------- Sidebar ---------- */
#sidebar {
  width: var(--sidebar-w); flex: 0 0 var(--sidebar-w);
  background: var(--sidebar); border-right: 1px solid var(--border-soft);
  display: flex; flex-direction: column; padding: 12px; gap: 10px;
}
.sidebar-top { display: flex; align-items: center; justify-content: space-between; padding: 4px 4px 2px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; letter-spacing: 0.2px; }
.logo {
  display: inline-grid; place-items: center; width: 28px; height: 28px; border-radius: 8px;
  background: linear-gradient(140deg, var(--accent), var(--accent-2)); color: #fff; font-weight: 700;
  box-shadow: 0 2px 10px rgba(211, 131, 95, 0.3);
}
#sidebar-close { display: grid; }
#new-chat {
  background: var(--surface); color: var(--text); border: 1px solid var(--border);
  border-radius: 10px; padding: 11px 14px; cursor: pointer; font-size: 14px; font-weight: 550;
  text-align: left; transition: all 0.15s;
}
#new-chat:hover { border-color: var(--accent); background: var(--surface-2); }

#conv-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; margin: 4px -4px; padding: 0 4px; }
#conv-list::-webkit-scrollbar { width: 8px; }
#conv-list::-webkit-scrollbar-thumb { background: #1c2028; border-radius: 8px; }
.conv-item {
  display: flex; align-items: center; gap: 8px; padding: 9px 11px; border-radius: 9px;
  cursor: pointer; color: var(--text-dim); font-size: 13.5px; transition: background 0.12s;
  white-space: nowrap; overflow: hidden;
}
.conv-item:hover { background: var(--surface); }
.conv-item.active { background: var(--surface-2); color: var(--text); }
.conv-item .title { flex: 1; overflow: hidden; text-overflow: ellipsis; }
.conv-item .del { opacity: 0; color: var(--muted); font-size: 15px; padding: 0 2px; }
.conv-item:hover .del { opacity: 1; }
.conv-item .del:hover { color: #e06a6a; }
.sidebar-foot {
  display: flex; flex-direction: column; gap: 8px;
  padding: 10px 4px 2px; border-top: 1px solid var(--border-soft);
}
.balance-pill {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 11px;
  padding: 9px 12px; cursor: pointer; color: var(--text); transition: all 0.15s;
}
.balance-pill:hover { border-color: var(--accent); background: var(--surface-2); }
.balance-pill.low .bal-amount { color: #ec7b7b; }
.bal-amount { font-weight: 650; font-size: 15px; }
.bal-add { font-size: 12px; color: var(--accent); font-weight: 600; }
.user-row { display: flex; align-items: center; gap: 6px; padding: 0 4px; color: var(--text-dim); font-size: 12.5px; }
#user-email { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#admin-link { flex: 0 0 auto; text-decoration: none; color: var(--muted); font-size: 15px; width: 30px; height: 30px; display: grid; place-items: center; border-radius: 8px; }
#admin-link:hover { color: var(--text); background: var(--surface-2); }
#logout-btn { flex: 0 0 auto; }

/* Top-up modal */
#topup-overlay { position: fixed; inset: 0; z-index: 90; padding: 24px; background: rgba(6, 7, 10, 0.72); backdrop-filter: blur(4px); display: grid; place-items: center; }
#topup-overlay[hidden] { display: none; }
.topup-card { position: relative; width: 100%; max-width: 460px; background: var(--surface); border: 1px solid var(--border); border-radius: 20px; padding: 30px 28px; text-align: center; box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5); }
.topup-x { position: absolute; top: 12px; right: 12px; }
.topup-card .welcome-logo { margin-bottom: 14px; }
.topup-card h2 { margin: 0 0 4px; font-size: 21px; font-weight: 650; }
.topup-sub { margin: 0 0 20px; color: var(--muted); font-size: 14px; }
.topup-sub strong { color: var(--text); }
.bundles { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 18px; }
.bundle { position: relative; background: var(--bg-elev); border: 1px solid var(--border); border-radius: 14px; padding: 16px 12px; text-align: center; }
.bundle .b-amount { font-size: 22px; font-weight: 700; }
.bundle .b-credit { font-size: 12.5px; color: var(--muted); margin-top: 2px; }
.bundle .b-bonus { position: absolute; top: -9px; right: 10px; background: var(--accent); color: #fff; font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 8px; }
.topup-note { font-size: 13px; color: var(--text-dim); background: var(--accent-soft); border: 1px solid var(--border-soft); border-radius: 12px; padding: 12px 14px; line-height: 1.55; text-align: left; }
.topup-card { max-height: 90vh; overflow-y: auto; }
.topup-label { text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: 0.5px; color: var(--muted); margin: 6px 0 10px; }
.amounts { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 10px; }
.amount-btn { background: var(--bg-elev); border: 1px solid var(--border); color: var(--text); border-radius: 11px; padding: 11px 0; cursor: pointer; transition: all 0.15s; line-height: 1.3; }
.amount-btn:hover { border-color: var(--accent); }
.amount-btn.sel { border-color: var(--accent); background: var(--accent-soft); }
.amount-btn.sel .amt-toman { color: var(--accent); }
.amt-toman { font-size: 15px; font-weight: 700; }
.amt-usd { font-size: 11.5px; color: var(--muted); margin-top: 1px; }
.custom-amount { display: flex; align-items: center; gap: 6px; background: var(--bg-elev); border: 1px solid var(--border); border-radius: 11px; padding: 0 12px; margin-bottom: 20px; }
.custom-amount:focus-within { border-color: var(--accent); }
.ca-suffix { color: var(--muted); font-weight: 600; font-size: 13px; }
.custom-hint { text-align: left; font-size: 12px; color: var(--muted); margin: -14px 0 18px 2px; }
.custom-hint:empty { display: none; }
#custom-amount { flex: 1; background: transparent; border: none; outline: none; color: var(--text); font: inherit; padding: 11px 0; }
.pay-head { text-align: left; font-size: 13.5px; color: var(--text-dim); margin-bottom: 10px; }
.pay-info { display: flex; flex-direction: column; gap: 8px; margin-bottom: 16px; }
.pay-row { display: flex; align-items: center; gap: 10px; background: var(--bg-elev); border: 1px solid var(--border); border-radius: 11px; padding: 10px 12px; text-align: left; }
.pay-row .pr-label { font-size: 12px; font-weight: 700; color: var(--accent); flex: 0 0 auto; min-width: 42px; }
.pay-row .pr-value { flex: 1; font-family: var(--mono); font-size: 12.5px; color: var(--text-dim); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pay-row .pr-copy { flex: 0 0 auto; background: var(--surface-2); border: 1px solid var(--border); color: var(--muted); border-radius: 7px; padding: 5px 10px; font-size: 12px; cursor: pointer; }
.pay-row .pr-copy:hover { color: var(--text); border-color: var(--accent); }

/* Auth screen */
#auth-overlay {
  position: fixed; inset: 0; z-index: 100; padding: 24px;
  background: radial-gradient(1200px 600px at 50% -10%, #191b22, var(--bg));
  display: grid; place-items: center;
}
#auth-overlay[hidden] { display: none; }
.auth-card {
  width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 20px; padding: 34px 28px; text-align: center; box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}
.auth-card .welcome-logo { margin-bottom: 16px; }
.auth-card h2 { margin: 0 0 6px; font-size: 22px; font-weight: 650; }
.auth-card p { margin: 0 0 22px; color: var(--muted); font-size: 14px; }
#auth-form { display: flex; flex-direction: column; gap: 11px; }
#auth-form input { background: var(--bg-elev); border: 1px solid var(--border); border-radius: 11px; padding: 12px 14px; color: var(--text); font: inherit; outline: none; }
#auth-form input:focus { border-color: var(--accent); }
#auth-submit { margin-top: 4px; background: linear-gradient(140deg, var(--accent), var(--accent-2)); color: #fff; border: none; border-radius: 11px; padding: 12px; font-size: 15px; font-weight: 600; cursor: pointer; transition: opacity 0.15s; }
#auth-submit:hover { opacity: 0.92; }
#auth-submit:disabled { opacity: 0.5; cursor: default; }
#auth-error { color: #ec7b7b; font-size: 13px; text-align: left; }
#auth-error:empty { display: none; }
.auth-toggle { margin-top: 18px; font-size: 14px; color: var(--muted); }
.auth-toggle button { background: none; border: none; color: var(--accent); cursor: pointer; font: inherit; font-weight: 600; padding: 0 4px; }
.auth-toggle button:hover { text-decoration: underline; }
.auth-link { background: none; border: none; color: var(--accent); cursor: pointer; font: inherit; font-size: 13.5px; padding: 4px; margin-top: 12px; }
.auth-link:hover { text-decoration: underline; }
.auth-full-btn { width: 100%; margin-top: 6px; background: linear-gradient(140deg, var(--accent), var(--accent-2)); color: #fff; border: none; border-radius: 11px; padding: 12px; font-size: 15px; font-weight: 600; cursor: pointer; transition: opacity 0.15s; }
.auth-full-btn:hover { opacity: 0.92; }
.auth-full-btn:disabled { opacity: 0.5; cursor: default; }
#auth-info { color: #5ecb8a; font-size: 13px; text-align: left; }
#auth-info:empty { display: none; }

/* Email verification gate */
#verify-overlay { position: fixed; inset: 0; z-index: 100; padding: 24px; background: radial-gradient(1200px 600px at 50% -10%, #191b22, var(--bg)); display: grid; place-items: center; }
#verify-overlay[hidden] { display: none; }
#verify-sub { color: var(--muted); font-size: 14px; margin: 0 0 18px; }
#verify-info { color: #5ecb8a; font-size: 13px; margin-bottom: 12px; }
#verify-info:empty { display: none; }

#sidebar-scrim { display: none; }

/* ---------- Main ---------- */
#main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
#topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 10px 16px; border-bottom: 1px solid var(--border-soft);
  background: rgba(12, 13, 17, 0.85); backdrop-filter: blur(10px);
}
#sidebar-open { display: grid; }
#topbar-title { font-size: 14px; color: var(--text-dim); font-weight: 550; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

#chat { flex: 1; overflow-y: auto; }
#chat::-webkit-scrollbar { width: 10px; }
#chat::-webkit-scrollbar-thumb { background: #22262f; border-radius: 10px; border: 3px solid var(--bg); }
#messages { width: 100%; max-width: 760px; margin: 0 auto; padding: 28px 22px 40px; }

.msg { display: flex; gap: 15px; padding: 8px 0 22px; animation: fade 0.3s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(5px); } to { opacity: 1; transform: none; } }
.msg .avatar {
  flex: 0 0 30px; width: 30px; height: 30px; border-radius: 9px; display: grid; place-items: center;
  font-size: 14px; font-weight: 700; margin-top: 2px; user-select: none;
}
.msg.user .avatar { background: var(--surface-2); color: var(--text-dim); border: 1px solid var(--border); }
.msg.assistant .avatar {
  background: linear-gradient(140deg, var(--accent), var(--accent-2)); color: #fff;
  box-shadow: 0 2px 10px rgba(211, 131, 95, 0.28);
}
.msg .body { flex: 1; min-width: 0; padding-top: 2px; }
.msg .body > *:first-child { margin-top: 0; }
.msg .body > *:last-child { margin-bottom: 0; }
.msg.user .body {
  white-space: pre-wrap; background: var(--surface); border: 1px solid var(--border-soft);
  border-radius: 14px; padding: 12px 16px;
}

/* Markdown */
.body p { margin: 0 0 14px; }
.body ul, .body ol { margin: 0 0 14px; padding-left: 24px; }
.body li { margin: 6px 0; }
.body li::marker { color: var(--muted); }
.body h1, .body h2, .body h3 { margin: 22px 0 12px; line-height: 1.35; font-weight: 650; }
.body h1 { font-size: 1.4em; } .body h2 { font-size: 1.25em; } .body h3 { font-size: 1.08em; }
.body a { color: var(--accent); text-decoration: none; border-bottom: 1px solid transparent; }
.body a:hover { border-bottom-color: var(--accent); }
.body strong { color: #fff; font-weight: 650; }
.body blockquote { margin: 0 0 14px; padding: 4px 16px; border-left: 3px solid var(--accent); color: var(--text-dim); background: var(--accent-soft); border-radius: 0 8px 8px 0; }
.body hr { border: none; border-top: 1px solid var(--border); margin: 20px 0; }
.body :not(pre) > code { background: var(--surface-2); border: 1px solid var(--border); padding: 1.5px 6px; border-radius: 6px; font-size: 0.86em; font-family: var(--mono); color: #f0c9b6; }
.body table { border-collapse: collapse; margin: 0 0 14px; display: block; overflow-x: auto; }
.body th, .body td { border: 1px solid var(--border); padding: 7px 12px; text-align: left; }
.body th { background: var(--surface); font-weight: 600; }

/* Code blocks */
.code-wrap { margin: 0 0 16px; border: 1px solid var(--border); border-radius: 12px; overflow: hidden; background: var(--code-bg); }
.code-head { display: flex; align-items: center; justify-content: space-between; padding: 7px 12px 7px 14px; background: #13151b; border-bottom: 1px solid var(--border); font-size: 12px; color: var(--muted); }
.code-head .lang { font-family: var(--mono); letter-spacing: 0.4px; }
.copy-btn { background: transparent; color: var(--muted); border: none; border-radius: 6px; padding: 3px 10px; font-size: 12px; cursor: pointer; transition: all 0.15s; }
.copy-btn:hover { color: var(--text); background: var(--surface-2); }
.code-wrap pre { margin: 0; padding: 14px 16px; overflow-x: auto; background: var(--code-bg); }
.code-wrap pre code { font-family: var(--mono); font-size: 13px; line-height: 1.6; background: none; padding: 0; }
.code-wrap pre::-webkit-scrollbar { height: 8px; }
.code-wrap pre::-webkit-scrollbar-thumb { background: #2a2f3a; border-radius: 8px; }

/* Welcome */
#welcome { margin: auto; max-width: 680px; text-align: center; padding: 7vh 22px; }
.welcome-logo { width: 52px; height: 52px; border-radius: 15px; margin: 0 auto 20px; display: grid; place-items: center; font-size: 26px; color: #fff; background: linear-gradient(140deg, var(--accent), var(--accent-2)); box-shadow: 0 6px 24px rgba(211, 131, 95, 0.32); }
#welcome h1 { font-size: 29px; margin: 0 0 10px; font-weight: 650; letter-spacing: -0.3px; }
#welcome p { color: var(--muted); margin: 0 0 30px; font-size: 16px; }
.suggestions { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; text-align: left; }
.chip { background: var(--surface); color: var(--text-dim); border: 1px solid var(--border-soft); border-radius: 13px; padding: 14px 16px; cursor: pointer; font-size: 13.5px; line-height: 1.45; transition: all 0.15s; }
.chip:hover { border-color: var(--accent); background: var(--surface-2); color: var(--text); transform: translateY(-1px); }

/* Composer */
#composer { background: linear-gradient(to top, var(--bg) 55%, transparent); padding: 6px 22px 14px; }
#chat-form { max-width: 760px; margin: 0 auto; display: flex; align-items: flex-end; gap: 6px; background: var(--surface); border: 1px solid var(--border); border-radius: 18px; padding: 6px 8px 6px 8px; box-shadow: 0 8px 30px rgba(0, 0, 0, 0.35); transition: border-color 0.15s; }
#chat-form:focus-within { border-color: var(--accent); }
#mic { align-self: flex-end; margin-bottom: 1px; }
#mic.active { color: #fff; background: var(--accent); animation: micpulse 1.2s infinite; }
@keyframes micpulse { 0%,100% { box-shadow: 0 0 0 0 rgba(211,131,95,0.5); } 50% { box-shadow: 0 0 0 6px rgba(211,131,95,0); } }
#attach { align-self: flex-end; margin-bottom: 1px; }

/* Pending attachment chips (above the input) */
#attach-chips { max-width: 760px; margin: 0 auto 8px; display: flex; flex-wrap: wrap; gap: 8px; }
#attach-chips:empty { display: none; }
.attach-chip {
  display: inline-flex; align-items: center; gap: 8px; background: var(--surface-2);
  border: 1px solid var(--border); border-radius: 10px; padding: 6px 8px 6px 10px;
  font-size: 13px; color: var(--text-dim); max-width: 240px;
}
.attach-chip .fname { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.attach-chip .x { cursor: pointer; color: var(--muted); font-size: 15px; line-height: 1; }
.attach-chip .x:hover { color: #e06a6a; }
.attach-chip.uploading { opacity: 0.6; }

/* Attachments shown inside a sent message */
.msg-attachments { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 10px; }
.msg-attach {
  display: inline-flex; align-items: center; gap: 7px; background: var(--surface-2);
  border: 1px solid var(--border); border-radius: 9px; padding: 5px 10px; font-size: 12.5px; color: var(--text-dim);
}
#input { flex: 1; resize: none; max-height: 240px; background: transparent; color: var(--text); border: none; outline: none; font: inherit; line-height: 1.6; padding: 8px 2px; }
#input::placeholder { color: var(--muted); }
#send { flex: 0 0 auto; width: 38px; height: 38px; border-radius: 12px; border: none; cursor: pointer; background: linear-gradient(140deg, var(--accent), var(--accent-2)); color: #fff; font-size: 15px; display: grid; place-items: center; align-self: flex-end; transition: transform 0.1s; }
#send:hover:not(:disabled) { transform: scale(1.05); }
#send:disabled { opacity: 0.4; cursor: default; }
.disclaimer { max-width: 760px; margin: 9px auto 0; text-align: center; color: var(--muted); font-size: 12px; }

.cursor::after { content: "●"; margin-left: 3px; font-size: 0.7em; color: var(--accent); animation: pulse 1.1s ease-in-out infinite; }
@keyframes pulse { 0%,100% { opacity: 0.25; } 50% { opacity: 1; } }

/* ---------- Desktop: collapsible sidebar ---------- */
@media (min-width: 821px) {
  #sidebar { transition: margin-left 0.22s ease; }
  #app.sidebar-collapsed #sidebar { margin-left: calc(-1 * var(--sidebar-w)); }
}

/* ---------- Responsive ---------- */
@media (max-width: 820px) {
  #sidebar {
    position: fixed; top: 0; left: 0; bottom: 0; z-index: 40;
    transform: translateX(-100%); transition: transform 0.22s ease;
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.4);
  }
  #app.sidebar-open #sidebar { transform: none; }
  #sidebar-close { display: grid; }
  #sidebar-open { display: grid; }
  #app.sidebar-open #sidebar-scrim { display: block; position: fixed; inset: 0; z-index: 30; background: rgba(0, 0, 0, 0.5); }
  .suggestions { grid-template-columns: 1fr; }
  #messages { padding: 22px 16px 40px; }
}
