:root {
  --bg: #0b1020;
  --bg-soft: #141b33;
  --panel: #10172b;
  --text: #e7ecff;
  --muted: #97a4cb;
  --accent: #6ee7ff;
  --good: #22c55e;
  --bad: #ef4444;
  --border: #253052;
  --sidebar-width: 280px;
  --sidebar-bg: #0d1427;
  --header-height: 64px;
  --transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: radial-gradient(circle at 20% -20%, #1c2c5c, var(--bg) 45%);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.app-container {
  display: flex;
  min-height: 100vh;
}

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

.sidebar-header {
  height: var(--header-height);
  padding: 0 24px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 1.25rem;
  font-weight: 800;
  color: var(--text);
  letter-spacing: -0.02em;
  text-decoration: none !important;
}

.brand-icon {
  color: var(--accent);
  font-size: 28px;
  filter: drop-shadow(0 0 8px rgba(110, 231, 255, 0.3));
}

.nav-groups {
  flex: 1;
  padding: 24px 16px;
  overflow-y: auto;
}

.nav-group {
  margin-bottom: 24px;
}

.nav-group-label {
  padding: 0 12px;
  margin-bottom: 8px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--muted);
  opacity: 0.8;
}

.nav-link {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  border-radius: 12px;
  color: var(--muted);
  font-weight: 500;
  margin-bottom: 2px;
  transition: var(--transition);
  text-decoration: none;
}

.nav-link:hover {
  background: rgba(110, 231, 255, 0.05);
  color: var(--text);
  text-decoration: none;
}

.nav-link.active {
  background: rgba(110, 231, 255, 0.1);
  color: var(--accent);
  font-weight: 600;
}

.nav-link .material-symbols-outlined {
  font-size: 20px;
}

.sidebar-footer {
  padding: 16px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.nav-btn-action {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px;
  border-radius: 12px;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  border: none;
  background: var(--accent);
  color: #0b1020;
  transition: var(--transition);
}

.nav-btn-action:hover {
  filter: brightness(1.1);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(110, 231, 255, 0.2);
}

.nav-btn-logout {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 12px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
  transition: var(--transition);
}

.nav-btn-logout:hover {
  background: rgba(239, 68, 68, 0.1);
  color: var(--bad);
  border-color: rgba(239, 68, 68, 0.2);
}

/* --- Main Content --- */
.main-content {
  flex: 1;
  margin-left: var(--sidebar-width);
  min-width: 0;
  padding-bottom: 40px;
}

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

.page-title h1 {
  margin: 0;
  font-size: 28px;
  font-weight: 800;
  letter-spacing: -0.02em;
}

.page-title p {
  margin: 4px 0 0 0;
  color: var(--muted);
  font-size: 15px;
}

.page-actions {
  display: flex;
  gap: 12px;
}

/* --- Mobile Nav --- */
.mobile-topbar {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 60px;
  background: rgba(11, 16, 32, 0.8);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  padding: 0 16px;
  align-items: center;
  justify-content: space-between;
  z-index: 100;
}

.mobile-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  z-index: 1000;
  backdrop-filter: blur(4px);
  animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.mobile-drawer {
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  right: -300px;
  bottom: 0;
  width: 300px;
  background: var(--sidebar-bg);
  z-index: 1001;
  transition: right 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: -10px 0 30px rgba(0,0,0,0.5);
}

.mobile-drawer.open {
  right: 0;
}

.mobile-overlay.open {
  display: block;
}

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

.drawer-content {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
}

.drawer-footer {
  padding: 20px;
  border-top: 1px solid var(--border);
}

/* --- Standard Components --- */
a { color: var(--accent); text-decoration: none; transition: var(--transition); }
a:hover { color: #fff; text-decoration: underline; }

.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: #1f2a48;
  color: #dbe6ff;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 18px;
  cursor: pointer;
  font-weight: 600;
  font-size: 14px;
  transition: var(--transition);
  text-decoration: none !important;
}

.btn:hover {
  background: #253358;
  border-color: var(--accent);
  color: var(--accent);
}

.btn-primary, .btn-accent {
  background: linear-gradient(135deg, #2a6ff6, #20c5e8);
  color: white !important;
  border: none;
}

.btn-primary:hover, .btn-accent:hover {
  filter: brightness(1.1);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(42, 111, 246, 0.3);
}

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 20px;
  padding: 0 32px 32px 32px;
}

.card, .panel, .login-card {
  background: linear-gradient(180deg, rgba(23, 34, 64, 0.7), rgba(16, 23, 43, 0.8));
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.2);
  backdrop-filter: blur(8px);
}

