:root {
  --bg-0: #0c0d10;
  --bg-1: #131418;
  --panel: rgba(255, 255, 255, 0.04);
  --panel-solid: #16171c;
  --border: rgba(255, 255, 255, 0.10);
  --text: #ecebe6;
  --muted: #9a9890;
  --gold: #d8a64a;
  --gold-soft: #b98a36;
  --user-bubble: linear-gradient(135deg, #d8a64a, #b9772e);
  --user-text: #1a1206;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.45);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  color: var(--text);
  font-family: "Segoe UI", system-ui, -apple-system, "Tahoma", "Noto Sans Arabic", Arial, sans-serif;
  /* Cinematic backdrop: warm low sun + deep vignette. */
  background:
    radial-gradient(1200px 600px at 70% -10%, rgba(216, 166, 74, 0.18), transparent 60%),
    radial-gradient(900px 500px at 10% 110%, rgba(80, 90, 120, 0.12), transparent 60%),
    linear-gradient(180deg, var(--bg-1), var(--bg-0));
  background-attachment: fixed;
}

.muted { color: var(--muted); }
.error { color: #e1645a; min-height: 1.2em; margin: 0; font-size: 0.9rem; }

/* ---------- Login ---------- */
.centered { display: flex; align-items: center; justify-content: center; height: 100%; }

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 36px;
  width: 340px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  backdrop-filter: blur(14px);
  box-shadow: var(--shadow);
}
.card h1 { margin: 0; font-size: 1.5rem; letter-spacing: 0.3px; }
.card p { margin: 0 0 12px; }
.card label { font-size: 0.85rem; margin-top: 8px; color: var(--muted); }
.card input {
  padding: 11px 13px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.25);
  color: var(--text);
  font-size: 1rem;
}
.card input:focus { outline: none; border-color: var(--gold); }
.card button, .composer button {
  margin-top: 16px;
  padding: 11px 16px;
  background: linear-gradient(135deg, var(--gold), var(--gold-soft));
  color: #1a1206;
  font-weight: 600;
  border: none;
  border-radius: 10px;
  font-size: 1rem;
  cursor: pointer;
  transition: filter 0.15s ease, transform 0.05s ease;
}
.card button:hover, .composer button:hover { filter: brightness(1.07); }
.composer button:active { transform: translateY(1px); }

