:root {
  /* -- PALETTE: Deep Space / Dashboard Theme -- */
  --bg: #0f172a;       /* Very dark slate blue */
  --card: #1e293b;     /* Lighter slate for panels */
  --surface-2: #334155;/* Interactive items */
  
  --accent: #3b82f6;   /* Bright Blue */
  --text: #f8fafc;     /* Almost white */
  --muted: #94a3b8;    /* Muted text */
  --border: rgba(255, 255, 255, 0.08); /* Subtle borders */

  --success: #10b981;  /* Emerald Green */
  --warn: #f59e0b;     /* Amber */
  --danger: #ef4444;   /* Red */
  
  --glass: rgba(255, 255, 255, 0.03);
}

:root { color-scheme: dark; }

html, body {
  height: 100%; margin: 0;
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  background: var(--bg); color: var(--text);
}

/* --- LOGIN PAGE --- */
body.login { display: grid; place-items: center; padding: 28px; }
.card {
  width: 400px; max-width: 100%;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px; padding: 24px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
}

h1 { margin: 0 0 8px; font-size: 1.5rem; letter-spacing: -0.5px; }
p.lead { margin: 0 0 20px; color: var(--muted); font-size: 0.9rem; }
label { display: block; font-size: 0.85rem; margin-bottom: 6px; color: var(--text); font-weight: 500; }

input[type="email"], input[type="password"], textarea, select {
  width: 100%; padding: 12px; border-radius: 8px;
  border: 1px solid var(--border);
  background: rgba(0, 0, 0, 0.2);
  color: var(--text); box-sizing: border-box; outline: none;
  transition: border-color 0.2s;
}
input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.15); }

.submit {
  width: 100%; padding: 12px; border-radius: 8px; border: 0;
  background: linear-gradient(135deg, var(--accent), #2563eb);
  color: white; font-weight: 600; cursor: pointer;
  box-shadow: 0 4px 12px rgba(37, 99, 235, 0.3);
  transition: transform 0.1s;
}
.submit:active { transform: scale(0.98); }

.error { color: var(--danger); margin-top: 10px; font-size: 0.85rem; background: rgba(239, 68, 68, 0.1); padding: 8px; border-radius: 6px; text-align: center; }
footer { margin-top: 20px; text-align: center; font-size: 0.8rem; color: var(--muted); }
footer a { color: var(--accent); text-decoration: none; }

/* --- MAIN DASHBOARD LAYOUT --- */
body.index { padding: 20px; }
header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }

.username { font-weight: 700; font-size: 1.1rem; }
.container { display: grid; grid-template-columns: 320px 1fr; gap: 20px; }

/* Panels (Cards) */
.card-wide, .panel {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.3);
}

/* --- LEFT SIDE: MACHINES --- */
h3 { margin: 0 0 12px 0; font-size: 0.95rem; text-transform: uppercase; letter-spacing: 0.5px; color: var(--muted); }

.machine-list { list-style: none; padding: 0; margin: 0; }
.machine-item {
  display: flex; justify-content: space-between; align-items: center;
  padding: 14px; border-radius: 8px; cursor: pointer;
  background: var(--surface-2);
  border-left: 3px solid transparent;
  margin-bottom: 10px;
  transition: all 0.2s ease;
}
.machine-item:hover {
  background: #3e4c63; /* Slightly lighter than surface-2 */
  border-left-color: var(--accent);
  transform: translateX(2px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.2);
}

.badge { padding: 4px 10px; border-radius: 20px; font-size: 0.75rem; font-weight: 600; text-transform: uppercase; }
.badge.online { background: rgba(16, 185, 129, 0.15); color: var(--success); box-shadow: 0 0 5px rgba(16, 185, 129, 0.1); }
.badge.offline { background: rgba(244, 63, 94, 0.1); color: var(--danger); }

/* --- RIGHT SIDE: WORKSPACE (Detail View) --- */
#detailPanel {
  background: radial-gradient(circle at top, #1e293b, #0f172a); /* Subtle gradient */
  min-height: 600px;
  display: flex; flex-direction: column;
}

#detailEmpty {
  flex-grow: 1; display: flex; align-items: center; justify-content: center;
  color: var(--muted); font-size: 1.1rem; opacity: 0.6;
}

.detail-header {
  border-bottom: 1px solid var(--border); padding-bottom: 16px; margin-bottom: 16px;
}
.detail-columns {
  display: grid; grid-template-columns: 2fr 1fr; /* Main controls wider than history */
  gap: 20px; align-items: start;
}

/* Sensors & Actuators */
.sensor-row, .actuator-row {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 16px; margin-bottom: 8px;
  background: rgba(0,0,0,0.2); border-radius: 8px;
  border: 1px solid transparent;
}
.sensor-row:hover, .actuator-row:hover { border-color: var(--border); }

.sensor-value { font-family: 'Roboto Mono', monospace; font-size: 1.1rem; color: var(--accent); }

