/* ── Theme Variables ── */
:root {
    --bg-body: #f1f5f9;
    --bg-card: #ffffff;
    --bg-sidebar: #0f172a;
    --bg-sidebar-hover: #1e293b;
    --bg-sidebar-active: rgba(16, 185, 129, 0.12);
    --bg-input: #ffffff;
    --bg-topbar: #ffffff;
    --bg-badge: #f0fdf4;
    --bg-modal: #ffffff;
    --bg-code: #f8fafc;
    --text-primary: #0f172a;
    --text-secondary: #475569;
    --text-muted: #94a3b8;
    --text-sidebar: #94a3b8;
    --text-sidebar-active: #10b981;
    --border: #e2e8f0;
    --border-light: #f1f5f9;
    --accent: #059669;
    --accent-hover: #047857;
    --green: #10b981;
    --green-bg: #dcfce7;
    --green-text: #166534;
    --amber: #f59e0b;
    --amber-bg: #fef3c7;
    --amber-text: #92400e;
    --red: #ef4444;
    --red-bg: #fee2e2;
    --red-text: #991b1b;
    --blue: #3b82f6;
    --blue-bg: #dbeafe;
    --blue-text: #1e40af;
    --gray-bg: #f3f4f6;
    --gray-text: #4b5563;
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.04);
    --shadow-md: 0 4px 6px -1px rgba(0,0,0,0.06), 0 2px 4px -2px rgba(0,0,0,0.04);
    --shadow-lg: 0 10px 15px -3px rgba(0,0,0,0.08);
    --radius: 10px;
    --radius-sm: 6px;
    --sidebar-w: 260px;
    --topbar-h: 64px;
    --transition: 0.2s ease;
}

[data-theme="dark"] {
    --bg-body: #0b0f19;
    --bg-card: #151b2b;
    --bg-sidebar: #0d1117;
    --bg-sidebar-hover: #161b22;
    --bg-sidebar-active: rgba(16, 185, 129, 0.1);
    --bg-input: #1e293b;
    --bg-topbar: #151b2b;
    --bg-badge: #064e3b;
    --bg-modal: #1e293b;
    --bg-code: #0f172a;
    --text-primary: #e2e8f0;
    --text-secondary: #94a3b8;
    --text-muted: #64748b;
    --text-sidebar: #64748b;
    --text-sidebar-active: #34d399;
    --border: #1e293b;
    --border-light: #1e293b;
    --green-bg: #064e3b;
    --green-text: #6ee7b7;
    --amber-bg: #451a03;
    --amber-text: #fcd34d;
    --red-bg: #450a0a;
    --red-text: #fca5a5;
    --blue-bg: #172554;
    --blue-text: #93c5fd;
    --gray-bg: #1e293b;
    --gray-text: #94a3b8;
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.3);
    --shadow-md: 0 4px 6px rgba(0,0,0,0.4);
    --shadow-lg: 0 10px 15px rgba(0,0,0,0.5);
}

/* ── Reset ── */
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
    font-family: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, sans-serif;
    background: var(--bg-body);
    color: var(--text-primary);
    -webkit-font-smoothing: antialiased;
    display: flex;
    min-height: 100vh;
}

/* ── Sidebar ── */
.sidebar {
    width: var(--sidebar-w);
    background: var(--bg-sidebar);
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 100;
    transition: transform var(--transition);
}

.sidebar-header {
    padding: 20px 20px 16px;
    border-bottom: 1px solid rgba(255,255,255,0.06);
}

.sidebar-logo {
    display: flex;
    align-items: center;
    gap: 12px;
}

