:root {
  --bg: #f4f6fb;
  --card: #ffffff;
  --text: #1c2330;
  --muted: #667085;
  --line: #e3e7ef;
  --input: #f8fafc;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --accent-soft: #e0ecff;
  --danger: #ef4444;
  --ok: #22c55e;
  --speak: #3b82f6;
  --shadow: 0 6px 24px rgba(20, 30, 60, .08);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1318;
    --card: #1a2028;
    --text: #eef1f5;
    --muted: #9aa4b2;
    --line: #2a323d;
    --input: #121820;
    --accent-soft: #1d2b44;
    --shadow: 0 6px 24px rgba(0, 0, 0, .35);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
}

[hidden] { display: none !important; }

h1 { font-size: 26px; margin: 8px 0; word-break: break-word; }
h2 { font-size: 20px; margin: 24px 0 12px; }
h3 { margin: 0 0 8px; }
.hint { color: var(--muted); font-size: 14px; font-weight: normal; }
.center-text { text-align: center; }
.error { color: var(--danger); margin: 0; }

/* ---------- Skärmar ---------- */
.screen { position: fixed; inset: 0; }
.center { display: flex; align-items: center; justify-content: center; }
.page {
  overflow-y: auto;
  padding: env(safe-area-inset-top) 0 calc(24px + env(safe-area-inset-bottom));
}
.narrow { max-width: 520px; margin: 0 auto; padding: 0 16px; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  max-width: 520px;
  margin: 0 auto;
  padding: 12px 16px 0;
  min-height: 56px;
}

.brand { display: flex; align-items: center; gap: 12px; justify-content: center; margin: 40px 0 24px; }
.brand-icon { width: 48px; height: 48px; border-radius: 12px; }
.brand.small { margin: 0; gap: 10px; font-weight: 700; font-size: 18px; }
.brand.small .brand-icon { width: 32px; height: 32px; border-radius: 8px; }

.card {
  background: var(--card);
  border-radius: 18px;
  padding: 20px;
  box-shadow: var(--shadow);
  margin-bottom: 16px;
}

.spinner {
  width: 40px;
  height: 40px;
  border: 4px solid var(--accent-soft);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Knappar & formulär ---------- */
.btn {
  border: 0;
  border-radius: 12px;
  padding: 12px 16px;
  font-size: 16px;
  cursor: pointer;
  background: var(--line);
  color: var(--text);
  font-family: inherit;
  text-decoration: none;
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}
.btn:hover { filter: brightness(.96); }
.btn.primary { background: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-hover); }
.btn.ghost { background: transparent; color: var(--accent); }
.btn.danger-outline { background: transparent; color: var(--danger); border: 2px solid var(--danger); }
.btn.big { width: 100%; padding: 16px; font-size: 18px; font-weight: 600; margin-top: 12px; }
.btn.full { width: 100%; margin-top: 8px; }
.btn:disabled { opacity: .55; cursor: default; }

.form { display: flex; flex-direction: column; gap: 14px; }
.form label { display: flex; flex-direction: column; gap: 6px; font-weight: 600; }
input, select {
  font: inherit;
  font-weight: normal;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1.5px solid var(--line);
  background: var(--input);
  color: var(--text);
  width: 100%;
  min-width: 0;
}
input:focus, select:focus { outline: none; border-color: var(--accent); }

.tabs { display: flex; background: var(--line); border-radius: 14px; padding: 4px; margin-bottom: 16px; }
.tab {
  flex: 1;
  border: 0;
  background: transparent;
  padding: 10px;
  border-radius: 10px;
  font: inherit;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
}
.tab.active { background: var(--card); color: var(--text); box-shadow: 0 1px 4px rgba(0, 0, 0, .1); }

/* ---------- Avatarer ---------- */
.avatar {
  border-radius: 50%;
  overflow: hidden;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-weight: 700;
  flex-shrink: 0;
  user-select: none;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }

.me-btn { border: 0; background: none; padding: 0; cursor: pointer; border-radius: 50%; }
.profile-avatar { display: flex; justify-content: center; margin-bottom: 8px; }
.row-center { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.file-btn { position: relative; overflow: hidden; }
.file-btn input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }

/* ---------- Rum ---------- */
.room {
  display: flex;
  align-items: center;
  gap: 14px;
  background: var(--card);
  border-radius: 18px;
  padding: 16px;
  box-shadow: var(--shadow);
  margin-bottom: 12px;
}
.room.live { box-shadow: 0 0 0 2px var(--ok), var(--shadow); }
.room-info { flex: 1; min-width: 0; }
.room-name { font-weight: 700; font-size: 18px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.room-who { color: var(--muted); font-size: 14px; margin-top: 4px; display: flex; align-items: center; gap: 8px; }
.room-who .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); flex-shrink: 0; }
.stack { display: flex; }
.stack .avatar { border: 2px solid var(--card); margin-left: -8px; }
.stack .avatar:first-child { margin-left: 0; }
.room .btn { flex-shrink: 0; }
.room .del { padding: 8px 10px; background: transparent; color: var(--muted); }

