/* --- Reset & Base --- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --bg: #131F28;
    --bg-card: #1A2A36;
    --bg-card-hover: #223442;
    --border: #2c3e4f;
    --text: #DCE1E4;
    --text-muted: #84A7BD;
    --accent: #69AADB;
    --accent-light: #88C5F3;
    --green: #22c55e;
    --green-bg: rgba(34, 197, 94, 0.12);
    --yellow: #eab308;
    --yellow-bg: rgba(234, 179, 8, 0.12);
    --red: #ef4444;
    --red-bg: rgba(239, 68, 68, 0.12);
}

[data-theme="light"] {
    --bg: #F5F7FA;
    --bg-card: #FFFFFF;
    --bg-card-hover: #F0F2F5;
    --border: #E2E8F0;
    --text: #1A202C;
    --text-muted: #718096;
    --accent: #3182CE;
    --accent-light: #4299E1;
    --green: #38A169;
    --green-bg: rgba(56, 161, 105, 0.1);
    --yellow: #D69E2E;
    --yellow-bg: rgba(214, 158, 46, 0.1);
    --red: #E53E3E;
    --red-bg: rgba(229, 62, 62, 0.1);
}

body {
    font-family: "Poppins", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    min-height: 100vh;
}

a { color: var(--accent); text-decoration: none; }
a:hover { color: var(--accent-light); }

/* --- Header --- */
header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 2rem;
    border-bottom: 1px solid var(--border);
    background: var(--bg-card);
}

.header-left { display: flex; align-items: center; gap: 1rem; }

.header-starface-icon {
    height: 36px;
    width: 36px;
    color: var(--text);
}

.header-pbx-icon {
    height: 36px;
    width: 36px;
    filter: brightness(0) invert(1);
}

[data-theme="light"] .header-pbx-icon {
    filter: brightness(0);
}

.footer-logo {
    height: 20px;
    width: auto;
    opacity: 0.5;
    margin-bottom: 0.25rem;
}

.header-user {
    display: flex;
    align-items: center;
    text-decoration: none;
    border-radius: 50%;
    transition: opacity 0.15s;
}

.header-user:hover { opacity: 0.75; }

.header-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    object-fit: cover;
}

.avatar-initials-sm {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--accent);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.75rem;
    font-weight: 700;
    flex-shrink: 0;
}

.header-left h1 { font-size: 1.3rem; font-weight: 600; }
.subtitle { color: var(--text-muted); font-size: 0.85rem; }

.header-right {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    font-size: 0.85rem;
    color: var(--text-muted);
}

.status-dot {
    width: 10px; height: 10px; border-radius: 50%;
    background: var(--text-muted);
}
.status-dot.online { background: var(--green); box-shadow: 0 0 6px var(--green); }
.status-dot.loading { background: var(--yellow); animation: pulse 1s infinite; }
.status-dot.error { background: var(--red); }

@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.4; } }

.btn-refresh {
    background: none;
    border: 1px solid var(--border);
    color: var(--text-muted);
    padding: 0.3rem 0.6rem;
    border-radius: 6px;
    cursor: pointer;
    font-size: 1.1rem;
    transition: all 0.2s;
}
.btn-refresh:hover { color: var(--text); border-color: var(--accent); }

/* --- KPI Bar --- */
.kpi-bar {
    display: flex;
    gap: 1rem;
    padding: 1.5rem 2rem;
    overflow-x: auto;
}

.kpi-card {
    flex: 1;
    min-width: 130px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 1rem 1.25rem;
    text-align: center;
}
.kpi-card.kpi-green { border-color: rgba(34, 197, 94, 0.3); }
.kpi-card.kpi-red { border-color: rgba(239, 68, 68, 0.3); }

.kpi-value { font-size: 1.8rem; font-weight: 700; line-height: 1.2; }
.kpi-green .kpi-value { color: var(--green); }
.kpi-red .kpi-value { color: var(--red); }

.kpi-label { font-size: 0.8rem; color: var(--text-muted); margin-top: 0.25rem; }

/* --- Tabs + Controls --- */
.controls-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid var(--border);
    padding-right: 2rem;
}
.controls-bar .tabs { border-bottom: none; }

.tabs { display: flex; gap: 0; padding: 0 2rem; border-bottom: 1px solid var(--border); }
.tab {
    background: none; border: none; color: var(--text-muted);
    padding: 0.75rem 1.5rem; font-size: 0.9rem; cursor: pointer;
    border-bottom: 2px solid transparent; transition: all 0.2s; font-family: inherit;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); }

