:root {
  --bg: #f8fafc;
  --panel: #ffffff;
  --border: #dbeafe;
  --text: #1e293b;
  --heading: #1e3a8a;
  --muted: #64748b;
  --primary: #1e40af;
  --primary-hover: #1b3480;
  --primary-soft: #eaf0fe;
  --amber: #d97706;
  --amber-soft: #fef3e2;
  --danger: #dc2626;
  --danger-soft: #fdecec;
  --zero: #94a3b8;
  --radius: 10px;
  --radius-sm: 6px;
  --font-sans: "Fira Sans", system-ui, -apple-system, "Segoe UI", Arial, sans-serif;
  --font-mono: "Fira Code", ui-monospace, SFMono-Regular, Consolas, monospace;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06);
  --shadow-md: 0 4px 14px rgba(15, 23, 42, 0.08);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1220;
    --panel: #111a2c;
    --border: #1e293b;
    --text: #e2e8f0;
    --heading: #93c5fd;
    --muted: #94a3b8;
    --primary: #60a5fa;
    --primary-hover: #3b82f6;
    --primary-soft: rgba(96, 165, 250, 0.14);
    --amber: #fbbf24;
    --amber-soft: rgba(251, 191, 36, 0.14);
    --danger: #f87171;
    --danger-soft: rgba(248, 113, 113, 0.14);
    --zero: #64748b;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 14px rgba(0, 0, 0, 0.4);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text);
}

:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
  border-radius: 4px;
}

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

.topbar h1 { font-size: 20px; margin: 0; font-weight: 600; color: var(--heading); }
.muted { color: var(--muted); font-weight: 400; }
.topbar-actions { display: flex; align-items: center; gap: 10px; }

.btn {
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  padding: 8px 14px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 14px;
  font-family: inherit;
  transition: background 150ms ease, border-color 150ms ease;
}
.btn-ghost:hover { background: var(--bg); }
.btn-primary {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}
.btn-primary:hover { background: var(--primary-hover); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-sm {
  padding: 6px 12px;
  font-size: 13px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.btn-sm svg { flex-shrink: 0; }

.panel {
  margin: 20px 24px;
  padding: 18px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.hidden { display: none !important; }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 14px;
}

label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
}

input[type=text], input[type=password], input[type=date], select {
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  font-family: inherit;
  color: var(--text);
  background: var(--panel);
}

.input-with-btn {
  display: flex;
  gap: 8px;
}
.input-with-btn input { flex: 1; min-width: 0; }
.input-with-btn .btn { flex-shrink: 0; }

.actions {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 16px;
  flex-wrap: wrap;
}

.checkbox {
  flex-direction: row;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  cursor: pointer;
}
.checkbox input { width: auto; cursor: pointer; }

.hint {
  margin: 12px 0 0;
  font-size: 12px;
  color: var(--muted);
  line-height: 1.5;
}

.error {
  margin: 0 24px 20px;
  padding: 14px 16px;
  border-radius: var(--radius);
  background: var(--danger-soft);
  color: var(--danger);
  border: 1px solid var(--danger);
  font-size: 14px;
  white-space: pre-wrap;
}

/* ---- Live / auto-refresh bar ---- */
.live-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  margin: 16px 24px 0;
  padding: 10px 16px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}
.live-status {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  color: var(--heading);
  font-size: 14px;
}
.live-meta {
  display: flex;
  align-items: center;
  gap: 16px;
  font-family: var(--font-mono);
  font-size: 13px;
}
.live-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  display: inline-block;
  background: var(--zero);
  flex-shrink: 0;
}
.live-dot-live {
  background: #16a34a;
  animation: live-pulse 2s ease-in-out infinite;
}
.live-dot-loading {
  background: var(--amber);
  animation: live-pulse 1s ease-in-out infinite;
}
.live-dot-paused { background: var(--zero); }
.live-dot-error { background: var(--danger); }

