:root {
  --primary: #C1440E;
  --primary-dark: #A8380B;
  --primary-light: #FDEEE0;
  --success: #0E7A3C;
  --warning: #8A5A00;
  --danger: #C2261B;
  --dark: #1A1614;
  --gray-900: #3E3833;
  --gray-600: #5F5852;
  --gray-300: #E4E1DC;
  --gray-100: #F7F6F4;
  --white: #FFFFFF;
  --sidebar-width: 260px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--gray-300); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--gray-600); }

body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--gray-100);
  color: var(--dark);
  min-height: 100vh;
}

/* ============ LOGIN ============ */
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #C1440E 0%, #7A2408 100%);
  padding: 1rem;
}

.login-card {
  background: var(--white);
  border-radius: 16px;
  padding: 3rem 2.5rem;
  width: 100%;
  max-width: 420px;
  box-shadow: 0 8px 40px rgba(0,0,0,0.2);
}

.login-card .logo {
  text-align: center;
  margin-bottom: 2rem;
}

/* El simbolo de marca hereda el color del contenedor, asi que vale igual en
   los dos temas sin duplicar marcado. 36 sobre 64 deja el aire justo. */
.logo-icon svg {
  width: 44px;
  height: 44px;
}

.login-card .logo-icon {
  width: 64px;
  height: 64px;
  border-radius: 16px;
  background: var(--primary-light);
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 0.75rem;
}

.login-card .logo h1 {
  font-size: 1.8rem;
  color: var(--primary);
  font-weight: 700;
}

.login-card .logo p {
  color: var(--gray-600);
  font-size: 0.9rem;
  margin-top: 0.25rem;
}

.auth-link {
  text-align: center;
  margin-top: 1.25rem;
  font-size: 0.9rem;
  color: var(--gray-600);
}

.auth-link a {
  color: var(--primary);
  text-decoration: none;
  font-weight: 600;
}

.auth-link a:hover {
  text-decoration: underline;
}

/* ============ LAYOUT ============ */
.app-layout {
  display: flex;
  min-height: 100vh;
}

/* Sidebar */
.sidebar {
  width: var(--sidebar-width);
  background: var(--white);
  border-right: 1px solid var(--gray-300);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  z-index: 100;
  transition: transform 0.3s ease;
}

.sidebar-header {
  padding: 1.25rem 1.5rem;
  border-bottom: 1px solid var(--gray-300);
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.sidebar-header h2 {
  font-size: 1.3rem;
  color: var(--primary);
  font-weight: 700;
}

.sidebar-header .user-info {
  margin-top: 0.75rem;
  display: flex;
  align-items: center;
  gap: 0.65rem;
}

.user-avatar {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: var(--primary);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.9rem;
  flex-shrink: 0;
}

.user-details {
  overflow: hidden;
}

.sidebar-header .user-name {
  font-weight: 600;
  color: var(--dark);
  font-size: 0.85rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar-header .user-balance {
  color: var(--success);
  font-weight: 700;
  font-size: 1rem;
}

.sidebar-nav {
  flex: 1;
  padding: 0.75rem 0;
  overflow-y: auto;
}

.sidebar-nav a {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 1.5rem;
  color: var(--gray-900);
  text-decoration: none;
  font-size: 0.9rem;
  transition: all 0.15s;
  border-left: 3px solid transparent;
}

.sidebar-nav a:hover {
  background: var(--gray-100);
  color: var(--primary);
  padding-left: 1.75rem;
}

.sidebar-nav a.active {
  background: var(--primary-light);
  color: var(--primary);
  font-weight: 600;
  border-left-color: var(--primary);
  padding-left: 1.75rem;
}

.sidebar-nav a svg {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
}

.sidebar-footer {
  padding: 1rem 1.5rem;
  border-top: 1px solid var(--gray-300);
}

.sidebar-footer a {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: var(--danger);
  text-decoration: none;
  font-size: 0.9rem;
}

/* Main content */
.main-content {
  flex: 1;
  margin-left: var(--sidebar-width);
  padding: 2rem;
  max-width: calc(100% - var(--sidebar-width));
}

.page-header {
  margin-bottom: 1.5rem;
}

.page-header h1 {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--dark);
}

.page-header p {
  color: var(--gray-600);
  font-size: 0.9rem;
  margin-top: 0.25rem;
}

/* Mobile hamburger */
.mobile-toggle {
  display: none;
  position: fixed;
  top: 1rem;
  left: 1rem;
  z-index: 200;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 8px;
  width: 40px;
  height: 40px;
  cursor: pointer;
  font-size: 1.2rem;
}

/* ============ VIEWS TRANSITIONS ============ */
.view {
  animation: viewFadeIn 0.25s ease;
}
@keyframes viewFadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ============ PHONE STATUS BAR ============ */
.phone-status-bar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 1.5rem;
  background: var(--success);
  color: white;
  font-size: 0.8rem;
  cursor: pointer;
  border-radius: 0 0 12px 12px;
  margin: 0 1rem 1rem;
  transition: background 0.2s;
}
.phone-status-bar:hover { background: #2d9249; }

/* ============ CARDS ============ */
.card {
  background: var(--white);
  border-radius: 12px;
  border: 1px solid var(--gray-300);
  padding: 1.5rem;
  margin-bottom: 1rem;
  transition: box-shadow 0.2s;
}
.card:hover {
  box-shadow: 0 2px 12px rgba(0,0,0,0.06);
}

.card-title {
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--gray-600);
  margin-bottom: 0.75rem;
  font-weight: 600;
}