.tab-content { display: none; padding: 1.5rem 2rem; }
.tab-content.active { display: block; }

.period-selector { display: flex; gap: 0.25rem; background: var(--bg); border-radius: 8px; padding: 0.25rem; }
.period-btn {
    background: none; border: none; color: var(--text-muted);
    padding: 0.4rem 0.8rem; font-size: 0.8rem; font-family: inherit;
    cursor: pointer; border-radius: 6px; transition: all 0.2s;
}
.period-btn:hover { color: var(--text); background: var(--bg-card); }
.period-btn.active { background: var(--accent); color: #fff; }

.period-divider { width: 1px; height: 1.4rem; background: var(--border); margin: 0 0.25rem; }

.date-input {
    background: var(--bg-card); border: 1px solid var(--border); color: var(--text);
    padding: 0.3rem 0.5rem; font-size: 0.8rem; font-family: inherit;
    border-radius: 6px; cursor: pointer;
}
.date-input:focus { outline: none; border-color: var(--accent); }
.date-separator { color: var(--text-muted); font-size: 0.8rem; margin: 0 0.15rem; }

/* --- Queue Grid --- */
.queue-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 1rem; }

.queue-card {
    background: var(--bg-card); border: 1px solid var(--border);
    border-radius: 10px; padding: 1.25rem; transition: transform 0.15s, background 0.15s;
}
.queue-card:hover { background: var(--bg-card-hover); transform: translateY(-2px); }
.queue-card.status-neutral { border-left: 2px solid var(--border); }
.queue-card.status-good { border-left: 4px solid var(--green); }
.queue-card.status-medium { border-left: 4px solid var(--yellow); }
.queue-card.status-bad { border-left: 4px solid var(--red); }
.queue-card.status-warning { border-left: 4px solid var(--yellow); box-shadow: inset 0 0 8px rgba(234,179,8,0.1); }
.queue-card.status-critical { border-left: 4px solid var(--red); box-shadow: inset 0 0 8px rgba(239,68,68,0.15); animation: pulse-border 2s infinite; }
@keyframes pulse-border { 0%, 100% { border-left-color: var(--red); } 50% { border-left-color: rgba(239,68,68,0.4); } }

.queue-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 1rem; }
.queue-header h3 { font-size: 1rem; font-weight: 600; }

.queue-status-badge {
    font-size: 0.7rem; padding: 0.2rem 0.6rem; border-radius: 20px;
    font-weight: 500; text-transform: uppercase; letter-spacing: 0.5px;
}
.queue-status-badge.neutral { background: rgba(139,143,163,0.1); color: var(--text-muted); }
.queue-status-badge.good { background: var(--green-bg); color: var(--green); }
.queue-status-badge.medium { background: var(--yellow-bg); color: var(--yellow); }
.queue-status-badge.bad { background: var(--red-bg); color: var(--red); }
.queue-status-badge.warning { background: var(--yellow-bg); color: var(--yellow); }
.queue-status-badge.critical { background: var(--red-bg); color: var(--red); animation: pulse 1s infinite; }

.queue-metrics { display: flex; gap: 1rem; margin-bottom: 1rem; }
.metric { flex: 1; text-align: center; }
.metric-value { font-size: 1.4rem; font-weight: 700; display: block; }
.metric-label { font-size: 0.7rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.5px; }

/* --- Agent List in Queue Cards --- */
.agent-list { border-top: 1px solid var(--border); padding: 0.6rem 0; margin-bottom: 0.5rem; }
.agent-row { display: flex; align-items: center; gap: 0.5rem; padding: 0.2rem 0; font-size: 0.8rem; }
.agent-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.agent-dot.online { background: var(--green); box-shadow: 0 0 4px var(--green); }
.agent-dot.available { background: var(--accent); box-shadow: 0 0 4px var(--accent); }
.agent-dot.busy { background: var(--red); box-shadow: 0 0 4px var(--red); }
.agent-dot.paused { background: var(--yellow); }
.agent-dot.offline { background: var(--red); opacity: 0.5; }
.agent-dot.inactive { background: var(--text-muted); opacity: 0.5; }

.agent-row-name { flex: 1; color: var(--text); }
.agent-row .status-pill { font-size: 0.65rem; padding: 0.1rem 0.4rem; }
.no-agents { color: var(--text-muted); font-size: 0.75rem; font-style: italic; }

.queue-history { border-top: 1px solid var(--border); padding-top: 0.75rem; font-size: 0.8rem; }
.hist-row { display: flex; justify-content: space-between; gap: 0.5rem; margin-bottom: 0.25rem; color: var(--text-muted); }