.new-room { display: flex; gap: 8px; padding: 12px; }

.install { display: flex; gap: 12px; align-items: center; background: var(--accent-soft); box-shadow: none; }
.install-text { flex: 1; display: flex; flex-direction: column; gap: 4px; font-size: 14px; }
.install-actions { display: flex; gap: 4px; align-items: center; }

/* ---------- Före samtalet ---------- */
.preview {
  position: relative;
  aspect-ratio: 4 / 3;
  background: #1b2027;
  border-radius: 18px;
  overflow: hidden;
  margin: 12px 0;
}
.preview video { width: 100%; height: 100%; object-fit: cover; transform: scaleX(-1); display: block; }
.pre-avatar { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; background: #1b2027; }
.pre-controls { position: absolute; left: 0; right: 0; bottom: 12px; display: flex; justify-content: center; gap: 14px; }

.meter-row { display: flex; align-items: center; gap: 10px; }
.meter { flex: 1; height: 10px; background: var(--line); border-radius: 6px; overflow: hidden; }
.meter div { height: 100%; width: 0; background: var(--ok); transition: width .08s linear; }

.devices { display: flex; flex-direction: column; gap: 10px; margin: 12px 0; }
.devices label { display: flex; flex-direction: column; gap: 4px; font-size: 14px; font-weight: 600; }

.error-box {
  background: #fdecec;
  color: #8a1c1c;
  border-radius: 14px;
  padding: 14px;
  margin: 12px 0;
}
.error-box p { margin: 0 0 10px; line-height: 1.45; }
@media (prefers-color-scheme: dark) { .error-box { background: #3a1717; color: #fecaca; } }

.help { margin-top: 20px; background: var(--card); border-radius: 14px; padding: 14px 16px; }
.help summary { cursor: pointer; font-weight: 600; }
.help li { margin: 8px 0; line-height: 1.45; }

/* ---------- Samtalsvyn ---------- */
#call { background: #0b0d10; color: #fff; }

#stage {
  position: absolute;
  left: 0;
  right: 0;
  top: env(safe-area-inset-top);
  bottom: calc(84px + env(safe-area-inset-bottom));
}

.tile {
  position: absolute;
  background: #222831;
  border-radius: 14px;
  overflow: hidden;
  transition: left .35s ease, top .35s ease, width .35s ease, height .35s ease, box-shadow .15s;
}
.tile video { width: 100%; height: 100%; object-fit: cover; display: block; background: #000; }
.tile.local:not(.screen) video { transform: scaleX(-1); }
.tile.local.rear video { transform: none; }
.tile.screen video { object-fit: contain; }
.tile.main.full { border-radius: 0; }
.tile.main.full video, .tile.fit video { object-fit: contain; }

/* Profilbild när kameran är av */
.tile .face {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #222831;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s;
}
.tile.cam-off .face { opacity: 1; }
.tile .face .avatar { transition: box-shadow .15s; }

/* Den blå ringen när någon pratar */
.tile.speaking { box-shadow: inset 0 0 0 4px var(--speak), 0 0 18px rgba(59, 130, 246, .55); }
.tile.speaking .face .avatar { box-shadow: 0 0 0 5px var(--speak), 0 0 24px rgba(59, 130, 246, .8); }

.tile .label {
  position: absolute;
  left: 8px;
  bottom: 8px;
  max-width: calc(100% - 16px);
  background: rgba(0, 0, 0, .55);
  padding: 3px 8px;
  border-radius: 8px;
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tile.thumb .label { font-size: 11px; left: 4px; bottom: 4px; padding: 2px 6px; }
.tile .muted-icon { margin-right: 4px; }
.tile .conn {
  position: absolute;
  top: 8px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0, 0, 0, .6);
  padding: 3px 10px;
  border-radius: 8px;
  font-size: 12px;
}

.waiting {
  position: absolute;
  left: 50%;
  top: 40%;
  transform: translate(-50%, -50%);
  text-align: center;
  background: rgba(0, 0, 0, .65);
  padding: 20px 24px;
  border-radius: 16px;
  z-index: 5;
  width: min(90vw, 360px);
}
.waiting p { margin: 0 0 12px; }

.controls {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: calc(84px + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  display: flex;
  gap: 12px;
  justify-content: center;
  align-items: center;
  background: #0b0d10;
  z-index: 6;
}
.ctl {
  position: relative;
  width: 54px;
  height: 54px;
  border-radius: 50%;
  border: 0;
  font-size: 22px;
  background: #2d3440;
  color: #fff;
  cursor: pointer;
  flex-shrink: 0;
}
.ctl:hover { background: #3a4351; }
.ctl.on { background: var(--accent); }
.ctl.off { background: #f3f4f6; }
.ctl.off::after {
  content: "";
  position: absolute;
  left: 12px;
  right: 12px;
  top: 50%;
  height: 3px;
  background: var(--danger);
  transform: rotate(-45deg);
}
.ctl.leave { background: var(--danger); }
.ctl.leave:hover { background: #dc2626; }
@media (max-width: 380px) {
  .controls { gap: 8px; }
  .ctl { width: 48px; height: 48px; font-size: 20px; }
}

.badge {
  position: absolute;
  top: -2px;
  right: -2px;
  min-width: 20px;
  height: 20px;
  padding: 0 5px;
  border-radius: 10px;
  background: var(--danger);
  font-size: 12px;
  line-height: 20px;
  font-weight: 700;
}

/* ---------- Chatt ---------- */
.chat {
  position: absolute;
  right: 0;
  top: 0;
  bottom: calc(84px + env(safe-area-inset-bottom));
  width: min(100%, 360px);
  background: var(--card);
  color: var(--text);
  z-index: 7;
  display: flex;
  flex-direction: column;
  box-shadow: -4px 0 20px rgba(0, 0, 0, .3);
  padding-top: env(safe-area-inset-top);
}
.chat-head { display: flex; justify-content: space-between; align-items: center; padding: 8px 8px 8px 16px; border-bottom: 1px solid var(--line); }
.chat-list { flex: 1; overflow-y: auto; padding: 12px 16px; display: flex; flex-direction: column; gap: 10px; }
.chat-msg { display: flex; gap: 8px; align-items: flex-start; }
.chat-msg .body { min-width: 0; }
.chat-msg .who { font-size: 13px; font-weight: 700; }
.chat-msg .who time { color: var(--muted); font-weight: normal; margin-left: 6px; }
.chat-msg .text { white-space: pre-wrap; word-break: break-word; line-height: 1.4; }
.chat-msg .text a { color: var(--accent); }
.chat-empty { color: var(--muted); text-align: center; margin-top: 20px; font-size: 14px; }
.chat-form { display: flex; gap: 8px; padding: 10px; border-top: 1px solid var(--line); }

/* ---------- Övrigt ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(110px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: #1c2330;
  color: #fff;
  padding: 12px 18px;
  border-radius: 12px;
  z-index: 20;
  font-size: 15px;
  max-width: calc(100% - 32px);
  text-align: center;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .3);
}

.offline {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 30;
  background: #f59e0b;
  color: #1c1300;
  text-align: center;
  padding: calc(6px + env(safe-area-inset-top)) 10px 6px;
  font-size: 14px;
  font-weight: 600;
}

.invite { background: var(--accent-soft); box-shadow: none; line-height: 1.5; }

/* Ljudinställningar före samtalet */
.sound { margin: 14px 0 0; padding: 14px 16px; }
.switch-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; cursor: pointer; }
.switch-row > span { display: flex; flex-direction: column; gap: 2px; }
.switch {
  appearance: none;
  -webkit-appearance: none;
  flex-shrink: 0;
  width: 50px;
  height: 30px;
  padding: 0;
  border: 0;
  border-radius: 15px;
  background: var(--line);
  position: relative;
  cursor: pointer;
  transition: background .2s;
}
.switch::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .3);
  transition: transform .2s;
}
.switch:checked { background: var(--ok); }
.switch:checked::after { transform: translateX(20px); }
.switch:disabled { opacity: .5; }

/* Videon förstoras max 2× sin verkliga upplösning (se fitVideo) – centrera den då i rutan */
.tile.remote { display: flex; align-items: center; justify-content: center; }
.tile.remote video { transition: max-width .4s ease, max-height .4s ease; }

/* Statistik (felsökning) – visas efter dubbelklick på videon */
#stage { touch-action: manipulation; }
.tile .stats {
  display: none;
  position: absolute;
  top: 6px;
  left: 6px;
  right: 6px;
  z-index: 3;
  white-space: pre;
  font: 11px/1.35 ui-monospace, Menlo, Consolas, monospace;
  background: rgba(0, 0, 0, .7);
  color: #9fffa6;
  padding: 5px 7px;
  border-radius: 6px;
  pointer-events: none;
  overflow: hidden;
}
.stats-on .tile .stats { display: block; }
.stats-on .tile.thumb .stats { font-size: 9px; padding: 3px 4px; }