/* Stats grid */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.stat-card {
  background: var(--white);
  border-radius: 12px;
  border: 1px solid var(--gray-300);
  padding: 1.25rem;
  transition: transform 0.15s, box-shadow 0.15s;
}
.stat-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(0,0,0,0.08);
}

.stat-card {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.stat-icon {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.stat-balance .stat-icon { background: #E7F4EC; color: var(--success); }
.stat-calls .stat-icon { background: #FDEEE0; color: var(--primary); }
.stat-minutes .stat-icon { background: #FEF3D6; color: #8A5A00; }
.stat-cost .stat-icon { background: #FCEAE8; color: var(--danger); }

.stat-card .stat-label {
  font-size: 0.75rem;
  color: var(--gray-600);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.stat-card .stat-value {
  font-size: 1.6rem;
  font-weight: 700;
  color: var(--dark);
  margin-top: 0.1rem;
  line-height: 1.2;
}

.stat-card .stat-value.balance {
  color: var(--success);
}

.stat-card .stat-value.cost {
  color: var(--danger);
}

/* ============ TABLES ============ */
.table-container {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

table th {
  background: var(--gray-100);
  padding: 0.75rem 1rem;
  text-align: left;
  font-weight: 600;
  color: var(--gray-900);
  border-bottom: 2px solid var(--gray-300);
  white-space: nowrap;
}

table td {
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--gray-300);
  color: var(--gray-900);
}

table tr {
  transition: background 0.15s;
}
table tr:hover {
  background: var(--primary-light);
}

table .number {
  font-family: 'Consolas', 'Monaco', monospace;
}

table .text-right {
  text-align: right;
}

/* ============ FORMS ============ */
.form-group {
  margin-bottom: 1rem;
}

.form-group label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--gray-900);
  margin-bottom: 0.35rem;
}

.form-control {
  width: 100%;
  padding: 0.65rem 0.85rem;
  border: 1px solid var(--gray-300);
  border-radius: 8px;
  font-size: 0.9rem;
  color: var(--dark);
  transition: border-color 0.15s;
  background: var(--white);
}

/* ---------------------------------------------------------------------------
 * FOCO DE TECLADO
 *
 * Antes solo habia una regla de foco y hacia `outline: none`, es decir, QUITABA
 * el anillo que dibuja el navegador. El portal se quedaba sin ninguna senal de
 * donde estas al navegar con el tabulador, y eso en una herramienta con
 * formularios, tablas y un marcador telefonico deja a la gente a ciegas.
 *
 * `:focus-visible` es la pieza que faltaba: el navegador solo lo aplica cuando
 * el foco viene del teclado, no de un clic. Asi se recupera la accesibilidad
 * sin que salga un recuadro cada vez que alguien pulsa un boton con el raton.
 * ------------------------------------------------------------------------- */

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

/* Al raton no se le dibuja el anillo; al teclado si. Sin esta linea, los
   navegadores que no distinguen ambos casos ensenarian el recuadro siempre. */
:focus:not(:focus-visible) {
  outline: none;
}

.form-control:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(193, 68, 14, 0.12);
}

/* El campo enfocado con teclado lleva ademas el anillo: el cambio de borde
   solo no basta, es un contraste demasiado sutil para ser una senal. */
.form-control:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 1px;
}

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

/* ============ BUTTONS ============ */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.65rem 1.25rem;
  border: none;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
  text-decoration: none;
}

.btn-primary {
  background: var(--primary);
  color: white;
}
.btn-primary:hover { background: var(--primary-dark); }

.btn-success {
  background: var(--success);
  color: white;
}

.btn-outline {
  background: transparent;
  border: 1px solid var(--gray-300);
  color: var(--gray-900);
}
.btn-outline:hover { background: var(--gray-100); }

.btn-block {
  width: 100%;
}

.btn-sm {
  padding: 0.4rem 0.75rem;
  font-size: 0.8rem;
}

.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* ============ FILTERS ============ */
.filters {
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
  align-items: end;
  margin-bottom: 1rem;
}

.filters .form-group {
  margin-bottom: 0;
}

.filters .form-control {
  min-width: 150px;
}

/* ============ PAGINATION ============ */
.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  margin-top: 1.5rem;
}

.pagination button {
  padding: 0.4rem 0.85rem;
  border: 1px solid var(--gray-300);
  background: var(--white);
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.85rem;
}

.pagination button.active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

.pagination button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.pagination .page-info {
  font-size: 0.85rem;
  color: var(--gray-600);
}

/* ============ ALERTS ============ */
.alert {
  padding: 0.75rem 1rem;
  border-radius: 8px;
  font-size: 0.9rem;
  margin-bottom: 1rem;
}

.alert-error {
  background: #FCEAE8;
  color: var(--danger);
  border: 1px solid #f5c6cb;
}

.alert-success {
  background: #E7F4EC;
  color: var(--success);
  border: 1px solid #b7e1cd;
}

/* ============ LOADING ============ */
.spinner {
  display: inline-block;
  width: 20px;
  height: 20px;
  border: 2px solid var(--gray-300);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.loading-overlay {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 3rem;
  color: var(--gray-600);
  gap: 0.75rem;
}

/* Skeleton loading */
.skeleton {
  background: linear-gradient(90deg, var(--gray-100) 25%, var(--gray-300) 50%, var(--gray-100) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
  border-radius: 6px;
}
@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}
.skeleton-text { height: 12px; margin-bottom: 0.5rem; }
.skeleton-value { height: 28px; width: 80%; }
.skeleton-row { height: 40px; margin-bottom: 0.5rem; border-radius: 6px; }
.skeleton-card { display: block !important; }

/* ============ BADGES ============ */
.badge {
  display: inline-block;
  padding: 0.2rem 0.6rem;
  border-radius: 20px;
  font-size: 0.75rem;
  font-weight: 600;
}

.badge-success { background: #E7F4EC; color: var(--success); }
.badge-warning { background: #FEF3D6; color: #8A5A00; }
.badge-danger { background: #FCEAE8; color: var(--danger); }

/* ============ DURATION ============ */
.duration {
  font-family: 'Consolas', 'Monaco', monospace;
  font-size: 0.85rem;
}

/* ============ EMPTY STATE ============ */
.empty-state {
  text-align: center;
  padding: 3rem;
  color: var(--gray-600);
}

.empty-state svg {
  width: 48px;
  height: 48px;
  margin-bottom: 1rem;
  opacity: 0.5;
}

/* ============ MOBILE BOTTOM NAV ============ */
.mobile-bottom-nav {
  display: none;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 200;
  background: var(--white);
  border-top: 1px solid var(--gray-300);
  padding: 0.4rem 0;
  padding-bottom: max(0.4rem, env(safe-area-inset-bottom));
  justify-content: space-around;
}
.mobile-nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  text-decoration: none;
  color: var(--gray-600);
  font-size: 0.6rem;
  padding: 0.2rem 0.5rem;
  border-radius: 8px;
  transition: color 0.15s;
  position: relative;
}
.mobile-nav-item svg { width: 22px; height: 22px; }
.mobile-nav-item.active { color: var(--primary); font-weight: 700; }
.mobile-nav-phone svg {
  background: var(--primary);
  stroke: white;
  border-radius: 12px;
  padding: 4px;
  width: 32px;
  height: 32px;
  margin-top: -8px;
  box-shadow: 0 2px 8px rgba(26,115,232,0.3);
}
.mobile-nav-badge {
  position: absolute;
  top: -2px;
  right: 0;
  background: var(--danger);
  color: white;
  font-size: 0.55rem;
  padding: 1px 4px;
  border-radius: 8px;
  font-weight: 700;
  min-width: 14px;
  text-align: center;
}

/* ============ RESPONSIVE ============ */
@media (max-width: 768px) {
  .sidebar {
    transform: translateX(-100%);
  }
  .sidebar.open {
    transform: translateX(0);
    box-shadow: 4px 0 20px rgba(0,0,0,0.15);
  }
  .mobile-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .main-content {
    margin-left: 0;
    max-width: 100%;
    padding: 0.5rem;
    padding-top: 4rem;
    padding-bottom: 5rem;
  }
  .mobile-bottom-nav {
    display: flex;
  }
  .stats-grid {
    grid-template-columns: 1fr 1fr;
  }
  .form-row {
    grid-template-columns: 1fr;
  }
  .filters {
    flex-direction: column;
  }
  .filters .form-control {
    min-width: auto;
    width: 100%;
  }
}

@media (max-width: 480px) {
  .stats-grid {
    grid-template-columns: 1fr;
  }
  .login-card {
    padding: 2rem 1.5rem;
  }
  table { font-size: 0.8rem; }
  table th, table td { padding: 0.35rem 0.5rem; }
  .page-header { padding: 0.3rem 0 !important; margin-bottom: 0.3rem !important; }
  .page-header h1 { font-size: 1.1rem; margin-bottom: 0; }
  .page-header p { font-size: 0.75rem; margin: 0; }
  .pagination button { padding: 0.3rem 0.6rem; font-size: 0.75rem; }
  #phoneStatusBar { padding: 0.2rem 0.5rem !important; font-size: 0.75rem !important; }
  .card { margin-bottom: 0.5rem; padding: 0.75rem; }
}

/* Hide sections */
.view { display: none; }
.view.active { display: block; }

/* ============ WEBPHONE ============ */
.phone-container {
  max-width: 340px;
  margin: 0 auto;
  text-align: center;
}

.phone-status {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.5rem;
  margin-bottom: 1rem;
  font-size: 0.85rem;
  color: var(--gray-600);
}

.phone-status-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  display: inline-block;
}

.phone-status-dot.offline { background: var(--gray-300); }
.phone-status-dot.connecting { background: var(--warning); animation: pulse 1s infinite; }
.phone-status-dot.registered { background: var(--success); }
.phone-status-dot.error { background: var(--danger); }

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

.phone-call-info {
  padding: 1.5rem 1rem;
  margin-bottom: 1rem;
  background: linear-gradient(135deg, var(--primary), #1a56db);
  border-radius: 16px;
  color: white;
}

.phone-call-avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: rgba(255,255,255,0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 0.75rem;
  font-size: 1.5rem;
  font-weight: 700;
}

.phone-call-number {
  font-size: 1.3rem;
  font-weight: 600;
  color: white;
  margin-bottom: 0.2rem;
}

.phone-call-status {
  font-size: 0.85rem;
  color: rgba(255,255,255,0.75);
  animation: pulse 1.5s infinite;
}

.phone-call-timer {
  font-size: 1.8rem;
  font-weight: 300;
  color: white;
  font-variant-numeric: tabular-nums;
  letter-spacing: 2px;
}

.phone-call-cost {
  font-size: 0.75rem;
  color: rgba(255,255,255,0.7);
  margin-top: 0.25rem;
}

.phone-input-wrap {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.phone-input {
  font-size: 1.3rem;
  text-align: center;
  letter-spacing: 1px;
  font-weight: 500;
}

.phone-dialpad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.dialpad-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 0.75rem;
  border: 1px solid var(--gray-300);
  border-radius: 12px;
  background: var(--white);
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
  min-height: 56px;
  user-select: none;
}

.dialpad-btn:hover {
  background: var(--gray-100);
}

.dialpad-btn:active {
  transform: scale(0.95);
  background: var(--gray-300);
}

.dialpad-btn .digit {
  font-size: 1.4rem;
  font-weight: 600;
  color: var(--dark);
  line-height: 1;
}

.dialpad-btn .sub {
  font-size: 0.6rem;
  color: var(--gray-600);
  letter-spacing: 2px;
  margin-top: 2px;
}

.phone-controls {
  display: flex;
  justify-content: center;
  gap: 1rem;
  margin-bottom: 1rem;
}

.phone-btn {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s;
}

.phone-btn:hover {
  transform: scale(1.05);
}

.phone-btn:active {
  transform: scale(0.95);
}

.phone-btn-call {
  background: var(--success);
  color: white;
  box-shadow: 0 4px 12px rgba(52, 168, 83, 0.4);
}

.phone-btn-hangup {
  background: var(--danger);
  color: white;
  box-shadow: 0 4px 12px rgba(234, 67, 53, 0.4);
}

.phone-incall-controls {
  display: flex;
  justify-content: center;
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.phone-incall-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 0.5rem 0.75rem !important;
  font-size: 0.7rem !important;
  min-width: 56px;
}
.phone-incall-controls .btn.active {
  background: var(--primary);
  color: white;
}

/* Speed dial */
.phone-speed-dial {
  display: flex;
  gap: 0.75rem;
  overflow-x: auto;
  padding: 0.5rem 0;
  margin-bottom: 0.75rem;
  scrollbar-width: none;
}
.phone-speed-dial::-webkit-scrollbar { display: none; }
.speed-dial-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.2rem;
  cursor: pointer;
  min-width: 56px;
  position: relative;
  transition: transform 0.15s;
}
.speed-dial-item:hover { transform: scale(1.05); }
.speed-dial-avatar {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: var(--primary);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 1rem;
}
.speed-dial-name {
  font-size: 0.65rem;
  color: var(--gray-600);
  max-width: 60px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-align: center;
}
.speed-dial-remove {
  position: absolute;
  top: -4px;
  right: 0;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: none;
  background: var(--danger);
  color: white;
  font-size: 0.7rem;
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
  line-height: 1;
}
.speed-dial-item:hover .speed-dial-remove { display: flex; }

/* Call quality indicator */
.phone-quality {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 3px;
  margin-top: 0.5rem;
}
.phone-quality .quality-dot {
  width: 6px;
  height: 14px;
  border-radius: 2px;
  background: var(--gray-200);
  transition: background 0.3s;
}
.phone-quality .quality-dot:nth-child(1) { height: 8px; }
.phone-quality .quality-dot:nth-child(2) { height: 12px; }
.phone-quality .quality-dot:nth-child(3) { height: 16px; }
.phone-quality.good .quality-dot { background: var(--success); }
.phone-quality.medium .quality-dot:nth-child(1),
.phone-quality.medium .quality-dot:nth-child(2) { background: var(--warning); }
.phone-quality.medium .quality-dot:nth-child(3) { background: var(--gray-200); }
.phone-quality.bad .quality-dot:nth-child(1) { background: var(--danger); }
.phone-quality.bad .quality-dot:nth-child(2),
.phone-quality.bad .quality-dot:nth-child(3) { background: var(--gray-200); }
.phone-quality .quality-dot {
  background: rgba(255,255,255,0.3);
}
.phone-quality .quality-label {
  font-size: 0.65rem;
  color: rgba(255,255,255,0.6);
  margin-left: 4px;
}

/* Recent calls */
.phone-recent {
  margin-top: 0.5rem;
  border-top: 1px solid var(--gray-100);
  padding-top: 0.75rem;
}
.phone-recent-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.45rem 0.5rem;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s;
}
.phone-recent-item:hover {
  background: var(--gray-100);
}
.phone-recent-icon {
  flex-shrink: 0;
  width: 20px;
  text-align: center;
}
.phone-recent-number {
  flex: 1;
  font-family: 'Consolas', 'Monaco', monospace;
  font-size: 0.9rem;
  color: var(--dark);
  text-align: left;
}
.phone-recent-meta {
  font-size: 0.7rem;
  color: var(--gray-500);
  white-space: nowrap;
}

/* Incoming call overlay */
#incomingCallOverlay {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0,0,0,0.6);
  backdrop-filter: blur(4px);
  animation: fadeIn 0.2s;
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.incoming-call-box {
  background: var(--white);
  border-radius: 20px;
  padding: 2.5rem 2rem 2rem;
  text-align: center;
  min-width: 280px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.3);
  position: relative;
}
.incoming-call-pulse {
  position: absolute;
  top: -30px;
  left: 50%;
  transform: translateX(-50%);
  width: 70px;
  height: 70px;
  border-radius: 50%;
  background: var(--success);
  animation: incomingPulse 1.5s infinite;
}
@keyframes incomingPulse {
  0% { box-shadow: 0 0 0 0 rgba(52,168,83,0.5); }
  70% { box-shadow: 0 0 0 20px rgba(52,168,83,0); }
  100% { box-shadow: 0 0 0 0 rgba(52,168,83,0); }
}
.incoming-call-icon {
  position: absolute;
  top: -17px;
  left: 50%;
  transform: translateX(-50%);
  width: 70px;
  height: 70px;
  border-radius: 50%;
  background: var(--success);
  display: flex;
  align-items: center;
  justify-content: center;
}
.incoming-call-label {
  margin-top: 1.5rem;
  font-size: 0.8rem;
  color: var(--gray-500);
  text-transform: uppercase;
  letter-spacing: 1px;
}
.incoming-call-caller {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--dark);
  margin: 0.5rem 0 1.5rem;
  font-family: 'Consolas', 'Monaco', monospace;
}
.incoming-call-actions {
  display: flex;
  justify-content: center;
  gap: 2rem;
}
.incoming-answer-btn {
  animation: answerPulse 1s infinite;
}
@keyframes answerPulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.1); }
}