/* Buttons inside Detail View */
.actuator-row button {
  padding: 6px 14px; border-radius: 6px; cursor: pointer; font-size: 0.85rem; font-weight: 600;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
  margin-left: 6px; transition: all 0.2s;
}
.actuator-row button:hover:not(:disabled) { background: var(--accent); color: white; border-color: var(--accent); }
.actuator-row button:disabled { opacity: 0.4; cursor: not-allowed; }

/* History Column */
.detail-history {
  background: rgba(0,0,0,0.2); border-radius: 8px; padding: 0;
  border: 1px solid var(--border); overflow: hidden;
}
.detail-history h3 { padding: 12px; margin: 0; background: rgba(255,255,255,0.03); border-bottom: 1px solid var(--border); font-size: 0.8rem; }
.history-wrap { max-height: 400px; overflow-y: auto; }
.history { width: 100%; border-collapse: collapse; font-size: 0.8rem; }
.history td, .history th { padding: 8px 12px; text-align: left; border-bottom: 1px solid var(--border); }
.history tr:last-child td { border-bottom: none; }
.history th { color: var(--muted); font-weight: 600; background: rgba(0,0,0,0.2); position: sticky; top: 0; }

/* --- HEADER CONTROLS --- */
.header-controls { display: flex; gap: 12px; align-items: center; }

/* The Connect Button - HERO STYLE */
.btn-connect {
  background: linear-gradient(135deg, var(--accent), #2563eb);
  color: white; border: none; padding: 10px 20px; border-radius: 8px;
  font-weight: 600; font-size: 0.85rem; cursor: pointer;
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
  text-transform: uppercase; letter-spacing: 0.5px;
  transition: all 0.2s;
}
.btn-connect:hover { transform: translateY(-1px); box-shadow: 0 6px 16px rgba(59, 130, 246, 0.5); }
.btn-connect:disabled { filter: grayscale(1); opacity: 0.7; cursor: not-allowed; }

/* Disconnect State */
.btn-connect.connected {
  background: linear-gradient(135deg, #ef4444, #dc2626); /* Red */
  box-shadow: 0 4px 12px rgba(239, 68, 68, 0.3);
}

/* Secondary Buttons */
.btn {
  background: transparent; border: 1px solid var(--border); color: var(--muted);
  padding: 8px 16px; border-radius: 8px; cursor: pointer; font-size: 0.85rem;
  transition: 0.2s;
}
.btn:hover { border-color: var(--text); color: var(--text); }

#adminBtn {
  border-color: var(--accent); color: var(--accent);
}
#adminBtn:hover { background: rgba(59, 130, 246, 0.1); }

/* LED Indicator */
.led {
  width: 12px; height: 12px; border-radius: 50%;
  background: #333; box-shadow: inset 0 1px 3px rgba(0,0,0,0.9);
  border: 1px solid #444; transition: 0.3s;
}
.led.on { background: #10b981; box-shadow: 0 0 10px #10b981, inset 0 1px 3px rgba(255,255,255,0.4); border-color: #10b981; }
.led.error { background: #ef4444; box-shadow: 0 0 10px #ef4444; }

/* --- ACTIVITY LOG --- */
.activity-stream {
  background: #020617; /* Very dark terminal color */
  border: 1px solid var(--border); border-radius: 8px;
  height: 180px; overflow-y: auto;
  padding: 0; list-style: none;
  font-family: 'Menlo', 'Consolas', monospace; font-size: 0.75rem;
  display: flex; flex-direction: column;
}
.activity-item { padding: 6px 12px; border-bottom: 1px solid rgba(255,255,255,0.05); display: flex; }
.activity-time { color: var(--muted); margin-right: 10px; min-width: 60px; }
.activity-msg { color: var(--text); }
.direction-tx { color: var(--accent); font-weight: bold; margin-right: 5px; }
.direction-rx { color: var(--success); font-weight: bold; margin-right: 5px; }

/* --- USER MANAGEMENT --- */
#userManagement {
  margin-top: 20px; background: rgba(0,0,0,0.2);
  border-top: 1px solid var(--border); padding: 15px 0 0 0;
}
.user-table { width: 100%; border-collapse: collapse; margin-top: 10px; }
.user-table th { text-align: left; color: var(--muted); font-size: 0.7rem; text-transform: uppercase; padding: 0 0 8px 0; border-bottom: 1px solid var(--border); }
.user-table td { padding: 12px 0; border-bottom: 1px solid rgba(255,255,255,0.03); font-size: 0.85rem; }
.user-table tr:last-child td { border-bottom: none; }
.user-table td:last-child { text-align: right; }

/* Toggle Switch */
.switch { position: relative; display: inline-block; width: 34px; height: 18px; vertical-align: middle; margin-right: 8px; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: #475569; transition: .4s; border-radius: 34px; }
.slider:before { position: absolute; content: ""; height: 12px; width: 12px; left: 3px; bottom: 3px; background-color: white; transition: .4s; border-radius: 50%; }
input:checked + .slider { background-color: var(--accent); }
input:checked + .slider:before { transform: translateX(16px); }
input:disabled + .slider { opacity: 0.4; cursor: not-allowed; }

/* Utility */
.hidden { display: none !important; }
@media (max-width: 900px) { .container { grid-template-columns: 1fr; } .detail-columns { grid-template-columns: 1fr; } }
