:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface2: #252836;
  --accent: #f97316;
  --text: #e2e8f0;
  --text-muted: #94a3b8;
  --success: #22c55e;
  --danger: #ef4444;
  --radius: 12px;
  --shadow: 0 4px 24px rgba(0,0,0,0.4);
}

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

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

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

h1 {
  font-size: 1.75rem;
  font-weight: 700;
  color: var(--text);
  letter-spacing: -0.02em;
}

h2 {
  font-size: 1.35rem;
  font-weight: 600;
  color: var(--text);
}

h3 {
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--text);
}

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

a:hover {
  text-decoration: underline;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  padding: 0.55rem 1.1rem;
  border-radius: 8px;
  border: none;
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  transition: opacity 0.15s, transform 0.1s, background 0.15s;
  white-space: nowrap;
  user-select: none;
  text-decoration: none;
}

.btn:active {
  transform: scale(0.97);
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

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

.btn-primary:hover:not(:disabled) {
  background: #ea6c0a;
}

.btn-danger {
  background: var(--danger);
  color: #fff;
}

.btn-danger:hover:not(:disabled) {
  background: #dc2626;
}

.btn-secondary {
  background: var(--surface2);
  color: var(--text);
  border: 1px solid rgba(255,255,255,0.08);
}

.btn-secondary:hover:not(:disabled) {
  background: #303447;
}

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

.btn-success:hover:not(:disabled) {
  background: #16a34a;
}

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

.btn-icon {
  padding: 0.5rem;
  width: 36px;
  height: 36px;
}

/* Cards */
.card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 0.6rem 0.875rem;
  border: 1px solid rgba(255,255,255,0.05);
}

.card-title {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  margin-bottom: 0.3rem;
}

/* Entity card — horizontal row layout */
.entity-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  min-height: 52px;
}

.entity-name {
  font-size: 0.9rem;
  font-weight: 500;
  color: var(--text);
  flex: 1;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Section label in grid */
.section-label {
  grid-column: 1 / -1;
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--text-muted);
  padding-bottom: 0.3rem;
  border-bottom: 1px solid rgba(255,255,255,0.07);
  margin-top: 0.25rem;
}

.section-label:first-child {
  margin-top: 0;
}

/* Grid */
.grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.5rem;
}

@media (min-width: 640px) {
  .grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (min-width: 1024px) {
  .grid {
    grid-template-columns: repeat(4, 1fr);
  }
}

/* Switch Toggle */
.switch-toggle {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 0.75rem;
  cursor: pointer;
  user-select: none;
}

.switch-toggle input {
  opacity: 0;
  width: 0;
  height: 0;
  position: absolute;
}

.switch-track {
  position: relative;
  width: 52px;
  height: 28px;
  background: var(--surface2);
  border-radius: 14px;
  transition: background 0.25s;
  border: 2px solid rgba(255,255,255,0.08);
  flex-shrink: 0;
}

.switch-track::after {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 20px;
  height: 20px;
  background: var(--text-muted);
  border-radius: 50%;
  transition: transform 0.25s, background 0.25s;
}

.switch-toggle input:checked + .switch-track {
  background: var(--success);
  border-color: transparent;
}

.switch-toggle input:checked + .switch-track::after {
  transform: translateX(24px);
  background: #fff;
}

.switch-toggle input:not(:checked) + .switch-track::after {
  background: #64748b;
}

.switch-label {
  font-size: 0.95rem;
  font-weight: 500;
  color: var(--text);
}

.switch-state {
  font-size: 0.8rem;
  font-weight: 600;
  padding: 0.2rem 0.5rem;
  border-radius: 4px;
  margin-left: auto;
}

.switch-state.on {
  background: rgba(34,197,94,0.15);
  color: var(--success);
}

.switch-state.off {
  background: rgba(148,163,184,0.1);
  color: var(--text-muted);
}

/* Sensor Value */
.sensor-value {
  display: flex;
  align-items: baseline;
  gap: 0.2rem;
}

.sensor-value .value {
  font-size: 1.4rem;
  font-weight: 700;
  color: var(--text);
  line-height: 1;
}

.sensor-value .unit {
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--text-muted);
}