.answer-rate { font-weight: 600; }
.answer-rate.good { color: var(--green); }
.answer-rate.medium { color: var(--yellow); }
.answer-rate.bad { color: var(--red); }

/* --- Tables --- */
.agent-table { width: 100%; border-collapse: collapse; }
.agent-table th {
    text-align: left; padding: 0.75rem 1rem; font-size: 0.75rem;
    color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.5px;
    border-bottom: 1px solid var(--border);
}
.agent-table td { padding: 0.75rem 1rem; border-bottom: 1px solid var(--border); font-size: 0.9rem; }
.agent-table tr:hover { background: var(--bg-card-hover); }

.agent-name { font-weight: 600; }
.agent-name .agent-dot { display: inline-block; vertical-align: middle; margin-right: 0.5rem; }
.unassigned-row { border-top: 2px solid var(--border); }
.unassigned-name { color: var(--text-muted); font-style: italic; }

.call-log-hint { font-size: 0.7rem; color: var(--yellow); text-align: center; padding: 0.4rem 1rem; margin-bottom: 0.75rem; }

.live-summary-bar {
    display: flex; gap: 1.5rem; padding: 0.75rem 1rem; margin-bottom: 0.5rem;
    background: var(--bg-card); border-radius: 8px; font-size: 0.85rem; color: var(--text-muted);
}
.live-stat strong { color: var(--text); }
.live-call-active { background: rgba(239, 68, 68, 0.06); }
.live-call-ringing { background: rgba(234, 179, 8, 0.08); animation: pulse 1.5s infinite; }
.num-green { color: var(--green); font-weight: 600; }
.num-red { color: var(--red); font-weight: 600; }
.queue-list { color: var(--text-muted); font-size: 0.8rem; }

.status-pill {
    display: inline-block; padding: 0.2rem 0.6rem; border-radius: 20px;
    font-size: 0.75rem; font-weight: 500;
}
.status-pill.online { background: var(--green-bg); color: var(--green); }
.status-pill.available { background: rgba(105, 170, 219, 0.12); color: var(--accent); }
.status-pill.busy { background: var(--red-bg); color: var(--red); }
.status-pill.paused { background: var(--yellow-bg); color: var(--yellow); }
.status-pill.offline { background: var(--red-bg); color: var(--red); opacity: 0.6; }
.status-pill.inactive { background: rgba(139,143,163,0.1); color: var(--text-muted); }

.no-data { text-align: center; color: var(--text-muted); padding: 3rem 1rem !important; font-style: italic; }

/* --- Call Log Filters --- */
.call-log-filters { display: flex; gap: 0.75rem; margin-bottom: 1rem; flex-wrap: wrap; }
.call-log-filters select {
    background: var(--bg-card); border: 1px solid var(--border); color: var(--text);
    padding: 0.5rem 0.75rem; border-radius: 6px; font-family: inherit; font-size: 0.85rem; cursor: pointer;
}
.call-log-filters select:focus { border-color: var(--accent); outline: none; }

.call-time { font-variant-numeric: tabular-nums; white-space: nowrap; font-size: 0.8rem; }
.call-number { font-weight: 500; font-variant-numeric: tabular-nums; }
.call-in { color: var(--accent); }
.call-out { color: var(--yellow); }

/* --- Footer --- */
footer {
    display: flex; flex-direction: column; align-items: center; gap: 0.3rem;
    padding: 1.5rem; color: var(--text-muted); font-size: 0.75rem;
    border-top: 1px solid var(--border); margin-top: 2rem;
}
.footer-sub { font-size: 0.65rem; opacity: 0.6; }

/* --- Login Page --- */
.login-page {
    display: flex; justify-content: center; align-items: center;
    min-height: 100vh; background: var(--bg);
}
.login-container { width: 100%; max-width: 400px; padding: 2rem; }
.login-card {
    background: var(--bg-card); border: 1px solid var(--border);
    border-radius: 12px; padding: 2.5rem;
}
.login-header { text-align: center; margin-bottom: 2rem; }
.login-header h1 { font-size: 1.5rem; font-weight: 700; margin-bottom: 0.25rem; }
.login-subtitle { color: var(--text-muted); font-size: 0.9rem; }
.login-error {
    background: var(--red-bg); color: var(--red); border: 1px solid rgba(239,68,68,0.3);
    border-radius: 8px; padding: 0.75rem 1rem; margin-bottom: 1.5rem; font-size: 0.85rem; text-align: center;
}
.login-footer { text-align: center; margin-top: 2rem; font-size: 0.7rem; color: var(--text-muted); opacity: 0.6; }