/* SIP Credentials table */
.sip-creds-table {
  width: 100%;
  border-collapse: collapse;
}

.sip-creds-table td {
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--gray-100);
}

.sip-creds-table td:first-child {
  font-weight: 600;
  color: var(--gray-600);
  white-space: nowrap;
  width: 140px;
}

.sip-creds-table td:last-child {
  font-family: 'Consolas', 'Monaco', monospace;
  color: var(--dark);
  word-break: break-all;
}

.sip-value-copy {
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 4px;
  transition: background 0.15s;
}
.sip-value-copy:hover {
  background: var(--gray-100);
}
.sip-value-copy:active {
  background: var(--primary-light, #e0e7ff);
}

/* ============ MESSAGES ============ */
.contact-item {
  padding: 0.6rem 0.75rem;
  border-bottom: 1px solid var(--gray-100);
  cursor: pointer;
  border-radius: 6px;
  transition: background 0.15s;
}
.contact-item:hover { background: var(--gray-100); }
.contact-item.active { background: var(--primary-light); }
.contact-item .contact-name { font-weight: 600; font-size: 0.9rem; }
.contact-item .contact-last { font-size: 0.8rem; color: var(--gray-600); overflow:hidden; text-overflow:ellipsis; white-space:nowrap; max-width:180px; }
.contact-item .contact-unread { background: var(--danger); color: white; border-radius: 10px; padding: 0 6px; font-size: 0.7rem; float: right; }

.msg-bubble {
  max-width: 75%;
  padding: 0.5rem 0.75rem;
  border-radius: 12px;
  font-size: 0.9rem;
  line-height: 1.4;
  word-wrap: break-word;
}
.msg-bubble.sent {
  background: var(--primary);
  color: white;
  align-self: flex-end;
  border-bottom-right-radius: 4px;
}
.msg-bubble.received {
  background: var(--gray-100);
  color: var(--dark);
  align-self: flex-start;
  border-bottom-left-radius: 4px;
}
.msg-time {
  font-size: 0.7rem;
  color: var(--gray-600);
  margin-top: 2px;
}
.msg-bubble.sent .msg-time { color: rgba(255,255,255,0.7); }

/* Mobile messages - full screen chat */
@media (max-width: 768px) {
  #messagesView .page-header {
    display: none !important;
  }
  #messagesView > div {
    flex-direction: column;
    height: calc(100vh - 60px) !important;
    min-height: auto !important;
    gap: 0 !important;
  }
  #messagesView .card {
    padding: 0.6rem !important;
    margin-bottom: 0 !important;
  }
  /* Contact list: show/hide with toggle */
  #messagesView .msg-contacts-card {
    width: 100% !important;
    max-height: none;
    flex-shrink: 0;
  }
  #messagesView .msg-contacts-card.hidden-mobile {
    display: none !important;
  }
  /* Chat area: full height */
  #messagesView .msg-chat-card {
    flex: 1 !important;
    display: flex !important;
    flex-direction: column !important;
    min-height: 0;
  }
  #messagesView .msg-chat-card.hidden-mobile {
    display: none !important;
  }
  /* Chat messages scrollable */
  #chatMessages {
    flex: 1 !important;
    min-height: 0 !important;
    overflow-y: auto !important;
  }
  /* Input area bigger */
  #chatInput {
    padding: 0.5rem 0 !important;
  }
  #chatInput .form-control {
    font-size: 16px !important; /* Prevents zoom on iOS */
    padding: 0.6rem !important;
  }
  #chatInput .btn {
    padding: 0.5rem 0.7rem !important;
    min-width: 40px;
    min-height: 40px;
  }
  /* Bigger touch targets for media buttons */
  #chatCameraBtn, #chatAudioBtn, #chatFileInput + label, #videoCallBtn {
    min-width: 44px !important;
    min-height: 44px !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
  }
  #chatCameraBtn svg, #chatAudioBtn svg, #videoCallBtn svg {
    width: 22px !important;
    height: 22px !important;
  }
  /* Back button in chat header */
  .msg-back-btn {
    display: inline-flex !important;
  }
  /* Contact search on mobile */
  #contactSearchInput {
    font-size: 16px !important;
  }
}