.sensor-updated {
  display: none;
}

/* Camera Card */
.camera-card {
  display: flex;
  flex-direction: column;
  padding: 0;
  overflow: hidden;
}

.camera-card .card-title {
  padding: 1rem 1rem 0.5rem;
  margin-bottom: 0;
}

.camera-card .camera-img-wrap {
  position: relative;
  background: #000;
  aspect-ratio: 16/9;
  overflow: hidden;
}

.camera-card .camera-img-wrap img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  transition: opacity 0.3s;
}

.camera-card .camera-img-wrap img.loading {
  opacity: 0.5;
}

.camera-card .camera-footer {
  padding: 0.75rem 1rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  background: var(--surface2);
}

.camera-card .camera-footer .last-updated {
  font-size: 0.75rem;
  color: var(--text-muted);
}

/* Badge */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 0.2rem 0.6rem;
  border-radius: 20px;
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.02em;
}

.badge-success {
  background: rgba(34,197,94,0.15);
  color: var(--success);
}

.badge-danger {
  background: rgba(239,68,68,0.15);
  color: var(--danger);
}

.badge-warning {
  background: rgba(249,115,22,0.15);
  color: var(--accent);
}

.badge-neutral {
  background: rgba(148,163,184,0.1);
  color: var(--text-muted);
}

/* Nav Tabs */
.tabs {
  display: flex;
  gap: 0.25rem;
  border-bottom: 2px solid var(--surface2);
  margin-bottom: 1.5rem;
  overflow-x: auto;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
}

.tabs::-webkit-scrollbar {
  display: none;
}

.tab {
  padding: 0.65rem 1.1rem;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  transition: color 0.15s, border-color 0.15s;
  white-space: nowrap;
  border-radius: 8px 8px 0 0;
}

.tab:hover {
  color: var(--text);
  background: var(--surface2);
}

.tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

.tab-panel {
  display: none;
}

.tab-panel.active {
  display: block;
}

/* Forms */
.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  margin-bottom: 1rem;
}

.form-group label {
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--text-muted);
}

.form-group input,
.form-group select,
.form-group textarea {
  background: var(--surface2);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 8px;
  color: var(--text);
  font-size: 0.95rem;
  padding: 0.6rem 0.85rem;
  width: 100%;
  transition: border-color 0.15s;
  outline: none;
  -webkit-appearance: none;
}

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
  border-color: var(--accent);
}

.form-group input::placeholder {
  color: var(--text-muted);
}

.form-group select option {
  background: var(--surface2);
  color: var(--text);
}

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

.form-check {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
}

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

.form-check label {
  font-size: 0.9rem;
  color: var(--text);
  cursor: pointer;
  margin: 0;
}

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

@media (min-width: 480px) {
  .form-row {
    grid-template-columns: 1fr 1fr;
  }
}

/* Alert */
.alert {
  padding: 0.85rem 1rem;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 500;
  margin-bottom: 1rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.alert-error {
  background: rgba(239,68,68,0.12);
  color: #fca5a5;
  border: 1px solid rgba(239,68,68,0.25);
}

.alert-success {
  background: rgba(34,197,94,0.12);
  color: #86efac;
  border: 1px solid rgba(34,197,94,0.25);
}

.alert-info {
  background: rgba(249,115,22,0.1);
  color: #fdba74;
  border: 1px solid rgba(249,115,22,0.2);
}

/* Loading Overlay */
#loading-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  flex-direction: column;
  gap: 1rem;
}

#loading-overlay.hidden {
  display: none;
}

.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid rgba(249,115,22,0.2);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

.spinner-sm {
  width: 18px;
  height: 18px;
  border-width: 2px;
  display: inline-block;
  flex-shrink: 0;
}

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

/* Log Table */
.log-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}

.log-table th {
  text-align: left;
  padding: 0.6rem 0.75rem;
  background: var(--surface2);
  color: var(--text-muted);
  font-size: 0.78rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  position: sticky;
  top: 0;
}

.log-table th:first-child {
  border-radius: 8px 0 0 0;
}

.log-table th:last-child {
  border-radius: 0 8px 0 0;
}

