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

:root {
  --sidebar-w: 240px;
  --sidebar-bg: #111111;
  --sidebar-border: #1e1e1e;
  --sidebar-text: #8a8a8a;
  --sidebar-hover: #1a1a1a;
  --sidebar-active-bg: #2d1800;
  --sidebar-active-text: #f97316;
  --accent: #f97316;
  --accent-dark: #ea6c00;
  --bg: #0f0f0f;
  --surface: #1a1a1a;
  --surface2: #222222;
  --text: #f0f0f0;
  --text-muted: #888888;
  --border: #2a2a2a;
  --success: #22c55e;
  --error: #ef4444;
  --warning: #f59e0b;
  --online: #22c55e;
  --offline: #ef4444;
  --radius: 10px;
  --shadow: 0 2px 12px rgba(0,0,0,0.5);
}

/* ── Light mode ────────────────────────────────────────────────────────────── */
[data-theme="light"] {
  --bg: #f4f4f5;
  --surface: #ffffff;
  --surface2: #f0f0f0;
  --text: #18181b;
  --text-muted: #71717a;
  --border: #e4e4e7;
  --shadow: 0 2px 12px rgba(0,0,0,0.08);
  /* Sidebar stays dark – brand identity */
}

body {
  font-family: 'Inter', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
}

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

/* ── Layout ───────────────────────────────────────────────────────────────── */
.app-layout {
  display: flex;
  min-height: 100vh;
  width: 100%;
}

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

.sidebar-logo {
  padding: 24px 20px 20px;
  border-bottom: 1px solid var(--sidebar-border);
}

.sidebar-logo img {
  height: 32px;
}

.sidebar-logo span {
  font-size: 20px;
  font-weight: 700;
  color: #fff;
  letter-spacing: -0.5px;
}

.sidebar-nav {
  flex: 1;
  padding: 12px 0;
  overflow-y: auto;
}

.nav-section {
  padding: 6px 12px 2px;
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: #4a5568;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  color: var(--sidebar-text);
  font-size: 14px;
  font-weight: 500;
  border-radius: 8px;
  margin: 2px 8px;
  transition: all 0.15s;
  cursor: pointer;
}

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

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

.nav-item i {
  width: 18px;
  text-align: center;
  font-size: 15px;
  flex-shrink: 0;
}

.nav-item.disabled {
  opacity: 0.4;
  pointer-events: none;
}

.nav-divider {
  border: none;
  border-top: 1px solid var(--sidebar-border);
  margin: 8px 12px;
}

.sidebar-footer {
  padding: 12px 8px;
  border-top: 1px solid var(--sidebar-border);
}

.user-info {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px;
  border-radius: 8px;
}

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

.user-name { font-size: 13px; font-weight: 600; color: #f4f4f5; }
.user-role { font-size: 11px; color: var(--sidebar-text); }

.theme-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  margin-top: 6px;
  padding: 8px 10px;
  background: none;
  border: 1px solid #2a2a2a;
  border-radius: 8px;
  color: #8a8a8a;
  font-size: 13px;
  cursor: pointer;
  transition: all 0.15s;
}
.theme-toggle:hover { border-color: var(--accent); color: var(--accent); }

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

