/* Mobile-first (phone se post karne ke liye), light + dark mode */
:root {
  --bg: #f4f6f5;
  --surface: #ffffff;
  --text: #17201d;
  --muted: #5b6662;
  --border: #d8dfdc;
  --primary: #1e6f5c;
  --on-primary: #ffffff;
  --danger: #b3261e;
  --live: #d32f2f;
  --ok-bg: #d6f2e6;
  --ok-text: #0d4a3b;
  --draft-bg: #ece6d9;
  --draft-text: #5a4a1f;
  --radius: 12px;
  --sidebar: 220px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101513;
    --surface: #1a211e;
    --text: #e3ebe7;
    --muted: #9aa8a2;
    --border: #2d3733;
    --primary: #8ad6be;
    --on-primary: #00382c;
    --danger: #ffb4ab;
    --live: #ff6b6b;
    --ok-bg: #12382d;
    --ok-text: #a6f2da;
    --draft-bg: #3b3420;
    --draft-text: #f0dfa8;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

h1, h2 { margin: 0 0 12px; line-height: 1.2; }
h1 { font-size: 1.25rem; }
h2 { font-size: 1.1rem; }
code { font-size: 0.9em; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.error { color: var(--danger); }

/* ---------- controls ---------- */
input, select, textarea {
  width: 100%;
  font: inherit;
  font-size: 16px; /* iOS zoom na kare */
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  margin-top: 4px;
}
textarea { resize: vertical; }
/* Kick-off time: two selects, always 24 h ("21 : 00") */
.field { margin-bottom: 14px; font-weight: 500; }
.time24 { display: flex; align-items: center; gap: 6px; }
.time24 select { flex: 1; min-width: 0; }
.time24 span { margin-top: 4px; font-weight: 700; }
input[type="checkbox"], input[type="radio"] { width: 20px; height: 20px; margin: 0; accent-color: var(--primary); flex: none; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: 0; }

label { display: block; margin-bottom: 14px; font-weight: 500; }
label.check { display: flex; gap: 10px; align-items: center; font-weight: 400; }
fieldset { border: 1px solid var(--border); border-radius: var(--radius); margin: 0 0 14px; padding: 12px; }
legend { padding: 0 6px; font-weight: 600; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 40px; padding: 8px 14px;
  border: 1px solid var(--border); border-radius: 999px;
  background: var(--surface); color: var(--text);
  font: inherit; font-weight: 600; text-decoration: none; cursor: pointer;
}
.btn:disabled { opacity: 0.55; cursor: progress; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
.btn.danger { color: var(--danger); }
.btn.live { color: var(--live); }
.btn.ghost { background: transparent; }
.btn.small { min-height: 34px; padding: 4px 12px; font-size: 0.85rem; }
.btn.block { width: 100%; }
.icon-btn { border: 0; background: transparent; color: inherit; font-size: 1.5rem; padding: 4px 10px; cursor: pointer; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
}

.badge {
  display: inline-block; padding: 2px 8px; border-radius: 6px;
  font-size: 0.75rem; font-weight: 700; background: var(--border); color: var(--text);
}
.badge.ok { background: var(--ok-bg); color: var(--ok-text); }
.badge.draft { background: var(--draft-bg); color: var(--draft-text); }
.badge.live { background: var(--live); color: #fff; }
.badge.article { background: var(--draft-bg); color: var(--draft-text); }
/* "What is this?" Article / Match choice */
fieldset.kind label.check { align-items: flex-start; padding: 8px 0; min-height: 44px; }
fieldset.kind label.check input { margin-top: 2px; }

/* ---------- login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 380px; }

/* ---------- layout ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 8px;
  padding: 8px 12px; padding-top: max(8px, env(safe-area-inset-top));
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.topbar h1 { flex: 1; margin: 0; }

.sidebar {
  position: fixed; top: 0; bottom: 0; left: 0; z-index: 40;
  width: var(--sidebar); padding: 16px 8px;
  background: var(--surface); border-right: 1px solid var(--border);
  transform: translateX(-100%); transition: transform 0.2s ease;
}
.sidebar a {
  display: block; padding: 12px 14px; border-radius: 999px;
  color: var(--text); text-decoration: none; font-weight: 600;
}
.sidebar a.active { background: var(--ok-bg); color: var(--ok-text); }
.scrim { display: none; position: fixed; inset: 0; z-index: 30; background: rgb(0 0 0 / 0.4); }
body.nav-open .sidebar { transform: none; }
body.nav-open .scrim { display: block; }

.main { padding: 16px; max-width: 1100px; margin: 0 auto; padding-bottom: 96px; }

@media (min-width: 900px) {
  .sidebar { transform: none; top: 0; padding-top: 72px; }
  .topbar { padding-left: calc(var(--sidebar) + 16px); }
  #menuBtn, .scrim { display: none !important; }
  .main { margin-left: var(--sidebar); max-width: none; padding: 24px; }
}

/* ---------- filters ---------- */
.filters { display: grid; gap: 8px; grid-template-columns: 1fr; margin-bottom: 8px; }
.filters input, .filters select { margin: 0; }
@media (min-width: 700px) {
  .filters { grid-template-columns: 2fr repeat(4, 1fr) auto; align-items: center; }
}

/* ---------- tables (phone par cards) ---------- */
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { text-align: left; padding: 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
.table th { font-size: 0.8rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.03em; }
.table td.actions { display: flex; flex-wrap: wrap; gap: 6px; }
.order-input { width: 80px; margin: 0; padding: 6px 8px; }

@media (max-width: 699px) {
  .table thead { display: none; }
  .table, .table tbody, .table tr, .table td { display: block; width: 100%; }
  .table tr {
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
    margin-bottom: 12px; padding: 6px 4px;
  }
  .table td { border: 0; padding: 6px 10px; }
  .table td[data-label]::before {
    content: attr(data-label); display: block;
    font-size: 0.75rem; color: var(--muted); text-transform: uppercase;
  }
  .table td.actions::before { width: 100%; }
}

/* ---------- forms ---------- */
.form { max-width: 820px; }
.grid-2 { display: grid; gap: 0 12px; grid-template-columns: 1fr; }
@media (min-width: 600px) { .grid-2 { grid-template-columns: 1fr 1fr; } }
.form-actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.form-actions.sticky {
  position: sticky; bottom: 0; z-index: 5;
  margin: 0 -16px -16px; padding: 12px 16px; padding-bottom: max(12px, env(safe-area-inset-bottom));
  background: var(--surface); border-top: 1px solid var(--border); border-radius: 0 0 var(--radius) var(--radius);
}
.error-list {
  margin: 0 0 14px; padding: 10px 10px 10px 28px;
  border: 1px solid var(--danger); border-radius: 8px; color: var(--danger);
}
.preview { display: block; max-width: 100%; max-height: 240px; margin: 10px 0; border-radius: 8px; }
progress { width: 100%; margin-top: 8px; }

.chip-group { display: flex; flex-wrap: wrap; gap: 8px; }
.chip-check {
  display: inline-flex; align-items: center; gap: 6px; margin: 0;
  padding: 6px 12px; border: 1px solid var(--border); border-radius: 999px; font-weight: 500;
}
.chip-check:has(input:checked) { background: var(--ok-bg); color: var(--ok-text); border-color: transparent; }

.page-list { display: grid; gap: 8px; margin-bottom: 16px; }
.page-item {
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
  padding: 14px; text-align: left; font: inherit; color: inherit; cursor: pointer;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
}
.page-item.active { outline: 2px solid var(--primary); }

dialog {
  width: min(420px, calc(100vw - 32px));
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--text); padding: 16px;
}
dialog::backdrop { background: rgb(0 0 0 / 0.5); }
/* Fallback for old browsers without showModal(): the box still appears, centred on top */
dialog.fallback-open { display: block; position: fixed; inset: 0; margin: auto; height: fit-content; z-index: 200; box-shadow: 0 0 0 100vmax rgb(0 0 0 / 0.5); }
#confirmMessage { white-space: pre-line; overflow-wrap: anywhere; }
/* Home page: section type as two big choices */
.type-choice { display: grid; gap: 8px; grid-template-columns: 1fr; border: 0; padding: 0; margin: 0 0 14px; }
.type-choice legend { font-weight: 500; margin-bottom: 6px; }
.choice { display: flex; gap: 10px; align-items: flex-start; padding: 12px; border: 1px solid var(--border); border-radius: 10px; cursor: pointer; margin: 0; font-weight: 400; }
.choice input { width: 20px; height: 20px; margin: 2px 0 0; accent-color: var(--primary); flex: none; }
.choice:has(input:checked) { border-color: var(--primary); outline: 1px solid var(--primary); }
@media (min-width: 640px) { .type-choice { grid-template-columns: 1fr 1fr; } }
/* Home page: article search results / chosen list */
.pick-list { list-style: none; margin: 4px 0 12px; padding: 0; }
.pick-list li { display: flex; gap: 6px; align-items: center; padding: 6px 0; border-bottom: 1px solid var(--border); }
.pick-list li > span { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.pick-list.scroll { max-height: 360px; overflow-y: auto; border: 1px solid var(--border); border-radius: 8px; padding: 0 8px; }
.pick-list.scroll:empty { display: none; }
#confirmOk.danger-fill { background: var(--danger); border-color: var(--danger); color: #fff; }

.toast {
  position: fixed; left: 50%; bottom: max(16px, env(safe-area-inset-bottom)); z-index: 100;
  transform: translateX(-50%); max-width: calc(100vw - 32px);
  padding: 12px 16px; border-radius: 10px;
  background: #202624; color: #fff; box-shadow: 0 4px 16px rgb(0 0 0 / 0.25); pointer-events: none; }  /* never blocks the button under it */
.toast.error { background: var(--danger); color: #fff; }
@media (prefers-color-scheme: dark) { .toast.error { color: #410002; } }

.hide-mobile { display: none; }
@media (min-width: 700px) { .hide-mobile { display: inline; } }

/* ---------- categories tree ---------- */
.toolbar-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 8px; }
.toolbar-row .spacer { flex: 1; }
.tree-card { padding: 8px; }
.tree, .tree-children { list-style: none; margin: 0; padding: 0; }
.tree-children { margin-left: 18px; padding-left: 10px; border-left: 2px solid var(--border); }
.tree-row {
  display: grid; grid-template-columns: 32px 1fr; gap: 6px 8px; align-items: center;
  padding: 8px 4px; border-bottom: 1px solid var(--border);
}
.tree-row.inactive .tree-name { opacity: 0.55; text-decoration: line-through; }
.tree-toggle {
  width: 32px; height: 32px; border: 0; border-radius: 8px; background: transparent;
  color: var(--text); font-size: 1.1rem; cursor: pointer;
}
.tree-toggle:hover { background: var(--bg); }
.tree-toggle.spacer { display: inline-block; }
.tree-main { display: flex; flex-direction: column; min-width: 0; }
.tree-name { overflow-wrap: anywhere; }
.tree-field { display: flex; align-items: center; gap: 6px; margin: 0; font-weight: 400; }
.tree-row .actions { display: flex; flex-wrap: wrap; gap: 6px; grid-column: 2; }
.tree-row .tree-field { grid-column: 2; }
@media (min-width: 900px) {
  .tree-row { grid-template-columns: 32px 1fr auto auto auto; }
  .tree-row .actions, .tree-row .tree-field { grid-column: auto; }
}

/* ---------- cascading category picker ---------- */
.field { margin-bottom: 14px; }
.field-label { display: block; font-weight: 500; }
.cascade { display: flex; flex-direction: column; gap: 6px; }
.cascade-select { margin-top: 0; animation: cascade-in 0.18s ease-out; }
@keyframes cascade-in { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; transform: none; } }

/* ---------- watch links rows ---------- */
.watch-rows { display: flex; flex-direction: column; gap: 10px; margin-bottom: 10px; }
.watch-row {
  display: grid; grid-template-columns: 1fr; gap: 6px; padding: 10px;
  border: 1px solid var(--border); border-radius: 10px; background: var(--bg);
}
.watch-row input { margin-top: 0; }
.watch-row-actions { display: flex; gap: 6px; justify-content: flex-end; }
@media (min-width: 700px) {
  .watch-row { grid-template-columns: 1.2fr 2fr 1fr auto; align-items: center; }
}

/* ---------- v2 (Cloudflare) additions ---------- */
label.inline { display: inline-flex; gap: 8px; align-items: center; margin: 0; }
label.inline input { width: auto; margin: 0; }
.small-preview { max-height: 64px; }
dialog.wide { width: min(760px, calc(100vw - 24px)); max-height: calc(100vh - 24px); }
.form-actions .spacer { flex: 1; }

/* sortable lists (drag + ↑/↓) */
.sort-list { list-style: none; margin: 8px 0 12px; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.sort-item {
  display: flex; flex-wrap: wrap; gap: 6px 10px; align-items: center;
  padding: 8px 10px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface);
}
.sort-item .handle { cursor: grab; color: var(--muted); font-size: 1.2rem; user-select: none; padding: 0 4px; }
.sort-item .grow { flex: 1; min-width: 180px; overflow-wrap: anywhere; }
.sort-item.dragging { opacity: 0.5; outline: 2px dashed var(--primary); }
.sort-item.is-hidden { opacity: 0.55; }
.sort-item.is-pinned { border-color: var(--primary); }
.sort-item input, .sort-item select { width: auto; margin: 0; padding: 6px 8px; }
.sort-item .title-input { flex: 1; min-width: 140px; }
.sort-item .count-input { width: 70px; }
.badge.pin { background: var(--primary); color: var(--on-primary); }
.badge.big { background: #c6f432; color: #111; }

/* layout token editor */
.layout-editor { margin-bottom: 16px; }
.layout-editor h3 { margin: 8px 0; font-size: 1rem; }
.token-row { display: flex; flex-wrap: wrap; gap: 6px; padding: 8px; min-height: 48px; border: 1px dashed var(--border); border-radius: 10px; }
.token {
  display: inline-flex; align-items: center; gap: 4px; padding: 6px 10px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--bg); cursor: grab; font-weight: 600; font-size: 0.9rem;
}
.token.ad { background: var(--draft-bg); color: var(--draft-text); }
.token button { border: 0; background: transparent; color: inherit; cursor: pointer; font-size: 1rem; padding: 0 2px; }
.token-palette { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }

/* phone-like previews */
.phone { max-width: 380px; margin: 0 auto; border: 1px solid var(--border); border-radius: 18px; padding: 10px; background: var(--bg); }
.phone .ph-ann { padding: 8px 10px; border-radius: 8px; font-weight: 600; margin-bottom: 8px; }
.phone .ph-ann.lime { background: #c6f432; color: #111; }
.phone .ph-ann.dark { background: #111; color: #fff; }
.phone .ph-sec { margin: 10px 0 4px; font-weight: 700; }
.phone .ph-item { display: flex; gap: 8px; align-items: center; padding: 6px; border-bottom: 1px solid var(--border); font-size: 0.9rem; }
.phone .ph-item img { width: 56px; height: 40px; object-fit: cover; border-radius: 6px; background: var(--border); }
.phone .ph-ad { height: 120px; display: grid; place-items: center; background: var(--border); color: var(--muted); border-radius: 8px; margin: 12px 0; font-size: 0.8rem; }
.phone .ph-p { margin: 8px 0; font-size: 0.9rem; }
.phone .ph-yt { position: relative; margin: 8px 0; }
.phone .ph-yt img { width: 100%; border-radius: 8px; }
.phone .ph-links { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0; }
.yt-thumb { width: 120px; border-radius: 6px; }

/* dashboard meter */
.meter { height: 16px; border-radius: 999px; background: var(--border); overflow: hidden; margin: 8px 0; }
.meter-bar { height: 100%; width: 0; background: var(--primary); transition: width 0.3s; }
.meter-bar.warn { background: var(--danger); }
.item-card { flex-direction: column; align-items: stretch; }
.item-card .grid-2 { width: 100%; }
.item-head { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }

.body-tools { gap: 4px; margin: 6px 0; }
.cfg-grid { display: grid; gap: 6px 12px; grid-template-columns: 1fr; }
@media (min-width: 700px) { .cfg-grid { grid-template-columns: repeat(3, 1fr); } }
.kv-row { display: grid; grid-template-columns: 1fr 2fr auto; gap: 6px; margin-bottom: 6px; }
.kv-row input { margin: 0; }
.poll-bar { height: 8px; background: var(--primary); border-radius: 4px; }
