:root {
  --bg: #0f1420; --panel: #171e2e; --panel2: #1e273a; --line: #2a3550;
  --text: #e8ecf4; --muted: #8b97b0; --accent: #4f8cff; --green: #2ecc8f; --yellow: #f5b83d; --red: #ff5d6c;
  --radius: 14px;
}
* { box-sizing: border-box; }
body { margin: 0; font-family: 'Cairo', system-ui, sans-serif; background: var(--bg); color: var(--text); font-size: 15px; }
.hidden { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; }
h2 { font-size: 20px; }

.btn { border: 1px solid var(--line); background: var(--panel2); padding: 8px 16px; border-radius: 10px; cursor: pointer; white-space: nowrap; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 700; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--red); }
.btn.small { padding: 4px 10px; font-size: 13px; border-radius: 8px; }
.btn:disabled { opacity: .5; cursor: wait; }

input, select, textarea { width: 100%; background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 9px 12px; margin-top: 4px; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); }
label { display: block; color: var(--muted); font-size: 13px; margin-bottom: 12px; }
label input, label select, label textarea { color: var(--text); font-size: 15px; }

/* login */
#login { min-height: 100vh; display: grid; place-items: center; }
.login-card { background: var(--panel); padding: 36px; border-radius: 20px; width: min(360px, 90vw); text-align: center; border: 1px solid var(--line); }
.login-card h1 { font-size: 22px; margin: 8px 0 20px; }
.login-card .btn { width: 100%; margin-top: 12px; padding: 11px; }
.logo { color: var(--accent); font-size: 26px; }
.err { color: var(--red); min-height: 1em; }

