:root {
    --primary: #4f46e5;
    --primary-hover: #4338ca;
    --bg: #f9fafb;
    --card-bg: #ffffff;
    --text: #1f2937;
    --text-light: #6b7280;
    --border: #e5e7eb;
    --success: #10b981;
    --warning: #f59e0b;
    --danger: #ef4444;
    --sidebar-bg: #1e1b4b;
    --sidebar-text: #c7d2fe;
    --sidebar-active: #4f46e5;
}

* { margin: 0; padding: 0; box-sizing: border-box; }
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: var(--bg); color: var(--text); line-height: 1.6; height: 100vh; overflow: hidden; }

/* Layout */
.app { display: flex; height: 100vh; }
/* The sidebar is injected via #sidebarSlot wrapper; display:contents lets .sidebar
   become the real flex child so it stretches to full page height. */
#sidebarSlot { display: contents; }
.sidebar { width: 240px; background: var(--sidebar-bg); color: var(--sidebar-text); padding: 20px 0; flex-shrink: 0; min-height: 100vh; display: flex; flex-direction: column; }
.sidebar .logo { padding: 0 20px 20px; font-size: 20px; font-weight: 700; color: #fff; border-bottom: 1px solid rgba(255,255,255,0.1); }
.sidebar .logo span { color: var(--primary); }
.sidebar nav { padding: 10px 0; flex: 1; display: flex; flex-direction: column; }
.sidebar nav a { display: flex; align-items: center; padding: 10px 20px; color: var(--sidebar-text); text-decoration: none; font-size: 14px; transition: all .2s; border-left: 3px solid transparent; }
.sidebar nav a:hover { background: rgba(255,255,255,0.05); color: #fff; }
.sidebar nav a.active { background: rgba(255,255,255,0.1); color: #fff; border-left-color: var(--primary); }
.sidebar nav .nav-bottom { margin-top: auto; padding-top: 16px; border-top: 1px solid rgba(255,255,255,0.1); display: flex; flex-direction: column; }
.sidebar nav .nav-bottom a { padding: 10px 20px; }
.main { flex: 1; padding: 24px; overflow-y: auto; }
.header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 24px; }
.header h1 { font-size: 24px; font-weight: 600; }
.header .user-info { font-size: 14px; color: var(--text-light); }

/* Cards */
.card { background: var(--card-bg); border: 1px solid var(--border); border-radius: 8px; padding: 20px; margin-bottom: 16px; }
.card-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; }
.card-header h2 { font-size: 16px; font-weight: 600; }

/* Grid */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 16px; }
.stats-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-bottom: 24px; }
.stat-card { background: var(--card-bg); border: 1px solid var(--border); border-radius: 8px; padding: 20px; }
.stat-card .label { font-size: 12px; color: var(--text-light); text-transform: uppercase; letter-spacing: 0.05em; }
.stat-card .value { font-size: 28px; font-weight: 700; margin-top: 4px; }

/* Forms */
.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-size: 14px; font-weight: 500; margin-bottom: 4px; color: var(--text); }
.form-group input, .form-group select, .form-group textarea { width: 100%; padding: 8px 12px; border: 1px solid var(--border); border-radius: 6px; font-size: 14px; outline: none; transition: border-color .2s; }
.form-group input:focus, .form-group select:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(79,70,229,0.1); }

