:root {
  --font: 'Cairo', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --font-ar: 'Amiri', 'Traditional Arabic', serif;
  --r-sm: 10px; --r: 14px; --r-lg: 20px;
  --ease: cubic-bezier(.4, 0, .2, 1);
}
[data-theme="dark"] {
  --bg: #0b1120; --bg2: #0f172a; --card: #131c31; --card2: #18233d; --input: #0d1526;
  --line: rgba(148, 163, 184, .14); --line2: rgba(148, 163, 184, .26);
  --text: #eef2f7; --text2: #a5b1c4; --muted: #71809a;
  --gold: #e3b95c; --gold2: #c99a3a; --gold-soft: rgba(227, 185, 92, .14);
  --green: #34d399; --green-soft: rgba(52, 211, 153, .13);
  --red: #f87171; --red-soft: rgba(248, 113, 113, .13);
  --blue: #60a5fa;
  --shadow: 0 10px 30px rgba(0, 0, 0, .35);
  --tg-bg: #0e1621; --tg-bubble: #182533; --tg-btn: #22303f;
}
[data-theme="light"] {
  --bg: #f6f3ec; --bg2: #efe9dd; --card: #ffffff; --card2: #faf7f1; --input: #f7f3ea;
  --line: rgba(60, 50, 30, .1); --line2: rgba(60, 50, 30, .2);
  --text: #1f2430; --text2: #4c5566; --muted: #7c8496;
  --gold: #b7862a; --gold2: #9a6e1c; --gold-soft: rgba(183, 134, 42, .12);
  --green: #0f9f6e; --green-soft: rgba(15, 159, 110, .1);
  --red: #dc2626; --red-soft: rgba(220, 38, 38, .08);
  --blue: #2563eb;
  --shadow: 0 10px 30px rgba(60, 40, 10, .08);
  --tg-bg: #c9d8c2; --tg-bubble: #ffffff; --tg-btn: rgba(0, 0, 0, .28);
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html, body { background: var(--bg); color: var(--text); font-family: var(--font); line-height: 1.6; }
body { min-height: 100vh; overflow-x: hidden; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
code { font-family: ui-monospace, Consolas, monospace; font-size: .85em; }
.hidden { display: none !important; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: .82rem; }
.mt { margin-top: 1rem; }

/* ===== Layout ===== */
.app { display: grid; grid-template-columns: 250px 1fr; min-height: 100vh; }
.sidebar {
  position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column; gap: 1.2rem;
  padding: 1.25rem 1rem; background: var(--bg2); border-inline-start: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: .7rem; padding: .2rem .4rem; }
.brand-mark {
  width: 44px; height: 44px; border-radius: 13px; display: grid; place-items: center; font-size: 1.4rem;
  background: linear-gradient(140deg, var(--gold), var(--gold2)); box-shadow: 0 6px 18px var(--gold-soft);
}
.brand-name { font-weight: 800; font-size: 1.02rem; }
.brand-sub { font-size: .76rem; color: var(--text2); display: flex; align-items: center; gap: .35rem; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); display: inline-block; }
.dot.on { background: var(--green); box-shadow: 0 0 0 3px var(--green-soft); }
.dot.off { background: var(--red); }
.nav { display: flex; flex-direction: column; gap: .25rem; }
.nav button {
  display: flex; align-items: center; gap: .7rem; padding: .7rem .85rem; border: 0; border-radius: var(--r-sm);
  background: transparent; color: var(--text2); font-weight: 600; text-align: start; transition: .15s var(--ease);
}
.nav button:hover { background: var(--card); color: var(--text); }
.nav button.active { background: var(--gold-soft); color: var(--gold); }
.nav .ico { width: 1.4rem; text-align: center; }
.sidebar-foot { margin-top: auto; display: flex; flex-direction: column; gap: .6rem; }
.channel-chip {
  font-size: .8rem; padding: .55rem .75rem; border-radius: var(--r-sm); background: var(--card);
  border: 1px solid var(--line); color: var(--text2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.main { padding: 1.6rem clamp(1rem, 3vw, 2.4rem) 6rem; min-width: 0; }
.view { display: none; animation: fade .25s var(--ease); }
.view.active { display: block; }
@keyframes fade { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
.view-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 1rem; margin-bottom: 1.3rem; flex-wrap: wrap; }
.view-head h1 { font-size: 1.55rem; font-weight: 800; line-height: 1.3; }
.head-actions { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }

/* ===== Cards & stats ===== */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--r-lg); padding: 1.15rem; margin-bottom: 1rem; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: .6rem; margin-bottom: .8rem; flex-wrap: wrap; }
.card h2 { font-size: 1.02rem; font-weight: 700; }
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: .8rem; margin-bottom: 1rem; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: var(--r-lg); padding: 1rem 1.1rem; position: relative; overflow: hidden; }
.stat::after { content: ""; position: absolute; inset-inline-end: -30px; top: -30px; width: 90px; height: 90px; border-radius: 50%; background: var(--gold-soft); }
.stat-label { font-size: .8rem; color: var(--text2); }
.stat-value { font-size: 1.7rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 1rem; }
.grid-publisher { display: grid; grid-template-columns: minmax(0, 1.6fr) minmax(0, 1fr); gap: 1rem; align-items: start; }
.tips ul { padding-inline-start: 1.1rem; color: var(--text2); display: grid; gap: .4rem; margin-top: .6rem; font-size: .9rem; }

