:root {
  color-scheme: dark;
  --bg: #05070a;
  --card: #0a0f0d;
  --text: #baffc9;
  --bright: #4dff88;
  --muted: #4a7a5c;
  --border: #1c3d28;
  --primary: #00ff66;
  --primary-hover: #33ff88;
  --danger: #ff3b3b;
  --online: #00ff66;
  --offline: #6b6b6b;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
}

body {
  margin: 0;
  font-family: ui-monospace, "SFMono-Regular", "Courier New", Consolas, monospace;
  background: var(--bg);
  color: var(--text);
  position: relative;
  overflow-x: hidden;
}

#matrixRain {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: -2;
  opacity: 0.16;
  pointer-events: none;
}

.scanlines {
  position: fixed;
  inset: 0;
  z-index: 999;
  pointer-events: none;
  background: repeating-linear-gradient(
    to bottom,
    rgba(0, 0, 0, 0) 0px,
    rgba(0, 0, 0, 0.15) 1px,
    rgba(0, 0, 0, 0) 2px
  );
  mix-blend-mode: overlay;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--border);
  background: linear-gradient(to bottom, rgba(0, 255, 102, 0.04), transparent);
}

.topbar h1 {
  font-size: 1.05rem;
  margin: 0;
  font-weight: 500;
  letter-spacing: 0.02em;
}

.topbar .prompt { color: var(--bright); text-shadow: 0 0 6px rgba(77, 255, 136, 0.6); }
.topbar .tilde { color: var(--muted); }

.blink-cursor {
  display: inline-block;
  animation: blink 1s steps(1) infinite;
  color: var(--bright);
}

@keyframes blink {
  0%, 49% { opacity: 1; }
  50%, 100% { opacity: 0; }
}

.user-box { display: flex; align-items: center; gap: 0.75rem; color: var(--muted); font-size: 0.85rem; }
.uid-label { color: var(--muted); letter-spacing: 0.08em; font-size: 0.7rem; }
#userEmail { color: var(--bright); }

main {
  max-width: 900px;
  margin: 0 auto;
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
  position: relative;
  z-index: 1;
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1.25rem;
  box-shadow: 0 0 0 1px rgba(0, 255, 102, 0.04), 0 0 24px rgba(0, 255, 102, 0.03);
  position: relative;
}

.card::before {
  content: "";
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 2px;
  background: linear-gradient(to right, var(--primary), transparent 60%);
  opacity: 0.6;
}

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

.card h2 { margin-top: 0; font-size: 1rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.03em; }

.auth-card { max-width: 400px; margin: 2.5rem auto; }

.tabs { display: flex; gap: 0.5rem; margin-bottom: 1rem; border-bottom: 1px solid var(--border); }

.tab-btn {
  flex: 1;
  padding: 0.5rem;
  border: none;
  background: transparent;
  color: var(--muted);
  border-bottom: 2px solid transparent;
  cursor: pointer;
  font-size: 0.9rem;
  font-family: inherit;
  text-transform: lowercase;
}

.tab-btn::before { content: "> "; opacity: 0; }
.tab-btn.active::before { opacity: 1; }
.tab-btn.active { color: var(--bright); border-bottom-color: var(--primary); }

.tab-panel { display: none; flex-direction: column; gap: 0.85rem; }
.tab-panel.active { display: flex; }

label { display: flex; flex-direction: column; gap: 0.3rem; font-size: 0.8rem; color: var(--muted); text-transform: lowercase; }

input {
  padding: 0.55rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: 2px;
  background: #050a07;
  color: var(--bright);
  font-size: 0.95rem;
  font-family: inherit;
}

input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 1px var(--primary), 0 0 8px rgba(0, 255, 102, 0.35);
}

input::placeholder { color: var(--muted); opacity: 0.7; }

.btn {
  padding: 0.55rem 1rem;
  border-radius: 2px;
  border: 1px solid var(--border);
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 500;
  font-family: inherit;
  background: transparent;
  color: var(--bright);
  transition: background 0.15s, box-shadow 0.15s;
}

.btn-primary { border-color: var(--primary); color: var(--primary); }
.btn-primary:hover { background: rgba(0, 255, 102, 0.1); box-shadow: 0 0 12px rgba(0, 255, 102, 0.35); }

.btn-ghost { background: transparent; color: var(--muted); border: 1px solid var(--border); }
.btn-ghost:hover { color: var(--bright); border-color: var(--muted); }

.btn-danger { background: transparent; color: var(--danger); border: 1px solid var(--danger); padding: 0.3rem 0.6rem; font-size: 0.78rem; }
.btn-danger:hover { background: rgba(255, 59, 59, 0.1); }