/* --- Form Elements --- */
.form-group { margin-bottom: 1rem; }
.form-group label { display: block; font-size: 0.8rem; color: var(--text-muted); margin-bottom: 0.3rem; font-weight: 500; }
.form-group input, .form-group select {
    width: 100%; background: var(--bg); border: 1px solid var(--border); color: var(--text);
    padding: 0.75rem 1rem; border-radius: 8px; font-family: inherit; font-size: 0.9rem;
    text-base sm:text-sm;
}
.form-group input:focus, .form-group select:focus { outline: none; border-color: var(--accent); }
.form-group input::placeholder { color: var(--text-muted); opacity: 0.5; }
.form-group small { font-size: 0.7rem; color: var(--text-muted); opacity: 0.7; }

.form-row { display: flex; gap: 1rem; }
.form-row .form-group { flex: 1; }

.btn-primary {
    width: 100%; background: var(--accent); color: #fff; border: none;
    padding: 0.75rem 1.5rem; border-radius: 8px; font-family: inherit;
    font-size: 0.9rem; font-weight: 600; cursor: pointer; transition: all 0.2s;
}
.btn-primary:hover { background: var(--accent-light); }

.btn-accent {
    display: inline-block; background: var(--accent); color: #fff;
    padding: 0.5rem 1rem; border-radius: 6px; font-size: 0.85rem; font-weight: 500;
    transition: all 0.2s; border: none; cursor: pointer; font-family: inherit;
}
.btn-accent:hover { background: var(--accent-light); color: #fff; }

.btn-ghost {
    display: inline-block; background: none; color: var(--text-muted);
    padding: 0.5rem 1rem; border: 1px solid var(--border); border-radius: 6px;
    font-size: 0.85rem; cursor: pointer; transition: all 0.2s; font-family: inherit;
}
.btn-ghost:hover { color: var(--text); border-color: var(--accent); }

.btn-sm { padding: 0.3rem 0.7rem; font-size: 0.75rem; border-radius: 5px; }
.btn-danger {
    background: var(--red-bg); color: var(--red); border: 1px solid rgba(239,68,68,0.3);
    cursor: pointer; font-family: inherit;
}
.btn-danger:hover { background: rgba(239,68,68,0.2); }

/* --- Admin Page --- */
.admin-header {
    display: flex; justify-content: space-between; align-items: center;
    padding: 1rem 2rem; border-bottom: 1px solid var(--border); background: var(--bg-card);
}
.admin-header .header-left { display: flex; align-items: center; gap: 1rem; }
.admin-header .header-right { display: flex; gap: 0.75rem; align-items: center; }
.admin-header h1 { font-size: 1.3rem; font-weight: 600; }

.back-link { color: var(--text-muted); font-size: 0.85rem; }
.back-link:hover { color: var(--accent); }

.admin-content { max-width: 1200px; margin: 0 auto; padding: 2rem; }
.admin-content h2 { font-size: 1.1rem; margin-bottom: 1.5rem; }

.admin-section {
    background: var(--bg-card); border: 1px solid var(--border);
    border-radius: 10px; padding: 1.5rem; margin-bottom: 2rem;
}
.admin-section h2 { font-size: 1rem; margin-bottom: 1rem; }
.admin-section h3 { font-size: 0.9rem; margin: 1.5rem 0 0.75rem; color: var(--text-muted); }

.admin-form { max-width: 800px; }
.admin-form .btn-primary { width: auto; }

/* --- Tenant Grid --- */
.tenant-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 1rem; }
.tenant-card {
    display: block; background: var(--bg-card); border: 1px solid var(--border);
    border-radius: 10px; padding: 1.25rem; transition: all 0.2s; text-decoration: none; color: var(--text);
}
.tenant-card:hover { background: var(--bg-card-hover); transform: translateY(-2px); color: var(--text); }

.tenant-card-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.5rem; }
.tenant-card-header h3 { font-size: 1rem; font-weight: 600; }
.tenant-card-meta { display: flex; gap: 1rem; font-size: 0.8rem; color: var(--text-muted); }
.meta-slug { font-family: monospace; }

.status-badge {
    font-size: 0.7rem; padding: 0.15rem 0.5rem; border-radius: 20px;
    text-transform: uppercase; font-weight: 500;
}
.status-badge.active { background: var(--green-bg); color: var(--green); }
.status-badge.inactive { background: rgba(139,143,163,0.1); color: var(--text-muted); }