@media (min-width: 769px) {
  .msg-back-btn { display: none !important; }
}

.sip-password-hidden {
  color: var(--gray-300);
  letter-spacing: 2px;
}

/* ============ DARK MODE ============ */
/* ---------------------------------------------------------------------------
 * TEMA OSCURO AUTOMATICO
 *
 * Antes el oscuro solo se activaba a mano. La app movil y la web publica si
 * siguen al sistema, asi que el portal era la unica de las tres superficies que
 * no lo hacia.
 *
 * Los tres estados: sin atributo manda el sistema; `data-theme="light"` y
 * `data-theme="dark"` son la eleccion explicita y ganan siempre. El
 * `:not([data-theme="light"])` es lo que hace que elegir claro con el sistema en
 * oscuro se respete — sin el, la eleccion del usuario se perderia.
 * ------------------------------------------------------------------------- */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
  --primary: #FFA24D;
  --primary-dark: #FFB067;
  --primary-light: #2E2119;
  --success: #3FD07E;
  --warning: #FFCA28;
  --danger: #FF7A6E;
  --dark: #F5F2EE;
  --gray-900: #B0A79F;
  --gray-600: #9C938B;
  --gray-300: #302C29;
  --gray-100: #121110;
  --white: #1C1A18;
  --sidebar-width: 260px;
  }
}