.page-header {
  padding: 28px 32px 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.page-title {
  font-size: 22px;
  font-weight: 700;
  color: var(--text);
}

.page-subtitle {
  font-size: 13px;
  color: var(--text-muted);
  margin-top: 2px;
}

.page-body {
  padding: 24px 32px 40px;
  flex: 1;
}

/* ── Flash messages ───────────────────────────────────────────────────────── */
.flash-container {
  position: fixed;
  top: 20px; right: 24px;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.flash {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 18px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  box-shadow: var(--shadow);
  animation: slideIn 0.2s ease;
  min-width: 260px;
}

.flash.success { background: #14532d; color: #86efac; border: 1px solid #166534; }
.flash.error   { background: #450a0a; color: #fca5a5; border: 1px solid #7f1d1d; }
.flash.info    { background: #2d1800; color: #fed7aa; border: 1px solid #7c2d12; }

[data-theme="light"] .flash.success { background: #dcfce7; color: #166534; border-color: #bbf7d0; }
[data-theme="light"] .flash.error   { background: #fee2e2; color: #991b1b; border-color: #fecaca; }
[data-theme="light"] .flash.info    { background: #fff7ed; color: #9a3412; border-color: #fed7aa; }

@keyframes slideIn {
  from { transform: translateX(20px); opacity: 0; }
  to   { transform: translateX(0);    opacity: 1; }
}

/* ── Cards grid (dashboard) ───────────────────────────────────────────────── */
.cards-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(148px, 172px));
  gap: 14px;
  margin-top: 0;
}

.device-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 14px 16px;
  cursor: pointer;
  transition: all 0.15s;
  text-decoration: none;
  color: inherit;
  display: block;
  text-align: center;
}

.device-card:hover {
  border-color: var(--accent);
  transform: translateY(-2px);
  box-shadow: 0 4px 20px rgba(249,115,22,0.15);
  color: inherit;
}

.card-icon {
  display: flex; align-items: center; justify-content: center;
  font-size: 40px;
  margin-bottom: 2px;
}

.card-estado {
  font-size: 12px;
  font-weight: 600;
  margin-bottom: 4px;
}

.status-dot {
  width: 9px; height: 9px;
  border-radius: 50%;
  flex-shrink: 0;
}

.status-dot.online  { background: var(--online);  box-shadow: 0 0 6px var(--online); }
.status-dot.offline { background: var(--offline); }

.card-name {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 4px;
}

.card-empresa {
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 8px;
}

.card-meta {
  font-size: 11px;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
}

.card-meta.online  { color: var(--online); }
.card-meta.offline { color: var(--offline); }

/* ── View toggle (dashboard toolbar) ─────────────────────────────────────── */
.view-toggle {
  display: flex;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  flex-shrink: 0;
}

.view-btn {
  background: var(--surface);
  border: none;
  color: var(--text-muted);
  padding: 7px 12px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.15s;
  line-height: 1;
}

.view-btn:hover { background: var(--surface2); color: var(--text); }
.view-btn.active { background: var(--accent); color: #fff; }

/* Empty state */
.empty-state {
  text-align: center;
  padding: 64px 24px;
  color: var(--text-muted);
}

.empty-state i { font-size: 48px; margin-bottom: 16px; opacity: 0.3; }
.empty-state h3 { font-size: 16px; font-weight: 600; margin-bottom: 8px; color: var(--text); }

/* ── Buttons ──────────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  border: none;
  cursor: pointer;
  transition: all 0.15s;
  text-decoration: none;
}

.btn-primary {
  background: var(--accent);
  color: #fff;
}
.btn-primary:hover { background: var(--accent-dark); color: #fff; }

.btn-secondary {
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
}
.btn-secondary:hover { background: var(--border); color: var(--text); }

.btn-danger {
  background: transparent;
  color: var(--error);
  border: 1px solid var(--error);
}
.btn-danger:hover { background: var(--error); color: #fff; }

.btn-icon {
  padding: 6px 10px;
  border-radius: 6px;
  background: var(--surface2);
  color: var(--text-muted);
  border: 1px solid var(--border);
  cursor: pointer;
  font-size: 13px;
  transition: all 0.15s;
}
.btn-icon:hover { color: var(--text); border-color: var(--accent); }
.btn-icon.danger:hover { color: var(--error); border-color: var(--error); }

/* ── Tables ───────────────────────────────────────────────────────────────── */
.table-container {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  margin-top: 20px;
}

.table-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  gap: 12px;
}

.search-input {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 14px;
  padding: 8px 14px;
  outline: none;
  width: 240px;
}

.search-input:focus { border-color: var(--accent); }
.search-input::placeholder { color: var(--text-muted); }

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

thead th {
  padding: 10px 16px;
  background: var(--surface2);
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  text-align: left;
  border-bottom: 1px solid var(--border);
}

tbody tr {
  border-bottom: 1px solid var(--border);
  transition: background 0.1s;
}

tbody tr:last-child { border-bottom: none; }
tbody tr:hover { background: var(--surface2); }

tbody td {
  padding: 12px 16px;
  color: var(--text);
  vertical-align: middle;
}

.td-muted { color: var(--text-muted); font-size: 13px; }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 10px;
  border-radius: 99px;
  font-size: 12px;
  font-weight: 600;
}

.badge-admin   { background: #2d1800; color: #fb923c; }
.badge-user    { background: var(--surface2); color: var(--text-muted); }
.badge-online  { background: #14532d; color: #86efac; }
.badge-offline { background: #450a0a; color: #fca5a5; }

[data-theme="light"] .badge-admin   { background: #fff7ed; color: #c2410c; }
[data-theme="light"] .badge-online  { background: #dcfce7; color: #166534; }
[data-theme="light"] .badge-offline { background: #fee2e2; color: #991b1b; }

/* ── Modals ───────────────────────────────────────────────────────────────── */
.modal-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  z-index: 1000;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 24px 16px;
}

.modal-backdrop.open {
  display: flex;
  align-items: flex-start;
  justify-content: center;
}

.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  width: 100%;
  max-width: 560px;
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 60px rgba(0,0,0,0.6);
  animation: modalIn 0.2s ease;
  flex-shrink: 0;
}

.modal.modal-lg { max-width: 760px; }

@keyframes modalIn {
  from { transform: scale(0.95); opacity: 0; }
  to   { transform: scale(1);    opacity: 1; }
}

@keyframes parpadeo {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0.2; }
}

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

.modal-title { font-size: 16px; font-weight: 700; color: var(--text); }

.modal-close {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 18px;
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  transition: color 0.15s;
}

.modal-close:hover { color: var(--text); }

.modal-body {
  padding: 20px 24px;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 16px 24px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

/* ── Forms ────────────────────────────────────────────────────────────────── */
.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}

.form-grid.cols-1 { grid-template-columns: 1fr; }
.form-grid.cols-3 { grid-template-columns: 1fr 1fr 1fr; }

.form-group { display: flex; flex-direction: column; gap: 6px; }
.form-group.span-2 { grid-column: span 2; }
.form-group.span-3 { grid-column: span 3; }

label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

input[type="text"],
input[type="password"],
input[type="email"],
input[type="tel"],
input[type="date"],
input[type="number"],
select,
textarea {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 14px;
  padding: 9px 12px;
  outline: none;
  transition: border-color 0.15s;
  width: 100%;
  font-family: inherit;
}

input:focus, select:focus, textarea:focus { border-color: var(--accent); }
input::placeholder, textarea::placeholder { color: var(--text-muted); }

select option { background: var(--surface2); }

textarea { resize: vertical; min-height: 80px; }

.form-check {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 0;
}

.form-check input[type="checkbox"] {
  width: 16px; height: 16px;
  accent-color: var(--accent);
  flex-shrink: 0;
}

.form-check label {
  font-size: 14px;
  font-weight: 500;
  text-transform: none;
  letter-spacing: 0;
  color: var(--text);
  cursor: pointer;
}

/* ── Section heading (inside modal/form) ──────────────────────────────────── */
.form-section {
  margin: 18px 0 10px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
}

/* ── Device detail ────────────────────────────────────────────────────────── */
.detail-header {
  display: flex;
  align-items: center;
  gap: 20px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  margin-top: 24px;
}

.detail-icon {
  width: 64px; height: 64px;
  background: var(--surface2);
  border-radius: 14px;
  display: flex; align-items: center; justify-content: center;
  font-size: 28px;
  color: var(--accent);
  flex-shrink: 0;
}

.detail-info { flex: 1; }
.detail-name { font-size: 22px; font-weight: 700; color: var(--text); }
.detail-meta { font-size: 13px; color: var(--text-muted); margin-top: 4px; }

.detail-status {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  font-weight: 600;
}

.detail-status.online  { color: var(--online); }
.detail-status.offline { color: var(--offline); }

.detail-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  margin-top: 16px;
}

.detail-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}

.detail-card-title {
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin-bottom: 14px;
}

.kv-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}

.kv-row:last-child { border-bottom: none; }
.kv-key   { color: var(--text-muted); }
.kv-value { color: var(--text); font-weight: 500; font-family: monospace; }

/* ── Config editor ────────────────────────────────────────────────────────── */
.config-estado-row {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 12px;
}

.config-estado-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.banderas-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin: 10px 0;
}

.bandera-check {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  color: var(--text-muted);
}

.bandera-check input[type="checkbox"] {
  width: 18px; height: 18px;
  accent-color: var(--accent);
}

.condicion-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 6px 0;
  font-size: 13px;
}

.condicion-row .form-check { padding: 0; }
.condicion-row input[type="number"] { width: 90px; }

/* ── Login page ───────────────────────────────────────────────────────────── */
.login-page {
  min-height: 100vh;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 40px;
  width: 100%;
  max-width: 400px;
  box-shadow: var(--shadow);
}

.login-logo {
  text-align: center;
  margin-bottom: 28px;
}

.login-logo img { height: 40px; }
.login-logo-text {
  font-size: 26px;
  font-weight: 800;
  color: #fff;
  letter-spacing: -1px;
}

.login-title {
  font-size: 18px;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 6px;
}

.login-sub {
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 24px;
}

.login-form .form-group { margin-bottom: 16px; }

.login-form input {
  width: 100%;
  padding: 11px 14px;
  font-size: 15px;
}

.login-btn {
  width: 100%;
  padding: 12px;
  font-size: 15px;
  font-weight: 600;
  margin-top: 8px;
}

.login-error {
  background: #450a0a;
  border: 1px solid #7f1d1d;
  color: #fca5a5;
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 13px;
  margin-bottom: 16px;
}

[data-theme="light"] .login-error { background: #fee2e2; border-color: #fecaca; color: #991b1b; }

/* ── Color swatch ─────────────────────────────────────────────────────────── */
.color-preview {
  width: 20px; height: 20px;
  border-radius: 4px;
  border: 1px solid var(--border);
  display: inline-block;
  vertical-align: middle;
}

input[type="color"] {
  padding: 2px;
  height: 38px;
  cursor: pointer;
}

/* ── Admin sub-nav ────────────────────────────────────────────────────────── */
.admin-subnav {
  display: flex;
  gap: 4px;
  margin-top: 20px;
  flex-wrap: wrap;
}

.subnav-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 7px 14px;
  border-radius: 7px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-muted);
  background: var(--surface);
  border: 1px solid var(--border);
  text-decoration: none;
  transition: all 0.15s;
}

.subnav-item:hover { color: var(--text); border-color: var(--accent); }
.subnav-item.active { background: var(--sidebar-active-bg); color: var(--sidebar-active-text); border-color: var(--accent); }

/* ── Responsive ───────────────────────────────────────────────────────────── */
@media (max-width: 768px) {
  .sidebar {
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    z-index: 200;
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 199;
  }
  .sidebar-overlay.open { display: block; }
  .main-content { margin-left: 0; }
  .form-grid { grid-template-columns: 1fr; }
  .form-group.span-2, .form-group.span-3 { grid-column: span 1; }
  .detail-grid { grid-template-columns: 1fr; }

  .page-header { padding: 16px 16px 0; }
  .page-body   { padding: 16px 16px 32px; }

  .cards-grid {
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 10px;
  }

  /* Modal en mobile */
  .modal-backdrop { padding: 0; align-items: flex-end; }
  .modal {
    border-radius: 16px 16px 0 0;
    max-width: 100%;
    max-height: 92vh;
    overflow-y: auto;
  }
  .modal.modal-lg { max-width: 100%; }

  /* Topbar hamburger */
  .mobile-topbar {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 16px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 50;
  }
  .hamburger {
    background: none;
    border: none;
    color: var(--text);
    font-size: 20px;
    cursor: pointer;
    padding: 4px;
  }
  .mobile-topbar-title {
    font-size: 16px;
    font-weight: 700;
    color: var(--text);
  }
}

@media (min-width: 769px) {
  .mobile-topbar { display: none; }
  .sidebar-overlay { display: none !important; }
}