/* Buttons */
.btn { display: inline-flex; align-items: center; justify-content: center; padding: 8px 16px; border: none; border-radius: 6px; font-size: 14px; font-weight: 500; cursor: pointer; transition: all .2s; }
.btn:disabled { opacity: 0.65; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover:not(:disabled) { background: #dc2626; }
.btn-sm { padding: 4px 10px; font-size: 12px; }
.btn-outline { background: transparent; border: 1px solid var(--border); color: var(--text); }
.btn-outline:hover { background: var(--bg); }

/* Tables */
.table-wrapper { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
table th { background: var(--bg); font-weight: 600; text-align: left; padding: 10px 12px; border-bottom: 2px solid var(--border); }
table td { padding: 10px 12px; border-bottom: 1px solid var(--border); }
table tr:hover { background: rgba(79,70,229,0.02); }

/* Usage summaries */
.usage-cell { white-space: normal; min-width: 220px; }
.usage-summary { display: grid; gap: 6px; min-width: 220px; max-width: 340px; }
.usage-summary-single { min-width: 250px; max-width: 290px; }
.usage-summary-embedded { min-width: 0; max-width: 100%; margin-top: 8px; }
.usage-period {
    padding: 7px 8px;
    border: 1px solid #e2e8f0;
    border-radius: 6px;
    background: #fff;
    box-shadow: inset 3px 0 0 var(--usage-accent, var(--primary));
}
.usage-period-week { --usage-accent: #0ea5e9; background: #f0f9ff; border-color: #bae6fd; }
.usage-period-month { --usage-accent: #10b981; background: #ecfdf5; border-color: #bbf7d0; }
.usage-period-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 5px; }
.usage-period-label { color: #0f172a; font-size: 12px; font-weight: 700; line-height: 1; }
.usage-call-count {
    display: inline-flex;
    align-items: center;
    max-width: 100%;
    padding: 1px 7px;
    border: 1px solid rgba(148, 163, 184, 0.35);
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.82);
    color: var(--text-light);
    font-size: 11px;
    font-weight: 600;
    line-height: 1.45;
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
}
.usage-token-row { display: flex; align-items: center; gap: 7px; flex-wrap: wrap; line-height: 1.2; }
.usage-token-row span:not(.usage-divider) { white-space: nowrap; }
.usage-token-row strong { color: var(--text); font-size: 13px; font-weight: 700; font-variant-numeric: tabular-nums; }
.usage-token-row em { margin-left: 3px; color: var(--text-light); font-size: 11px; font-style: normal; }
.usage-divider { width: 1px; height: 12px; background: rgba(100, 116, 139, 0.35); }

/* Status badges */
.badge { display: inline-flex; padding: 2px 8px; border-radius: 9999px; font-size: 12px; font-weight: 500; }
.badge-success { background: #d1fae5; color: #065f46; }
.badge-warning { background: #fef3c7; color: #92400e; }
.badge-danger { background: #fee2e2; color: #991b1b; }

/* Switch toggle (inline status editor for list tables) */
.switch { position: relative; display: inline-block; width: 40px; height: 22px; flex-shrink: 0; vertical-align: middle; }
.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; z-index: 2; }
.switch-slider { position: absolute; inset: 0; cursor: pointer; background: #cbd5e1; border-radius: 22px; transition: background .2s ease; }
.switch-slider::before { content: ""; position: absolute; height: 16px; width: 16px; left: 3px; bottom: 3px; background: #fff; border-radius: 50%; transition: transform .2s ease; box-shadow: 0 1px 3px rgba(0,0,0,0.25); }
.switch input:checked + .switch-slider { background: var(--success); }
.switch input:checked + .switch-slider::before { transform: translateX(18px); }
.switch input:disabled + .switch-slider { opacity: 0.65; cursor: not-allowed; }
.switch input:focus-visible + .switch-slider { box-shadow: 0 0 0 3px rgba(16,185,129,0.25); }
.switch-label { display: inline-flex; align-items: center; gap: 8px; }
.switch-label .switch-text { font-size: 12px; color: var(--text-light); white-space: nowrap; }

/* Login page */
.login-wrapper { display: flex; align-items: center; justify-content: center; min-height: 100vh; background: linear-gradient(135deg, #1e1b4b 0%, #4f46e5 100%); }
.login-card { background: #fff; border-radius: 12px; padding: 40px; width: 400px; box-shadow: 0 20px 60px rgba(0,0,0,0.3); }
.login-card h1 { text-align: center; font-size: 24px; margin-bottom: 8px; }
.login-card p { text-align: center; color: var(--text-light); margin-bottom: 24px; }
.login-card .form-group { margin-bottom: 16px; }
.login-card .btn { width: 100%; padding: 12px; }
.login-card .captcha-row { display: flex; gap: 8px; align-items: center; }
.login-card .captcha-row input { flex: 1; }
.login-card .captcha-row img { height: 40px; border-radius: 4px; cursor: pointer; border: 1px solid var(--border); }
.login-link-row { margin-top: 16px; text-align: center; font-size: 14px; }
.login-link-row a, .link-button { color: var(--primary); text-decoration: none; font-size: 14px; font-weight: 500; }
.login-link-row a:hover, .link-button:hover { color: var(--primary-hover); text-decoration: underline; }
.link-button { background: transparent; border: 0; cursor: pointer; padding: 0; font-family: inherit; }

/* Modal */
.modal-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); display: none; align-items: center; justify-content: center; z-index: 1000; }
.modal-overlay.active { display: flex; }
.modal { background: #fff; border-radius: 12px; padding: 24px; width: 500px; max-width: 90vw; max-height: 80vh; overflow-y: auto; }
.modal h3 { margin-bottom: 16px; font-size: 18px; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 20px; }

/* Pagination */
.pagination { display: flex; gap: 4px; margin-top: 16px; justify-content: center; }
.pagination button { min-width: 32px; }

/* Toast */
.toast { position: fixed; top: 20px; right: 20px; padding: 12px 20px; border-radius: 8px; color: #fff; font-size: 14px; z-index: 2000; animation: slideIn .3s ease; }
.toast-success { background: var(--success); }
.toast-error { background: var(--danger); }
@keyframes slideIn { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }

/* Log detail modal */
.modal-lg { width: 800px; max-width: 95vw; }
.detail-section { margin-top: 8px; }
.detail-section h4 { font-size: 13px; font-weight: 600; color: var(--text-light); margin: 12px 0 4px; text-transform: uppercase; letter-spacing: 0.05em; }
.detail-section h4:first-child { margin-top: 0; }
.json-detail { background: #1e1e2e; color: #cdd6f4; padding: 12px 16px; border-radius: 6px; font-family: 'Fira Code', 'Consolas', monospace; font-size: 12px; line-height: 1.5; max-height: 400px; overflow: auto; white-space: pre-wrap; word-break: break-word; }

/* Scrollbars — slim rounded thumbs tinted to the theme instead of the
   browser default. The 2px transparent border + background-clip leaves a
   gap around the thumb so it reads as a floating pill. Dark surfaces
   override the two custom properties with a light thumb. */
:root {
    --scrollbar-thumb: rgba(107, 114, 128, 0.35);
    --scrollbar-thumb-hover: rgba(79, 70, 229, 0.5);
    --scrollbar-thumb-active: rgba(79, 70, 229, 0.7);
}
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-corner { background: transparent; }
::-webkit-scrollbar-thumb {
    background: var(--scrollbar-thumb);
    border-radius: 999px;
    border: 2px solid transparent;
    background-clip: padding-box;
}
::-webkit-scrollbar-thumb:hover { background-color: var(--scrollbar-thumb-hover); }
::-webkit-scrollbar-thumb:active { background-color: var(--scrollbar-thumb-active); }

/* Firefox */
* { scrollbar-width: thin; scrollbar-color: var(--scrollbar-thumb) transparent; }

/* Dark surfaces: sidebar, mobile drawer, JSON detail view */
.sidebar, .sidebar *,
.mobile-drawer, .mobile-drawer *,
.json-detail {
    --scrollbar-thumb: rgba(255, 255, 255, 0.25);
    --scrollbar-thumb-hover: rgba(255, 255, 255, 0.4);
    --scrollbar-thumb-active: rgba(255, 255, 255, 0.55);
}
