:root {
  color-scheme: light dark;
  --bg: #f3f6f3;
  --surface: #ffffff;
  --surface-2: #eef2ee;
  --fg: #141a16;
  --fg-muted: #5b655e;
  --primary: #1b6b30;
  --primary-hover: #155526;
  --primary-fg: #ffffff;
  --danger: #b91c1c;
  --danger-hover: #991b1b;
  --border: #e1e6e2;
  --shadow-sm: 0 1px 2px rgba(20, 26, 22, 0.06);
  --shadow-md: 0 6px 20px rgba(20, 26, 22, 0.08), 0 1px 3px rgba(20, 26, 22, 0.06);
  --focus-ring: 0 0 0 3px rgba(27, 107, 48, 0.25);
  --radius-lg: 18px;
  --radius-md: 14px;
  --radius-sm: 10px;
  --radius-pill: 999px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0c0f0d;
    --surface: #161a17;
    --surface-2: #1f2420;
    --fg: #eef2ef;
    --fg-muted: #9aa39c;
    --primary: #2e9e52;
    --primary-hover: #279046;
    --danger: #dc2626;
    --danger-hover: #c0221f;
    --border: #2b322d;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 10px 28px rgba(0, 0, 0, 0.4), 0 1px 3px rgba(0, 0, 0, 0.3);
    --focus-ring: 0 0 0 3px rgba(46, 158, 82, 0.35);
  }
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--fg);
  font-size: 18px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

h1,
h2,
h3 {
  letter-spacing: -0.01em;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.9rem 1.25rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 800;
  font-size: 1.2rem;
  color: var(--fg);
  text-decoration: none;
  letter-spacing: -0.02em;
}

.brand-mark {
  width: 26px;
  height: 26px;
  flex-shrink: 0;
}

.page,
.auth-page {
  max-width: 480px;
  margin: 0 auto;
  padding: 1.5rem 1.1rem 3rem;
}

.auth-page {
  min-height: calc(100dvh - 2rem);
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 0.25rem;
}

.auth-brand {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.6rem;
  margin-bottom: 1.5rem;
}

.auth-brand .brand-mark {
  width: 48px;
  height: 48px;
}

.auth-brand h1 {
  margin: 0;
  font-size: 1.6rem;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  padding: 1.35rem;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}

.stack {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.card-title {
  margin: 0;
  font-size: 1.1rem;
}

label {
  font-weight: 600;
  font-size: 0.92rem;
}

input {
  font-size: 1.1rem;
  padding: 0.75rem 0.85rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--fg);
  width: 100%;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: var(--focus-ring);
  background: var(--surface);
}

button {
  font-size: 1.1rem;
  font-weight: 700;
  padding: 0.9rem;
  border-radius: var(--radius-md);
  border: none;
  background: var(--primary);
  color: var(--primary-fg);
  cursor: pointer;
  min-height: 48px;
  box-shadow: var(--shadow-sm);
  transition: background-color 0.15s ease, transform 0.05s ease, opacity 0.15s ease;
}

button:hover {
  background: var(--primary-hover);
}

button:active {
  transform: scale(0.98);
}

button:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
}

.link-btn {
  display: inline-flex;
  align-items: center;
  background: var(--surface);
  color: var(--fg);
  font-weight: 600;
  min-height: 44px;
  padding: 0.5rem 0.9rem;
  text-decoration: none;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-sm);
}

.link-btn:hover {
  border-color: var(--primary);
  color: var(--primary);
}

.btn-outline {
  background: transparent;
  color: var(--fg);
  border: 1px solid var(--border);
  box-shadow: none;
}

.btn-outline:hover {
  background: var(--surface-2);
  border-color: var(--fg-muted);
}

.error {
  color: var(--danger);
  font-weight: 600;
  margin: 0;
}

.release-result {
  background: var(--surface-2);
  border-radius: var(--radius-md);
  padding: 0.9rem;
}

.pairing-code {
  font-family: ui-monospace, 'SFMono-Regular', Menlo, Consolas, monospace;
  font-size: 1.4rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  margin: 0.25rem 0 0;
}