.sidebar-title { display: flex; flex-direction: column; }
.title-main { color: #f1f5f9; font-size: 1rem; font-weight: 700; }
.title-sub { color: #64748b; font-size: 0.75rem; font-weight: 400; letter-spacing: 0.5px; }

.sidebar-nav {
    flex: 1;
    padding: 12px 10px;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.nav-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 14px;
    border-radius: 8px;
    color: var(--text-sidebar);
    text-decoration: none;
    font-size: 0.9rem;
    font-weight: 500;
    transition: all var(--transition);
}

.nav-item:hover {
    background: var(--bg-sidebar-hover);
    color: #e2e8f0;
}

.nav-item.active {
    background: var(--bg-sidebar-active);
    color: var(--text-sidebar-active);
}

.sidebar-footer {
    padding: 16px 16px;
    border-top: 1px solid rgba(255,255,255,0.06);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.admin-info {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
}

.admin-avatar {
    width: 32px;
    height: 32px;
    border-radius: 8px;
    background: var(--accent);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.8rem;
    font-weight: 700;
    flex-shrink: 0;
}

.admin-email {
    color: #94a3b8;
    font-size: 0.8rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.btn-logout {
    width: 34px;
    height: 34px;
    border-radius: 8px;
    border: 1px solid rgba(255,255,255,0.08);
    background: transparent;
    color: #64748b;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all var(--transition);
    flex-shrink: 0;
}

.btn-logout:hover { background: #dc2626; color: white; border-color: #dc2626; }

/* ── Mobile sidebar ── */
.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 90;
}

.mobile-menu-btn {
    display: none;
    background: none;
    border: none;
    color: var(--text-primary);
    cursor: pointer;
    padding: 4px;
}

/* ── Main content ── */
.main-content {
    flex: 1;
    margin-left: var(--sidebar-w);
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

.topbar {
    height: var(--topbar-h);
    background: var(--bg-topbar);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 28px;
    position: sticky;
    top: 0;
    z-index: 50;
    box-shadow: var(--shadow-sm);
}

.topbar-left { display: flex; align-items: center; gap: 12px; }
.topbar-right { display: flex; align-items: center; gap: 12px; }

.page-title {
    font-size: 1.15rem;
    font-weight: 700;
    letter-spacing: -0.3px;
}

.env-badge {
    font-size: 0.7rem;
    font-weight: 700;
    padding: 4px 12px;
    border-radius: 20px;
    background: var(--bg-badge);
    color: var(--green);
    letter-spacing: 0.5px;
    text-transform: uppercase;
}

.theme-toggle {
    width: 36px;
    height: 36px;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: var(--bg-card);
    color: var(--text-secondary);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all var(--transition);
}

.theme-toggle:hover { border-color: var(--accent); color: var(--accent); }

[data-theme="light"] .icon-moon { display: none; }
[data-theme="dark"] .icon-sun { display: none; }

.content-area {
    flex: 1;
    padding: 28px;
}

/* ── Views ── */
.view { display: none; }
.view.active { display: block; }

/* ── Stats grid ── */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 16px;
    margin-bottom: 24px;
}

.stat-card {
    background: var(--bg-card);
    border-radius: var(--radius);
    padding: 20px;
    display: flex;
    align-items: flex-start;
    gap: 16px;
    box-shadow: var(--shadow-sm);
    border: 1px solid var(--border-light);
    transition: box-shadow var(--transition);
}

.stat-card:hover { box-shadow: var(--shadow-md); }

.stat-icon {
    width: 48px;
    height: 48px;
    border-radius: var(--radius);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.stat-icon.blue { background: var(--blue-bg); color: var(--blue); }
.stat-icon.green { background: var(--green-bg); color: var(--green); }
.stat-icon.amber { background: var(--amber-bg); color: var(--amber); }
.stat-icon.red { background: var(--red-bg); color: var(--red); }

.stat-body { min-width: 0; }
.stat-value { font-size: 1.5rem; font-weight: 800; letter-spacing: -0.5px; line-height: 1.2; }
.stat-label { font-size: 0.8rem; color: var(--text-secondary); margin-top: 4px; }

/* ── Cards ── */
.card {
    background: var(--bg-card);
    border-radius: var(--radius);
    box-shadow: var(--shadow-sm);
    border: 1px solid var(--border-light);
    margin-bottom: 24px;
    overflow: hidden;
}

.card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 18px 24px;
    border-bottom: 1px solid var(--border);
}

.card-header h3 { font-size: 0.95rem; font-weight: 700; }

.refresh-note { font-size: 0.75rem; color: var(--text-muted); }

.row-2col {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 24px;
    margin-bottom: 24px;
}

/* ── Today's summary ── */
.today-stats {
    padding: 20px 24px;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
}

.today-item { display: flex; flex-direction: column; }
.today-label { font-size: 0.8rem; color: var(--text-secondary); margin-bottom: 4px; }
.today-value { font-size: 1.25rem; font-weight: 700; }
.green-text { color: var(--green); }
.blue-text { color: var(--blue); }

/* ── Channel list ── */
.channel-list { padding: 8px 24px 16px; }

.channel-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 0;
    border-bottom: 1px solid var(--border-light);
}

.channel-item:last-child { border-bottom: none; }
.channel-name { font-size: 0.875rem; color: var(--text-secondary); }

.channel-count {
    font-size: 0.9rem;
    font-weight: 700;
    background: var(--bg-body);
    padding: 2px 12px;
    border-radius: 20px;
    min-width: 36px;
    text-align: center;
}

/* ── Tables ── */
.table-wrapper { overflow-x: auto; }

table { width: 100%; border-collapse: collapse; }

th {
    text-align: left;
    padding: 12px 16px;
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-body);
    white-space: nowrap;
}

td {
    padding: 12px 16px;
    font-size: 0.85rem;
    border-bottom: 1px solid var(--border-light);
    white-space: nowrap;
}

tr:hover td { background: rgba(0,0,0,0.015); }
[data-theme="dark"] tr:hover td { background: rgba(255,255,255,0.02); }

.empty-state {
    text-align: center;
    color: var(--text-muted);
    padding: 40px 16px !important;
    font-size: 0.9rem;
}

/* ── Status badges ── */
.badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 3px 10px;
    border-radius: 20px;
    font-size: 0.7rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.3px;
}

.badge.green, .badge.live, .status-COMPLETED { background: var(--green-bg); color: var(--green-text); }
.status-PENDING { background: var(--amber-bg); color: var(--amber-text); }
.status-FAILED { background: var(--red-bg); color: var(--red-text); }
.status-CANCELLED { background: var(--gray-bg); color: var(--gray-text); }

.badge.live::before {
    content: "";
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: currentColor;
    animation: pulse-dot 2s infinite;
}

@keyframes pulse-dot {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.3; }
}

