* { box-sizing: border-box; }
:root {
  color-scheme: light;
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --text: #0f172a;
  --text-muted: #475569;
  --text-faint: #94a3b8;
  --primary: #3b6bff;
  --primary-hover: #1f40b8;
  --primary-soft: #e0e7ff;
  --accent: #16a34a;
  --accent-soft: #dcfce7;
  --warn: #d97706;
  --warn-soft: #fef3c7;
  --danger: #dc2626;
  --danger-hover: #991b1b;
  --danger-soft: #fee2e2;
  --shadow-sm: 0 1px 2px rgba(15,23,42,.05);
  --shadow-md: 0 4px 12px rgba(15,23,42,.08);
}

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  display: flex;
  align-items: stretch;
  justify-content: center;
  padding: 16px;
  padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 24px;
  width: 100%;
  max-width: 460px;
  box-shadow: var(--shadow-sm);
  align-self: center;
}
.card-wide { max-width: 1080px; }
.page-admin .card-wide { max-width: min(1600px, calc(100vw - 32px)); }

.brand h1 { margin: 0 0 6px; font-size: 22px; letter-spacing: -0.01em; }
.subtitle { color: var(--text-muted); font-size: 14px; margin: 0 0 20px; }

label {
  display: block;
  font-size: 13px;
  color: var(--text-muted);
  margin: 12px 0 6px;
  font-weight: 500;
}

input[type=tel],
input[type=text],
input[type=email],
input[type=password],
select {
  width: 100%;
  padding: 11px 12px;
  border-radius: 8px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  font-size: 15px;
  font-family: inherit;
  -webkit-appearance: none;
  appearance: none;
  transition: border-color .15s, box-shadow .15s;
}
input:focus, select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(59,107,255,.15);
}

button, .btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 100%;
  padding: 11px 16px;
  border-radius: 8px;
  border: 0;
  background: var(--primary);
  color: white;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  font-family: inherit;
  margin-top: 4px;
  min-height: 42px;
  transition: background .15s, opacity .15s;
}
button:hover { background: var(--primary-hover); }
button:active { transform: scale(.99); }
button:disabled { opacity: .55; cursor: not-allowed; }

button.secondary {
  background: var(--surface);
  border: 1px solid var(--border-strong);
  color: var(--text);
}
button.secondary:hover { background: var(--surface-2); }

button.danger { background: var(--danger); color: white; }
button.danger:hover { background: var(--danger-hover); }

button.ghost {
  background: transparent;
  color: var(--text-muted);
  padding: 8px 12px;
  min-height: 36px;
}
button.ghost:hover { background: var(--surface-2); color: var(--text); }

.error {
  color: var(--danger);
  font-size: 13px;
  min-height: 18px;
  margin: 12px 0 0;
}

.hint {
  font-size: 13px;
  color: var(--text-muted);
  line-height: 1.5;
  margin: 14px 0 0;
}
.hint ol { padding-left: 18px; margin: 6px 0; }
.hint strong { color: var(--text); }
.center { text-align: center; }
.muted-text { color: var(--text-muted); }

a { color: var(--primary); text-decoration: none; font-weight: 500; }
a:hover { text-decoration: underline; }

.row { display: flex; gap: 10px; align-items: center; justify-content: space-between; flex-wrap: wrap; }

.status {
  padding: 10px 12px;
  border-radius: 8px;
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 14px 0;
  font-size: 14px;
  border: 1px solid;
}
.status.connected { background: var(--accent-soft); color: var(--accent); border-color: rgba(22,163,74,.25); }
.status.disconnected { background: var(--danger-soft); color: var(--danger); border-color: rgba(220,38,38,.25); }
.status.connecting { background: var(--warn-soft); color: var(--warn); border-color: rgba(217,119,6,.25); }

.qr-wrap {
  background: white;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  display: flex;
  justify-content: center;
  margin: 14px 0;
}
.qr-wrap img { display: block; width: 100%; max-width: 280px; height: auto; }

.spinner {
  display: inline-block; width: 14px; height: 14px;
  border: 2px solid currentColor; border-right-color: transparent;
  border-radius: 50%; animation: spin .7s linear infinite;
  flex-shrink: 0;
}
@keyframes spin { to { transform: rotate(360deg); } }

.hidden { display: none !important; }

.badge {
  display: inline-flex; align-items: center;
  padding: 2px 10px; border-radius: 999px;
  font-size: 12px; font-weight: 500;
  border: 1px solid;
}
.badge.ok { background: var(--accent-soft); color: var(--accent); border-color: rgba(22,163,74,.25); }
.badge.pending { background: var(--warn-soft); color: var(--warn); border-color: rgba(217,119,6,.25); }
.badge.connected { background: var(--accent-soft); color: var(--accent); border-color: rgba(22,163,74,.25); }
.badge.disconnected { background: var(--danger-soft); color: var(--danger); border-color: rgba(220,38,38,.25); }