.card { padding: 20px; }
.card-title { color: var(--muted); font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.05em; }
.card-value { font-size: 28px; font-weight: 800; margin-top: 10px; color: var(--text); }
.card-value.small { font-size: 16px; word-break: break-all; }

.panel {
  margin: 0 32px 24px 32px;
  padding: 24px;
}

.panel-header {
  display: flex;
  gap: 16px;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  flex-wrap: wrap;
}

.filters { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }

input, select, textarea {
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #0d1427;
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  transition: var(--transition);
}

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(110, 231, 255, 0.1);
}

.table-wrap { width: 100%; overflow-x: auto; border-radius: 12px; border: 1px solid var(--border); }
table { width: 100%; border-collapse: collapse; font-size: 14px; background: rgba(0,0,0,0.1); }
th, td { border-bottom: 1px solid var(--border); text-align: left; padding: 14px 16px; vertical-align: middle; }
th { background: rgba(255,255,255,0.03); color: var(--muted); font-weight: 700; font-size: 12px; text-transform: uppercase; letter-spacing: 0.05em; }
tr:last-child td { border-bottom: none; }
tr:hover td { background: rgba(255,255,255,0.02); }

.badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 4px 10px;
  border-radius: 8px;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.badge-good { background: rgba(34,197,94,0.1); color: #4ade80; border: 1px solid rgba(34,197,94,0.2); }
.badge-bad { background: rgba(239,68,68,0.1); color: #f87171; border: 1px solid rgba(239,68,68,0.2); }

/* --- Status badge tooltips --- */
.status-badge-with-error {
  cursor: help;
  position: relative;
}

.status-badge-with-error:hover::after {
  content: attr(data-error);
  position: absolute;
  bottom: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  background: #0b1020;
  border: 1px solid var(--bad);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 12px;
  font-weight: 400;
  color: #e7ecff;
  white-space: normal;
  max-width: 280px;
  min-width: 160px;
  z-index: 1000;
  box-shadow: 0 4px 20px rgba(0,0,0,0.6);
  line-height: 1.4;
  text-align: left;
}

.status-badge-with-error:hover::before {
  content: '';
  position: absolute;
  bottom: calc(100% + 3px);
  left: 50%;
  transform: translateX(-50%);
  border: 6px solid transparent;
  border-top-color: var(--bad);
  z-index: 1001;
}

.status-error-indicator {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  background: var(--bad);
  color: white;
  border-radius: 50%;
  font-size: 11px;
  font-weight: 900;
  margin-left: 6px;
  line-height: 1;
}

.row-actions { display: flex; gap: 8px; justify-content: flex-end; }
.row-actions button { margin: 0; }

.btn-action {
  width: 36px;
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  transition: var(--transition);
  color: var(--muted);
  border: 1px solid var(--border);
  background: rgba(255,255,255,0.03);
  text-decoration: none !important;
}

.btn-action:hover {
  background: rgba(255,255,255,0.08);
  color: var(--text);
  transform: translateY(-1px);
}

.btn-action-edit:hover { color: var(--accent); border-color: var(--accent); }
.btn-action-delete:hover { color: var(--bad); border-color: var(--bad); }

.alerts { margin: 16px 32px; }
.alert { border-radius: 12px; padding: 14px 18px; margin-bottom: 12px; border: 1px solid; display: flex; align-items: center; gap: 12px; }
.alert-success { background: rgba(34,197,94,0.1); border-color: rgba(34,197,94,0.3); color: #4ade80; }
.alert-error { background: rgba(239,68,68,0.1); border-color: rgba(239,68,68,0.3); color: #f87171; }

.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.login-card { width: min(420px, 100%); padding: 40px; }

.nav-action-form {
  margin: 0;
  width: 100%;
}

/* --- Searchable Select --- */
.searchable-select {
  position: relative;
  width: 320px;
}

.searchable-select input[type="text"] {
  width: 100%;
  padding: 10px 14px 10px 40px;
  background: #0d1427 url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='18' height='18' viewBox='0 0 24 24' fill='none' stroke='%2397a4cb' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Ccircle cx='11' cy='11' r='8'%3E%3C/circle%3E%3Cline x1='21' y1='21' x2='16.65' y2='16.65'%3E%3C/line%3E%3C/svg%3E") no-repeat left 14px center;
}

.searchable-select .dropdown-options {
  position: absolute;
  top: calc(100% + 8px);
  left: 0;
  right: 0;
  max-height: 400px;
  overflow-y: auto;
  background: var(--sidebar-bg);
  border: 1px solid var(--border);
  border-radius: 14px;
  z-index: 1000;
  display: none;
  box-shadow: 0 10px 30px rgba(0,0,0,0.5);
  animation: slideDown 0.2s ease;
}

@keyframes slideDown {
  from { opacity: 0; transform: translateY(-10px); }
  to { opacity: 1; transform: translateY(0); }
}

.searchable-select.active .dropdown-options {
  display: block;
}

.searchable-select .option {
  padding: 12px 16px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text);
  border-bottom: 1px solid rgba(255,255,255,0.03);
  transition: var(--transition);
}

.searchable-select .option:last-child {
  border-bottom: none;
}

.searchable-select .option:hover, .searchable-select .option.focused {
  background: rgba(110, 231, 255, 0.08);
  color: var(--accent);
}

.searchable-select .option.selected {
  background: rgba(110, 231, 255, 0.15);
  color: var(--accent);
  font-weight: 700;
}

.searchable-select .option.hidden {
  display: none;
}

/* --- History Graphs --- */
.chart-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
  gap: 24px;
  padding: 0 32px 32px 32px;
}

.chart-card {
  background: linear-gradient(180deg, rgba(23, 34, 64, 0.7), rgba(16, 23, 43, 0.8));
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 24px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.3);
}

.chart-card h3 {
  margin: 0 0 20px 0;
  font-size: 13px;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.1em;
}

.apex-chart {
  min-height: 300px;
}

#historySummary {
  margin: 0 32px 32px 32px;
  padding: 16px 20px;
  background: rgba(255,255,255,0.02);
  border-radius: 12px;
  border-left: 4px solid var(--accent);
}