.type-badge {
    padding: 2px 8px;
    border-radius: 4px;
    font-size: 0.7rem;
    font-weight: 600;
    background: var(--bg-body);
    color: var(--text-secondary);
}

/* ── Filters ── */
.filters {
    display: flex;
    gap: 10px;
    align-items: center;
}

.filter-input, .filter-select {
    padding: 8px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg-input);
    color: var(--text-primary);
    font-size: 0.85rem;
    outline: none;
    transition: border-color var(--transition);
}

.filter-input { width: 260px; }

.filter-input:focus, .filter-select:focus {
    border-color: var(--accent);
}

/* ── Pagination ── */
.pagination {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 16px;
    border-top: 1px solid var(--border-light);
}

.pagination button {
    padding: 6px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg-card);
    color: var(--text-primary);
    font-size: 0.8rem;
    cursor: pointer;
    transition: all var(--transition);
}

.pagination button:hover { border-color: var(--accent); color: var(--accent); }
.pagination button:disabled { opacity: 0.4; cursor: not-allowed; }
.pagination span { font-size: 0.8rem; color: var(--text-secondary); }

/* ── Health grid ── */
.health-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
    gap: 16px;
}

.health-card {
    background: var(--bg-card);
    border-radius: var(--radius);
    padding: 20px 24px;
    border: 1px solid var(--border-light);
    box-shadow: var(--shadow-sm);
}

.health-card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 8px;
}

.health-card-header h4 {
    font-size: 0.85rem;
    font-weight: 600;
    text-transform: capitalize;
}

.health-status {
    width: 10px;
    height: 10px;
    border-radius: 50%;
}

