/* ── Design Tokens — KSN GSM Switch (emerald/teal theme) ───────────────────── */
:root {
  --sidebar-bg:       #04302b;
  --sidebar-hover:    #0a473f;
  --sidebar-active:   linear-gradient(135deg, #10b981, #059669);
  --sidebar-text:     #8fbdb3;
  --sidebar-text-act: #ffffff;
  --sidebar-width:    240px;

  --bg:               #eef6f2;
  --card-bg:          #ffffff;
  --border:           #dbe8e2;
  --border-focus:     #10b981;

  --primary:          #059669;
  --primary-dark:     #047857;
  --primary-light:    #ecfdf5;
  --success:          #059669;
  --success-light:    #ecfdf5;
  --danger:           #e11d48;
  --danger-light:     #fff1f2;
  --warning:          #d97706;
  --warning-light:    #fffbeb;
  --muted:            #64748b;

  --text:             #12231d;
  --text-secondary:   #40564d;

  --radius:           10px;
  --radius-sm:        8px;
  --radius-lg:        16px;
  --shadow:           0 1px 3px rgba(4,48,43,.08), 0 1px 2px rgba(4,48,43,.06);
  --shadow-md:        0 4px 10px rgba(4,48,43,.1), 0 2px 4px rgba(4,48,43,.06);

  --transition:       150ms ease;
  --font:             'Inter', system-ui, -apple-system, sans-serif;
}

/* ── Reset ──────────────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 15px; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
}
a { text-decoration: none; color: inherit; }
button { cursor: pointer; border: none; background: none; font: inherit; }
input, select, textarea { font: inherit; }
img { display: block; max-width: 100%; }

/* ── App Shell ──────────────────────────────────────────────────────────────── */
#app {
  display: flex;
  min-height: 100vh;
}