@keyframes live-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(22, 163, 74, 0.45); }
  50% { box-shadow: 0 0 0 6px rgba(22, 163, 74, 0); }
}
@media (prefers-reduced-motion: reduce) {
  .live-dot { animation: none !important; }
}

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 14px;
  margin: 20px 24px;
}

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow-sm);
}
.card .label { font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.card .value { font-size: 24px; font-weight: 600; font-family: var(--font-mono); color: var(--heading); }

/* ---- Rankings ---- */
.rankings {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 16px;
  margin: 0 24px 20px;
}
.rank-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow-sm);
}
.rank-card h2 {
  font-size: 15px;
  margin: 0;
  font-weight: 600;
  color: var(--heading);
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.rank-sub { font-size: 12px; font-weight: 400; color: var(--muted); }
.rank-list {
  list-style: none;
  margin: 14px 0 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 9px;
}
.rank-item {
  display: grid;
  grid-template-columns: 16px 32px minmax(80px, 1fr) minmax(70px, 2.2fr) 34px;
  align-items: center;
  gap: 10px;
  font-size: 13px;
}

/* ---- Avatars ---- */
.avatar-wrap {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  overflow: hidden;
  flex-shrink: 0;
}
.avatar-fallback {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 11px;
  font-weight: 700;
}
.avatar-img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.agent-cell {
  display: flex;
  align-items: center;
  gap: 10px;
  white-space: nowrap;
}
.rank-num { color: var(--muted); font-family: var(--font-mono); text-align: right; }
.rank-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rank-bar-track {
  background: var(--bg);
  border-radius: 999px;
  height: 8px;
  overflow: hidden;
}
.rank-bar-fill {
  display: block;
  height: 100%;
  border-radius: 999px;
  transition: width 400ms ease;
}
.rank-fill-called { background: var(--amber); }
.rank-fill-answered { background: var(--primary); }
.rank-value { text-align: right; font-family: var(--font-mono); font-weight: 600; }
.rank-empty { color: var(--muted); font-size: 13px; padding: 8px 0; }
@media (prefers-reduced-motion: reduce) {
  .rank-bar-fill { transition: none; }
}

.table-wrap {
  margin: 0 24px 32px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.table-toolbar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  gap: 12px;
}
.table-toolbar input {
  flex: 1;
  min-width: 180px;
  max-width: 320px;
}
.ramal-filter { min-width: 200px; cursor: pointer; }
.toolbar-checkbox { font-size: 13px; white-space: nowrap; }
.table-toolbar #rowCount { margin-left: auto; font-family: var(--font-mono); font-size: 13px; }

.table-scroll { overflow-x: auto; }

table { border-collapse: collapse; width: 100%; font-size: 14px; }

th, td {
  padding: 10px 14px;
  text-align: left;
  white-space: nowrap;
  border-bottom: 1px solid var(--border);
}

th {
  background: var(--bg);
  font-weight: 600;
  cursor: pointer;
  user-select: none;
  position: sticky;
  top: 0;
  color: var(--heading);
}
th.group-inbound, td.group-inbound { background: rgba(30, 64, 175, 0.04); }
th.group-outbound, td.group-outbound { background: rgba(217, 119, 6, 0.05); }
th.group-total, td.group-total { background: rgba(100, 116, 139, 0.06); }

th .arrow { font-size: 11px; margin-left: 4px; color: var(--muted); }

td.num-pos { color: var(--primary); font-weight: 600; font-family: var(--font-mono); }
td.num-zero { color: var(--zero); font-family: var(--font-mono); }

tr:hover td { background: var(--bg); }

.empty {
  padding: 40px;
  text-align: center;
  color: var(--muted);
  margin: 0;
}

/* ---- Modo TV: alterna slides de 5 em 5s, sem rolagem, texto maior ---- */
.tv-slide-hidden { display: none !important; }

body.tv-mode {
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
body.tv-mode #settingsPanel,
body.tv-mode #toggleSettings,
body.tv-mode #toggleAutoRefresh,
body.tv-mode .table-toolbar #searchBox,
body.tv-mode .table-toolbar .ramal-filter,
body.tv-mode .table-toolbar .toolbar-checkbox {
  display: none;
}
body.tv-mode .topbar,
body.tv-mode .live-bar,
body.tv-mode #errorBox,
body.tv-mode #summaryCards {
  flex: 0 0 auto;
}
body.tv-mode #rankingsSection {
  flex: 1 1 auto;
  min-height: 0;
  margin-bottom: 16px;
  grid-template-rows: 1fr;
}
body.tv-mode .rank-card {
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 28px 32px;
}
body.tv-mode .rank-list {
  flex: 1 1 auto;
  min-height: 0;
  justify-content: space-evenly;
  gap: 6px;
  overflow-y: auto;
}
body.tv-mode .rank-item {
  font-size: 20px;
  grid-template-columns: 26px 40px minmax(120px, 1fr) minmax(100px, 2.2fr) 54px;
  gap: 14px;
}
body.tv-mode .rank-bar-track { height: 14px; }
body.tv-mode .table-wrap {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  margin-bottom: 16px;
}
body.tv-mode .table-scroll {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
}
body.tv-mode .topbar h1 { font-size: 26px; }
body.tv-mode .card .value { font-size: 34px; }
body.tv-mode .card .label { font-size: 14px; }
body.tv-mode .rank-card h2 { font-size: 23px; }
body.tv-mode .rank-sub { font-size: 15px; }
body.tv-mode table { font-size: 17px; }
body.tv-mode th, body.tv-mode td { padding: 12px 16px; }
