:root {
  --bg: #0b0f1a;
  --panel: #121a2b;
  --panel-border: #22304d;
  --text: #e8edf7;
  --muted: #8b9bb8;
  --accent: #00e5a0;
  --accent-2: #00b3ff;
  --danger: #ff5c7a;
  --warn: #ffb020;
}

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

html,
body {
  height: 100%;
}

body {
  background: radial-gradient(1200px 700px at 80% -10%, #16233f 0%, var(--bg) 55%);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}

/* ------------------------------------------------------------------ Header */
header {
  display: flex;
  align-items: baseline;
  gap: 12px;
  padding: 14px 20px;
  flex-wrap: wrap;
}

header h1 {
  font-size: 1.25rem;
  letter-spacing: 0.02em;
}

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

.badge {
  margin-left: auto;
  font-size: 0.78rem;
  font-weight: 600;
  padding: 4px 12px;
  border-radius: 999px;
  border: 1px solid var(--panel-border);
}

.badge-wait { color: var(--warn); border-color: rgba(255, 176, 32, 0.4); }
.badge-ok { color: var(--accent); border-color: rgba(0, 229, 160, 0.4); }
.badge-error { color: var(--danger); border-color: rgba(255, 92, 122, 0.4); }

.header-right {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 10px;
}

.header-right .badge {
  margin-left: 0;
}

.lang-switch {
  display: flex;
  border: 1px solid var(--panel-border);
  border-radius: 999px;
  overflow: hidden;
  background: #0d1524;
}

.lang-btn {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 0.75rem;
  font-weight: 700;
  padding: 4px 10px;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}

.lang-btn + .lang-btn {
  border-left: 1px solid var(--panel-border);
}

.lang-btn.active {
  background: var(--accent);
  color: #052019;
}

/* ------------------------------------------------------------------- Main */
main {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 320px;
  gap: 16px;
  padding: 0 20px 20px;
  min-height: 0;
}

.stage-wrap {
  position: relative;
  min-width: 0;
  min-height: 0;
}

.stage {
  position: relative;
  width: 100%;
  height: 100%;
  min-height: 320px;
  background: #05070d;
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  overflow: hidden;
}

.stage.mirrored {
  transform: scaleX(-1);
}

video,
canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

canvas {
  z-index: 2;
}

.idle-hint {
  position: absolute;
  inset: 0;
  z-index: 3;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  text-align: center;
  color: var(--muted);
  background: rgba(5, 7, 13, 0.55);
  padding: 24px;
}

.idle-icon {
  font-size: 2.6rem;
  filter: grayscale(0.3);
}

/* ------------------------------------------------------------------- Panel */
.panel {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  overflow-y: auto;
}

.controls {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.btn {
  appearance: none;
  border: 1px solid var(--panel-border);
  background: #1a2740;
  color: var(--text);
  font: inherit;
  font-size: 0.95rem;
  font-weight: 600;
  padding: 12px 16px;
  border-radius: 10px;
  cursor: pointer;
  transition: filter 0.15s ease, transform 0.05s ease;
}

.btn:hover:not(:disabled) { filter: brightness(1.15); }
.btn:active:not(:disabled) { transform: scale(0.98); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }

.btn-primary {
  background: linear-gradient(135deg, #00b386, #00e5a0);
  color: #052019;
  border: none;
}

.btn-stop {
  background: linear-gradient(135deg, #d63d5c, #ff5c7a);
  color: #2b050d;
}

.toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.9rem;
  color: var(--muted);
  cursor: pointer;
  user-select: none;
  padding: 2px 0;
}

.toggle input {
  width: 16px;
  height: 16px;
  accent-color: var(--accent);
  cursor: pointer;
}

.metrics {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.metric-row {
  background: #0d1524;
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  font-size: 0.78rem;
  color: var(--muted);
}

.metric-value {
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--text);
}

.panel h2 {
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.angles-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.angle-cell {
  background: #0d1524;
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  padding: 8px 10px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.angle-name {
  font-size: 0.72rem;
  color: var(--muted);
}

.angle-value {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--accent-2);
  font-variant-numeric: tabular-nums;
}

.error {
  color: var(--danger);
  background: rgba(255, 92, 122, 0.08);
  border: 1px solid rgba(255, 92, 122, 0.35);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 0.85rem;
}

.footnote {
  font-size: 0.72rem;
  color: var(--muted);
  line-height: 1.45;
}

.hidden {
  display: none !important;
}

/* ----------------------------------------------------------------- Loading */
.loading {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(5, 7, 13, 0.88);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  color: var(--muted);
}

.spinner {
  width: 46px;
  height: 46px;
  border: 4px solid var(--panel-border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}

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

/* ------------------------------------------------------------------ Mobile */
@media (max-width: 860px) {
  main {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
  }

  .stage {
    min-height: 46dvh;
  }

  .panel {
    order: 2;
  }
}

/* ----------------------------------------------------- Painel de exercício */
.exercise-panel {
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: #0d1524;
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  padding: 12px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.field label {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.field select,
.field input {
  appearance: none;
  background: #1a2740;
  border: 1px solid var(--panel-border);
  color: var(--text);
  font: inherit;
  font-size: 0.95rem;
  padding: 8px 10px;
  border-radius: 8px;
  width: 100%;
}

.field input[type="number"] {
  font-variant-numeric: tabular-nums;
}

.field-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

/* ---------------------------------------------- Overlay de reps no vídeo */
.rep-overlay {
  position: absolute;
  left: 16px;
  bottom: 16px;
  z-index: 4;
  display: flex;
  align-items: flex-end;
  gap: 14px;
  pointer-events: none;
}

.rep-count {
  display: flex;
  align-items: baseline;
  background: rgba(8, 12, 20, 0.78);
  border: 1px solid rgba(0, 229, 160, 0.45);
  border-radius: 14px;
  padding: 8px 18px;
  font-variant-numeric: tabular-nums;
}

.rep-count #rep-done {
  font-size: 2.4rem;
  font-weight: 800;
  color: var(--accent);
}

.rep-count .rep-total {
  font-size: 1.2rem;
  color: var(--muted);
  margin-left: 6px;
}

.rep-count.complete {
  border-color: var(--accent);
  animation: pulse 1s ease-in-out infinite;
}

.rep-feedback {
  background: rgba(8, 12, 20, 0.82);
  border-radius: 12px;
  padding: 10px 16px;
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--accent);
  opacity: 0;
  transition: opacity 0.25s ease;
  max-width: 240px;
}

.rep-feedback.show {
  opacity: 1;
}

.rep-feedback.warn {
  color: var(--warn);
  border: 1px solid rgba(255, 176, 32, 0.5);
}

.rep-feedback.complete {
  color: var(--accent);
  border: 1px solid var(--accent);
}

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(0, 229, 160, 0.35); }
  50% { box-shadow: 0 0 0 10px rgba(0, 229, 160, 0); }
}

/* ------------------------------------------------------ Gauge de amplitude */
.gauge {
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(8, 12, 20, 0.78);
  border-radius: 12px;
  padding: 10px 12px;
}

.gauge-bar {
  position: relative;
  width: 18px;
  height: 150px;
  background: #1a2740;
  border-radius: 9px;
  overflow: hidden;
}

.gauge-zone {
  position: absolute;
  left: 2px;
  right: 2px;
  background: rgba(0, 229, 160, 0.55);
  border-radius: 7px;
  transition: bottom 0.2s ease, height 0.2s ease;
}

.gauge-marker {
  position: absolute;
  left: -5px;
  right: -5px;
  height: 5px;
  background: #ffffff;
  border-radius: 3px;
  box-shadow: 0 0 6px rgba(255, 255, 255, 0.9);
  transition: bottom 0.12s linear;
}

.gauge-labels {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  height: 150px;
  font-size: 0.72rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

#gauge-value {
  color: var(--text);
  font-weight: 700;
}

/* Mobile: empilhar contador/feedback/gauge para não transbordar */
@media (max-width: 860px) {
  .rep-overlay {
    left: 10px;
    bottom: 10px;
    gap: 10px;
  }

  .rep-count #rep-done {
    font-size: 1.8rem;
  }

  .rep-feedback {
    position: absolute;
    bottom: 100%;
    left: 0;
    margin-bottom: 10px;
    font-size: 0.95rem;
  }

  .gauge-bar,
  .gauge-labels {
    height: 110px;
  }
}

/* ------------------------------------------------------ Modal resumo/dor */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(5, 7, 13, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.modal {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  padding: 22px;
  width: 100%;
  max-width: 440px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.modal h2 {
  font-size: 1.15rem;
}

.modal-sub {
  color: var(--muted);
  font-size: 0.85rem;
  line-height: 1.45;
}

.pain-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.pain-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  background: #0d1524;
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  padding: 12px;
  font-size: 0.85rem;
  color: var(--muted);
}

.pain-field input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

.pain-field output {
  font-size: 1.4rem;
  font-weight: 800;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}

.field-label {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.field-hint {
  margin: -0.25rem 0 0.75rem;
  font-size: 0.78rem;
  line-height: 1.35;
  color: var(--muted);
}

.modal textarea {
  width: 100%;
  background: #0d1524;
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  color: var(--text);
  font: inherit;
  font-size: 0.95rem;
  padding: 8px 10px;
  resize: vertical;
}

.modal-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
}

/* ----------------------------------------------------- Navegação e contas */
.nav-links {
  display: flex;
  gap: 4px;
}

.nav-links a {
  color: var(--muted);
  text-decoration: none;
  font-size: 0.85rem;
  font-weight: 600;
  padding: 6px 10px;
  border-radius: 8px;
  transition: background 0.15s ease, color 0.15s ease;
}

.nav-links a:hover {
  background: #16233f;
  color: var(--text);
}

.user-area {
  display: flex;
  align-items: center;
  gap: 8px;
}

.user-name {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--accent);
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-decoration: none;
  border-bottom: 1px dotted currentColor;
}

.user-name:hover,
.user-name:focus-visible {
  border-bottom-style: solid;
}

.btn-small {
  font-size: 0.8rem;
  padding: 6px 12px;
}

.view {
  flex: 1;
  display: flex;
  min-height: 0;
  padding: 0 20px 20px;
}

.view.hidden {
  display: none;
}

.page-wrap {
  width: 100%;
  max-width: 900px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.page-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.page-head h2 {
  font-size: 1.3rem;
}

.page-actions {
  display: flex;
  gap: 10px;
}

.hint-line {
  color: var(--muted);
  font-size: 0.9rem;
}

.section-title {
  font-size: 1rem;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--muted);
}

/* ---------------------------------------------------------- Form auth */
.auth-card {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  padding: 24px;
  width: 100%;
  max-width: 420px;
  margin: 4vh auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-self: flex-start;
}

.auth-card h2,
.auth-card h3 {
  font-size: 1.2rem;
}

/* A conta é um formulário estreito: encolhe o wrap para o título alinhar
   com os cartões, e tira-lhes a margem própria (o gap do wrap chega). */
#view-account .page-wrap {
  max-width: 420px;
}

#view-account .auth-card {
  margin: 0;
}

.auth-card input[type="text"],
.auth-card input[type="password"] {
  width: 100%;
  background: #0d1524;
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  color: var(--text);
  font: inherit;
  font-size: 0.95rem;
  padding: 9px 12px;
}

.role-toggle {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.role-opt {
  display: flex;
  align-items: center;
  gap: 8px;
  background: #0d1524;
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 0.88rem;
  cursor: pointer;
}

.role-opt:has(input:checked) {
  border-color: var(--accent);
}

.form-switch {
  font-size: 0.85rem;
  color: var(--muted);
  text-align: center;
}

.form-switch a {
  color: var(--accent);
  margin-left: 6px;
}

/* ------------------------------------------------------- Histórico */
.history-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.session-card {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
}

.session-card summary {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  cursor: pointer;
  flex-wrap: wrap;
  font-size: 0.9rem;
}

.session-card .s-date {
  font-weight: 700;
}

.session-card .s-ex {
  color: var(--accent-2);
  font-weight: 600;
}

.session-card .s-pain {
  color: var(--muted);
}

.session-card .s-quality.ok {
  color: var(--accent);
}

.session-card .s-quality.warn {
  color: var(--warn);
}

.session-card .s-pending {
  font-size: 0.75rem;
  color: var(--warn);
  border: 1px solid rgba(255, 176, 32, 0.5);
  border-radius: 999px;
  padding: 2px 8px;
}

.session-card .s-body {
  padding: 4px 14px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.session-card .s-set {
  font-size: 0.85rem;
  color: var(--muted);
}

.session-card .s-reps-list {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 4px;
}

.rep-chip {
  font-size: 0.75rem;
  border-radius: 6px;
  padding: 2px 6px;
  background: #1a2740;
  font-variant-numeric: tabular-nums;
}

.rep-chip.good {
  color: var(--accent);
}

.rep-chip.shallow {
  color: var(--warn);
}

.rep-chip.deep {
  color: var(--danger);
}

.session-card .s-notes {
  font-size: 0.85rem;
  color: var(--text);
}

/* ------------------------------------------------------ Dashboard */
.invites-list {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.invite-row {
  display: flex;
  align-items: center;
  gap: 10px;
  background: #0d1524;
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 0.85rem;
}

.invite-code {
  font-family: ui-monospace, monospace;
  font-size: 1rem;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: 0.1em;
}

.invite-used {
  color: var(--muted);
}

.invite-pending {
  color: var(--warn);
}

.patients-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.patient-card {
  appearance: none;
  text-align: left;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: 12px 14px;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  cursor: pointer;
  font: inherit;
  color: var(--text);
  transition: border-color 0.15s ease;
}

.patient-card:hover {
  border-color: var(--accent);
}

.patient-card .p-name {
  font-weight: 700;
  font-size: 1rem;
}

.patient-card .p-meta,
.patient-card .p-last {
  color: var(--muted);
  font-size: 0.82rem;
}

.alerts {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.alert {
  font-size: 0.85rem;
  border-radius: 10px;
  padding: 8px 12px;
  border: 1px solid;
}

.alert-pain_spike {
  color: var(--danger);
  border-color: rgba(255, 92, 122, 0.4);
  background: rgba(255, 92, 122, 0.07);
}

.alert-inactive {
  color: var(--warn);
  border-color: rgba(255, 176, 32, 0.4);
  background: rgba(255, 176, 32, 0.07);
}

.alert-quality {
  color: var(--accent-2);
  border-color: rgba(0, 179, 255, 0.4);
  background: rgba(0, 179, 255, 0.07);
}

.charts-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 12px;
}

.chart-card {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: 14px;
}

.chart-card .chart-title {
  font-size: 0.95rem;
  margin-bottom: 8px;
}

.chart-card svg {
  display: block;
  background: #0d1524;
  border-radius: 8px;
}

.chart-note {
  font-size: 0.75rem;
  color: var(--muted);
  margin-top: 6px;
}
