:root {
  --bg: #f7f7f5;
  --surface: #ffffff;
  --surface-2: #f1f1ee;
  --border: #e3e2dd;
  --text: #1d1d1b;
  --muted: #6b6a65;
  --accent: #0f766e;
  --accent-ink: #ffffff;
  --accent-soft: #e6f3f1;
  --danger: #b42318;
  --danger-soft: #fdecea;
  --warn: #9a6700;
  --warn-soft: #fff4d6;
  --ok: #127a3a;
  --ok-soft: #e5f5ea;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 8px 24px rgba(0,0,0,.06);
  --radius: 14px;
  --font: 'IBM Plex Sans Arabic', system-ui, -apple-system, 'Segoe UI', Tahoma, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161615;
    --surface: #1f1f1d;
    --surface-2: #2a2a27;
    --border: #353531;
    --text: #ecebe6;
    --muted: #a3a29b;
    --accent: #2bb3a5;
    --accent-ink: #0b1513;
    --accent-soft: #173330;
    --danger: #ff8a7e;
    --danger-soft: #3a1f1c;
    --warn: #f0c35a;
    --warn-soft: #3a3020;
    --ok: #6fd69a;
    --ok-soft: #1c3326;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 8px 24px rgba(0,0,0,.35);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font-family: var(--font); font-size: 16px; line-height: 1.7;
  display: flex; flex-direction: column;
}
[hidden] { display: none !important; }
button, input, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex; align-items: center; gap: 16px;
  padding: 10px 20px; border-bottom: 1px solid var(--border);
  background: var(--surface); position: sticky; top: 0; z-index: 10;
}
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: var(--text); font-weight: 700; }
.brand-mark { width: 34px; height: 34px; border-radius: 10px; display: grid; place-items: center; background: var(--accent); color: var(--accent-ink); }
.topnav { display: flex; gap: 4px; margin-inline-start: 12px; }
.topnav a { padding: 6px 12px; border-radius: 999px; text-decoration: none; color: var(--muted); font-weight: 500; }
.topnav a.active, .topnav a:hover { background: var(--surface-2); color: var(--text); }
.top-actions { margin-inline-start: auto; display: flex; align-items: center; gap: 8px; }
.balance-pill { padding: 4px 12px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); font-weight: 600; font-size: 14px; white-space: nowrap; }
.ghost-btn { border: 1px solid var(--border); background: transparent; padding: 5px 12px; border-radius: 999px; cursor: pointer; }
.ghost-btn:hover { background: var(--surface-2); }

.config-warning { background: var(--warn-soft); color: var(--warn); padding: 10px 20px; text-align: center; font-size: 14px; }
.config-warning code { direction: ltr; display: inline-block; }

main { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.view { flex: 1; display: flex; flex-direction: column; min-height: 0; }

/* ---------- Buttons & forms ---------- */
.primary-btn {
  background: var(--accent); color: var(--accent-ink); border: 0; border-radius: 10px;
  padding: 10px 18px; font-weight: 600; cursor: pointer; width: 100%;
}
.primary-btn:disabled { opacity: .5; cursor: not-allowed; }
.link-btn { background: none; border: 0; color: var(--accent); cursor: pointer; padding: 0; justify-self: start; }
.form { display: grid; gap: 14px; }
.form label { display: grid; gap: 6px; font-weight: 500; font-size: 14px; }
.form input, .card input[type=number] {
  border: 1px solid var(--border); background: var(--surface); border-radius: 10px; padding: 10px 12px; font-size: 16px;
}
.form input:focus, textarea:focus, .card input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.form-msg { min-height: 1.5em; margin: 10px 0 0; font-size: 14px; color: var(--muted); }
.form-msg.error { color: var(--danger); }
.form-msg.ok { color: var(--ok); }

.tabs { display: flex; gap: 4px; background: var(--surface-2); padding: 4px; border-radius: 12px; margin-bottom: 18px; }
.tab { flex: 1; border: 0; background: transparent; padding: 8px 12px; border-radius: 9px; cursor: pointer; font-weight: 500; color: var(--muted); }
.tab.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

/* ---------- Auth ---------- */
.auth-view { align-items: center; justify-content: center; padding: 32px 16px; }
.auth-card { width: 100%; max-width: 420px; background: var(--surface); border: 1px solid var(--border); border-radius: 18px; padding: 28px; box-shadow: var(--shadow); }
.auth-title { margin: 0; font-size: 26px; }
.auth-sub { margin: 4px 0 20px; color: var(--muted); }

/* ---------- Chat ---------- */
.chat-view { position: relative; }
.thread { flex: 1; overflow-y: auto; padding: 24px 16px 16px; }
.empty-state { max-width: 760px; margin: 8vh auto 0; text-align: center; }
.empty-state h1 { font-size: clamp(24px, 4vw, 32px); margin: 0 0 6px; }
.empty-state p { color: var(--muted); margin: 0 0 24px; }
.suggestions { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 10px; text-align: start; }
.suggestion {
  border: 1px solid var(--border); background: var(--surface); border-radius: var(--radius);
  padding: 12px 14px; cursor: pointer; display: flex; gap: 10px; align-items: flex-start; text-align: start;
}
.suggestion:hover { border-color: var(--accent); }
.suggestion .s-icon { flex: none; width: 34px; height: 34px; border-radius: 9px; display: grid; place-items: center; }
.suggestion b { display: block; font-size: 14px; }
.suggestion span.s-text { font-size: 13px; color: var(--muted); line-height: 1.5; }

.messages { list-style: none; margin: 0 auto; padding: 0; max-width: 760px; display: grid; gap: 18px; }
.msg { display: flex; flex-direction: column; gap: 6px; }
.msg.user { align-items: flex-start; }
.msg.user .bubble { background: var(--surface-2); border-radius: 18px; padding: 10px 16px; max-width: 85%; white-space: pre-wrap; word-break: break-word; }
.msg .meta { font-size: 12px; color: var(--muted); display: flex; gap: 8px; align-items: center; }
.msg.bot { align-items: stretch; }
.msg.bot .bot-row { display: flex; gap: 12px; align-items: flex-start; }
.bot-avatar { flex: none; width: 30px; height: 30px; border-radius: 9px; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; }
.bot-avatar svg { width: 16px; height: 16px; }
.bot-body { flex: 1; padding-top: 3px; }
.status { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; padding: 2px 10px; border-radius: 999px; font-weight: 500; }
.status.queued, .status.working, .status.pending { background: var(--warn-soft); color: var(--warn); }
.status.done, .status.approved { background: var(--ok-soft); color: var(--ok); }
.status.failed, .status.rejected { background: var(--danger-soft); color: var(--danger); }
.dot-pulse { width: 7px; height: 7px; border-radius: 50%; background: currentColor; animation: pulse 1.2s infinite; }
@keyframes pulse { 0%,100% { opacity: .3 } 50% { opacity: 1 } }
.result-link { display: inline-flex; align-items: center; gap: 8px; margin-top: 8px; padding: 8px 14px; border-radius: 10px; border: 1px solid var(--border); text-decoration: none; font-weight: 600; background: var(--surface); }
.result-link:hover { border-color: var(--accent); }

.type-badge { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; padding: 1px 8px; border-radius: 999px; font-weight: 600; }

.composer-wrap { padding: 8px 16px 16px; background: linear-gradient(to top, var(--bg) 70%, transparent); }
.context-bar { max-width: 760px; margin: 0 auto 8px; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; font-size: 13px; }
.ctx-label { color: var(--muted); }
.context-bar select {
  border: 1px solid var(--border); background: var(--surface); color: var(--text); border-radius: 999px;
  padding: 5px 12px; font-size: 13px; max-width: 100%; cursor: pointer;
}
.context-bar select:disabled { opacity: .5; cursor: not-allowed; }
.context-bar select.set { border-color: var(--accent); color: var(--accent); font-weight: 600; }
#ctxLesson { max-width: 260px; }
.ctx-clear { border: 0; background: none; color: var(--accent); cursor: pointer; font-size: 13px; padding: 0 4px; }
.ctx-note { color: var(--muted); font-size: 12px; }
.ctx-badge { font-size: 12px; color: var(--muted); }
.composer { position: relative; max-width: 760px; margin: 0 auto; background: var(--surface); border: 1px solid var(--border); border-radius: 26px; box-shadow: var(--shadow); padding: 8px; }
.composer:focus-within { border-color: var(--accent); }
.composer-row { display: flex; align-items: flex-end; gap: 8px; }
.composer-main { flex: 1; display: flex; flex-direction: column; gap: 6px; min-width: 0; padding: 4px 0; }
.composer textarea { border: 0; outline: none; resize: none; background: transparent; width: 100%; max-height: 220px; line-height: 1.6; padding: 4px 2px; font-size: 16px; }
.composer textarea:focus { outline: none; }
.plus-btn, .send-btn { flex: none; width: 40px; height: 40px; border-radius: 50%; border: 0; display: grid; place-items: center; cursor: pointer; }
.plus-btn { background: var(--surface-2); color: var(--text); transition: transform .15s; }
.plus-btn[aria-expanded=true] { transform: rotate(45deg); }
.send-btn { background: var(--accent); color: var(--accent-ink); }
.send-btn:disabled { background: var(--surface-2); color: var(--muted); cursor: not-allowed; }
.type-chip { align-self: flex-start; display: inline-flex; align-items: center; gap: 6px; padding: 3px 6px 3px 10px; border-radius: 999px; font-size: 13px; font-weight: 600; }
.type-chip svg { width: 15px; height: 15px; }
.chip-x { border: 0; background: transparent; cursor: pointer; font-size: 16px; line-height: 1; padding: 0 4px; color: inherit; }
.composer-hint { max-width: 760px; margin: 6px auto 0; text-align: center; font-size: 12px; color: var(--muted); }

.type-menu {
  position: absolute; bottom: calc(100% + 8px); inset-inline-start: 0;
  width: min(340px, 100%); background: var(--surface); border: 1px solid var(--border);
  border-radius: 16px; box-shadow: var(--shadow); padding: 6px; display: grid; gap: 2px; z-index: 5;
}
.type-option { display: flex; align-items: center; gap: 12px; padding: 8px 10px; border: 0; background: transparent; border-radius: 10px; cursor: pointer; text-align: start; }
.type-option:hover, .type-option:focus-visible, .type-option.selected { background: var(--surface-2); outline: none; }
.type-option .t-icon { width: 34px; height: 34px; border-radius: 9px; display: grid; place-items: center; flex: none; }
.type-option .t-name { font-weight: 600; font-size: 15px; display: block; }
.type-option .t-desc { font-size: 12px; color: var(--muted); }
.type-option .t-left { margin-inline-start: auto; font-size: 12px; color: var(--muted); white-space: nowrap; }
.type-option .t-left.zero { color: var(--danger); }

/* type colors */
.c-pptx { background: #fde9df; color: #c2410c; }
.c-html { background: #e0ecff; color: #1d4ed8; }
.c-scorm { background: #ede6ff; color: #6d28d9; }
.c-docx { background: #dfeafe; color: #1e40af; }
.c-pdf { background: #fde2e1; color: #b91c1c; }
.c-infographic { background: #dcf5e8; color: #047857; }
@media (prefers-color-scheme: dark) {
  .c-pptx { background: #3a2318; color: #fdba8c; }
  .c-html { background: #182a45; color: #93b4ff; }
  .c-scorm { background: #2a2045; color: #c4b0ff; }
  .c-docx { background: #19284a; color: #9db8f5; }
  .c-pdf { background: #3d1c1c; color: #fca5a5; }
  .c-infographic { background: #163326; color: #7fdcb0; }
}

/* ---------- Pages (pay/admin) ---------- */
.page { max-width: 1000px; width: 100%; margin: 0 auto; padding: 28px 16px 48px; overflow-y: auto; }
.page h1 { margin: 0 0 4px; font-size: 28px; }
.lead { color: var(--muted); margin: 0 0 22px; }
.pay-grid { display: grid; grid-template-columns: 1.3fr 1fr; gap: 18px; align-items: start; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 18px; padding: 20px; box-shadow: var(--shadow); }
.card h2 { font-size: 16px; margin: 18px 0 10px; }
.card h2:first-child { margin-top: 0; }
.qty-list { display: grid; gap: 8px; }
.qty-row { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border: 1px solid var(--border); border-radius: 12px; }
.qty-row .t-icon { width: 32px; height: 32px; border-radius: 9px; display: grid; place-items: center; flex: none; }
.qty-row .q-name { font-weight: 600; font-size: 14px; }
.qty-row .q-price { font-size: 12px; color: var(--muted); }
.stepper { margin-inline-start: auto; display: flex; align-items: center; border: 1px solid var(--border); border-radius: 999px; overflow: hidden; }
.stepper button { border: 0; background: var(--surface-2); width: 32px; height: 32px; cursor: pointer; font-size: 18px; }
.stepper input[type=number] { width: 46px; height: 32px; border: 0; border-radius: 0; text-align: center; background: transparent; padding: 0 4px; font-size: 15px; font-weight: 600; -moz-appearance: textfield; }
.stepper input::-webkit-outer-spin-button, .stepper input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.total-row { display: flex; justify-content: space-between; align-items: center; margin-top: 12px; padding: 12px 14px; background: var(--accent-soft); border-radius: 12px; font-size: 17px; }
.total-row strong { color: var(--accent); font-size: 20px; }
.bank { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 0; padding: 12px 14px; border: 1px dashed var(--border); border-radius: 12px; font-size: 14px; }
.bank dt { color: var(--muted); }
.bank dd { margin: 0; font-weight: 600; }
.drop { display: block; border: 2px dashed var(--border); border-radius: 14px; padding: 20px; text-align: center; cursor: pointer; color: var(--muted); margin-bottom: 14px; }
.drop:hover, .drop.has-file { border-color: var(--accent); color: var(--text); }
.drop input { display: none; }
.credit-list, .pay-history { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.credit-list li { display: flex; justify-content: space-between; align-items: center; padding: 6px 2px; border-bottom: 1px solid var(--border); font-size: 14px; }
.credit-list li:last-child { border-bottom: 0; }
.pay-history li { padding: 10px 12px; border: 1px solid var(--border); border-radius: 12px; font-size: 13px; display: grid; gap: 4px; }
.pay-history .ph-top { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.empty-note { color: var(--muted); font-size: 14px; }

/* ---------- Admin ---------- */
.admin-tabs { max-width: 560px; }
.admin-table { width: 100%; border-collapse: collapse; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; overflow: hidden; font-size: 14px; }
.admin-table th, .admin-table td { padding: 10px 12px; border-bottom: 1px solid var(--border); text-align: start; vertical-align: top; }
.admin-table th { background: var(--surface-2); font-weight: 600; font-size: 13px; color: var(--muted); }
.admin-table tr:last-child td { border-bottom: 0; }
.admin-table .actions { display: flex; gap: 6px; flex-wrap: wrap; }
.small-btn { border: 1px solid var(--border); background: var(--surface); border-radius: 8px; padding: 4px 10px; cursor: pointer; font-size: 13px; }
.small-btn.approve { background: var(--ok-soft); color: var(--ok); border-color: transparent; font-weight: 600; }
.small-btn.reject { background: var(--danger-soft); color: var(--danger); border-color: transparent; font-weight: 600; }
.pkg-list { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; }
.pkg-card { position: relative; display: grid; gap: 4px; justify-items: center; text-align: center; padding: 16px 10px 12px; border: 2px solid var(--border); border-radius: 14px; background: var(--surface); cursor: pointer; font: inherit; color: inherit; }
.pkg-card.selected { border-color: var(--accent); background: var(--accent-soft); }
.pkg-tag { position: absolute; top: -10px; background: var(--accent); color: var(--accent-ink); font-size: 11px; padding: 2px 10px; border-radius: 999px; }
.pkg-name { font-weight: 600; font-size: 14px; }
.pkg-points { font-size: 22px; font-weight: 800; color: var(--accent); }
.pkg-price { font-weight: 700; }
.pkg-note { font-size: 12px; color: var(--muted); }
.cost-list { list-style: none; margin: 12px 0 0; padding: 0; display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 6px; }
.cost-list li { display: flex; align-items: center; gap: 8px; font-size: 13px; padding: 6px 8px; border: 1px solid var(--border); border-radius: 10px; }
.cost-list li strong { margin-inline-start: auto; }
.cost-list .t-icon { width: 26px; height: 26px; border-radius: 8px; display: grid; place-items: center; flex: none; }
.cost-list .t-icon svg { width: 16px; height: 16px; }
.wallet-big { font-size: 28px; font-weight: 800; color: var(--accent); margin: 4px 0 16px; }
.settings-box { display: grid; gap: 12px; max-width: 520px; padding: 14px; border: 1px solid var(--border); border-radius: 12px; }
.settings-box label { display: grid; gap: 6px; font-size: 14px; }
.settings-box label.check { display: flex; align-items: center; gap: 8px; }
.price-input.wide { width: 160px; }
.price-input { width: 110px; border: 1px solid var(--border); border-radius: 8px; padding: 6px 8px; background: var(--surface); }
.table-scroll { overflow-x: auto; }

/* ---------- Toast ---------- */
.toast { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 12px; box-shadow: var(--shadow); z-index: 50; font-size: 14px; max-width: calc(100% - 32px); }

/* ---------- Mobile ---------- */
@media (max-width: 720px) {
  .topbar { padding: 8px 12px; gap: 8px; flex-wrap: wrap; }
  .brand-name { display: none; }
  .topnav { order: 3; width: 100%; margin: 0; overflow-x: auto; }
  .pay-grid { grid-template-columns: 1fr; }
  .composer-wrap { padding: 6px 10px 10px; }
  .empty-state { margin-top: 3vh; }
}

/* ---------- Chat answers (Markdown) ---------- */
.chat-answer { min-width: 0; flex: 1; }
.md { line-height: 1.9; font-size: 15px; overflow-wrap: anywhere; }
.md > :first-child { margin-top: 0; }
.md h2, .md h3, .md h4, .md h5 { margin: 14px 0 6px; line-height: 1.5; color: var(--accent); }
.md h2 { font-size: 18px; } .md h3 { font-size: 16.5px; } .md h4, .md h5 { font-size: 15.5px; }
.md p { margin: 6px 0; }
.md ul, .md ol { margin: 6px 0; padding-inline-start: 22px; }
.md li { margin: 3px 0; }
.md li.sub { margin-inline-start: 18px; list-style: circle; }
.md hr { border: 0; border-top: 1px solid var(--border); margin: 12px 0; }
.md blockquote { margin: 8px 0; padding: 6px 12px; border-inline-start: 3px solid var(--accent); background: var(--accent-soft); border-radius: 8px; }
.md code { background: var(--surface-2); padding: 1px 6px; border-radius: 6px; font-size: 13px; }
.md-table { overflow-x: auto; margin: 8px 0; }
.md table { border-collapse: collapse; min-width: 60%; font-size: 14px; }
.md th, .md td { border: 1px solid var(--border); padding: 6px 10px; text-align: start; vertical-align: top; }
.md th { background: var(--accent-soft); font-weight: 600; }
.msg-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.mini-btn { display: inline-flex; align-items: center; gap: 5px; border: 1px solid var(--border); background: var(--surface); color: inherit; border-radius: 999px; padding: 4px 12px; font: inherit; font-size: 12.5px; cursor: pointer; }
.mini-btn:hover { border-color: var(--accent); color: var(--accent); }
.mini-btn svg { width: 14px; height: 14px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.typing { display: inline-flex; gap: 5px; padding: 6px 2px; }
.typing span { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); opacity: .35; animation: typing 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: .2s; } .typing span:nth-child(3) { animation-delay: .4s; }
@keyframes typing { 0%, 80%, 100% { opacity: .25; transform: translateY(0); } 40% { opacity: 1; transform: translateY(-3px); } }

/* ---------- Previous conversations ---------- */
.history-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.primary-btn.small { width: auto; padding: 8px 16px; font-size: 14px; text-decoration: none; display: inline-block; }
.history-search { width: 100%; margin: 12px 0; padding: 10px 14px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); color: inherit; font: inherit; }
.history-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.history-list li { position: relative; }
.history-item { display: flex; align-items: center; gap: 12px; padding: 12px 14px; padding-inline-end: 44px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); color: inherit; text-decoration: none; }
.history-item:hover, .history-item.current { border-color: var(--accent); }
.h-icon { width: 30px; height: 30px; border-radius: 9px; display: grid; place-items: center; background: var(--accent-soft); color: var(--accent); flex: none; }
.h-icon svg { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.h-title { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 500; }
.h-date { font-size: 12px; color: var(--muted); flex: none; }
.h-del { position: absolute; inset-inline-end: 10px; top: 50%; transform: translateY(-50%); border: 0; background: transparent; color: var(--muted); font-size: 20px; cursor: pointer; width: 28px; height: 28px; border-radius: 8px; }
.h-del:hover { background: var(--surface-2); color: #c0392b; }
@media (max-width: 560px) { .h-date { display: none; } }