/* ---------- App shell ---------- */
.app { display: flex; flex-direction: column; height: 100%; }

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 22px;
  background: rgba(12, 13, 16, 0.6);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(12px);
  position: sticky;
  top: 0;
  z-index: 5;
}
.brand {
  font-weight: 700;
  letter-spacing: 0.4px;
  background: linear-gradient(135deg, #f3d79a, var(--gold));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.spacer { flex: 1; }
.quota { font-size: 0.82rem; }
.link-btn { background: none; border: none; color: var(--muted); cursor: pointer; font-size: 0.9rem; }
.link-btn:hover { color: var(--text); }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 28px 16px 8px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  max-width: 880px;
  width: 100%;
  margin: 0 auto;
}

.row { display: flex; }
.row.user { justify-content: flex-end; }
.row.assistant { justify-content: flex-start; }

.bubble {
  max-width: 78%;
  padding: 13px 17px;
  border-radius: 18px;
  line-height: 1.7;
  word-wrap: break-word;
  font-size: 0.98rem;
}
.row.user .bubble {
  background: var(--user-bubble);
  color: var(--user-text);
  border-bottom-right-radius: 5px;
}
.row.assistant .bubble {
  background: var(--panel);
  border: 1px solid var(--border);
  border-bottom-left-radius: 5px;
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
}
.bubble.typing { color: var(--muted); font-style: italic; }

/* Markdown + RTL */
.md[dir="rtl"] { text-align: right; }
.md[dir="ltr"] { text-align: left; }
.md p { margin: 0 0 0.6em; }
.md p:last-child { margin-bottom: 0; }
.md h4 { margin: 0.4em 0 0.4em; font-size: 1.02rem; color: var(--gold); }
.row.user .md h4 { color: #5a3c10; }
.md ul, .md ol { margin: 0.3em 0; padding-inline-start: 1.4em; }
.md li { margin: 0.25em 0; }
.md strong { font-weight: 700; }
.md code {
  background: rgba(0, 0, 0, 0.3);
  padding: 1px 6px;
  border-radius: 6px;
  font-size: 0.9em;
}

.gen-image {
  display: block;
  max-width: 100%;
  border-radius: 14px;
  margin-top: 10px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
}
.gen-image:first-child { margin-top: 0; }

/* ---------- Cinematic waiting loader ---------- */
.bubble.loading {
  background:
    radial-gradient(120px 80px at 90% 0%, rgba(216, 166, 74, 0.16), transparent 70%),
    var(--panel);
}
.vf-loader { display: flex; flex-direction: column; gap: 12px; width: 360px; max-width: 100%; }
.vf-loader-head { display: flex; align-items: center; gap: 10px; }
.vf-status {
  color: var(--gold);
  font-weight: 600;
  font-size: 0.95rem;
  transition: opacity 0.22s ease;
}
.vf-orb {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: radial-gradient(circle at 30% 30%, #ffe6ad, var(--gold));
  box-shadow: 0 0 0 0 rgba(216, 166, 74, 0.55);
  animation: vf-pulse 1.6s ease-out infinite;
  flex: 0 0 auto;
}
@keyframes vf-pulse {
  0% { box-shadow: 0 0 0 0 rgba(216, 166, 74, 0.5); transform: scale(0.9); }
  70% { box-shadow: 0 0 0 14px rgba(216, 166, 74, 0); transform: scale(1.1); }
  100% { box-shadow: 0 0 0 0 rgba(216, 166, 74, 0); transform: scale(0.9); }
}
.vf-aha-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  min-height: 104px; /* fixed box: no resize as cards swap */
  border: 1px solid var(--border);
  border-radius: 14px;
  background: rgba(0, 0, 0, 0.22);
  transition: opacity 0.24s ease, transform 0.24s ease;
}
.vf-aha-card.fade { opacity: 0; transform: translateY(6px); }
.vf-aha-icon {
  flex: 0 0 auto;
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border-radius: 10px;
  color: var(--gold);
  background: rgba(216, 166, 74, 0.12);
  border: 1px solid rgba(216, 166, 74, 0.25);
}
.vf-aha-icon svg {
  width: 21px; height: 21px;
  fill: none; stroke: currentColor;
  stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round;
}
.vf-aha-text { display: flex; flex-direction: column; gap: 2px; }
.vf-aha-title { font-weight: 700; font-size: 0.96rem; }
.vf-aha-sub { font-size: 0.72rem; color: var(--muted); letter-spacing: 0.3px; }
.vf-aha-body {
  font-size: 0.88rem;
  color: #d6d4cc;
  line-height: 1.55;
  margin-top: 2px;
  display: -webkit-box;
  -webkit-line-clamp: 2; /* clamp to 2 lines so the box height stays stable */
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* ---------- Lightbox ---------- */
.lightbox {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  flex-direction: column;
  background: rgba(6, 7, 9, 0.86);
  backdrop-filter: blur(10px);
}
.lightbox[hidden] { display: none; }
.lightbox-bar {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  justify-content: flex-end;
  align-items: center;
}
.lb-btn {
  min-width: 40px;
  height: 40px;
  padding: 0 12px;
  display: inline-grid;
  place-items: center;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.06);
  color: var(--text);
  font-size: 1.2rem;
  text-decoration: none;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}
.lb-btn:hover { background: rgba(216, 166, 74, 0.18); color: var(--gold); }
.lb-btn.lb-text { font-size: 0.9rem; font-weight: 600; }
.lightbox-stage {
  flex: 1;
  overflow: hidden;
  display: grid;
  place-items: center;
  touch-action: none;
}
#lb-img {
  max-width: 92vw;
  max-height: 80vh;
  border-radius: 10px;
  user-select: none;
  transition: transform 0.08s linear;
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.6);
}

/* ---------- Composer ---------- */
.composer-wrap {
  max-width: 880px;
  width: 100%;
  margin: 0 auto;
  padding: 10px 16px 18px;
}

.attachment-preview {
  display: inline-flex;
  align-items: center;
  position: relative;
  margin-bottom: 10px;
  padding: 6px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--panel);
}
.attachment-preview img {
  max-height: 90px;
  border-radius: 8px;
  display: block;
}
#attachment-remove {
  position: absolute;
  top: -8px;
  inset-inline-end: -8px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: none;
  background: #e1645a;
  color: #fff;
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: rgba(20, 21, 26, 0.7);
  backdrop-filter: blur(14px);
  box-shadow: var(--shadow);
}
.composer:focus-within { border-color: var(--gold); }

.attach-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 42px;
  height: 42px;
  border-radius: 10px;
  color: var(--muted);
  cursor: pointer;
  flex: 0 0 auto;
  transition: color 0.15s ease, background 0.15s ease;
}
.attach-btn:hover { color: var(--gold); background: rgba(255, 255, 255, 0.05); }

.composer textarea {
  flex: 1;
  resize: none;
  padding: 10px 6px;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 1rem;
  font-family: inherit;
  max-height: 180px;
  line-height: 1.6;
}
.composer textarea:focus { outline: none; }
.composer textarea::placeholder { color: var(--muted); }
.composer button { margin-top: 0; align-self: flex-end; flex: 0 0 auto; }
.composer button:disabled { opacity: 0.5; cursor: not-allowed; }

/* ---------- Plan / billing UI ---------- */
.plan-btn {
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 5px 14px;
  color: var(--gold);
}
.plan-btn:hover { border-color: var(--gold); }

