/* === Base === */
* { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --bg: #f4f4f9;
  --bg-light: #eeeef4;
  --surface: #ffffff;
  --surface-hover: #fafafa;
  --border: #e8e8ed;
  --text: #1a1a2e;
  --text-secondary: #333350;
  --text-muted: #8e8ea0;
  --primary: #0028a5;
  --primary-hover: #001d7a;
  --danger: #c9184a;
  --success: #12b886;
  --warning: #f08c00;
}

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

/* === Logo === */
.logo {
  display: inline-flex;
  align-items: center;
  gap: 0.6rem;
}

.logo-icon {
  width: 30px;
  height: 30px;
  flex-shrink: 0;
}

.logo-icon-lg {
  width: 52px;
  height: 52px;
}

/* === Login === */
.login-body {
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-light);
}

.login-container {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 3rem;
  width: 100%;
  max-width: 420px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
}

.login-header {
  text-align: center;
  margin-bottom: 2rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.4rem;
}

.login-header h1 {
  font-size: 2rem;
  font-weight: 800;
  color: var(--primary);
}

.subtitle {
  color: var(--text-muted);
  margin-top: 0.1rem;
  font-size: 0.9rem;
}

.form-group {
  margin-bottom: 1.1rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.4rem;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-secondary);
}

.form-group input {
  width: 100%;
  padding: 0.7rem 1rem;
  background: var(--bg-light);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 1rem;
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.form-group input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(0, 40, 165, 0.08);
}

.error {
  color: var(--danger);
  font-size: 0.875rem;
  margin-top: 0.75rem;
  text-align: center;
}

/* === Buttons === */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.6rem 1.2rem;
  border: none;
  border-radius: 24px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.btn:hover { transform: translateY(-1px); }
.btn:active { transform: translateY(0) scale(0.98); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; transform: none; }

.btn-primary {
  background: var(--primary);
  color: white;
  width: 100%;
  padding: 0.8rem;
  margin-top: 0.75rem;
  font-size: 1rem;
}

.btn-primary:hover {
  background: var(--primary-hover);
}

.btn-danger {
  background: var(--primary);
  color: white;
}

.btn-danger:hover {
  background: var(--primary-hover);
}

.btn-sm {
  background: transparent;
  color: var(--text-muted);
  font-size: 0.8rem;
  padding: 0.4rem 0.85rem;
  border: 1px solid var(--border);
}

.btn-sm:hover {
  background: var(--bg-light);
  color: var(--text);
}

.btn-large {
  padding: 0.9rem 2.5rem;
  font-size: 1rem;
}

.btn-icon {
  font-size: 1.2rem;
}

/* === Header === */
header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.85rem 2rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.header-left {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.header-left .logo {
  align-items: center;
}

.header-left h1 {
  font-size: 1.15rem;
  font-weight: 800;
  color: var(--primary);
}

.header-subtitle {
  color: var(--text-muted);
  font-size: 0.85rem;
  margin-left: 0.25rem;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 1rem;
}

#currentUser {
  color: var(--text-muted);
  font-size: 0.85rem;
}

/* === Main === */
main {
  max-width: 900px;
  margin: 2rem auto;
  padding: 0 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

/* === Cards === */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1.5rem;
}

.card h2 {
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.1em;
  margin-bottom: 1rem;
}

/* === Status === */
.status-row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0;
}

.status-indicator {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  font-size: 1.5rem;
  font-weight: 800;
}

.status-dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  animation: pulse 2s infinite;
}

.status-indicator.online .status-dot { background: var(--success); box-shadow: 0 0 8px rgba(18, 184, 134, 0.4); }
.status-indicator.online #statusText { color: var(--success); }

.status-indicator.offline .status-dot { background: var(--danger); box-shadow: 0 0 8px rgba(201, 24, 74, 0.4); }
.status-indicator.offline #statusText { color: var(--danger); }

.status-indicator.booting .status-dot { background: var(--warning); box-shadow: 0 0 8px rgba(240, 140, 0, 0.4); animation: pulse 0.5s infinite; }
.status-indicator.booting #statusText { color: var(--warning); }

.status-indicator.disconnected .status-dot { background: var(--text-muted); }
.status-indicator.disconnected #statusText { color: var(--text-muted); }

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

.status-since {
  color: var(--text-muted);
  font-size: 0.85rem;
  margin-left: 0.85rem;
  margin-right: 1.25rem;
}

.status-row-sep {
  width: 1px;
  height: 2.5rem;
  background: var(--border);
  margin-right: 1.25rem;
  flex-shrink: 0;
}

.device-info-inline {
  display: flex;
  align-items: center;
}

/* === Reboot Terminal === */
.reboot-terminal {
  display: none;
  margin-top: 1rem;
  background: #0d1117;
  border: 1px solid #30363d;
  border-radius: 10px;
  overflow: hidden;
}