/* ── Sidebar ────────────────────────────────────────────────────────────────── */
.sidebar {
  position: fixed;
  top: 0; left: 0;
  width: var(--sidebar-width);
  height: 100vh;
  background: var(--sidebar-bg);
  display: flex;
  flex-direction: column;
  z-index: 100;
  transition: transform var(--transition);
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 20px 20px 16px;
  border-bottom: 1px solid #1e293b;
}
.sidebar-brand svg { flex-shrink: 0; }
.sidebar-brand-text { color: #fff; font-weight: 700; font-size: .95rem; line-height: 1.3; }
.sidebar-brand-sub  { color: var(--sidebar-text); font-size: .7rem; font-weight: 400; }

.sidebar-nav {
  flex: 1;
  padding: 12px 10px;
  overflow-y: auto;
}
.sidebar-nav-label {
  color: #475569;
  font-size: .65rem;
  font-weight: 600;
  letter-spacing: .08em;
  text-transform: uppercase;
  padding: 12px 10px 4px;
}
.nav-link {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 12px;
  border-radius: var(--radius-sm);
  color: var(--sidebar-text);
  font-size: .875rem;
  font-weight: 500;
  transition: background var(--transition), color var(--transition);
  margin-bottom: 2px;
}
.nav-link:hover {
  background: var(--sidebar-hover);
  color: #e2e8f0;
}
.nav-link.active {
  background: var(--sidebar-active);
  color: var(--sidebar-text-act);
  box-shadow: 0 2px 8px rgba(16,185,129,.35);
}
.nav-link svg { flex-shrink: 0; opacity: .8; }
.nav-link.active svg { opacity: 1; }

.sidebar-footer {
  padding: 16px;
  border-top: 1px solid #1e293b;
  display: flex;
  align-items: center;
  gap: 10px;
}
.user-avatar {
  width: 36px; height: 36px;
  border-radius: 50%;
  background: var(--sidebar-active);
  display: flex; align-items: center; justify-content: center;
  color: #fff;
  font-weight: 700;
  font-size: .875rem;
  flex-shrink: 0;
}
.user-meta { flex: 1; min-width: 0; }
.user-meta-name { color: #e2e8f0; font-size: .8rem; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-meta-role { color: var(--sidebar-text); font-size: .7rem; }
.logout-btn {
  color: var(--sidebar-text);
  padding: 6px;
  border-radius: var(--radius-sm);
  transition: color var(--transition), background var(--transition);
  display: flex;
}
.logout-btn:hover { color: #ef4444; background: rgba(239,68,68,.1); }

/* ── Content Wrapper ────────────────────────────────────────────────────────── */
.content-wrapper {
  flex: 1;
  margin-left: var(--sidebar-width);
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* ── Top Bar ────────────────────────────────────────────────────────────────── */
.topbar {
  background: var(--card-bg);
  border-bottom: 1px solid var(--border);
  padding: 0 28px;
  height: 60px;
  display: flex;
  align-items: center;
  gap: 16px;
  position: sticky;
  top: 0;
  z-index: 50;
}
.topbar-title {
  font-weight: 700;
  font-size: 1.05rem;
  flex: 1;
}
.mobile-toggle {
  display: none;
  padding: 6px;
  color: var(--muted);
}

.mqtt-indicator {
  display: flex;
  align-items: center;
  gap: 7px;
  background: var(--bg);
  padding: 5px 12px;
  border-radius: 20px;
  font-size: .78rem;
  font-weight: 500;
  color: var(--text-secondary);
}
.mqtt-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: #94a3b8;
  transition: background .3s;
}
.mqtt-dot.connected  { background: var(--success); box-shadow: 0 0 0 2px rgba(22,163,74,.2); }
.mqtt-dot.connecting { background: var(--warning); animation: pulse 1.2s infinite; }
.mqtt-dot.error      { background: var(--danger); }

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

/* ── Main Content ───────────────────────────────────────────────────────────── */
.main-content {
  flex: 1;
  padding: 28px;
}

/* ── Panels ─────────────────────────────────────────────────────────────────── */
.panel { display: none; }
.panel.active { display: block; }

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
  flex-wrap: wrap;
  gap: 12px;
}
.panel-header h2 { font-size: 1.25rem; font-weight: 700; }

/* ── Stat Cards ─────────────────────────────────────────────────────────────── */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 16px;
  margin-bottom: 28px;
}
.stat-card {
  background: var(--card-bg);
  border-radius: var(--radius-lg);
  padding: 20px 22px;
  box-shadow: var(--shadow);
  display: flex;
  align-items: flex-start;
  gap: 14px;
}
.stat-icon {
  width: 44px; height: 44px;
  border-radius: var(--radius);
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.stat-icon.blue   { background: #ecfdf5; color: var(--primary); }
.stat-icon.green  { background: #f0fdf4; color: var(--success); }
.stat-icon.orange { background: #fff7ed; color: #ea580c; }
.stat-icon.purple { background: #faf5ff; color: #7c3aed; }
.stat-label { font-size: .78rem; color: var(--muted); font-weight: 500; }
.stat-value { font-size: 1.75rem; font-weight: 800; color: var(--text); line-height: 1.1; margin-top: 2px; }

/* ── Device Grid ────────────────────────────────────────────────────────────── */
.devices-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 18px;
}

/* ── Device Card ────────────────────────────────────────────────────────────── */
.device-card {
  background: var(--card-bg);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  overflow: hidden;
  transition: box-shadow var(--transition);
}
.device-card:hover { box-shadow: var(--shadow-md); }

.device-card-header {
  padding: 16px 18px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: nowrap;
  border-bottom: 1px solid var(--border);
}
.user-guide-content {
  display: grid;
  gap: 14px;
}
.guide-section {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
  background: #fafcff;
}
.guide-section h3 {
  margin: 0 0 10px;
  font-size: .95rem;
}
.guide-list {
  margin: 0;
  padding-left: 18px;
  color: var(--text);
}
.guide-list li + li {
  margin-top: 8px;
}
.sched-mc-caution {
  margin: 10px 0 4px;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid #f59e0b;
  background: #fffbeb;
  color: #92400e;
  font-size: .82rem;
  line-height: 1.45;
}
.sched-mc-caution.hidden { display: none; }
.device-info { flex: 1; min-width: 0; }
.device-name { font-weight: 700; font-size: .95rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.device-location { font-size: .78rem; color: var(--muted); margin-top: 2px; }
.device-token { font-size: .68rem; color: #94a3b8; font-family: monospace; margin-top: 4px; }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 9px;
  border-radius: 20px;
  font-size: .7rem;
  font-weight: 600;
  white-space: nowrap;
  flex-shrink: 0;
}
.badge::before { content: ''; width: 6px; height: 6px; border-radius: 50%; }
.badge-online  { background: var(--success-light); color: var(--success); }
.badge-online::before  { background: var(--success); }
.badge-offline { background: #f1f5f9; color: var(--muted); }
.badge-offline::before { background: #94a3b8; }

.device-card-controls {
  padding: 14px 18px;
  display: flex;
  align-items: center;
  gap: 10px;
  border-bottom: 1px solid var(--border);
}

/* Multi-output rows / WiFi motor controls */
.device-outputs {
  border-bottom: 1px solid var(--border);
  max-height: 360px;
  overflow-y: auto;
}
.device-outputs.motor-controls {
  max-height: none;
}
.output-all-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 18px;
  background: var(--surface-2, #f8fafc);
  border-bottom: 2px solid var(--border);
}
.output-all-label {
  font-size: .75rem;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .05em;
  flex: 1;
}
.output-row {
  display: flex;
  align-items: center;
  flex-wrap: nowrap;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 18px;
  border-bottom: 1px solid var(--border);
}
.output-row:last-child { border-bottom: none; }
.output-row.motor-row {
  background: linear-gradient(90deg, rgba(37, 99, 235, 0.06), transparent);
  padding: 14px 18px;
}
.output-label {
  font-size: .78rem;
  font-weight: 600;
  color: var(--muted);
  width: 32px;
  flex-shrink: 0;
}
.output-label.motor-label {
  width: auto;
  min-width: 4.5rem;
  max-width: 40%;
  font-size: .82rem;
  color: var(--text, #0f172a);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.output-row .state-value {
  flex: 1 1 auto;
  min-width: 0;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.output-dly {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-left: auto;
}
.dly-label {
  font-size: .68rem;
  font-weight: 700;
  color: var(--muted);
  letter-spacing: .04em;
}
.dly-input {
  width: 52px;
  padding: 4px 6px;
  font-size: .78rem;
  border: 1px solid var(--border, #e2e8f0);
  border-radius: 8px;
  text-align: center;
  background: var(--surface, #fff);
  color: var(--text);
}
.dly-unit {
  padding: 4px 4px;
  font-size: .72rem;
  border: 1px solid var(--border, #e2e8f0);
  border-radius: 8px;
  background: var(--surface, #fff);
  color: var(--text);
  max-width: 58px;
}
.output-actions { display: flex; gap: 6px; flex-shrink: 0; }
.output-row.motor-row .output-actions .btn { min-width: 72px; }
.output-row.pulse-row {
  padding: 6px 18px 10px;
  background: linear-gradient(90deg, rgba(37, 99, 235, 0.03), transparent);
}
.pulse-actions { align-items: center; }
.pulse-input {
  width: 58px;
  padding: 4px 8px;
  font-size: .82rem;
  border: 1px solid var(--border, #e2e8f0);
  border-radius: 8px;
  text-align: center;
}
.pulse-unit { font-size: .75rem; color: var(--muted); }
.btn-xs { padding: 2px 10px; font-size: .75rem; }
.state-label { font-size: .78rem; color: var(--muted); font-weight: 500; flex: 1; }
.state-value { font-weight: 700; font-size: .9rem; }
.state-value.on      { color: var(--success); }
.state-value.off     { color: var(--muted); }
.state-value.offline { color: #94a3b8; font-style: italic; }
.state-value.sending { color: var(--warning); }
.state-value.failed  { color: var(--danger); }
.state-value.unknown { color: #94a3b8; }

.device-card-sensors {
  padding: 10px 18px 12px;
  border-bottom: 1px solid var(--border);
}
.sensor-list {
  display: flex;
  flex-direction: column;
  gap: 0;
}
.sensor-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  flex-wrap: nowrap;
}
.sensor-row:last-child { border-bottom: none; }
.sensor-label {
  font-size: .78rem;
  font-weight: 600;
  color: var(--muted);
  white-space: nowrap;
}
.sensor-value {
  font-size: .95rem;
  font-weight: 700;
  color: var(--text);
  text-align: right;
  white-space: nowrap;
}
.sensor-unit  { font-size: .7rem; color: var(--muted); font-weight: 400; }
.sensor-placeholder { color: #cbd5e1; font-size: .85rem; font-style: italic; }

/* Motor Starter v1 — reuses .output-row like relay cards; keep action column width fixed */
.device-outputs.motor-controls .output-actions {
  width: 7.75rem;
  min-width: 7.75rem;
  justify-content: flex-end;
}
.device-outputs.motor-controls .output-actions .btn-xs {
  min-width: 3.5rem;
  font-weight: 800;
  letter-spacing: 0.03em;
}
.device-outputs.motor-controls .output-actions .btn-success {
  background: #16a34a;
  box-shadow: none;
}
.device-outputs.motor-controls .output-actions .btn-danger {
  background: #dc2626;
  box-shadow: none;
}
.device-outputs.motor-controls .output-actions-spacer {
  visibility: hidden;
  pointer-events: none;
}
.motor-runtime-value {
  font-variant-numeric: tabular-nums;
}
.motor-runtime-wrap {
  display: inline-flex;
  align-items: baseline;
  justify-content: center;
  gap: 6px;
  flex: 1 1 auto;
  flex-wrap: wrap;
  min-width: 0;
  text-align: center;
}
.motor-dly-balance {
  font-size: .78rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: #b45309;
  white-space: nowrap;
}
.motor-dly-balance.hidden { display: none; }
.motor-trip-badge {
  font-size: .72rem;
  font-weight: 700;
  letter-spacing: .02em;
  color: #b91c1c;
  background: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: 6px;
  padding: 4px 18px;
  margin: 0;
  text-align: center;
}
.motor-trip-badge.trip-locked {
  cursor: pointer;
  user-select: none;
  box-shadow: inset 0 0 0 1px #fca5a5;
  animation: trip-pulse 1.6s ease-in-out infinite;
}
.motor-trip-badge.trip-locked:active {
  background: #fee2e2;
}
@keyframes trip-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: .78; }
}
.motor-trip-badge.hidden { display: none; }
.motor-pending-hint {
  font-size: .72rem;
  font-weight: 600;
  color: var(--warning);
  padding: 6px 18px 10px;
}
.motor-pending-hint.hidden { display: none; }

.sensor-pick {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 14px;
  padding: 8px 0 2px;
}
.sensor-pick-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: .82rem;
  font-weight: 600;
  color: var(--text);
  cursor: pointer;
  user-select: none;
}
.sensor-pick-item input { margin: 0; }

.device-settings-fields {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.device-settings-fields .device-setting-row {
  margin: 0;
}
.device-settings-fields .form-label {
  font-size: .78rem;
  margin-bottom: 4px;
}

.device-card-footer {
  padding: 12px 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: nowrap;
}
.device-card-footer > .flex {
  flex-wrap: nowrap;
  flex-shrink: 0;
}
.device-actions { display: flex; gap: 8px; }
.last-seen { font-size: .68rem; color: #94a3b8; }

/* ── Buttons ────────────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border-radius: 999px;
  font-size: .8rem;
  font-weight: 700;
  letter-spacing: .01em;
  transition: filter var(--transition), opacity var(--transition),
              transform var(--transition), box-shadow var(--transition);
  white-space: nowrap;
}
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .5; cursor: not-allowed; transform: none; }

.btn-primary  {
  background: linear-gradient(135deg, #10b981, #047857);
  color: #fff;
  box-shadow: 0 2px 8px rgba(5,150,105,.35);
}
.btn-primary:hover:not(:disabled) {
  filter: brightness(1.08);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(5,150,105,.45);
}

.btn-success  {
  background: linear-gradient(135deg, #34d399, #059669);
  color: #fff;
  box-shadow: 0 2px 6px rgba(16,185,129,.35);
}
.btn-success:hover:not(:disabled) {
  filter: brightness(1.08);
  transform: translateY(-1px);
  box-shadow: 0 4px 10px rgba(16,185,129,.45);
}

.btn-danger   {
  background: linear-gradient(135deg, #fb7185, #e11d48);
  color: #fff;
  box-shadow: 0 2px 6px rgba(225,29,72,.3);
}
.btn-danger:hover:not(:disabled) {
  filter: brightness(1.08);
  transform: translateY(-1px);
  box-shadow: 0 4px 10px rgba(225,29,72,.4);
}

.btn-ghost    { background: #fff; color: var(--text-secondary); border: 1.5px solid var(--border); }
.btn-ghost:hover:not(:disabled) {
  border-color: var(--primary);
  color: var(--primary);
  background: var(--primary-light);
}

.btn-sm { padding: 5px 12px; font-size: .75rem; }
.btn-icon { padding: 7px; border-radius: 50%; }

/* ── Tables ─────────────────────────────────────────────────────────────────── */
.table-wrapper {
  background: var(--card-bg);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  overflow: hidden;
}
.table-responsive { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: .855rem; }
thead tr { background: var(--bg); }
th {
  text-align: left;
  padding: 11px 16px;
  font-size: .72rem;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .05em;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: #f8fafc; }

.role-badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: .68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .04em;
}
.role-badge.admin { background: #ede9fe; color: #6d28d9; }
.role-badge.user  { background: #ecfdf5; color: #047857; }

.status-dot {
  display: inline-block;
  width: 8px; height: 8px;
  border-radius: 50%;
  margin-right: 6px;
}
.status-dot.active   { background: var(--success); }
.status-dot.inactive { background: #94a3b8; }

/* ── Forms & Modals ─────────────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15,23,42,.5);
  backdrop-filter: blur(2px);
  z-index: 200;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.modal-overlay.open { display: flex; }

.modal {
  background: var(--card-bg);
  border-radius: var(--radius-lg);
  box-shadow: 0 25px 50px rgba(0,0,0,.2);
  width: 100%;
  max-width: 480px;
  max-height: 90vh;
  overflow-y: auto;
  animation: modal-in .15s ease;
}
@keyframes modal-in {
  from { opacity: 0; transform: translateY(-12px) scale(.97); }
  to   { opacity: 1; transform: none; }
}
.modal-header {
  padding: 20px 24px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.modal-title { font-weight: 700; font-size: 1rem; }
.modal-close {
  color: var(--muted);
  padding: 4px;
  border-radius: var(--radius-sm);
  display: flex;
}
.modal-close:hover { background: var(--bg); color: var(--text); }
.modal-body   { padding: 20px 24px; }
.modal-footer {
  padding: 16px 24px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.form-group { margin-bottom: 16px; }
.form-label { display: block; font-size: .8rem; font-weight: 600; margin-bottom: 5px; color: var(--text-secondary); }
.form-control {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: #fff;
  font-size: .875rem;
  color: var(--text);
  transition: border-color var(--transition), box-shadow var(--transition);
}
.form-control:focus {
  outline: none;
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px rgba(37,99,235,.1);
}
.form-control::placeholder { color: #94a3b8; }

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

/* ── Empty & Error States ───────────────────────────────────────────────────── */
.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--muted);
}
.empty-state svg { margin: 0 auto 14px; opacity: .3; }
.empty-state h3 { font-size: 1rem; font-weight: 600; margin-bottom: 6px; color: var(--text-secondary); }
.empty-state p  { font-size: .85rem; }

.alert {
  padding: 11px 14px;
  border-radius: var(--radius-sm);
  font-size: .83rem;
  font-weight: 500;
  margin-bottom: 16px;
}
.alert-error   { background: var(--danger-light); color: #991b1b; border: 1px solid #fecaca; }
.alert-success { background: var(--success-light); color: #14532d; border: 1px solid #bbf7d0; }

/* ── Toast Notifications ────────────────────────────────────────────────────── */
.toast-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 999;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.toast {
  padding: 12px 16px;
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  font-size: .83rem;
  font-weight: 500;
  color: #fff;
  max-width: 320px;
  animation: slide-in .2s ease;
  display: flex;
  align-items: center;
  gap: 8px;
}
.toast-success { background: var(--success); }
.toast-error   { background: var(--danger); }
.toast-info    { background: var(--primary); }
@keyframes slide-in {
  from { opacity: 0; transform: translateX(20px); }
  to   { opacity: 1; transform: none; }
}

/* ── Loading ────────────────────────────────────────────────────────────────── */
#loading-screen {
  position: fixed;
  inset: 0;
  background: linear-gradient(160deg, #04302b, #065f46);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  gap: 20px;
}
.loading-brand { color: #fff; font-weight: 800; font-size: 1.4rem; }
.loading-brand span { color: #34d399; }
.spinner {
  width: 36px; height: 36px;
  border: 3px solid rgba(255,255,255,.15);
  border-top-color: #34d399;
  border-radius: 50%;
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Settings Panel ─────────────────────────────────────────────────────────── */
.settings-card {
  background: var(--card-bg);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  max-width: 480px;
}
.settings-card-header { padding: 18px 24px; border-bottom: 1px solid var(--border); }
.settings-card-header h3 { font-weight: 700; font-size: .95rem; }
.settings-card-body { padding: 24px; }

/* ── Login Page ─────────────────────────────────────────────────────────────── */
.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(160deg, #04302b, #065f46 55%, #0d9488);
  padding: 20px;
}
.login-card {
  width: 100%;
  max-width: 400px;
  background: #fff;
  border-radius: var(--radius-lg);
  box-shadow: 0 25px 50px rgba(0,0,0,.4);
  overflow: hidden;
}
.login-header {
  background: linear-gradient(135deg, #10b981, #047857);
  padding: 32px 32px 28px;
  text-align: center;
  color: #fff;
}
.login-logo { display: flex; align-items: center; justify-content: center; gap: 10px; margin-bottom: 8px; }
.login-logo-text { font-weight: 800; font-size: 1.25rem; }
.login-tagline { font-size: .8rem; opacity: .85; }
.login-body { padding: 32px; }
.login-title { font-weight: 700; font-size: 1.1rem; margin-bottom: 6px; }
.login-subtitle { font-size: .82rem; color: var(--muted); margin-bottom: 24px; }

/* ── Utility ────────────────────────────────────────────────────────────────── */
.hidden { display: none !important; }
.text-muted  { color: var(--muted); }
.text-sm     { font-size: .8rem; }
.text-center { text-align: center; }
.fw-600      { font-weight: 600; }
.mt-4  { margin-top:    16px; }
.mb-4  { margin-bottom: 16px; }
.gap-2 { gap: 8px; }
.flex  { display: flex; }
.items-center { align-items: center; }
.justify-between { justify-content: space-between; }
.w-full { width: 100%; }

/* ── Device Assignment Checkbox List ───────────────────────────────────────── */
.assign-device-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: background var(--transition), border-color var(--transition);
}
.assign-device-item:hover { background: var(--bg); }
.assign-device-item.checked { background: var(--primary-light); border-color: var(--primary); }
.assign-device-item input[type="checkbox"] { width: 16px; height: 16px; accent-color: var(--primary); cursor: pointer; flex-shrink: 0; }
.assign-device-info { flex: 1; min-width: 0; }
.assign-device-name { font-weight: 600; font-size: .875rem; }
.assign-device-meta { font-size: .72rem; color: var(--muted); font-family: monospace; margin-top: 1px; }

/* ── Schedule Components ───────────────────────────────────────────────────── */
.schedule-type-tabs {
  display: flex;
  background: var(--bg);
  padding: 4px;
  border-radius: var(--radius);
  margin-bottom: 16px;
}
.schedule-type-tab {
  flex: 1;
  padding: 7px 10px;
  border-radius: var(--radius-sm);
  font-size: .78rem;
  font-weight: 600;
  color: var(--muted);
  text-align: center;
  transition: all var(--transition);
}
.schedule-type-tab.active {
  background: var(--card-bg);
  color: var(--primary);
  box-shadow: var(--shadow);
}
.schedule-type-tab:hover:not(.active) { color: var(--text); }

.day-pills { display: flex; gap: 6px; flex-wrap: wrap; }
.day-pill {
  width: 44px;
  height: 36px;
  border-radius: 20px;
  font-size: .72rem;
  font-weight: 600;
  color: var(--muted);
  border: 1px solid var(--border);
  background: var(--card-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--transition);
}
.day-pill:hover { border-color: var(--primary); color: var(--primary); }
.day-pill.active {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}
.relay-pill {
  width: auto;
  min-width: 36px;
  padding: 0 8px;
}

.schedule-panel { animation: fade-in .15s ease; }
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

.countdown-input-group { display: flex; gap: 10px; }

.schedules-list {
  max-height: 220px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.schedule-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  background: var(--bg);
  border-radius: var(--radius);
}
.schedule-item-detail { flex: 1; min-width: 0; }
.schedule-item-detail .sched-main { font-size: .82rem; font-weight: 600; color: var(--text); }
.schedule-item-detail .sched-sub  { font-size: .72rem; color: var(--muted); margin-top: 1px; }

.sched-cmd {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: .68rem;
  font-weight: 700;
  text-transform: uppercase;
  flex-shrink: 0;
}
.sched-cmd-on  { background: var(--success-light); color: var(--success); }
.sched-cmd-off { background: var(--danger-light); color: var(--danger); }

.schedule-badge {
  position: absolute;
  top: -4px;
  right: -4px;
  min-width: 16px;
  height: 16px;
  padding: 0 4px;
  border-radius: 8px;
  background: var(--primary);
  color: #fff;
  font-size: .6rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.btn-schedule-wrap { position: relative; display: inline-flex; }

.sched-delete-btn {
  color: var(--muted);
  padding: 4px;
  border-radius: var(--radius-sm);
  display: flex;
  flex-shrink: 0;
}
.sched-delete-btn:hover { background: var(--danger-light); color: var(--danger); }

.board-dly-header,
.board-dly-row {
  display: grid;
  grid-template-columns: 72px 1fr 72px;
  gap: 8px;
  align-items: center;
  font-size: .78rem;
}
.board-dly-header {
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .04em;
  margin-bottom: 4px;
  padding: 0 2px;
}
.board-dly-rows { max-height: 220px; overflow-y: auto; }
.board-dly-row { padding: 4px 2px; border-bottom: 1px solid var(--border); }
.board-dly-row:last-child { border-bottom: none; }
.board-dly-name { font-weight: 700; }
.board-dly-row .form-control { padding: 4px 8px; font-size: .82rem; }

.seq-intro { margin-bottom: 12px; line-height: 1.45; }
.seq-board-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 14px;
}
.seq-default-row { align-items: flex-end; gap: 8px; }
.seq-cyclic-label {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  margin: 4px 0 12px;
  font-size: .82rem;
  line-height: 1.35;
  cursor: pointer;
  color: var(--text);
}
.seq-cyclic-label input { margin-top: 2px; flex-shrink: 0; }
.seq-relay-header,
.seq-relay-row {
  display: grid;
  grid-template-columns: 48px 56px 1fr;
  align-items: center;
  gap: 10px;
  padding: 6px 4px;
  font-size: .75rem;
}
.seq-relay-header {
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .04em;
  border-bottom: 1px solid var(--border);
  margin-bottom: 4px;
}
.seq-relay-rows {
  max-height: 240px;
  overflow-y: auto;
  margin-bottom: 10px;
}
.seq-relay-row {
  border-bottom: 1px solid var(--border);
}
.seq-relay-row:last-child { border-bottom: none; }
.seq-relay-row.is-skipped { opacity: .45; }
.seq-relay-row.is-skipped .seq-delay-input { pointer-events: none; background: var(--surface-2, #f1f5f9); }
.seq-relay-name { font-weight: 700; font-size: .82rem; }
.seq-skip-label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: .78rem;
  cursor: pointer;
}
.sequence-summary {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 12px;
  font-size: .78rem;
  color: var(--muted);
  padding: 10px 12px;
  background: var(--surface-2, #f8fafc);
  border-radius: 8px;
  border: 1px solid var(--border);
}
.sequence-summary strong { color: var(--text); }
.sched-cmd-seq { background: #f0fdf4; color: #15803d; }

.schedules-empty {
  text-align: center;
  padding: 16px;
  color: var(--muted);
  font-size: .82rem;
}

.sched-auto-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  margin-bottom: 10px;
}
.sched-auto-toggle input[type="checkbox"] {
  width: 16px;
  height: 16px;
  accent-color: var(--primary);
  cursor: pointer;
  flex-shrink: 0;
}
.sched-auto-label {
  font-size: .82rem;
  font-weight: 600;
  color: var(--text-secondary);
}

.sched-duration-hint {
  font-size: .7rem;
  color: var(--muted);
  margin-top: -6px;
  margin-bottom: 8px;
  font-style: italic;
}

/* ── Activity Log ──────────────────────────────────────────────────────────── */
.log-type-badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: .68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .02em;
  white-space: nowrap;
}
.log-type-badge.command        { background: #ecfdf5; color: #047857; }
.log-type-badge.command_result { background: #f0fdf4; color: #16a34a; }
.log-type-badge.command_failed { background: #fef2f2; color: #dc2626; }
.log-type-badge.schedule_create  { background: #faf5ff; color: #7c3aed; }
.log-type-badge.schedule_execute { background: #f0fdf4; color: #16a34a; }
.log-type-badge.schedule_delete  { background: #fff7ed; color: #ea580c; }
.log-type-badge.schedule_failed  { background: #fef2f2; color: #dc2626; }
.log-type-badge.device_online  { background: #f0fdf4; color: #16a34a; }
.log-type-badge.device_offline { background: #f1f5f9; color: #64748b; }
.log-type-badge.motor_trip     { background: #fef2f2; color: #dc2626; }
.log-type-badge.login          { background: #ecfdf5; color: #047857; }
.log-type-badge.logout         { background: #f1f5f9; color: #64748b; }
.log-type-badge.user_action    { background: #fff7ed; color: #ea580c; }

.log-status {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: .75rem;
  font-weight: 600;
}
.log-status.ok     { color: var(--success); }
.log-status.failed { color: var(--danger); }
.log-status.pending { color: var(--warning); }

.log-details {
  font-size: .78rem;
  color: var(--text-secondary);
  max-width: 280px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.log-time {
  font-size: .75rem;
  color: var(--muted);
  white-space: nowrap;
}

.log-device {
  font-weight: 600;
  font-size: .82rem;
}
.log-device-token {
  font-size: .68rem;
  color: #94a3b8;
  font-family: monospace;
}

.log-user {
  font-weight: 600;
  font-size: .82rem;
}
.log-user-role {
  font-size: .68rem;
  color: var(--muted);
}

/* ── Device Filter Toolbar ──────────────────────────────────────────────────── */
.device-filters {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}
.device-search-wrap {
  position: relative;
  flex: 1;
  min-width: 200px;
  max-width: 320px;
}
.device-search-wrap svg {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--muted);
}
.device-search-input {
  width: 100%;
  padding: 8px 12px 8px 34px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--card-bg);
  font-size: .82rem;
  color: var(--text);
  transition: border-color var(--transition), box-shadow var(--transition);
}
.device-search-input:focus {
  outline: none;
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px rgba(37,99,235,.1);
}
.filter-select {
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--card-bg);
  font-size: .82rem;
  color: var(--text);
  min-width: 130px;
}
.filter-select:focus {
  outline: none;
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px rgba(37,99,235,.1);
}
.device-count-label {
  font-size: .8rem;
  color: var(--muted);
  margin-left: auto;
  white-space: nowrap;
}

/* ── Responsive ─────────────────────────────────────────────────────────────── */
@media (max-width: 768px) {
  .sidebar {
    transform: translateX(calc(-1 * var(--sidebar-width)));
  }
  .sidebar.open {
    transform: translateX(0);
  }
  .content-wrapper { margin-left: 0; }
  .mobile-toggle { display: flex; }
  .topbar { padding: 0 16px; }
  .main-content { padding: 16px; }
  .devices-grid { grid-template-columns: 1fr; }
  .stats-grid { grid-template-columns: 1fr 1fr; }
  .form-row { grid-template-columns: 1fr; }
  .output-row {
    display: flex;
    flex-wrap: nowrap;
    justify-content: space-between;
    align-items: center;
    gap: 6px;
    padding: 10px 14px;
  }
  .output-label {
    width: auto;
    min-width: 3.2rem;
    max-width: 34%;
  }
  .output-row .state-value {
    flex: 1 1 auto;
    text-align: center;
    font-size: .78rem;
  }
  .output-actions {
    flex-shrink: 0;
    gap: 4px;
  }
  .output-actions .btn-xs {
    padding: 4px 10px;
    font-size: .72rem;
  }
  .device-outputs.motor-controls .output-actions {
    width: 7.25rem;
    min-width: 7.25rem;
  }
  .seq-relay-header,
  .seq-relay-row {
    grid-template-columns: 40px 48px minmax(0, 1fr);
    gap: 6px;
  }
}
@media (max-width: 480px) {
  .stats-grid { grid-template-columns: 1fr; }
  .output-label.motor-label { font-size: .78rem; }
  .dly-input { width: 46px; }
  .btn-xs { padding: 4px 12px; }
}