.health-status.ok { background: var(--green); box-shadow: 0 0 6px var(--green); }
.health-status.warning { background: var(--amber); box-shadow: 0 0 6px var(--amber); }
.health-status.error { background: var(--red); box-shadow: 0 0 6px var(--red); }

.health-detail {
    font-size: 0.85rem;
    color: var(--text-secondary);
    word-break: break-all;
}

.loading-card { padding: 40px; text-align: center; color: var(--text-muted); }

/* ── Settings ── */
.settings-grid {
    padding: 8px 24px;
}

.setting-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 0;
    border-bottom: 1px solid var(--border-light);
}

.setting-item:last-child { border-bottom: none; }
.setting-label { font-size: 0.85rem; color: var(--text-secondary); }
.setting-value { font-size: 0.85rem; font-weight: 600; }

.endpoints-list { padding: 8px 24px 16px; }

.endpoint {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 0;
    border-bottom: 1px solid var(--border-light);
}

.endpoint:last-child { border-bottom: none; }

.method {
    font-size: 0.65rem;
    font-weight: 800;
    padding: 3px 8px;
    border-radius: 4px;
    min-width: 44px;
    text-align: center;
    text-transform: uppercase;
}

.method.get { background: var(--blue-bg); color: var(--blue-text); }
.method.post { background: var(--green-bg); color: var(--green-text); }

.endpoint code {
    font-size: 0.82rem;
    font-family: "SF Mono", "Fira Code", monospace;
    background: var(--bg-code);
    padding: 3px 8px;
    border-radius: 4px;
    color: var(--text-primary);
}

.ep-desc { font-size: 0.8rem; color: var(--text-muted); margin-left: auto; }

/* ── Modal ── */
.modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 200;
    align-items: center;
    justify-content: center;
}

.modal-overlay.open { display: flex; }

.modal {
    background: var(--bg-modal);
    border-radius: var(--radius);
    width: 90%;
    max-width: 700px;
    max-height: 80vh;
    display: flex;
    flex-direction: column;
    box-shadow: var(--shadow-lg);
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 24px;
    border-bottom: 1px solid var(--border);
}

.modal-header h3 { font-size: 1rem; font-weight: 700; }

.modal-close {
    width: 32px;
    height: 32px;
    border: none;
    background: none;
    color: var(--text-secondary);
    font-size: 1.3rem;
    cursor: pointer;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.modal-close:hover { background: var(--bg-body); }

.modal-body {
    padding: 20px 24px;
    overflow: auto;
    font-family: "SF Mono", "Fira Code", monospace;
    font-size: 0.82rem;
    line-height: 1.5;
    color: var(--text-primary);
    background: var(--bg-code);
    white-space: pre-wrap;
    word-break: break-all;
}

/* ── Payload button ── */
.btn-payload {
    padding: 4px 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg-card);
    color: var(--accent);
    font-size: 0.75rem;
    font-weight: 600;
    cursor: pointer;
    transition: all var(--transition);
}

.btn-payload:hover { background: var(--accent); color: white; border-color: var(--accent); }

/* ── Primary / Secondary Buttons ── */
.btn-primary {
    padding: 8px 18px;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
    transition: background var(--transition);
}

.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-secondary {
    padding: 8px 18px;
    background: var(--bg-body);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
    transition: all var(--transition);
}

.btn-secondary:hover { border-color: var(--text-secondary); }

/* ── Client Apps Table Extras ── */
.api-key-masked {
    font-family: "SF Mono", "Fira Code", monospace;
    font-size: 0.78rem;
    background: var(--bg-code);
    padding: 2px 6px;
    border-radius: 4px;
    color: var(--text-secondary);
}

.btn-copy {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 26px;
    height: 26px;
    border: 1px solid var(--border);
    border-radius: 4px;
    background: var(--bg-card);
    color: var(--text-secondary);
    cursor: pointer;
    margin-left: 6px;
    vertical-align: middle;
    transition: all var(--transition);
}

.btn-copy:hover { border-color: var(--accent); color: var(--accent); }