.empty-state { text-align: center; padding: 4rem 2rem; color: var(--text-muted); }
.empty-state p { margin-bottom: 1.5rem; }

/* --- Connection Cards --- */
.connection-list { display: flex; flex-direction: column; gap: 1rem; margin-bottom: 1.5rem; }
.connection-card {
    background: var(--bg); border: 1px solid var(--border);
    border-radius: 8px; padding: 1rem;
}
.connection-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.5rem; }
.connection-header h3 { font-size: 0.95rem; font-weight: 600; }
.connection-actions { display: flex; gap: 0.5rem; }
.connection-meta { display: flex; gap: 1.5rem; font-size: 0.8rem; color: var(--text-muted); flex-wrap: wrap; }
.conn-status.ok { color: var(--green); }
.conn-status.pending { color: var(--text-muted); }
.connection-result { margin-top: 0.5rem; font-size: 0.85rem; }
.connection-edit-form .form-row { margin-bottom: 0; }
.connection-edit-form .form-group { margin-bottom: 0.75rem; }
.connection-edit-form .form-group input { font-size: 0.85rem; padding: 0.5rem 0.75rem; }
.connection-status-bar {
    display: flex; justify-content: space-between; align-items: center;
    padding-top: 0.5rem; border-top: 1px solid var(--border); margin-top: 0.5rem;
}

.manual-queue-add { margin-top: 0.75rem; font-size: 0.85rem; }
.manual-queue-add summary { cursor: pointer; color: var(--text-muted); }
.manual-queue-add summary:hover { color: var(--accent); }
.manual-queue-form .form-row { align-items: flex-end; gap: 0.5rem; margin-top: 0.5rem; }
.manual-queue-form .form-group { margin-bottom: 0; }
.queue-tag {
    display: inline-block; background: rgba(105,170,219,0.15); color: var(--accent);
    padding: 0.2rem 0.5rem; border-radius: 4px; font-size: 0.8rem; margin: 0.2rem;
}

.text-green { color: var(--green); }
.text-red { color: var(--red); }
.text-muted { color: var(--text-muted); }

/* --- Queue Selection --- */
.queue-selection { margin-top: 1rem; }
.queue-selection h4 { font-size: 0.85rem; color: var(--text-muted); margin-bottom: 0.5rem; }
.queue-checkboxes { display: flex; flex-wrap: wrap; gap: 0.5rem 1rem; margin-bottom: 0.75rem; }
.checkbox-label {
    display: flex; align-items: center; gap: 0.35rem;
    font-size: 0.85rem; cursor: pointer;
}
.checkbox-label input { accent-color: var(--accent); }
.select-all { margin-bottom: 0.25rem; }

/* --- Admin Table --- */
.admin-table { width: 100%; border-collapse: collapse; margin-bottom: 1rem; }
.admin-table th {
    text-align: left; padding: 0.6rem 0.75rem; font-size: 0.75rem;
    color: var(--text-muted); text-transform: uppercase; border-bottom: 1px solid var(--border);
}
.admin-table td { padding: 0.6rem 0.75rem; border-bottom: 1px solid var(--border); font-size: 0.85rem; }
.admin-table tr:hover { background: var(--bg-card-hover); }

.role-badge {
    font-size: 0.7rem; padding: 0.15rem 0.5rem; border-radius: 20px;
    background: rgba(105, 170, 219, 0.12); color: var(--accent);
    text-transform: uppercase;
}

/* --- Alert --- */
.alert { padding: 0.75rem 1rem; border-radius: 8px; margin-bottom: 1.5rem; font-size: 0.85rem; }
.alert-error { background: var(--red-bg); color: var(--red); border: 1px solid rgba(239,68,68,0.3); }

/* --- Responsive --- */
@media (max-width: 768px) {
    header, .admin-header { flex-direction: column; gap: 0.5rem; text-align: center; }
    .kpi-bar { flex-wrap: wrap; padding: 1rem; }
    .kpi-card { min-width: calc(50% - 0.5rem); }
    .queue-grid, .tenant-grid { grid-template-columns: 1fr; }
    .tabs { padding: 0 1rem; }
    .tab-content { padding: 1rem; }
    .agent-table { font-size: 0.8rem; }
    .agent-table th, .agent-table td { padding: 0.5rem; }
    .form-row { flex-direction: column; gap: 0; }
    .controls-bar { flex-direction: column; padding: 0; }
    .period-selector { padding: 0.5rem 1rem; flex-wrap: wrap; justify-content: center; }
    .admin-content { padding: 1rem; }
}