.log-table td {
  padding: 0.65rem 0.75rem;
  border-bottom: 1px solid rgba(255,255,255,0.04);
  color: var(--text);
  vertical-align: middle;
}

.log-table tr:hover td {
  background: rgba(255,255,255,0.02);
}

.log-table tr:last-child td {
  border-bottom: none;
}

.table-wrap {
  overflow-x: auto;
  border-radius: var(--radius);
  border: 1px solid rgba(255,255,255,0.06);
}

/* Header / Nav */
.app-header {
  background: var(--surface);
  border-bottom: 1px solid rgba(255,255,255,0.07);
  padding: 0 1rem;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: 0 2px 12px rgba(0,0,0,0.3);
}

.app-header-inner {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  height: 58px;
}

.app-logo {
  font-size: 1.4rem;
  font-weight: 800;
  color: var(--accent);
  display: flex;
  align-items: center;
  gap: 0.4rem;
  text-decoration: none;
  flex-shrink: 0;
}

.app-logo:hover {
  text-decoration: none;
}

.app-header-right {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.user-chip {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  background: var(--surface2);
  border-radius: 20px;
  padding: 0.3rem 0.75rem;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.user-chip strong {
  color: var(--text);
}

/* Main Content */
.main-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 1.5rem 1rem;
}

/* Push Notification Section */
.push-section {
  margin-top: 2rem;
  padding-top: 1.5rem;
  border-top: 1px solid rgba(255,255,255,0.07);
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
}

.push-section .push-status {
  font-size: 0.9rem;
  color: var(--text-muted);
}

/* Code / URL display */
.code-box {
  background: var(--surface2);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 8px;
  padding: 0.65rem 0.85rem;
  font-family: 'Courier New', Courier, monospace;
  font-size: 0.85rem;
  color: var(--accent);
  word-break: break-all;
  flex: 1;
}

.copy-row {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

/* Section headings */
.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1rem;
  gap: 1rem;
}

.section-header h2 {
  font-size: 1.1rem;
}

/* Divider */
.divider {
  height: 1px;
  background: rgba(255,255,255,0.07);
  margin: 1.5rem 0;
}

/* Empty state */
.empty-state {
  text-align: center;
  padding: 2.5rem 1rem;
  color: var(--text-muted);
  font-size: 0.9rem;
}

/* Login page */
.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 2.25rem 2rem;
  border: 1px solid rgba(255,255,255,0.06);
}

.login-icon {
  font-size: 3rem;
  text-align: center;
  margin-bottom: 0.5rem;
  line-height: 1;
}

.login-title {
  text-align: center;
  margin-bottom: 0.25rem;
}

.login-subtitle {
  text-align: center;
  color: var(--text-muted);
  font-size: 0.88rem;
  margin-bottom: 1.75rem;
}

/* Webhook URL display */
.webhook-url {
  background: var(--surface2);
  border-radius: 8px;
  padding: 0.5rem 0.75rem;
  font-size: 0.78rem;
  color: var(--text-muted);
  font-family: monospace;
  word-break: break-all;
  margin-top: 0.25rem;
}

/* Checkbox list for notify_users */
.checkbox-list {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  background: var(--surface2);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 8px;
  padding: 0.6rem 0.75rem;
  max-height: 120px;
  overflow-y: auto;
}

/* Responsive helpers */
.hide-mobile {
  display: none;
}

.show-mobile {
  display: inline;
}

@media (min-width: 640px) {
  .hide-mobile {
    display: inline;
  }
  .show-mobile {
    display: none;
  }
}

/* Utility */
.text-muted { color: var(--text-muted); }
.text-success { color: var(--success); }
.text-danger { color: var(--danger); }
.text-accent { color: var(--accent); }
.fw-bold { font-weight: 700; }
.mt-1 { margin-top: 0.5rem; }
.mt-2 { margin-top: 1rem; }
.mt-3 { margin-top: 1.5rem; }
.mb-1 { margin-bottom: 0.5rem; }
.mb-2 { margin-bottom: 1rem; }
.flex { display: flex; }
.items-center { align-items: center; }
.gap-1 { gap: 0.5rem; }
.gap-2 { gap: 1rem; }
.justify-between { justify-content: space-between; }
.w-full { width: 100%; }