[data-theme="dark"] {
  --primary: #FFA24D;
  --primary-dark: #FFB067;
  --primary-light: #2E2119;
  --success: #3FD07E;
  --warning: #FFCA28;
  --danger: #FF7A6E;
  --dark: #F5F2EE;
  --gray-900: #B0A79F;
  --gray-600: #9C938B;
  --gray-300: #302C29;
  --gray-100: #121110;
  --white: #1C1A18;
  --sidebar-width: 260px;
}

[data-theme="dark"] body {
  background: #171717;
  color: #F5F2EE;
}

[data-theme="dark"] .sidebar {
  background: #1e1e1e;
  border-right-color: #333;
}

[data-theme="dark"] .main-content {
  background: #171717;
}

[data-theme="dark"] .card {
  background: #262320;
  border-color: #333;
}

[data-theme="dark"] .stat-card {
  background: #262320;
  border-color: #333;
}

[data-theme="dark"] .form-control {
  background: #2d2d2d;
  border-color: #444;
  color: #F5F2EE;
}

[data-theme="dark"] .form-control:focus {
  border-color: var(--primary);
}

[data-theme="dark"] table thead {
  background: #302C29;
}

[data-theme="dark"] table td {
  border-bottom-color: #333;
}

[data-theme="dark"] .login-card {
  background: #262320;
}

