: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; height: 100dvh; overflow: hidden; }
/* iOS inflates text in landscape unless this is pinned. */
html { -webkit-text-size-adjust: 100%; }
.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); }
/* MUST come after the rule above: an explicit `display` beats the [hidden] attribute,
   which was leaking the admin icon to every customer. */
#admin-link[hidden] { display: none !important; }
#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; }

/* ---------- Usage & cost transparency ---------- */
.legal-links { display: flex; flex-wrap: wrap; gap: 4px 10px; padding: 6px 12px 8px; direction: rtl; }
.legal-links a { color: var(--muted); font-size: 11.5px; text-decoration: none; }
.legal-links a:hover { color: var(--accent); text-decoration: underline; }
.usage-link { width: 100%; background: none; border: 0; color: var(--text-dim); font-size: 12.5px; text-align: left; padding: 7px 12px 2px; cursor: pointer; border-radius: 9px; }
.usage-link:hover { color: var(--accent); background: var(--surface-2); }
#usage-overlay { position: fixed; inset: 0; z-index: 91; padding: 24px; background: rgba(6, 7, 10, 0.72); backdrop-filter: blur(4px); display: grid; place-items: center; }
#usage-overlay[hidden] { display: none; }
.usage-card { position: relative; width: 100%; max-width: 520px; max-height: 90vh; overflow-y: auto; background: var(--surface); border: 1px solid var(--border); border-radius: 20px; padding: 28px; box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5); }
.usage-card h2 { margin: 0 0 18px; font-size: 21px; font-weight: 650; text-align: center; }
.usage-stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 10px; margin-bottom: 22px; }
.ustat { background: var(--bg-elev); border: 1px solid var(--border); border-radius: 14px; padding: 14px 8px; text-align: center; }
.ustat-n { display: block; font-size: 16px; font-weight: 700; }
.ustat-l { display: block; font-size: 11.5px; color: var(--muted); margin-top: 3px; }
.usage-note { font-size: 12.5px; color: var(--muted); margin: -4px 0 10px; text-align: left; }
.usage-costs { display: flex; flex-direction: column; gap: 1px; background: var(--border); border: 1px solid var(--border); border-radius: 12px; overflow: hidden; margin-bottom: 10px; }
.cost-row { display: flex; align-items: center; gap: 8px; background: var(--bg-elev); padding: 10px 13px; font-size: 13px; text-align: left; }
.cost-model { flex: 1; min-width: 0; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cost-tier { color: var(--accent); font-weight: 700; margin-left: 6px; font-size: 12px; }
.cost-msgs { color: var(--text); font-weight: 650; white-space: nowrap; }
.cost-each { color: var(--muted); font-size: 11.5px; white-space: nowrap; min-width: 74px; text-align: right; }
.usage-fineprint { font-size: 11.5px; color: var(--muted); line-height: 1.55; text-align: left; margin-bottom: 20px; }
.usage-list { display: flex; flex-direction: column; }
.usage-row { display: flex; align-items: center; gap: 10px; padding: 10px 2px; border-bottom: 1px solid var(--border-soft); font-size: 13px; text-align: left; }
.usage-row:last-child { border-bottom: 0; }
.u-what { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.u-when { color: var(--muted); font-size: 11.5px; white-space: nowrap; }
.u-amt { font-weight: 650; white-space: nowrap; min-width: 76px; text-align: right; }
.u-amt.plus { color: #6fc48a; }
.usage-empty { color: var(--muted); font-size: 13px; padding: 14px 2px; text-align: center; }

/* ---------- Low-balance banner ---------- */
.low-banner { display: flex; align-items: center; gap: 10px; margin: 0 auto 8px; max-width: 780px; background: var(--accent-soft); border: 1px solid var(--accent); border-radius: 12px; padding: 8px 12px; font-size: 13px; }
.low-banner[hidden] { display: none; }
.low-banner.empty { background: rgba(236, 123, 123, 0.14); border-color: #ec7b7b; }
.low-banner span { flex: 1; min-width: 0; }
.low-banner button { background: var(--accent); border: 0; color: #fff; font-weight: 650; font-size: 12.5px; padding: 5px 12px; border-radius: 9px; cursor: pointer; }
.low-banner.empty button { background: #ec7b7b; }
.low-banner .low-x { background: none; color: var(--text-dim); padding: 4px 6px; font-size: 13px; }
.low-banner .low-x[hidden] { display: none; }
.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; }
.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 {
  position: relative;
  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);
  font-family: Vazirmatn, "IRANSans", "Segoe UI", Tahoma, var(--font);
}
/* Without this the signup screen is a dead end — the only way back to the public
   site is the browser's back button. */
.auth-home {
  position: absolute; top: 14px; inset-inline-start: 16px;
  color: var(--muted); font-size: 12.5px; text-decoration: none;
}
.auth-home:hover { color: var(--accent); }
/* Latin credentials stay LTR inside the RTL card. */
#auth-form input { direction: ltr; text-align: start; }
.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); }
.pw-wrap { position: relative; display: flex; }
.pw-wrap input { flex: 1; padding-right: 44px; }
.pw-eye {
  position: absolute; right: 6px; top: 50%; transform: translateY(-50%);
  background: none; border: none; cursor: pointer; font-size: 16px; line-height: 1;
  width: 34px; height: 34px; border-radius: 8px; opacity: 0.75;
}
.pw-eye:hover { opacity: 1; background: var(--surface-2); }
#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 { flex: 1; font-size: 14px; color: var(--text-dim); font-weight: 550; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.model-select {
  flex: 0 0 auto; background: var(--surface); color: var(--text); border: 1px solid var(--border);
  border-radius: 9px; padding: 7px 10px; font: inherit; font-size: 13px; font-weight: 550; cursor: pointer;
  max-width: 190px; outline: none;
}
.model-select:hover { border-color: var(--accent); }
.model-select:focus { border-color: var(--accent); }
.model-select optgroup { background: var(--bg-elev); color: var(--muted); font-weight: 700; }
.model-select option { background: var(--bg-elev); color: var(--text); font-weight: 500; }

#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; }
.lang-chip {
  align-self: flex-end; margin-bottom: 5px; background: var(--surface-2); border: 1px solid var(--border);
  color: var(--muted); border-radius: 7px; padding: 4px 7px; font-size: 11px; font-weight: 700;
  cursor: pointer; letter-spacing: 0.4px; transition: all 0.15s;
}
.lang-chip:hover { color: var(--text); border-color: var(--accent); }

/* ---------- Persian / RTL support ---------- */
/* Text direction is auto-detected per block (dir="auto"), so Farsi flows right-to-left
   and English stays left-to-right — even mixed in the same conversation. */
#input { text-align: start; }
.msg .body { text-align: start; }
/* Code must ALWAYS stay left-to-right, even inside Persian text. */
.code-wrap, .code-wrap pre, .code-wrap code { direction: ltr; text-align: left; }
.body :not(pre) > code { direction: ltr; unicode-bidi: isolate; }
.body [dir="rtl"] { line-height: 1.9; }
/* Persian-capable font fallbacks */
:root { --font: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Vazirmatn", "IRANSans", Tahoma, "Noto Sans Arabic", Helvetica, Arial, sans-serif; }

/* 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 { color: var(--text); }
.spinner {
  flex: 0 0 auto; width: 12px; height: 12px; border-radius: 50%;
  border: 2px solid var(--border); border-top-color: var(--accent);
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* 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; min-width: 0; 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) {
  /* Cost rows have 3 columns of numbers — stack the per-message price under the
     model name instead of letting the row overflow on a narrow screen. */
  #usage-overlay { padding: 12px; }
  .usage-card { padding: 22px 16px; max-height: 94vh; }
  .usage-stats { gap: 6px; }
  .ustat { padding: 11px 4px; }
  .ustat-n { font-size: 14px; }
  .ustat-l { font-size: 10.5px; }
  .cost-row { flex-wrap: wrap; row-gap: 2px; padding: 9px 11px; }
  .cost-model { flex: 1 1 100%; }
  .cost-msgs { flex: 1; font-size: 12.5px; }
  .cost-each { min-width: 0; }
  .low-banner { font-size: 12.5px; padding: 7px 10px; gap: 7px; }
  .low-banner button { padding: 5px 10px; font-size: 12px; }
  #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; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }

  /* iOS zooms the whole page when a focused input is under 16px. Never go below it. */
  #input { font-size: 16px; }

  /* Keep the composer on ONE row: no wrapping, and the textarea absorbs the slack. */
  #chat-form { flex-wrap: nowrap; gap: 4px; padding: 5px 6px; border-radius: 16px; }
  #composer {
    padding: 6px 12px 10px;
    padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px)); /* clear the home indicator */
  }
  #attach-chips { padding: 0 4px; }
  .attach-chip { max-width: 180px; font-size: 12px; }

  /* Comfortable thumb targets (34px was too small to hit reliably). */
  .icon-btn { width: 40px; height: 40px; }
  #send { width: 40px; height: 40px; }
  .lang-chip { margin-bottom: 6px; padding: 5px 6px; }

  /* Stop the model picker from crushing the chat title on narrow screens. */
  #topbar { padding-top: env(safe-area-inset-top, 0px); gap: 6px; }
  #topbar-title { font-size: 13px; }
  .model-select { max-width: 42vw; }

  #sidebar { padding-bottom: env(safe-area-inset-bottom, 0px); }
}