.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 1.5rem;
}

.page-header h1 {
  margin: 0;
  font-size: 1.5rem;
}

.empty-state {
  text-align: center;
  color: var(--fg-muted);
  padding: 2.5rem 1rem;
}

.device-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 1.1rem;
  align-items: start;
}

.device-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  padding: 1.1rem;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}

.device-card-header {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.device-card-title {
  flex: 1;
  min-width: 0;
}

/* Um card = uma placa fisica (device_uid); .device-channels segura um bloco
   por rele/canal, lado a lado quando o card tem espaco (ver .device-list). */
.device-channels {
  display: flex;
  flex-wrap: wrap;
  gap: 0.85rem;
}

.device-channel {
  /* min-width alto de proposito: no celular (card estreito) isso forca o
     flex-wrap do .device-channels a empilhar um canal por linha, com
     espaco de sobra pro seletor de duracao — lado a lado so quando o card
     realmente tem espaco (tablet/desktop). */
  flex: 1 1 200px;
  min-width: 200px;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.device-channel-label {
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--fg-muted);
  text-decoration: none;
}

.device-channel-label:hover {
  color: var(--primary);
}

.device-remove-btn {
  flex-shrink: 0;
  min-height: 38px;
  padding: 0 0.75rem;
  font-size: 0.82rem;
  font-weight: 600;
  background: transparent;
  color: var(--danger);
  border: 1px solid var(--border);
  box-shadow: none;
}

.device-remove-btn:hover {
  background: var(--surface-2);
  border-color: var(--danger);
}

.dot {
  width: 11px;
  height: 11px;
  border-radius: 50%;
  flex-shrink: 0;
}

.dot-online {
  background: #2e9e52;
  box-shadow: 0 0 0 3px rgba(46, 158, 82, 0.18);
}

.dot-offline {
  background: #9aa39c;
}

.device-name {
  display: inline-block;
  font-weight: 700;
  font-size: 1.08rem;
  color: var(--fg);
  text-decoration: none;
}

.device-name:hover {
  color: var(--primary);
}

.device-location {
  font-size: 0.88rem;
  color: var(--fg-muted);
}

.device-meta {
  font-size: 0.85rem;
  color: var(--fg-muted);
  margin: 0;
}

.device-btn {
  min-height: 56px;
  font-size: 1.15rem;
  width: 100%;
}

.device-btn-open {
  background: var(--primary);
}

.device-btn-open:hover {
  background: var(--primary-hover);
}

.device-btn-close {
  background: var(--danger);
}

.device-btn-close:hover {
  background: var(--danger-hover);
}

.device-btn[disabled] {
  background: var(--surface-2);
  color: var(--fg-muted);
  cursor: not-allowed;
  box-shadow: none;
}

.device-countdown {
  text-align: center;
  font-size: 1.6rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.02em;
  margin: 0;
}

.duration-picker {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.duration-picker label {
  font-weight: 600;
  font-size: 0.95rem;
  white-space: nowrap;
}

.duration-input {
  width: 5.5rem;
  text-align: center;
  padding: 0.6rem;
}

.status-badge {
  display: inline-block;
  font-size: 0.72rem;
  font-weight: 700;
  padding: 0.22rem 0.6rem;
  border-radius: var(--radius-pill);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  background: var(--surface-2);
  color: var(--fg-muted);
}

.status-acked {
  background: rgba(46, 158, 82, 0.15);
  color: #1b6b30;
}

.status-expired,
.status-failed {
  background: rgba(185, 28, 28, 0.12);
  color: var(--danger);
}

.status-sent,
.status-queued {
  background: var(--surface-2);
  color: var(--fg-muted);
}

@media (prefers-color-scheme: dark) {
  .status-acked {
    color: #4ade80;
  }
}

.history-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}

.history-item {
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.85rem;
}

.history-item:last-child {
  border-bottom: none;
  padding-bottom: 0;
}

.history-main {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.history-meta {
  font-size: 0.85rem;
  color: var(--fg-muted);
  margin-top: 0.2rem;
}

.success {
  color: var(--primary);
  font-weight: 600;
  margin: 0;
}