[data-theme="dark"] .btn-outline {
  border-color: #555;
  color: #F5F2EE;
}

[data-theme="dark"] .btn-outline:hover {
  background: #333;
}

[data-theme="dark"] .sidebar-nav a:hover,
[data-theme="dark"] .sidebar-nav a.active {
  background: #333;
}

[data-theme="dark"] .dialpad-btn {
  background: #2d2d2d;
  border-color: #444;
}

[data-theme="dark"] .dialpad-btn:hover {
  background: #3a3a3a;
}

[data-theme="dark"] .badge-success { background: #14261C; color: #3FD07E; }
[data-theme="dark"] .badge-warning { background: #2A2213; color: #FFCA28; }
[data-theme="dark"] .badge-danger { background: #2A1917; color: #FF7A6E; }

[data-theme="dark"] .stat-balance .stat-icon { background: #14261C; }
[data-theme="dark"] .stat-calls .stat-icon { background: #2E2119; }
[data-theme="dark"] .stat-minutes .stat-icon { background: #2A2213; }
[data-theme="dark"] .stat-cost .stat-icon { background: #2A1917; }

[data-theme="dark"] .user-avatar { background: var(--primary); }
[data-theme="dark"] .logo-icon { background: #2E2119; }

[data-theme="dark"] .phone-recent-item:hover { background: #333; }
[data-theme="dark"] .speed-dial-avatar { background: var(--primary); }
[data-theme="dark"] .sip-value-copy:hover { background: #333; }

[data-theme="dark"] .incoming-call-box { background: #262320; }
[data-theme="dark"] .incoming-call-caller { color: #F5F2EE; }

[data-theme="dark"] .mobile-bottom-nav { background: #1e1e1e; border-top-color: #333; }
[data-theme="dark"] .mobile-nav-item { color: #9aa0a6; }
[data-theme="dark"] .mobile-nav-item.active { color: var(--primary); }

[data-theme="dark"] .skeleton {
  background: linear-gradient(90deg, #302C29 25%, #3a3a3a 50%, #302C29 75%);
  background-size: 200% 100%;
}

[data-theme="dark"] .alert-error { background: #2A1917; border-color: #5a2a2a; }
[data-theme="dark"] .alert-success { background: #14261C; border-color: #2a5a3a; }

[data-theme="dark"] table tr:hover { background: #2E2119; }

/* Dark mode toggle */
.theme-toggle {
  cursor: pointer;
  padding: 0.4rem 0.6rem;
  border-radius: 6px;
  border: 1px solid var(--gray-300);
  background: transparent;
  color: var(--gray-600);
  font-size: 1.1rem;
  line-height: 1;
  transition: background 0.2s;
}
.theme-toggle:hover { background: var(--gray-100); }