.trial-banner {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
  flex-wrap: wrap;
  padding: 9px 16px;
  background: linear-gradient(90deg, rgba(216,166,74,0.16), rgba(185,119,46,0.10));
  border-bottom: 1px solid var(--border);
  color: var(--text);
  font-size: 0.9rem;
}
.trial-banner.urgent { background: linear-gradient(90deg, rgba(225,100,90,0.18), rgba(216,166,74,0.12)); }
.banner-cta {
  background: var(--user-bubble);
  color: var(--user-text);
  border: none;
  border-radius: 999px;
  padding: 5px 16px;
  font-weight: 600;
  cursor: pointer;
}
.banner-cta:hover { filter: brightness(1.05); }

.quality-btn {
  align-self: flex-end;
  flex: 0 0 auto;
  margin: 0 2px;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: transparent;
  color: var(--muted);
  font-weight: 700;
  font-size: 0.78rem;
  cursor: pointer;
}
.quality-btn.active { border-color: var(--gold); color: var(--gold); background: rgba(216,166,74,0.12); }

/* Inline upgrade CTA inside an assistant bubble */
.upgrade-cta {
  margin-top: 10px;
  background: var(--user-bubble);
  color: var(--user-text);
  border: none;
  border-radius: 999px;
  padding: 8px 18px;
  font-weight: 600;
  cursor: pointer;
}
.upgrade-cta:hover { filter: brightness(1.05); }

/* ---------- Plans modal ---------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
  padding: 20px;
}
.modal[hidden] { display: none; }
.modal-card {
  position: relative;
  background: var(--panel-solid);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: var(--shadow);
  width: min(880px, 100%);
  max-height: 90vh;
  overflow-y: auto;
  padding: 28px;
}
.modal-close {
  position: absolute;
  top: 12px;
  left: 14px;
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 1.6rem;
  cursor: pointer;
  line-height: 1;
}
.modal-title { margin: 0 0 4px; font-size: 1.4rem; }
.modal-sub { margin: 0 0 18px; color: var(--muted); font-size: 0.92rem; }
.modal-note { margin: 16px 0 0; color: var(--muted); font-size: 0.82rem; text-align: center; }

.plans-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 14px;
}
.plan-card {
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  background: rgba(255,255,255,0.02);
}
.plan-card.featured { border-color: var(--gold); box-shadow: 0 0 0 1px var(--gold-soft) inset; }
.plan-card.current { outline: 2px solid var(--gold-soft); }
.plan-name { font-size: 1.15rem; font-weight: 700; }
.plan-price { font-size: 1.5rem; font-weight: 800; color: var(--gold); }
.plan-price small { font-size: 0.8rem; color: var(--muted); font-weight: 500; }
.plan-feats { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-direction: column; gap: 5px; font-size: 0.86rem; color: var(--text); }
.plan-feats li::before { content: "✓ "; color: var(--gold); }
.plan-badge { align-self: flex-start; font-size: 0.72rem; background: rgba(216,166,74,0.18); color: var(--gold); padding: 2px 9px; border-radius: 999px; }
.plan-cta {
  margin-top: auto;
  padding: 10px;
  border: none;
  border-radius: 10px;
  background: var(--user-bubble);
  color: var(--user-text);
  font-weight: 700;
  cursor: pointer;
}
.plan-cta:disabled { opacity: 0.5; cursor: default; background: var(--panel); color: var(--muted); }
.plan-cta.secondary { background: transparent; border: 1px solid var(--border); color: var(--text); }
.plan-cta:hover:not(:disabled) { filter: brightness(1.05); }

/* ---------- Admin dashboard ---------- */
.admin-wrap { max-width: 1100px; margin: 0 auto; padding: 22px 18px 60px; width: 100%; }
.admin-h { margin: 26px 0 10px; font-size: 1.1rem; }
.admin-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
}
.admin-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 16px;
  backdrop-filter: blur(10px);
}
.admin-card-wide { grid-column: 1 / -1; color: var(--muted); font-size: 0.9rem; }
.admin-card-val { font-size: 1.5rem; font-weight: 800; color: var(--gold); }
.admin-card-label { margin-top: 4px; font-size: 0.85rem; }
.admin-card-sub { margin-top: 2px; font-size: 0.76rem; color: var(--muted); }

.admin-table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: 12px; }
.admin-table { width: 100%; border-collapse: collapse; font-size: 0.88rem; }
.admin-table th, .admin-table td { padding: 10px 12px; text-align: right; white-space: nowrap; }
.admin-table thead th { background: rgba(255,255,255,0.04); color: var(--muted); font-weight: 600; }
.admin-table tbody tr { border-top: 1px solid var(--border); }
.admin-table tbody tr.near-limit { background: rgba(225,100,90,0.10); }
.admin-table td.pos { color: #7bbf6a; }
.admin-table td.neg { color: #e1645a; }