/* ===== Buttons & inputs ===== */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem; border: 1px solid transparent;
  border-radius: var(--r-sm); padding: .6rem 1.05rem; font-weight: 700; font-size: .9rem; transition: .15s var(--ease); white-space: nowrap;
}
a.btn { text-decoration: none; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: linear-gradient(140deg, var(--gold), var(--gold2)); color: #1a1406; box-shadow: 0 6px 16px var(--gold-soft); }
.btn-primary:hover:not(:disabled) { filter: brightness(1.07); transform: translateY(-1px); }
.btn-ghost { background: var(--card2); border-color: var(--line); color: var(--text); }
.btn-ghost:hover:not(:disabled) { border-color: var(--gold); color: var(--gold); }
.btn-danger { background: var(--red-soft); color: var(--red); border-color: transparent; }
.btn-success { background: var(--green-soft); color: var(--green); }
.btn-sm { padding: .38rem .7rem; font-size: .8rem; }
.btn-lg { padding: .85rem 1.2rem; font-size: 1rem; }
.btn-block { width: 100%; }
.linklike { background: none; border: 0; color: var(--gold); font-weight: 700; text-decoration: underline; }
input[type=text], input[type=url], input[type=number], input[type=password], select, textarea {
  width: 100%; background: var(--input); border: 1px solid var(--line2); border-radius: var(--r-sm);
  padding: .65rem .8rem; outline: none; transition: border-color .15s, box-shadow .15s;
}
input:focus, select:focus, textarea:focus { border-color: var(--gold); box-shadow: 0 0 0 3px var(--gold-soft); }
textarea { resize: vertical; min-height: 76px; }
textarea.ar, input.ar { font-family: var(--font-ar); font-size: 1.12rem; line-height: 1.9; }
.label { display: block; font-size: .85rem; font-weight: 700; color: var(--text2); margin: .9rem 0 .4rem; }
.field { margin-bottom: .9rem; }
.field-head { display: flex; justify-content: space-between; align-items: baseline; gap: .5rem; margin-bottom: .35rem; }
.field-head label { font-weight: 700; font-size: .88rem; }
.counter { font-size: .72rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.counter.warn { color: var(--red); }
.help { font-size: .76rem; color: var(--muted); margin-top: .25rem; }
.row-actions { display: flex; gap: .5rem; margin-top: .7rem; flex-wrap: wrap; }
.copy-row { display: flex; align-items: center; gap: .5rem; background: var(--input); padding: .5rem .7rem; border-radius: var(--r-sm); border: 1px solid var(--line); }
.copy-row code { flex: 1; direction: ltr; text-align: left; overflow: hidden; text-overflow: ellipsis; }

/* ===== Chips / tabs ===== */
.chips { display: flex; flex-wrap: wrap; gap: .5rem; }
.chip {
  border: 1px solid var(--line2); background: var(--card2); border-radius: 999px; padding: .45rem .9rem;
  font-weight: 600; font-size: .86rem; transition: .15s var(--ease); display: inline-flex; gap: .35rem; align-items: center;
}
.chip:hover { border-color: var(--gold); }
.chip.active { background: var(--gold-soft); border-color: var(--gold); color: var(--gold); }
.chip .badge { font-size: .66rem; background: var(--gold); color: #1a1406; border-radius: 999px; padding: 0 .4rem; }
.chips.small .chip { padding: .3rem .7rem; font-size: .8rem; }
.theme-chip { padding: .35rem .8rem .35rem .45rem; }
.swatch { width: 22px; height: 22px; border-radius: 50%; border: 2px solid var(--line2); }
.sw-sky { background: linear-gradient(160deg, #cfe6f0, #9aa6a4); }
.sw-beige { background: linear-gradient(160deg, #ebe6df, #cca880); }
.sw-royal { background: linear-gradient(160deg, #16305a, #c9a24a); }
.sw-emerald { background: linear-gradient(160deg, #07402f, #d4af5a); }
.tabs { display: flex; gap: .3rem; background: var(--input); padding: .3rem; border-radius: var(--r); margin-bottom: 1rem; overflow-x: auto; }
.tabs button { flex: 1; border: 0; background: transparent; padding: .5rem .7rem; border-radius: var(--r-sm); font-weight: 700; font-size: .84rem; color: var(--text2); white-space: nowrap; }
.tabs button.active { background: var(--card); color: var(--gold); box-shadow: var(--shadow); }
.segmented { display: grid; grid-template-columns: repeat(4, 1fr); gap: .3rem; background: var(--input); padding: .3rem; border-radius: var(--r); }
.segmented button { border: 0; background: transparent; padding: .55rem .3rem; border-radius: var(--r-sm); font-weight: 700; font-size: .85rem; color: var(--text2); }
.segmented button.active { background: var(--card); color: var(--gold); box-shadow: var(--shadow); }

/* ===== Studio ===== */
.studio { display: grid; grid-template-columns: minmax(0, 1.25fr) minmax(320px, .95fr); gap: 1.2rem; align-items: start; }
.studio > *, .grid-publisher > *, .grid-2 > *, .app > * { min-width: 0; }
.studio-preview { position: sticky; top: 1rem; display: flex; flex-direction: column; gap: .8rem; }
.goal-row { display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; }
.goal-row input { max-width: 150px; font-weight: 800; font-size: 1.1rem; }
.phone { background: var(--tg-bg); border-radius: 24px; padding: .9rem; border: 1px solid var(--line); box-shadow: var(--shadow); }
.phone-top { display: flex; align-items: center; gap: .5rem; font-weight: 700; font-size: .9rem; margin-bottom: .6rem; color: var(--text); }
[data-theme="light"] .phone-top { color: #1f2a1c; }
.avatar { width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center; background: var(--gold-soft); }
.bubble { background: var(--tg-bubble); border-radius: 16px; overflow: hidden; }
.pv-img-wrap { position: relative; aspect-ratio: 1; background: var(--card2); }
.pv-img-wrap img { width: 100%; height: 100%; object-fit: cover; display: block; transition: opacity .2s; }
.pv-loading { position: absolute; inset: 0; display: grid; place-items: center; background: rgba(0,0,0,.18); opacity: 0; pointer-events: none; transition: opacity .15s; }
.pv-loading.on { opacity: 1; }
.spinner { width: 34px; height: 34px; border: 3px solid rgba(255,255,255,.35); border-top-color: #fff; border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.pv-caption { padding: .7rem .85rem .85rem; font-size: .88rem; white-space: pre-wrap; line-height: 1.75; max-height: 360px; overflow: auto; }
.pv-caption code { background: rgba(127,127,127,.18); padding: 0 .25rem; border-radius: 4px; }
.pv-tools { display: flex; gap: .4rem; padding: .6rem .85rem 0; flex-wrap: wrap; }
.pv-tools .btn { background: rgba(127,127,127,.15); border-color: transparent; }
.pv-editor { padding: .6rem .85rem .85rem; display: grid; gap: .45rem; }
.pv-editor textarea { min-height: 300px; font-size: .9rem; line-height: 1.8; background: rgba(0,0,0,.18); color: inherit; }
[data-theme="light"] .pv-editor textarea { background: #f7f7f2; }
.pv-counter { font-size: .72rem; color: var(--muted); text-align: end; }
.pv-counter.warn { color: var(--red); }
.caption-note { background: var(--gold-soft); border: 1px solid var(--gold); color: var(--text); border-radius: var(--r-sm); padding: .6rem .8rem; font-size: .84rem; margin-bottom: .9rem; }
.pv-buttons { display: grid; gap: .3rem; margin-top: .35rem; }
.pv-buttons .row { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: .3rem; }
.pv-buttons span { background: var(--tg-btn); color: #fff; text-align: center; padding: .5rem; border-radius: 10px; font-size: .82rem; font-weight: 600; }
.sim { margin: 0; }
.sim input[type=range] { width: 100%; accent-color: var(--gold); }
.sim-row { display: flex; align-items: center; gap: .6rem; }
.sim-row .label { margin: 0; white-space: nowrap; }
.sim-row input { max-width: 110px; }
.edit-banner { background: var(--gold-soft); border: 1px solid var(--gold); color: var(--gold); padding: .6rem .9rem; border-radius: var(--r); margin-bottom: 1rem; font-weight: 700; }

/* ===== Campaigns ===== */
.camp-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 1rem; }
.camp-list.compact { grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: .7rem; }
.camp { background: var(--card); border: 1px solid var(--line); border-radius: var(--r-lg); padding: 1rem; display: flex; flex-direction: column; gap: .7rem; }
.compact .camp { background: var(--card2); }
.camp-top { display: flex; justify-content: space-between; gap: .5rem; align-items: flex-start; }
.camp-title { font-family: var(--font-ar); font-size: 1.15rem; font-weight: 700; line-height: 1.6; }
.camp-meta { font-size: .78rem; color: var(--muted); }
.status { font-size: .72rem; font-weight: 800; padding: .15rem .6rem; border-radius: 999px; white-space: nowrap; }
.status.active { background: var(--green-soft); color: var(--green); }
.status.completed { background: var(--gold-soft); color: var(--gold); }
.status.closed { background: var(--red-soft); color: var(--red); }
.progress { height: 9px; background: var(--input); border-radius: 999px; overflow: hidden; }
.progress > div { height: 100%; background: linear-gradient(90deg, var(--gold2), var(--gold)); border-radius: 999px; transition: width .5s var(--ease); }
.progress.done > div { background: linear-gradient(90deg, #10b981, var(--green)); }
.camp-nums { display: flex; justify-content: space-between; font-size: .85rem; color: var(--text2); font-variant-numeric: tabular-nums; }
.camp-nums b { color: var(--text); font-size: 1.15rem; }
.camp-actions { display: flex; flex-wrap: wrap; gap: .4rem; }
.empty { text-align: center; padding: 2.5rem 1rem; color: var(--muted); grid-column: 1 / -1; }
.empty .big { font-size: 2.4rem; }
.top-list { display: grid; gap: .35rem; margin-top: .6rem; }
.top-list div { display: flex; justify-content: space-between; background: var(--card2); padding: .45rem .7rem; border-radius: var(--r-sm); font-size: .9rem; }

/* ===== Publisher ===== */
.font-preview { margin-top: .4rem; padding: .6rem .8rem; border-radius: var(--r-sm); background: var(--input); color: var(--text2); min-height: 2.4rem; font-size: 1.05rem; }
.dropzone { border: 2px dashed var(--line2); border-radius: var(--r); padding: 1.3rem; text-align: center; transition: .15s; background: var(--input); }
.dropzone.over { border-color: var(--gold); background: var(--gold-soft); }
.thumbs { display: grid; grid-template-columns: repeat(auto-fill, minmax(86px, 1fr)); gap: .5rem; margin-top: .7rem; }
.thumb { position: relative; aspect-ratio: 1; border-radius: var(--r-sm); overflow: hidden; background: var(--card2); border: 1px solid var(--line); }
.thumb img { width: 100%; height: 100%; object-fit: cover; }
.thumb button { position: absolute; top: 4px; inset-inline-start: 4px; width: 24px; height: 24px; border-radius: 50%; border: 0; background: rgba(0,0,0,.65); color: #fff; font-size: .75rem; }
.thumb .n { position: absolute; bottom: 4px; inset-inline-end: 4px; background: rgba(0,0,0,.6); color: #fff; font-size: .7rem; padding: 0 .35rem; border-radius: 6px; }
.url-row { display: flex; gap: .5rem; margin-top: .6rem; }
.table-editor-wrap { overflow-x: auto; }
.table-editor { border-collapse: separate; border-spacing: 4px; width: 100%; }
.table-editor input { min-width: 90px; padding: .45rem .55rem; }
.table-editor tr:first-child input { font-weight: 800; border-color: var(--gold); }

/* ===== Status / modal / toasts / login ===== */
.channel-status { margin-top: .8rem; font-size: .9rem; }
.check-list { display: grid; gap: .35rem; }
.check-list div { display: flex; gap: .5rem; align-items: center; }
.ok { color: var(--green); } .bad { color: var(--red); }
.modal { position: fixed; inset: 0; background: rgba(4, 8, 18, .6); backdrop-filter: blur(4px); display: grid; place-items: center; z-index: 100; padding: 1rem; }
.modal-card { background: var(--card); border: 1px solid var(--line); border-radius: var(--r-lg); padding: 1.3rem; width: min(520px, 100%); max-height: 88vh; overflow: auto; position: relative; box-shadow: var(--shadow); }
.modal-close { position: absolute; top: .7rem; inset-inline-start: .7rem; border: 0; background: var(--card2); width: 32px; height: 32px; border-radius: 50%; }
.modal-card h3 { margin-bottom: .6rem; }
.toasts { position: fixed; bottom: 1.2rem; inset-inline-start: 50%; transform: translateX(-50%); display: grid; gap: .5rem; z-index: 200; width: min(440px, calc(100% - 2rem)); }
[dir=rtl] .toasts { transform: translateX(50%); }
.toast { background: var(--card); border: 1px solid var(--line2); border-inline-start: 4px solid var(--gold); border-radius: var(--r); padding: .75rem 1rem; box-shadow: var(--shadow); font-weight: 600; font-size: .9rem; animation: fade .2s; }
.toast.success { border-inline-start-color: var(--green); }
.toast.error { border-inline-start-color: var(--red); }
.toast a { color: var(--gold); }
.login { position: fixed; inset: 0; z-index: 300; background: radial-gradient(circle at 50% 20%, var(--gold-soft), transparent 60%), var(--bg); display: grid; place-items: center; padding: 1rem; }
.login-card { background: var(--card); border: 1px solid var(--line); border-radius: 24px; padding: 2rem 1.6rem; width: min(380px, 100%); text-align: center; display: grid; gap: .8rem; box-shadow: var(--shadow); }
.login-logo { font-size: 2.6rem; }
.login-card h1 { font-size: 1.4rem; }
.login-error { color: var(--red); font-size: .85rem; min-height: 1.2em; }
.tabbar { display: none; }
.mobile-bar { display: none; }
.user-chip { font-size: .8rem; color: var(--text2); padding: .2rem .3rem; }
.verify-code { font-size: 2.6rem; font-weight: 800; letter-spacing: .5rem; color: var(--gold); direction: ltr; margin: .2rem 0 .6rem; font-variant-numeric: tabular-nums; }
.login-steps, .link-steps { text-align: start; padding-inline-start: 1.2rem; color: var(--text2); font-size: .88rem; display: grid; gap: .25rem; margin: .8rem 0; }
.login-wait { display: flex; align-items: center; justify-content: center; gap: .5rem; color: var(--text2); font-size: .88rem; }
.login-wait .spinner { width: 16px; height: 16px; border-width: 2px; border-color: var(--line2); border-top-color: var(--gold); }
.channel-list { display: grid; gap: .6rem; }
.channel-row { display: flex; align-items: center; gap: .8rem; padding: .75rem .9rem; border-radius: var(--r); background: var(--card2); border: 1px solid var(--line); flex-wrap: wrap; }
.channel-row.active { border-color: var(--gold); background: var(--gold-soft); }
.channel-row .ch-avatar { width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; background: var(--input); font-size: 1.2rem; flex-shrink: 0; }
.channel-row .ch-info { flex: 1; min-width: 150px; }
.channel-row .ch-title { font-weight: 800; }
.channel-row .ch-sub { font-size: .78rem; color: var(--muted); }
.channel-row .ch-actions { display: flex; gap: .4rem; flex-wrap: wrap; }
.brand-mark.sm { width: 34px; height: 34px; font-size: 1.05rem; border-radius: 10px; flex-shrink: 0; }

/* ===== Responsive ===== */
@media (max-width: 1100px) {
  .studio { grid-template-columns: minmax(0, 1fr); }
  .studio-preview { position: static; order: -1; }
  .phone { max-width: 460px; margin-inline: auto; width: 100%; }
  .grid-publisher { grid-template-columns: 1fr; }
}
@media (max-width: 860px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .sidebar { display: none; }
  .mobile-bar { display: flex; align-items: center; gap: .6rem; margin-bottom: 1rem; }
  .main { padding: 1rem 1rem calc(5.5rem + env(safe-area-inset-bottom)); }
  .stats { grid-template-columns: 1fr 1fr; }
  .grid-2 { grid-template-columns: 1fr; }
  .view-head h1 { font-size: 1.3rem; }
  .camp-list { grid-template-columns: 1fr; }
  .tabbar {
    display: grid; grid-template-columns: repeat(5, 1fr); position: fixed; bottom: 0; inset-inline: 0; z-index: 50;
    background: var(--bg2); border-top: 1px solid var(--line); padding: .35rem .3rem calc(.35rem + env(safe-area-inset-bottom));
  }
  .tabbar button { border: 0; background: none; color: var(--muted); font-size: .68rem; font-weight: 700; display: grid; justify-items: center; gap: .1rem; padding: .3rem 0; border-radius: var(--r-sm); }
  .tabbar button span { font-size: 1.2rem; }
  .tabbar button.active { color: var(--gold); background: var(--gold-soft); }
  .segmented { grid-template-columns: repeat(2, 1fr); }
  #view-studio { padding-bottom: 4.5rem; }
  .studio-preview .publish-bar { position: fixed; bottom: calc(4.4rem + env(safe-area-inset-bottom)); inset-inline: 1rem; z-index: 40; }
  .publish-bar .btn { box-shadow: 0 8px 24px rgba(0,0,0,.35); }
}