.actions-cell {
    display: flex;
    gap: 6px;
    align-items: center;
}

.btn-action {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 30px;
    height: 30px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg-card);
    color: var(--text-secondary);
    cursor: pointer;
    transition: all var(--transition);
}

.btn-action:hover { border-color: var(--accent); color: var(--accent); }
.btn-action.warn:hover { border-color: var(--amber); color: var(--amber); }
.btn-action.green:hover { border-color: var(--green); color: var(--green); }

/* ── Client App Modal Form ── */
.modal-form {
    padding: 20px 24px;
    display: flex;
    flex-direction: column;
    gap: 14px;
}

.form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 14px;
}

.form-group {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.form-group label {
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-secondary);
}

.form-actions {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    padding-top: 6px;
}

/* ── Simulation ── */
.sim-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
    gap: 20px;
}

.sim-card { overflow: visible; }

.sim-body {
    padding: 20px 24px;
    display: flex;
    flex-direction: column;
    gap: 14px;
}

.sim-field {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.sim-field label {
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-secondary);
}

.sim-input {
    padding: 9px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg-input);
    color: var(--text-primary);
    font-size: 0.85rem;
    outline: none;
    transition: border-color var(--transition);
    width: 100%;
    font-family: inherit;
}

.sim-input:focus { border-color: var(--accent); }

textarea.sim-input { resize: vertical; }

.btn-sim { align-self: flex-start; }

.sim-result {
    min-height: 0;
    font-size: 0.85rem;
}

.sim-success {
    color: var(--green-text);
    background: var(--green-bg);
    padding: 6px 12px;
    border-radius: var(--radius-sm);
    display: inline-block;
    font-weight: 600;
    font-size: 0.8rem;
}

.sim-error {
    color: var(--red-text);
    background: var(--red-bg);
    padding: 6px 12px;
    border-radius: var(--radius-sm);
    display: inline-block;
    font-weight: 600;
    font-size: 0.8rem;
}

.sim-loading {
    color: var(--amber-text);
    background: var(--amber-bg);
    padding: 6px 12px;
    border-radius: var(--radius-sm);
    display: inline-block;
    font-weight: 600;
    font-size: 0.8rem;
}

.sim-json {
    margin-top: 8px;
    padding: 12px;
    background: var(--bg-code);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-family: "SF Mono", "Fira Code", monospace;
    font-size: 0.78rem;
    line-height: 1.5;
    overflow-x: auto;
    white-space: pre-wrap;
    word-break: break-all;
    color: var(--text-primary);
}

.qr-display {
    text-align: center;
    min-height: 0;
}

.qr-img {
    max-width: 280px;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    margin-top: 8px;
}

.sandbox-badge {
    background: var(--amber-bg);
    color: var(--amber-text);
}

/* ── Responsive ── */
@media (max-width: 1100px) {
    .stats-grid { grid-template-columns: repeat(2, 1fr); }
    .row-2col { grid-template-columns: 1fr; }
}

@media (max-width: 768px) {
    .sidebar {
        transform: translateX(-100%);
    }

    .sidebar.open {
        transform: translateX(0);
    }

    .sidebar-overlay.open { display: block; }

    .mobile-menu-btn { display: flex; }

    .main-content { margin-left: 0; }

    .content-area { padding: 16px; }
    .topbar { padding: 0 16px; }

    .stats-grid { grid-template-columns: repeat(2, 1fr); gap: 12px; }
    .stat-card { padding: 14px; }
    .stat-icon { width: 40px; height: 40px; }
    .stat-value { font-size: 1.2rem; }

    .filters { flex-wrap: wrap; }
    .filter-input { width: 100%; }

    .card-header { flex-direction: column; align-items: flex-start; gap: 8px; }

    .sim-grid { grid-template-columns: 1fr; }
    .form-row { grid-template-columns: 1fr; }
}

@media (max-width: 480px) {
    .stats-grid { grid-template-columns: 1fr 1fr; gap: 8px; }
    .stat-card { flex-direction: column; gap: 8px; padding: 12px; }
}
