:root {
  --bg: #0a0a0a;
  --surface: #1a1a1c;
  --surface-2: #242428;
  --border: #333338;
  --text: #f2f2f4;
  --text-dim: #9a9aa2;
  --accent: #4a9eff;
  --green: #34c759;
  --grey: #6b6b72;
  --red: #ff4d4d;
  --radius: 16px;
  --radius-sm: 10px;
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;
  --tap-min: 44px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  -webkit-tap-highlight-color: transparent;
  height: 100%;
}

#app {
  min-height: 100vh;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}

button {
  font-family: inherit;
  border: none;
  background: none;
  color: inherit;
  cursor: pointer;
}

a { color: var(--accent); }

/* ---------- PIN screen ---------- */
.pin-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: var(--space-5);
  gap: var(--space-5);
}

.pin-error {
  color: var(--red);
  min-height: 20px;
  font-size: 14px;
}

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-4) var(--space-4) var(--space-2);
}

.topbar h1 { font-size: 22px; margin: 0; }

.icon-btn {
  width: var(--tap-min);
  height: var(--tap-min);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
}

/* ---------- Scene row ---------- */
.scene-row {
  display: flex;
  gap: var(--space-3);
  overflow-x: auto;
  padding: var(--space-2) var(--space-4) var(--space-4);
  -webkit-overflow-scrolling: touch;
}

.scene-pill {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: var(--space-2);
  background: var(--surface);
  border-radius: 999px;
  padding: var(--space-3) var(--space-4);
  min-height: var(--tap-min);
  font-size: 15px;
  white-space: nowrap;
}

.scene-pill:active { background: var(--surface-2); }

.scene-pill.add { color: var(--text-dim); }

/* ---------- Device grid ---------- */
.device-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: var(--space-3);
  padding: 0 var(--space-4);
}

.device-card.dragging {
  opacity: 0.6;
  box-shadow: 0 0 0 2px var(--accent);
  z-index: 10;
}

.drag-handle {
  position: absolute;
  bottom: var(--space-3);
  right: var(--space-3);
  width: var(--tap-min);
  height: var(--tap-min);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  color: var(--text-dim);
}

.device-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: var(--space-4);
  padding-bottom: calc(var(--space-3) * 2 + var(--tap-min));
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  min-height: 150px;
  position: relative;
}

.device-card.pending { opacity: 0.5; }

.device-card .icon { font-size: 28px; }

.device-card .name {
  font-size: 15px;
  font-weight: 600;
  padding-right: calc(var(--space-2) + var(--tap-min));
}

.device-card .status-row {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  margin-top: auto;
}

.status-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--grey);
}

.status-dot.on { background: var(--green); }
.status-dot.mixed { background: var(--accent); }
.status-dot.error { background: var(--red); }

.status-text { font-size: 13px; color: var(--text-dim); }

.toggle-btn {
  position: absolute;
  top: var(--space-3);
  right: var(--space-3);
  width: var(--tap-min);
  height: var(--tap-min);
  border-radius: 50%;
  background: var(--surface-2);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
}

.toggle-btn.on { background: var(--green); }

.details-btn {
  position: absolute;
  bottom: var(--space-3);
  right: var(--space-3);
  width: var(--tap-min);
  height: var(--tap-min);
  border-radius: 50%;
  background: var(--surface-2);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  color: var(--text-dim);
}

.pending-badge {
  position: absolute;
  top: var(--space-3);
  left: var(--space-3);
  font-size: 12px;
  background: var(--surface-2);
  border-radius: 999px;
  padding: 2px var(--space-2);
}

/* ---------- Wake time widget ---------- */
.wake-widget {
  margin: var(--space-4);
  background: var(--surface);
  border-radius: var(--radius);
  padding: var(--space-4);
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: var(--tap-min);
}

.wake-widget .label { color: var(--text-dim); font-size: 13px; }
.wake-widget .value { font-size: 20px; font-weight: 600; }

/* ---------- Bottom sheet ---------- */
.sheet-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: flex-end;
  z-index: 50;
}

.sheet {
  background: var(--surface);
  border-radius: var(--radius) var(--radius) 0 0;
  width: 100%;
  max-height: 85vh;
  overflow-y: auto;
  padding: var(--space-4);
  padding-bottom: calc(var(--space-6) + env(safe-area-inset-bottom));
}

.sheet-handle {
  width: 40px;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  margin: 0 auto var(--space-4);
}

.sheet h2 {
  margin: 0 0 var(--space-4);
  font-size: 20px;
}

.field {
  margin-bottom: var(--space-4);
}

.field label {
  display: block;
  font-size: 13px;
  color: var(--text-dim);
  margin-bottom: var(--space-2);
}

.field input, .field select {
  width: 100%;
  min-height: var(--tap-min);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  padding: var(--space-3);
  font-size: 16px;
}

.chip-row { display: flex; gap: var(--space-2); flex-wrap: wrap; }

.chip {
  min-width: var(--tap-min);
  min-height: var(--tap-min);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  padding: 0 var(--space-3);
}

.chip.selected { background: var(--accent); color: #fff; }

.btn {
  min-height: var(--tap-min);
  width: 100%;
  border-radius: var(--radius-sm);
  background: var(--accent);
  color: #fff;
  font-size: 16px;
  font-weight: 600;
}

.btn.secondary { background: var(--surface-2); color: var(--text); }
.btn.danger { background: var(--red); color: #fff; }

.list-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-3) 0;
  border-bottom: 1px solid var(--border);
  min-height: var(--tap-min);
}

/* A failed action's reason, under the log entry. Vendor errors are long
   (SwitchBot returns a whole JSON body), so let it wrap rather than truncate —
   the statusCode buried in it is the whole diagnostic value. */
.log-error {
  color: var(--red);
  font-size: 12px;
  line-height: 1.35;
  margin-top: 4px;
  overflow-wrap: anywhere;
}

.log-entry {
  align-items: flex-start;
  gap: var(--space-3);
}

.badge {
  font-size: 11px;
  color: var(--text-dim);
  background: var(--surface-2);
  border-radius: 999px;
  padding: 2px 8px;
  white-space: nowrap;
}

/* ---------- Settings ---------- */
.settings-section { padding: 0 var(--space-4); margin-bottom: var(--space-5); }
.settings-section h3 { font-size: 14px; color: var(--text-dim); text-transform: uppercase; }

/* ---------- Offline / install banners ---------- */
.banner {
  margin: var(--space-4);
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  padding: var(--space-3) var(--space-4);
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
}

.offline-overlay {
  text-align: center;
  padding: var(--space-6) var(--space-4);
  color: var(--text-dim);
}

/* ---------- Nav ---------- */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}

.bottom-nav button {
  flex: 1;
  min-height: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
}

.bottom-nav button.active { color: var(--accent); }