.form-msg { min-height: 1.2em; font-size: 0.82rem; margin: 0; }
.form-msg.error { color: var(--danger); }
.form-msg.error::before { content: "! "; }
.form-msg.success { color: var(--online); }

.inline-form { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; }
.inline-form input { flex: 1; min-width: 200px; }

.section-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.section-head .card-title { margin-bottom: 0; }

table { width: 100%; border-collapse: collapse; margin-top: 1rem; font-size: 0.85rem; }
th, td { text-align: left; padding: 0.6rem 0.5rem; border-bottom: 1px solid var(--border); }
th { color: var(--muted); font-weight: 500; font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.05em; }
td { color: var(--text); }

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.2rem 0.6rem;
  border-radius: 2px;
  font-size: 0.75rem;
  background: rgba(255, 255, 255, 0.02);
  border: 1px solid var(--border);
  text-transform: uppercase;
}

.status-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--offline); }
.status-pill.online .status-dot { background: var(--online); box-shadow: 0 0 6px var(--online); }
.status-pill.online { color: var(--online); border-color: rgba(0, 255, 102, 0.3); }

.row-actions { display: flex; gap: 0.4rem; flex-wrap: wrap; }

.empty-state { color: var(--muted); text-align: center; padding: 1rem 0; font-style: normal; }

.hint { color: var(--muted); font-size: 0.8rem; margin: 0 0 0.75rem; }
.hint code { background: rgba(0, 255, 102, 0.08); color: var(--bright); padding: 0.1rem 0.35rem; border-radius: 2px; }

.api-key-banner { border-color: var(--primary); box-shadow: 0 0 20px rgba(0, 255, 102, 0.15); }
#apiKeyValue { font-family: ui-monospace, monospace; color: var(--bright); }

.events-list { list-style: none; margin: 0.5rem 0 0; padding: 0; display: flex; flex-direction: column; gap: 0.5rem; }
.event-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 2px;
  font-size: 0.85rem;
  background: rgba(0, 255, 102, 0.02);
}
.event-type { font-weight: 500; color: var(--bright); }
.event-type::before { content: "> "; color: var(--muted); }
.event-payload { color: var(--muted); font-size: 0.78rem; font-family: ui-monospace, monospace; }
.event-time { color: var(--muted); font-size: 0.78rem; white-space: nowrap; }

::selection { background: var(--primary); color: #000; }

.hidden { display: none !important; }

/* ---------- Responsive ---------- */

@media (max-width: 700px) {
  .topbar {
    flex-wrap: wrap;
    gap: 0.6rem;
    padding: 0.85rem 1rem;
  }

  .topbar h1 { font-size: 0.92rem; }

  .user-box { font-size: 0.8rem; gap: 0.5rem; flex-wrap: wrap; }

  main {
    padding: 1rem;
    gap: 1rem;
  }

  .card { padding: 1rem; }

  .auth-card { margin: 1.25rem auto; max-width: 100%; }

  .section-head {
    flex-wrap: wrap;
    align-items: flex-start;
  }

  .inline-form { flex-direction: column; align-items: stretch; }
  .inline-form input { min-width: 0; width: 100%; }

  .btn, input {
    font-size: 1rem;
    padding: 0.7rem 0.9rem;
    min-height: 44px;
  }

  .row-actions { width: 100%; }
  .row-actions .btn { flex: 1 1 auto; min-width: 0; }

  .event-row { flex-direction: column; align-items: flex-start; gap: 0.2rem; }
}

@media (max-width: 860px) {
  table, thead, tbody, th, tr, td { display: block; }

  #devicesTable thead {
    position: absolute;
    width: 1px; height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
  }

  #devicesTable tr {
    margin-bottom: 0.9rem;
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 0.5rem 0.75rem;
    background: rgba(0, 255, 102, 0.02);
  }

  #devicesTable td {
    border-bottom: none;
    padding: 0.4rem 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.75rem;
    text-align: right;
  }

  #devicesTable td[data-label]:not([data-label=""])::before {
    content: attr(data-label);
    color: var(--muted);
    text-transform: uppercase;
    font-size: 0.68rem;
    letter-spacing: 0.05em;
    text-align: left;
  }

  #devicesTable td.row-actions {
    flex-wrap: wrap;
    justify-content: flex-start;
    padding-top: 0.6rem;
  }
}

@media (max-width: 420px) {
  .topbar h1 { font-size: 0.8rem; }

  .tabs { gap: 0.25rem; }
  .tab-btn { padding: 0.5rem 0.25rem; font-size: 0.85rem; }
}

@media (hover: none) and (pointer: coarse) {
  .btn { min-height: 44px; }
}