/* ============ Header (admin/painel) ============ */
.app-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 16px 24px;
  display: flex; align-items: center; justify-content: space-between;
  margin: -24px -24px 24px;
}
.app-title { font-size: 18px; font-weight: 600; margin: 0; }
.app-subtitle { font-size: 13px; color: var(--text-muted); margin-top: 2px; }
.app-header button { width: auto; min-height: 36px; padding: 8px 14px; font-size: 13px; margin: 0; }

.header { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; margin-bottom: 16px; }
.header h1 { font-size: 20px; margin: 0; font-weight: 600; }
.header .meta { font-size: 13px; color: var(--text-muted); margin-top: 4px; }
#logout { width: auto; padding: 8px 14px; font-size: 13px; min-height: 0; margin: 0; }

/* ============ Painel cliente: cards de instância ============ */
.page-painel, .page-admin { align-items: flex-start; }

.inst-card {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
  margin-top: 16px;
  background: var(--surface);
  box-shadow: var(--shadow-sm);
}
.inst-header h2 { font-size: 15px; margin: 0 0 4px; font-weight: 600; }
.inst-meta { font-size: 12px; color: var(--text-muted); }
.inst-actions { margin-top: 12px; display: flex; flex-direction: column; gap: 8px; }
.inst-status { margin-top: 12px; }
.qr-section { margin-top: 12px; }

/* ============ Tabela admin ============ */
.toolbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; margin-bottom: 12px; flex-wrap: wrap;
}
.toolbar h2 { margin: 0; font-size: 16px; font-weight: 600; }
.toolbar-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.toolbar button { width: auto; min-height: 38px; padding: 9px 14px; font-size: 13px; margin: 0; }

.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  box-shadow: var(--shadow-sm);
}
table.tbl {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}
table.tbl thead th {
  text-align: left;
  padding: 12px 14px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
  color: var(--text-muted);
  font-weight: 500;
  font-size: 13px;
  white-space: nowrap;
}
table.tbl tbody td {
  padding: 12px 12px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
table.tbl tbody td:first-child { padding-left: 16px; }
table.tbl tbody td:last-child { padding-right: 16px; }
@media (min-width: 1280px) {
  table.tbl tbody td, table.tbl thead th { white-space: nowrap; }
  table.tbl td .inst-list li, table.tbl td .token-cell .token-pill { white-space: nowrap; }
}
table.tbl tbody tr:last-child td { border-bottom: 0; }
table.tbl tbody tr:hover { background: var(--surface-2); }

.cell-name { font-weight: 600; color: var(--text); }
.cell-meta { font-size: 12px; color: var(--text-muted); margin-top: 2px; word-break: break-word; }
.cell-actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.cell-actions button { width: auto; min-height: 32px; padding: 6px 10px; font-size: 12px; margin: 0; white-space: nowrap; }

.inst-list { display: flex; flex-direction: column; gap: 4px; margin: 0; padding: 0; list-style: none; }
.inst-list li { font-size: 13px; }
.inst-list .inst-name-mono { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 12px; color: var(--text); }

.token-cell { display: flex; flex-direction: column; gap: 4px; }
.token-pill {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 11px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  padding: 3px 8px;
  border-radius: 6px;
  cursor: pointer;
  user-select: all;
  display: inline-block;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  transition: background .1s, color .1s;
}
.token-pill:hover { background: var(--primary-soft); color: var(--primary); }
.token-pill.copied { background: var(--accent-soft); color: var(--accent); border-color: rgba(22,163,74,.25); }

.empty-state {
  padding: 48px 16px;
  text-align: center;
  color: var(--text-muted);
}
.empty-state h3 { margin: 0 0 6px; color: var(--text); font-weight: 600; }

/* ============ Modal ============ */
.modal {
  position: fixed; inset: 0; background: rgba(15,23,42,.5); backdrop-filter: blur(4px);
  display: flex; align-items: flex-start; justify-content: center;
  padding: 16px; padding-top: 5vh; z-index: 100; overflow-y: auto;
}
.modal.hidden { display: none; }
.modal-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; padding: 24px; width: 100%; max-width: 540px;
  box-shadow: var(--shadow-md);
  margin-bottom: 16px;
}
.modal-card h2 { margin: 0 0 6px; font-size: 18px; font-weight: 600; }
.modal-card h3 { margin: 20px 0 8px; font-size: 14px; font-weight: 600; color: var(--text); }
.modal-card .meta { font-size: 13px; color: var(--text-muted); margin: 0; }

#link-output {
  margin-top: 12px;
  padding: 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
}
#link-value {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 12px;
  background: var(--surface);
}

/* ============ Toast ============ */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: white;
  padding: 10px 18px; border-radius: 999px;
  font-size: 13px; font-weight: 500;
  box-shadow: var(--shadow-md);
  opacity: 0; transition: opacity .2s, transform .2s;
  pointer-events: none; z-index: 200;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(-4px); }

/* ============ Mobile ============ */
@media (max-width: 640px) {
  .card { padding: 20px 16px; border-radius: 10px; }
  .app-header { padding: 14px 16px; margin: -20px -16px 20px; }
  table.tbl tbody td, table.tbl thead th { padding: 10px 10px; font-size: 13px; }
  .cell-actions button { font-size: 11px; padding: 6px 8px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    transition-duration: .01ms !important;
  }
}