.field-help {
  margin-top: 6px;
  font-size: 12px;
  color: var(--muted);
  line-height: 1.4;
}

.truncate {
  max-width: 250px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.read-more-wrapper {
  position: relative;
  max-width: 300px;
}

.read-more-content {
  max-height: 1.5em;
  overflow: hidden;
  transition: max-height 0.3s ease;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.read-more-wrapper:hover .read-more-content {
  max-height: 1000px;
  white-space: normal;
  word-break: break-all;
  background: var(--panel);
  position: absolute;
  top: -4px;
  left: -4px;
  padding: 8px;
  z-index: 10;
  border: 1px solid var(--accent);
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.5);
}

label {
  display: block;
  margin-bottom: 8px;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
}

button {
  margin-bottom: 12px;
}

.form-actions {
  margin-top: 32px;
  margin-bottom: 24px;
  display: flex;
  gap: 16px;
  align-items: center;
}

.config-fields-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 20px;
  margin-top: 24px;
}

.config-field {
  margin-bottom: 4px;
}

.settings-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 24px;
  margin-bottom: 24px;
}

.settings-card {
  background: rgba(255, 255, 255, 0.03);
  padding: 20px;
  border-radius: 12px;
  border: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}

.admin-form {
  display: flex;
  flex-direction: column;
}

/* Scrollbar styling */
::-webkit-scrollbar {
  width: 8px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 10px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--muted);
}

@media (max-width: 992px) {
  .chart-grid {
    grid-template-columns: 1fr;
    padding: 0 20px 20px 20px;
  }
}

@media (max-width: 992px) {
  .sidebar {
    transform: translateX(-100%);
  }
  .main-content {
    margin-left: 0;
    padding-top: 60px;
  }
  .mobile-topbar {
    display: flex;
  }
  .page-header {
    padding: 24px 20px 12px 20px;
  }
  .cards { padding: 0 20px 20px 20px; }
  .panel { margin: 0 20px 20px 20px; padding: 20px; }
}

@media (max-width: 600px) {
  .page-header {
    flex-direction: column;
    align-items: stretch;
  }
  .page-actions {
    margin-top: 12px;
  }
  .page-actions .btn {
    flex: 1;
    justify-content: center;
  }
  .searchable-select {
    width: 100%;
  }
  .filters {
    flex-direction: column;
    align-items: stretch;
  }
  .filters select {
    width: 100%;
  }
}
