: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;
  }
}
