:root {
    --bg: #f5f5f5;
    --surface: #ffffff;
    --ink: #000000;
    --muted: #6f7171;
    --line: #e4e4e4;
    --brand: #ee001e;
    --brand-dark: #c40018;
    --navy: #000000;
    --ok: #0f7a43;
    --warn: #b7791f;
    --danger: #b42318;
    --info: #175cd3;
    --shadow: 0 10px 28px rgba(0, 0, 0, 0.06);
    --radius: 16px;
    --font: "Inter", "Helvetica Neue", Helvetica, Arial, sans-serif;
    --display: "Inter", "Helvetica Neue", Helvetica, Arial, sans-serif;
    --cream: #f8f3e9;
    --yellow: #f8ff3c;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
    font-family: var(--font);
    color: var(--ink);
    background: var(--bg);
    min-height: 100vh;
}

a { color: var(--brand-dark); text-decoration: none; }
a:hover { text-decoration: underline; }

.app-shell { display: grid; grid-template-columns: 272px 1fr; min-height: 100vh; }
.sidebar {
    display: flex;
    flex-direction: column;
    background: #000;
    color: #fff;
    border-right: 1px solid #1a1a1a;
    position: sticky;
    top: 0;
    height: 100vh;
    overflow: hidden;
}
.sidebar-top { padding: 20px 16px 8px; flex: 0 0 auto; }
.sidebar-nav {
    flex: 1 1 auto;
    overflow-y: auto;
    padding: 4px 12px 16px;
    scrollbar-width: thin;
    scrollbar-color: rgba(255,255,255,.2) transparent;
}
.sidebar-foot {
    flex: 0 0 auto;
    padding: 14px 16px 18px;
    border-top: 1px solid rgba(255,255,255,0.08);
    background: rgba(0,0,0,0.18);
    display: grid;
    gap: 8px;
}
.sidebar-user strong { display:block; font-size:13px; }
.sidebar-user small { display:block; color:#9db0c3; font-size:11px; margin-top:2px; }
.sidebar-btn { width: 100%; justify-content: center; }
.brand {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px 8px 16px;
}
.brand-mark {
    width: 40px;
    height: 40px;
    border-radius: 12px;
    background: linear-gradient(145deg, var(--brand), #ff3b2f);
    display: grid;
    place-items: center;
    font-weight: 800;
    letter-spacing: 0.5px;
    box-shadow: 0 10px 24px rgba(225, 6, 0, 0.28);
}
.brand strong { display: block; font-size: 14px; letter-spacing: -0.01em; }
.brand span { display: block; color: #9db0c3; font-size: 11px; margin-top: 2px; text-transform: uppercase; letter-spacing: .06em; font-weight: 700; }

.nav-label {
    margin: 16px 10px 8px;
    color: #7f93a8;
    font-size: 10px;
    font-weight: 800;
    letter-spacing: .1em;
    text-transform: uppercase;
}
.nav-link {
    display: flex;
    align-items: center;
    gap: 10px;
    color: #c9d6e4;
    padding: 10px 12px;
    border-radius: 12px;
    margin-bottom: 3px;
    text-decoration: none;
    font-size: 14px;
    font-weight: 600;
    border: 1px solid transparent;
    transition: .15s ease;
}
.nav-ico {
    width: 22px;
    height: 22px;
    border-radius: 7px;
    display: grid;
    place-items: center;
    font-size: 11px;
    color: #9db0c3;
    background: rgba(255,255,255,0.05);
    flex: 0 0 auto;
}
.nav-link:hover {
    background: rgba(255,255,255,0.07);
    color: #fff;
    text-decoration: none;
}
.nav-link:hover .nav-ico { color: #fff; background: rgba(255,255,255,0.1); }
.nav-link.active {
    background: rgba(225, 6, 0, 0.16);
    color: #fff;
    border-color: rgba(225, 6, 0, 0.28);
    box-shadow: inset 3px 0 0 var(--brand);
}
.nav-link.active .nav-ico {
    background: rgba(225, 6, 0, 0.28);
    color: #fff;
}
.impersonation-banner {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
    margin-bottom: 18px;
    padding: 12px 14px;
    border-radius: 14px;
    background: linear-gradient(90deg, #1d2433, #2a1a1a);
    color: #fff;
    border: 1px solid rgba(225,6,0,.35);
}
.impersonation-banner strong { color: #ffb4ae; }
.app-shell.is-impersonating .sidebar {
    box-shadow: inset 0 0 0 2px rgba(225, 6, 0, 0.35);
}

.main { padding: 28px; }
.topbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 16px;
    margin-bottom: 22px;
}
.page-title h1 {
    margin: 0;
    font-family: var(--display);
    font-weight: 400;
    font-size: 34px;
    letter-spacing: -0.03em;
}
.page-title p { margin: 6px 0 0; color: var(--muted); }

.user-chip {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 999px;
    padding: 8px 14px;
    display: flex;
    gap: 10px;
    align-items: center;
    box-shadow: var(--shadow);
}
.user-chip small { color: var(--muted); display: block; }

.grid { display: grid; gap: 16px; }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }

.card {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 18px;
}
.stat-card {
    position: relative;
    overflow: hidden;
}
.stat-card::before {
    content: "";
    position: absolute;
    left: 0; top: 0; bottom: 0;
    width: 4px;
    background: var(--brand);
}
.stat-label { color: var(--muted); font-size: 13px; font-weight: 600; }
.stat-value { font-size: 30px; font-weight: 800; margin-top: 8px; letter-spacing: -0.03em; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 12px 10px; border-bottom: 1px solid var(--line); font-size: 14px; }
th { color: var(--muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; }
tr:hover td { background: #fafbfc; }

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    border: 0;
    border-radius: 10px;
    padding: 10px 14px;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
}
.btn:hover { text-decoration: none; }
.btn-primary { background: #000; color: #fff; border-radius: 999px; }
.btn-primary:hover { background: #222; color: #fff; }
.btn-secondary { background: #e8eef4; color: var(--navy); }
.btn-danger { background: #fee4e2; color: var(--danger); }
.btn-sm { padding: 7px 10px; font-size: 13px; }

.badge {
    display: inline-flex;
    align-items: center;
    border-radius: 999px;
    padding: 4px 10px;
    font-size: 12px;
    font-weight: 700;
    text-transform: capitalize;
}
.badge-active, .badge-paid, .badge-completed, .badge-approved, .badge-resolved { background: #dcfae6; color: var(--ok); }
.badge-pending, .badge-unpaid, .badge-open, .badge-medium { background: #fef0c7; color: var(--warn); }
.badge-inactive, .badge-cancelled, .badge-closed, .badge-overdue { background: #fee4e2; color: var(--danger); }
.badge-processing, .badge-in_progress, .badge-partial, .badge-high, .badge-urgent { background: #d1e9ff; color: var(--info); }
.badge-low { background: #e8eef4; color: var(--navy); }

.form-grid { display: grid; gap: 14px; }
.form-grid.two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 6px; }
input, select, textarea {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 11px 12px;
    font: inherit;
    background: #fff;
}
input:focus, select:focus, textarea:focus {
    outline: 2px solid rgba(213, 43, 30, 0.25);
    border-color: var(--brand);
}
.help { color: var(--muted); font-size: 12px; margin-top: 6px; }
.actions { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 8px; }
.alert {
    border-radius: 12px;
    padding: 12px 14px;
    margin-bottom: 16px;
    border: 1px solid transparent;
}
.alert-success { background: #dcfae6; border-color: #abefc6; color: var(--ok); }
.alert-error { background: #fee4e2; border-color: #fecdca; color: var(--danger); }
.error-list { margin: 0; padding-left: 18px; color: var(--danger); }

.guest-wrap {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 24px;
}
.guest-card {
    width: min(440px, 100%);
    background: rgba(255,255,255,0.96);
    border: 1px solid var(--line);
    border-radius: 20px;
    box-shadow: var(--shadow);
    padding: 28px;
}
.guest-card h1 { margin: 0 0 8px; font-size: 28px; }
.guest-card p { margin: 0 0 22px; color: var(--muted); }
.checkbox { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.checkbox input { width: auto; }

.reply {
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 14px;
    margin-bottom: 12px;
    background: #fafbfc;
}
.reply.internal { background: #fff7ed; border-color: #fdc88a; }
.muted { color: var(--muted); }
.stack-sm > * + * { margin-top: 10px; }
.mt-16 { margin-top: 16px; }
.mt-24 { margin-top: 24px; }
.flex-between { display: flex; justify-content: space-between; gap: 12px; align-items: center; flex-wrap: wrap; }

.pagination { margin-top: 18px; }
.pagination nav { display: flex; gap: 6px; flex-wrap: wrap; }
.pagination a, .pagination span {
    display: inline-flex;
    padding: 8px 11px;
    border-radius: 8px;
    border: 1px solid var(--line);
    background: #fff;
    color: var(--ink);
    text-decoration: none;
}
.pagination .active span { background: var(--navy); color: #fff; border-color: var(--navy); }

@media (max-width: 1100px) {
    .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 860px) {
    .app-shell { grid-template-columns: 1fr; }
    .sidebar {
        position: relative;
        height: auto;
        overflow: visible;
    }
    .sidebar-nav { max-height: none; overflow: visible; }
    .grid-2, .form-grid.two { grid-template-columns: 1fr; }
    .main { padding: 18px; }
}

/* Admin + gateway console */
.admin-grid { display:grid; grid-template-columns:minmax(0,1.5fr) minmax(260px,.7fr); gap:18px; }
.admin-panel, .gateway-card {
    background:var(--surface); border:1px solid var(--line); border-radius:18px;
    box-shadow:var(--shadow); padding:22px;
}
.admin-panel.soft { background:linear-gradient(180deg,#fff,#f7f9fb); }
.admin-panel-head {
    display:flex; justify-content:space-between; gap:12px; align-items:flex-start; margin-bottom:14px;
}
.admin-panel-head h3, .gateway-card h3, .admin-panel h3 { margin:0 0 6px; }
.admin-panel-head p, .gateway-card p { color:var(--muted); margin:0 0 0; line-height:1.55; }
.admin-panel .form-grid { margin-top:4px; }
.admin-divider { height:1px; background:var(--line); margin:22px 0; }
.choice-row { display:flex; flex-wrap:wrap; gap:10px; }
.choice-card {
    display:flex; align-items:center; gap:8px; padding:12px 14px; border:1px solid var(--line);
    border-radius:12px; background:#fafbfc; font-weight:600; font-size:14px;
}
.choice-card input { width:auto; }
.side-link {
    display:block; padding:12px 14px; border-radius:12px; border:1px solid var(--line);
    margin-bottom:8px; color:var(--ink); text-decoration:none; font-weight:600;
}
.side-link:hover { background:#f4f7fa; text-decoration:none; }
.gateway-grid { display:grid; grid-template-columns:repeat(2,minmax(0,1fr)); gap:16px; }
.gateway-meta { display:flex; flex-wrap:wrap; gap:10px; color:var(--muted); font-size:13px; margin:12px 0 16px; }
.eyebrow { color:var(--brand); font-weight:800; letter-spacing:.06em; text-transform:uppercase; font-size:11px; }
.warn-panel { border-color:#f5d0c5; background:linear-gradient(180deg,#fff,#fff7f5); }
@media (max-width:960px) {
    .admin-grid, .gateway-grid { grid-template-columns:1fr; }
}

/* ===== Extensions grid + modal studio ===== */
.ext-studio { display:grid; gap:18px; }
.ext-studio-bar {
    display:flex; justify-content:space-between; gap:16px; align-items:flex-end; flex-wrap:wrap;
    padding:22px 24px; border-radius:22px; color:#fff;
    background:
        radial-gradient(circle at 85% 10%, rgba(225,6,0,.35), transparent 40%),
        linear-gradient(135deg, #071526, #12263d 60%, #1a1428);
}
.ext-studio-bar h2 {
    margin:4px 0 6px; font-family:var(--display); font-weight:400; font-size:clamp(28px,3vw,38px);
}
.ext-studio-bar p { margin:0; color:rgba(255,255,255,.7); }
.ext-studio-pills { display:flex; gap:8px; flex-wrap:wrap; }
.ext-pill {
    padding:8px 12px; border-radius:999px; font-size:12px; font-weight:800;
    background:rgba(255,255,255,.08); border:1px solid rgba(255,255,255,.12); color:#fff;
}
.ext-pill.is-on { background:rgba(15,122,67,.25); border-color:rgba(15,122,67,.4); }

.ext-grid {
    display:grid; grid-template-columns:repeat(3, minmax(0,1fr)); gap:16px;
}
.ext-card {
    text-align:left; cursor:pointer; border:1px solid var(--line); border-radius:22px;
    background:#fff; box-shadow:var(--shadow); padding:20px; display:grid; gap:10px;
    transition: transform .18s ease, box-shadow .18s ease, border-color .18s ease;
    font: inherit; color: inherit;
}
.ext-card:hover {
    transform: translateY(-4px);
    border-color: rgba(225,6,0,.28);
    box-shadow: 0 22px 50px rgba(7,21,38,.12);
}
.ext-card.is-live { border-color: rgba(15,122,67,.28); }
.ext-card-top { display:flex; justify-content:space-between; align-items:flex-start; gap:10px; }
.ext-card-tag {
    font-size:11px; font-weight:800; letter-spacing:.06em; text-transform:uppercase;
    color:var(--brand); background:rgba(225,6,0,.08); padding:6px 10px; border-radius:999px;
}
.ext-card h3 { margin:0; font-size:18px; }
.ext-card p { margin:0; color:var(--muted); line-height:1.5; font-size:14px; min-height:42px; }
.ext-card-foot { display:flex; justify-content:space-between; gap:10px; align-items:center; flex-wrap:wrap; }
.ext-card-meta { color:var(--muted); font-size:12px; font-weight:600; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; max-width:58%; }
.ext-live-dot {
    display:inline-flex; align-items:center; gap:6px; font-size:12px; font-weight:800;
    padding:5px 10px; border-radius:999px;
}
.ext-live-dot::before { content:""; width:8px; height:8px; border-radius:50%; background:currentColor; }
.ext-live-dot.on { color:var(--ok); background:#dcfae6; }
.ext-live-dot.off { color:var(--danger); background:#fee4e2; }
.ext-card-cta { color:var(--brand-dark); font-weight:800; font-size:13px; }

.ext-module-brand {
    width:52px; height:52px; border-radius:16px; display:grid; place-items:center;
    font-weight:800; color:#fff; font-size:16px; flex:0 0 auto;
}
.ext-brand-ga { background:linear-gradient(145deg,#f9ab00,#e37400); }
.ext-brand-meta { background:linear-gradient(145deg,#1877f2,#0b5ed7); font-size:22px; }
.ext-brand-adsense { background:linear-gradient(145deg,#34a853,#0f9d58); }
.ext-brand-gads { background:linear-gradient(145deg,#4285f4,#1a73e8); }
.ext-brand-chat { background:linear-gradient(145deg,#e10600,#ff4d2e); font-size:20px; }

.ext-switch {
    position:relative; width:52px; height:30px; display:inline-block; flex:0 0 auto;
}
.ext-switch input { opacity:0; width:0; height:0; position:absolute; }
.ext-switch > span {
    position:absolute; inset:0; background:#d5dee7; border-radius:999px; transition:.2s ease;
}
.ext-switch > span::before {
    content:""; position:absolute; width:24px; height:24px; left:3px; top:3px;
    background:#fff; border-radius:50%; transition:.2s ease; box-shadow:0 2px 8px rgba(0,0,0,.15);
}
.ext-switch input:checked + span { background:var(--brand); }
.ext-switch input:checked + span::before { transform:translateX(22px); }

body.ext-modal-open { overflow:hidden; }
.ext-modal[hidden] { display:none !important; }
.ext-modal {
    position:fixed; inset:0; z-index:80; display:grid; place-items:center; padding:18px;
}
.ext-modal-backdrop {
    position:absolute; inset:0; background:rgba(7,21,38,.55); backdrop-filter:blur(6px);
}
.ext-modal-panel {
    position:relative; z-index:1; width:min(780px, 100%); max-height:min(90vh, 920px);
    overflow:auto; background:#fff; border-radius:24px;
    box-shadow:0 30px 80px rgba(0,0,0,.28); border:1px solid rgba(255,255,255,.4);
    animation: extPop .18s ease;
}
@keyframes extPop {
    from { opacity:0; transform:translateY(10px) scale(.98); }
    to { opacity:1; transform:translateY(0) scale(1); }
}
.ext-modal-head {
    display:flex; justify-content:space-between; gap:12px; align-items:flex-start;
    padding:20px 22px 14px; border-bottom:1px solid var(--line);
    position:sticky; top:0; background:#fff; z-index:2;
}
.ext-modal-identity { display:flex; gap:14px; align-items:center; min-width:0; }
.ext-modal-kicker { display:flex; gap:8px; align-items:center; flex-wrap:wrap; margin-bottom:4px; }
.ext-modal-head h3 { margin:0; font-size:24px; font-family:var(--display); font-weight:400; }
.ext-modal-x {
    width:36px; height:36px; border-radius:12px; border:1px solid var(--line);
    background:#f7f9fb; font-size:22px; line-height:1; cursor:pointer; color:var(--ink);
}
.ext-modal-body { padding:18px 22px 8px; display:grid; gap:14px; }
.ext-modal-foot {
    display:flex; justify-content:flex-end; gap:10px; flex-wrap:wrap;
    padding:14px 22px 20px; border-top:1px solid var(--line);
    position:sticky; bottom:0; background:#fff;
}
.ext-enable-row {
    display:flex; justify-content:space-between; gap:16px; align-items:center;
    padding:14px 16px; border-radius:16px; background:linear-gradient(180deg,#f8fafc,#f3f6f9);
    border:1px solid var(--line); cursor:pointer;
}
.ext-enable-row strong { display:block; }
.ext-enable-row small { color:var(--muted); }
.ext-section {
    border:1px solid var(--line); border-radius:18px; padding:16px;
    background:#fff;
}
.ext-section-head { margin-bottom:14px; }
.ext-section-head h4 { margin:0 0 4px; font-size:15px; font-weight:800; }
.ext-section-head p { margin:0; color:var(--muted); font-size:13px; line-height:1.45; }
.ext-toggle-list { display:grid; gap:8px; }
.ext-toggle-item {
    display:flex; justify-content:space-between; gap:14px; align-items:center;
    padding:12px 14px; border-radius:14px; border:1px solid var(--line);
    background:#f9fbfd; cursor:pointer;
}
.ext-toggle-item strong { display:block; font-size:14px; }
.ext-toggle-item small { color:var(--muted); font-size:12px; }
.ext-ad-shell { padding:18px 0 8px; }
.ext-ad-frame {
    border:1px dashed rgba(7,21,38,.18); border-radius:16px; padding:12px;
    background:linear-gradient(180deg,#fff,#f7f9fb);
}
.ext-ad-label {
    font-size:11px; font-weight:800; letter-spacing:.08em; text-transform:uppercase;
    color:var(--muted); margin-bottom:8px;
}
.ext-ad-shell.is-test .ext-ad-frame { border-style:solid; border-color:rgba(225,6,0,.25); }
.ext-ad-test-box {
    min-height:90px; display:grid; place-items:center; border-radius:12px;
    background:repeating-linear-gradient(-45deg,#fff,#fff 10px,#f3f6f9 10px,#f3f6f9 20px);
    color:var(--muted); font-size:13px; font-weight:700; text-align:center; padding:16px;
}

@media (max-width:1100px) {
    .ext-grid { grid-template-columns:repeat(2,minmax(0,1fr)); }
}
@media (max-width:700px) {
    .ext-grid { grid-template-columns:1fr; }
}