.reboot-terminal.visible { display: block; }

.reboot-terminal.collapsed .terminal-body { display: none; }

.reboot-terminal.collapsed .terminal-header { cursor: pointer; border-radius: 0 0 10px 10px; border-bottom: none; }
.reboot-terminal.collapsed .terminal-header:hover { background: #1c2128; }

.terminal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.55rem 1rem;
  background: #161b22;
  border-bottom: 1px solid #30363d;
}

.terminal-dots {
  display: flex;
  gap: 0.4rem;
  align-items: center;
}

.terminal-dot {
  width: 11px;
  height: 11px;
  border-radius: 50%;
}

.dot-red    { background: #ff5f57; }
.dot-yellow { background: #ffbd2e; }
.dot-green  { background: #28ca41; }

.terminal-title {
  font-size: 0.72rem;
  font-weight: 600;
  color: #8b949e;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-family: 'SF Mono', Monaco, monospace;
}

.terminal-close {
  background: none;
  border: none;
  color: #484f58;
  font-size: 0.75rem;
  cursor: pointer;
  padding: 0.1rem 0.3rem;
  border-radius: 4px;
  transition: color 0.15s;
}

.terminal-close:hover { color: #8b949e; }

.terminal-body {
  padding: 0.9rem 1rem;
  font-family: 'SF Mono', Monaco, 'Cascadia Code', monospace;
  font-size: 0.78rem;
  min-height: 80px;
  max-height: 260px;
  overflow-y: auto;
}

.terminal-line {
  display: flex;
  align-items: baseline;
  gap: 0.65rem;
  margin-bottom: 0.3rem;
  line-height: 1.6;
  animation: terminalFadeIn 0.15s ease;
}

@keyframes terminalFadeIn {
  from { opacity: 0; transform: translateY(2px); }
  to   { opacity: 1; transform: translateY(0); }
}

.terminal-time  { color: #484f58; flex-shrink: 0; font-size: 0.72rem; }
.terminal-icon  { flex-shrink: 0; width: 1rem; text-align: center; }
.terminal-text  { color: #8b949e; }

.terminal-line.success .terminal-icon,
.terminal-line.success .terminal-text { color: #3fb950; }

.terminal-line.error .terminal-icon   { color: #f85149; }
.terminal-line.error .terminal-text   { color: #6e7681; }

.terminal-line.warning .terminal-icon,
.terminal-line.warning .terminal-text { color: #d29922; }

.terminal-line.info .terminal-icon    { color: #58a6ff; }
.terminal-line.info .terminal-text    { color: #8b949e; }

.terminal-done {
  margin-top: 0.6rem;
  padding: 0.45rem 0.9rem;
  background: rgba(63, 185, 80, 0.12);
  border: 1px solid rgba(63, 185, 80, 0.3);
  border-radius: 6px;
  color: #3fb950;
  font-family: 'SF Mono', Monaco, monospace;
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-align: center;
  animation: terminalFadeIn 0.2s ease;
}

/* === Actions === */
.actions-grid {
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
}

.action-feedback {
  align-items: center;
  gap: 0.75rem;
  margin-top: 1rem;
  padding: 0.75rem 1rem;
  background: var(--bg-light);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.9rem;
  display: none;
}

.action-feedback.visible {
  display: flex;
}

.feedback-spinner {
  width: 16px;
  height: 16px;
  border: 2px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

/* === History === */
.history-table-wrapper {
  overflow-x: auto;
  max-height: 300px;
  overflow-y: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}

th {
  text-align: left;
  padding: 0.6rem 0.75rem;
  color: var(--text-muted);
  font-weight: 600;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--surface);
}

td {
  padding: 0.6rem 0.75rem;
  border-bottom: 1px solid var(--border);
}

tr:hover td { background: var(--surface-hover); }

.empty {
  text-align: center;
  color: var(--text-muted);
  padding: 2rem !important;
}

/* === Footer === */
footer {
  text-align: center;
  padding: 2rem;
  color: var(--text-muted);
  font-size: 0.75rem;
}

/* === MQTT Info Table === */
.mqtt-info-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
  margin: 0.5rem 0;
}

.mqtt-info-table td {
  padding: 0.3rem 0.5rem;
  border-bottom: 1px solid var(--border);
}

.mqtt-info-table td:first-child {
  color: var(--text-muted);
  font-weight: 500;
  width: 160px;
  white-space: nowrap;
}

.mqtt-info-table td.mono {
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 0.8rem;
}

/* === MQTT Client Actions Bar === */
.mqtt-client-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.75rem;
}

.btn-edit-client {
  background: var(--primary);
  color: white;
  border: none;
}

.btn-edit-client:hover {
  background: var(--primary-hover);
}

.btn-defaults-client {
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
}

.btn-defaults-client:hover {
  background: var(--bg-light);
  color: var(--text);
}

/* === MQTT Edit Section (hidden by default) === */
.mqtt-client-edit-section {
  margin-top: 0.75rem;
  padding: 0.75rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
}

.mqtt-client-config {
  display: flex;
  align-items: flex-end;
  gap: 0.75rem;
}

.mqtt-config-field {
  flex: 1;
}

.mqtt-config-field-port {
  flex: 0 0 90px;
}

.mqtt-config-field label {
  display: block;
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 0.3rem;
}

.mqtt-client-config input {
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 0.82rem;
  padding: 0.4rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  width: 100%;
  box-sizing: border-box;
}

.mqtt-client-config input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(0, 40, 165, 0.15);
}

.mqtt-client-config input.changed {
  border-color: #f59e0b;
  background: #fffbeb;
}

.mqtt-edit-buttons {
  display: flex;
  gap: 0.5rem;
  margin-top: 0.6rem;
}

.btn-save-client {
  background: var(--primary);
  color: white;
  border: none;
}

.btn-save-client:hover:not(:disabled) {
  background: var(--primary-hover);
}

.btn-save-client:disabled {
  opacity: 0.4;
  cursor: default;
}

.btn-cancel-edit {
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
}

.btn-cancel-edit:hover {
  background: var(--bg-light);
  color: var(--text);
}

/* === MQTT Config Feedback === */
.mqtt-config-feedback {
  margin-top: 0.75rem;
  font-size: 0.82rem;
  padding: 0.5rem 0.75rem;
  border-radius: 6px;
  display: none;
}

.mqtt-config-feedback.success {
  display: block;
  background: #ecfdf5;
  color: #065f46;
  border: 1px solid #a7f3d0;
}

.mqtt-config-feedback.error {
  display: block;
  background: #fef2f2;
  color: #991b1b;
  border: 1px solid #fecaca;
}

/* === MQTT Card === */
.mqtt-card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
}

.mqtt-card-header h2 {
  margin-bottom: 0;
}

.mqtt-grid {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.mqtt-client {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.9rem 1.1rem;
}

.mqtt-client-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.6rem;
}

.mqtt-client-title {
  display: flex;
  align-items: baseline;
  gap: 0.6rem;
}

.mqtt-name {
  font-weight: 700;
  font-size: 0.9rem;
  color: var(--text);
}

.mqtt-badge {
  font-size: 0.7rem;
  font-weight: 700;
  padding: 0.2rem 0.55rem;
  border-radius: 20px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.mqtt-badge.connected { background: #d3f9e8; color: #087f5b; }
.mqtt-badge.error     { background: #ffe0e7; color: #a61e4d; }
.mqtt-badge.unknown   { background: #e9ecef; color: #495057; }

.mqtt-broker {
  font-size: 0.78rem;
  color: var(--text-muted);
  font-family: 'SF Mono', Monaco, monospace;
}

.mqtt-stats {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 0;
}

.mqtt-stat {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  font-size: 0.8rem;
  padding-right: 1.25rem;
}

.mqtt-stat-divider {
  width: 1px;
  height: 2rem;
  background: var(--border);
  margin-right: 1.25rem;
  flex-shrink: 0;
}

.mqtt-stat-label { color: var(--text-muted); font-size: 0.73rem; }
.mqtt-stat-value { font-weight: 600; color: var(--text); }

.mqtt-details {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-top: 0.65rem;
  padding-top: 0.65rem;
  border-top: 1px solid var(--border);
}

.mqtt-detail-pill {
  font-size: 0.72rem;
  font-weight: 500;
  color: var(--text-muted);
  background: var(--bg-light);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 0.15rem 0.55rem;
}

.mqtt-detail-sep {
  width: 1px;
  height: 1rem;
  background: var(--border);
  margin: 0 0.25rem;
  flex-shrink: 0;
}

.mqtt-will {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.72rem;
  font-family: 'SF Mono', Monaco, 'Cascadia Code', monospace;
}

.mqtt-will-label {
  color: var(--text-muted);
  font-family: inherit;
  font-weight: 600;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.mqtt-will-topic { color: var(--text-secondary); }
.mqtt-will-arrow { color: var(--text-muted); }
.mqtt-will-value { color: var(--danger); font-weight: 600; }

.mqtt-loading, .mqtt-error {
  color: var(--text-muted);
  font-size: 0.9rem;
  padding: 1rem 0;
}

.mqtt-error { color: var(--danger); }

/* === Responsive === */
@media (max-width: 600px) {
  main { padding: 0 1rem; margin: 1rem auto; }
  header { padding: 0.75rem 1rem; }
  .status-indicator { font-size: 1.2rem; }
  .login-container { margin: 1rem; padding: 2rem; }
}