/* Small phones (iPhone SE / mini and most Android at 360px) */
@media (max-width: 430px) {
  #welcome h1 { font-size: 22px; }
  #welcome p { font-size: 14.5px; }
  .welcome-logo { transform: scale(0.85); }
  #messages { padding: 16px 12px 32px; }
  #topbar-title { font-size: 12.5px; }
  .model-select { max-width: 38vw; font-size: 12px; }
  .chip { padding: 12px 13px; font-size: 13px; }
  /* Voice-language chip is the least essential control — it goes first when space is tight. */
  .lang-chip { padding: 4px 5px; font-size: 10px; }
}

/* Landscape phones: the keyboard eats most of the screen, so trim vertical padding. */
@media (max-height: 500px) and (orientation: landscape) {
  #messages { padding-top: 12px; padding-bottom: 20px; }
  #composer { padding-bottom: calc(6px + env(safe-area-inset-bottom, 0px)); }
  #welcome { padding-top: 8px; }
}

/* ---------------- Chat | Image mode toggle ---------------- */
.mode-toggle {
  display: inline-flex; gap: 2px; padding: 2px;
  background: var(--bg-elev); border: 1px solid var(--border); border-radius: 10px;
}
.mode-btn {
  border: 0; background: none; color: var(--text-dim);
  font-size: 13px; font-weight: 600; padding: 5px 12px; border-radius: 8px;
  cursor: pointer; transition: background 0.12s, color 0.12s;
}
.mode-btn:hover { color: var(--text); }
.mode-btn.is-active { background: linear-gradient(140deg, var(--accent), var(--accent-2)); color: #fff; }

/* ---------------- Generated media cards (image + video) ---------------- */
.img-result, .vid-result { display: flex; flex-direction: column; gap: 10px; max-width: 460px; width: 100%; }
.img-result img, .vid-result video {
  width: 100%; height: auto; border-radius: 16px; border: 1px solid var(--border);
  background: var(--bg-elev); display: block; box-shadow: 0 10px 34px rgba(0, 0, 0, 0.30);
}
.vid-result video { background: #000; }
.img-result img { cursor: zoom-in; transition: transform 0.15s ease; }
.img-result img:hover { transform: translateY(-2px); }
.media-caption { font-size: 12.5px; color: var(--text-dim); line-height: 1.5; margin: 0; }
.img-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.img-dl {
  display: inline-flex; align-items: center; gap: 6px; text-decoration: none; cursor: pointer;
  background: var(--surface); border: 1px solid var(--border); color: var(--text);
  font-size: 12.5px; font-weight: 600; padding: 7px 13px; border-radius: 10px; transition: all 0.13s;
}
.img-dl:hover { border-color: var(--accent); color: var(--accent); background: var(--surface-2); }

/* Loading: shimmer skeleton */
.media-skeleton {
  width: 340px; max-width: 100%; aspect-ratio: 1 / 1; border-radius: 16px;
  border: 1px solid var(--border); position: relative; overflow: hidden; background: var(--bg-elev);
}
.media-skeleton.wide { aspect-ratio: 16 / 9; }
.media-skeleton.tall { aspect-ratio: 9 / 16; max-width: 260px; }
.media-skeleton::after {
  content: ""; position: absolute; inset: 0; transform: translateX(-100%);
  background: linear-gradient(100deg, transparent 20%, var(--surface-2) 50%, transparent 80%);
  animation: shimmer 1.4s infinite;
}
@keyframes shimmer { to { transform: translateX(100%); } }
.gen-status { color: var(--text-dim); font-size: 13px; display: inline-flex; align-items: center; gap: 8px; margin-top: 9px; }

/* Video render progress */
.gen-progress { display: flex; align-items: center; gap: 10px; color: var(--text-dim); font-size: 13.5px; }
.gen-progress .elapsed { color: var(--text); font-weight: 650; font-variant-numeric: tabular-nums; }
.gen-progress .spin {
  width: 15px; height: 15px; border: 2px solid var(--border); border-top-color: var(--accent);
  border-radius: 50%; animation: gen-spin 0.8s linear infinite; flex: none;
}
@keyframes gen-spin { to { transform: rotate(360deg); } }

/* Media gallery on the Image/Video welcome screen */
.img-gallery {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 10px; width: 100%; max-width: 780px; margin: 16px auto 0;
}
.img-gallery a {
  display: block; position: relative; border-radius: 12px; overflow: hidden;
  border: 1px solid var(--border); aspect-ratio: 1; cursor: zoom-in; transition: border-color 0.13s, transform 0.13s;
}
.img-gallery a:hover { border-color: var(--accent); transform: translateY(-2px); }
.img-gallery img, .img-gallery video { width: 100%; height: 100%; object-fit: cover; display: block; }
.g-overlay {
  position: absolute; inset: 0; display: flex; align-items: flex-end; padding: 8px;
  background: linear-gradient(to top, rgba(0, 0, 0, 0.7), transparent 55%);
  color: #fff; font-size: 11px; line-height: 1.35; opacity: 0; transition: opacity 0.15s;
}
.g-overlay span { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.img-gallery a:hover .g-overlay { opacity: 1; }
.g-play {
  position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
  width: 38px; height: 38px; border-radius: 50%; background: rgba(0, 0, 0, 0.55);
  display: flex; align-items: center; justify-content: center; color: #fff; font-size: 14px; pointer-events: none;
}
.gallery-empty { color: var(--text-dim); font-size: 13px; padding: 12px 2px; grid-column: 1 / -1; }

/* Full-screen media viewer */
.lightbox {
  position: fixed; inset: 0; z-index: 120; background: rgba(0, 0, 0, 0.88);
  display: flex; align-items: center; justify-content: center; padding: 28px;
  backdrop-filter: blur(4px);
}
.lightbox[hidden] { display: none; }
.lightbox-body { display: flex; align-items: center; justify-content: center; }
.lightbox-body img, .lightbox-body video {
  max-width: 92vw; max-height: 84vh; border-radius: 12px; box-shadow: 0 24px 70px rgba(0, 0, 0, 0.6); display: block;
}
.lightbox-close {
  position: absolute; top: 16px; right: 20px; width: 40px; height: 40px; border-radius: 50%;
  background: rgba(255, 255, 255, 0.12); border: 0; color: #fff; font-size: 18px; cursor: pointer; transition: background 0.13s;
}
.lightbox-close:hover { background: rgba(255, 255, 255, 0.26); }
.lightbox-cap {
  position: absolute; bottom: 20px; left: 50%; transform: translateX(-50%); max-width: 80vw;
  color: #eaeaea; font-size: 13px; text-align: center; background: rgba(0, 0, 0, 0.5); padding: 7px 14px; border-radius: 9px;
}
.lightbox-cap:empty { display: none; }