/* header */
header { display: flex; align-items: center; gap: 20px; padding: 12px 24px; background: var(--panel); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5; flex-wrap: wrap; }
.brand { font-weight: 800; font-size: 18px; display: flex; gap: 8px; align-items: center; }
nav { display: flex; gap: 4px; flex: 1; overflow-x: auto; }
nav button { background: none; border: 0; padding: 8px 14px; border-radius: 10px; cursor: pointer; color: var(--muted); font-weight: 600; }
nav button.active { background: var(--panel2); color: var(--text); }
.badge { background: var(--red); color: #fff; border-radius: 20px; padding: 0 7px; font-size: 12px; margin-right: 4px; }

.master { display: flex; align-items: center; gap: 8px; margin: 0; color: var(--text); font-weight: 600; cursor: pointer; }
.master input { display: none; }
.slider { width: 44px; height: 24px; background: var(--line); border-radius: 20px; position: relative; transition: .2s; }
.slider::after { content: ''; position: absolute; top: 3px; right: 3px; width: 18px; height: 18px; background: #fff; border-radius: 50%; transition: .2s; }
.master input:checked + .slider { background: var(--green); }
.master input:checked + .slider::after { right: 23px; }

main { padding: 24px; max-width: 1300px; margin: 0 auto; }
.row-head { display: flex; justify-content: space-between; align-items: center; margin: 24px 0 14px; gap: 12px; flex-wrap: wrap; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.actions select { width: auto; margin: 0; }

/* stats */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.stat .k { color: var(--muted); font-size: 13px; }
.stat .v { font-size: 26px; font-weight: 800; margin-top: 4px; }
.stat .s { color: var(--muted); font-size: 12px; }
.stat.warn .v { color: var(--yellow); }

/* products */
.products { display: grid; grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); gap: 14px; }
.product { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.product.m-active { border-color: color-mix(in srgb, var(--green) 50%, var(--line)); }
.product.m-analyze_only { border-color: color-mix(in srgb, var(--yellow) 50%, var(--line)); }
.p-head { display: flex; justify-content: space-between; gap: 10px; align-items: start; }
.p-head h3 { font-size: 17px; }
.p-head .price { color: var(--muted); font-size: 13px; }
.thumbs { display: flex; gap: 6px; flex-wrap: wrap; }
.thumb { position: relative; width: 62px; height: 62px; border-radius: 10px; overflow: hidden; background: var(--bg); }
.thumb img { width: 100%; height: 100%; object-fit: cover; }
.thumb button { position: absolute; top: 2px; left: 2px; background: rgba(0,0,0,.65); border: 0; color: #fff; border-radius: 6px; width: 20px; height: 20px; line-height: 18px; cursor: pointer; font-size: 12px; }
.thumb.add { display: grid; place-items: center; border: 1px dashed var(--line); cursor: pointer; color: var(--muted); font-size: 22px; }

.modes { display: grid; grid-template-columns: repeat(3, 1fr); background: var(--bg); border-radius: 12px; padding: 4px; gap: 4px; }
.modes button { border: 0; background: none; padding: 8px 4px; border-radius: 9px; cursor: pointer; color: var(--muted); font-weight: 600; font-size: 13px; }
.modes button.on[data-mode=active] { background: var(--green); color: #06291b; }
.modes button.on[data-mode=analyze_only] { background: var(--yellow); color: #3a2800; }
.modes button.on[data-mode=off] { background: var(--red); color: #fff; }
.mode-hint { font-size: 12px; color: var(--muted); margin-top: -4px; }

.p-stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; text-align: center; }
.p-stats div { background: var(--panel2); border-radius: 10px; padding: 8px 4px; }
.p-stats b { display: block; font-size: 16px; }
.p-stats span { color: var(--muted); font-size: 11px; }
.p-actions { display: flex; gap: 6px; flex-wrap: wrap; }

/* creatives */
.creative-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 14px; }
.creative { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; display: flex; flex-direction: column; }
.creative .media { aspect-ratio: 4/5; background: var(--bg); position: relative; }
.creative .media img { width: 100%; height: 100%; object-fit: cover; }
.creative .tag { position: absolute; top: 8px; right: 8px; padding: 2px 10px; border-radius: 20px; font-size: 12px; font-weight: 700; }
.tag.live { background: var(--green); color: #06291b; }
.tag.paused { background: var(--line); }
.tag.failed { background: var(--red); color: #fff; }
.tag.draft, .tag.publishing { background: var(--yellow); color: #3a2800; }
.creative .kind { position: absolute; top: 8px; left: 8px; background: rgba(0,0,0,.6); padding: 2px 8px; border-radius: 20px; font-size: 12px; }
.creative .body { padding: 12px; display: flex; flex-direction: column; gap: 8px; flex: 1; }
.creative .title { font-weight: 700; }
.creative .meta { color: var(--muted); font-size: 12px; }
.creative .nums { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; font-size: 12px; text-align: center; }
.creative .nums div { background: var(--panel2); border-radius: 8px; padding: 5px 2px; }
.creative .nums b { display: block; font-size: 14px; }
.creative details { font-size: 13px; color: var(--muted); white-space: pre-wrap; }
.creative .errtxt { color: var(--red); font-size: 12px; }
.creative .p-actions { margin-top: auto; }

/* reports */
.report { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 12px; }
.report.pending { border-color: var(--yellow); }
.report pre { white-space: pre-wrap; font-family: inherit; margin: 10px 0 0; line-height: 1.8; }
.report .r-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; }
.pill { padding: 2px 10px; border-radius: 20px; font-size: 12px; background: var(--panel2); }
.pill.pending { background: var(--yellow); color: #3a2800; }
.pill.executed { background: var(--green); color: #06291b; }
.pill.rejected { background: var(--red); color: #fff; }

/* settings */
.settings { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 14px; margin-top: 10px; }
fieldset { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin: 0; }
legend { font-weight: 700; padding: 0 6px; }
.hint { color: var(--muted); font-size: 12px; margin: 0; }
.sticky { grid-column: 1 / -1; }
#testResult { grid-column: 1 / -1; }
.test-row { padding: 8px 12px; border-radius: 10px; margin-bottom: 6px; background: var(--panel); }
.test-row.ok { border-right: 4px solid var(--green); }
.test-row.bad { border-right: 4px solid var(--red); }

.logs { font-size: 13px; }
.log { padding: 6px 10px; border-bottom: 1px solid var(--line); display: flex; gap: 12px; }
.log time { color: var(--muted); white-space: nowrap; direction: ltr; }
.log.error { color: var(--red); }
.log.warn { color: var(--yellow); }
.log span { white-space: pre-wrap; }

dialog { background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 18px; width: min(560px, 94vw); padding: 22px; }
dialog::backdrop { background: rgba(0,0,0,.6); }
dialog h3 { margin-bottom: 16px; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.three { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; }

#toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--panel2); border: 1px solid var(--line); padding: 10px 20px; border-radius: 12px; opacity: 0; transition: .25s; pointer-events: none; z-index: 20; max-width: 90vw; }
#toast.show { opacity: 1; }
#toast.bad { border-color: var(--red); }
.empty { color: var(--muted); text-align: center; padding: 40px; background: var(--panel); border-radius: var(--radius); border: 1px dashed var(--line); }

@media (max-width: 640px) {
  main { padding: 14px; }
  header { padding: 10px 14px; gap: 10px; }
  .products { grid-template-columns: 1fr; }
}
