:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --brand: #0d9488;
  --brand-ink: #ffffff;
  --brand-soft: #ccfbf1;
  --mine: #0d9488;
  --mine-ink: #ffffff;
  --theirs: #ffffff;
  --theirs-ink: #0f172a;
  --danger: #e11d48;
  --ok: #16a34a;
  --warn: #d97706;
  --radius: 18px;
  --shadow: 0 1px 2px rgb(15 23 42 / 0.06), 0 4px 16px rgb(15 23 42 / 0.06);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1114;
    --surface: #141c20;
    --text: #e6edf0;
    --muted: #8b9aa3;
    --line: #22303a;
    --brand: #14b8a6;
    --brand-ink: #04201d;
    --brand-soft: #0f3330;
    --mine: #0f766e;
    --mine-ink: #ecfeff;
    --theirs: #1c262c;
    --theirs-ink: #e6edf0;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", "Noto Sans Thai", "Noto Sans JP", "Noto Sans TC", sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
body { overflow: hidden; }

h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 2.2rem; letter-spacing: -0.02em; }
h2 { font-size: 1.5rem; }
h3 { font-size: 1.15rem; }

.screen { height: 100dvh; }
.screen-center {
  display: grid;
  place-items: center;
  padding: 24px 16px calc(24px + env(safe-area-inset-bottom));
  overflow-y: auto;
  text-align: center;
}
.stack { display: grid; gap: 16px; width: min(400px, 100%); justify-items: center; }
.stack-sm { display: grid; gap: 10px; width: 100%; }
.row { display: flex; gap: 8px; width: 100%; }
.row > * { flex: 1; }
.lead { margin: 0; color: var(--muted); }
.fine { margin: 0; color: var(--muted); font-size: 0.85rem; }
.label { font-size: 0.85rem; color: var(--muted); text-align: left; }
.error { margin: 0; min-height: 1.2em; color: var(--danger); font-size: 0.9rem; }

.logo {
  width: 72px; height: 72px; border-radius: 22px;
  background: url("/icon.svg") center / cover;
  box-shadow: var(--shadow);
}
.pulse { animation: pulse 1.4s ease-in-out infinite; }
@keyframes pulse { 50% { transform: scale(0.92); opacity: 0.7; } }

input, textarea {
  font: inherit;
  color: inherit;
  width: 100%;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--surface);
  outline: none;
}
input:focus, textarea:focus { border-color: var(--brand); box-shadow: 0 0 0 3px color-mix(in srgb, var(--brand) 25%, transparent); }

.btn {
  font: inherit; font-weight: 600;
  padding: 12px 16px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.danger-outline { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, var(--line)); }
.btn:disabled { opacity: 0.6; cursor: default; }
.btn:active { transform: scale(0.98); }

.card {
  width: 100%;
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.code {
  font: 700 2rem/1.2 ui-monospace, "SF Mono", Menlo, monospace;
  letter-spacing: 0.08em;
  padding: 8px;
  border-radius: 12px;
  background: var(--brand-soft);
  user-select: all;
}
.code.small { font-size: 1.2rem; }
#join-code { text-transform: uppercase; letter-spacing: 0.1em; text-align: center; }
.divider { display: flex; align-items: center; gap: 12px; width: 100%; color: var(--muted); font-size: 0.85rem; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--line); }
.waiting { display: flex; align-items: center; justify-content: center; gap: 8px; }
.dot-pulse { width: 8px; height: 8px; border-radius: 50%; background: var(--brand); animation: pulse 1.2s infinite; }

/* ---------- chat ---------- */

.chat { display: flex; flex-direction: column; }
.chat-header {
  display: flex; align-items: center; gap: 10px;
  padding: calc(10px + env(safe-area-inset-top)) 12px 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.avatar {
  flex: none;
  width: 40px; height: 40px; border-radius: 50%;
  display: grid; place-items: center;
  background: var(--brand-soft); color: var(--brand);
  font-weight: 700;
}
.avatar.xl { width: 112px; height: 112px; font-size: 2.6rem; }
.peer { flex: 1; min-width: 0; }
.peer-name { font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.peer-status { font-size: 0.8rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.peer-status.online { color: var(--ok); }

.icon-btn {
  width: 42px; height: 42px; border-radius: 50%;
  border: 0; background: transparent; color: var(--brand);
  display: grid; place-items: center; cursor: pointer;
}
.icon-btn:hover { background: var(--brand-soft); }
.icon-btn svg { width: 22px; height: 22px; fill: currentColor; }
#btn-menu { color: var(--muted); }

.conn-banner {
  padding: 6px 16px;
  font-size: 0.85rem; text-align: center;
  background: color-mix(in srgb, var(--warn) 15%, var(--surface));
  color: var(--warn);
}

.messages {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 12px 12px 8px;
  display: flex; flex-direction: column; gap: 4px;
}
.empty { margin: auto; max-width: 280px; text-align: center; color: var(--muted); }
.day {
  align-self: center;
  margin: 12px 0 6px;
  padding: 2px 10px;
  font-size: 0.75rem; color: var(--muted);
  background: var(--surface); border-radius: 999px; border: 1px solid var(--line);
}
.msg {
  max-width: min(78%, 560px);
  padding: 8px 12px 6px;
  border-radius: var(--radius);
  box-shadow: 0 1px 1px rgb(0 0 0 / 0.05);
}
.msg .text { white-space: pre-wrap; overflow-wrap: anywhere; }
.msg .meta { font-size: 0.7rem; opacity: 0.7; text-align: right; margin-top: 2px; }
.msg.mine { align-self: flex-end; background: var(--mine); color: var(--mine-ink); border-bottom-right-radius: 6px; }
.msg.theirs { align-self: flex-start; background: var(--theirs); color: var(--theirs-ink); border-bottom-left-radius: 6px; }
.msg.pending { opacity: 0.7; }
.msg.muted .text { font-style: italic; opacity: 0.8; }
.call-line { align-self: center; margin: 6px 0; font-size: 0.85rem; color: var(--muted); }

.composer {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 8px 10px calc(8px + env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--line);
}
.composer textarea { resize: none; border-radius: 22px; padding: 10px 16px; max-height: 160px; }
.send-btn {
  flex: none;
  width: 44px; height: 44px; border-radius: 50%;
  border: 0; background: var(--brand); color: var(--brand-ink);
  display: grid; place-items: center; cursor: pointer;
}
.send-btn svg { width: 20px; height: 20px; fill: currentColor; }

/* ---------- call ---------- */

.call {
  position: fixed; inset: 0; z-index: 10;
  background: radial-gradient(circle at 50% 30%, #134e4a, #071312 70%);
  color: #fff;
  display: flex; flex-direction: column; align-items: center; justify-content: space-between;
  padding: calc(48px + env(safe-area-inset-top)) 16px calc(32px + env(safe-area-inset-bottom));
}
#remote-video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; display: none; background: #000; }
.call.has-video #remote-video { display: block; }
.call.has-video .call-center { margin-top: 0; text-shadow: 0 1px 4px rgb(0 0 0 / 0.6); }
.call.has-video .call-center .avatar { display: none; }
.call-center { position: relative; display: grid; justify-items: center; gap: 10px; margin-top: 8vh; text-align: center; }
.call-center .avatar { background: rgb(255 255 255 / 0.12); color: #fff; }
.call-name { font-size: 1.6rem; font-weight: 650; }
.call-status { opacity: 0.85; font-variant-numeric: tabular-nums; }
.call-quality { font-size: 0.8rem; padding: 3px 10px; border-radius: 999px; background: rgb(255 255 255 / 0.12); }
.call-quality[data-q="good"]::before { content: "● "; color: #4ade80; }
.call-quality[data-q="fair"]::before { content: "● "; color: #fbbf24; }
.call-quality[data-q="poor"]::before { content: "● "; color: #f87171; }
#local-video {
  position: absolute;
  right: 16px; top: calc(16px + env(safe-area-inset-top));
  width: 28vw; max-width: 160px; aspect-ratio: 3 / 4;
  object-fit: cover; border-radius: 14px;
  transform: scaleX(-1);
  box-shadow: 0 4px 20px rgb(0 0 0 / 0.4);
}
.call-controls { position: relative; display: flex; gap: 20px; }
.round {
  width: 64px; height: 64px; border-radius: 50%;
  border: 0; cursor: pointer;
  background: rgb(255 255 255 / 0.16); color: #fff;
  display: grid; place-items: center;
}
.round svg { width: 28px; height: 28px; fill: currentColor; }
.round.off { background: #fff; color: #0f172a; }
.round.danger { background: var(--danger); }
.round.accept { background: var(--ok); }
#call-hangup svg, #call-decline svg { transform: rotate(0deg); }

/* ---------- settings ---------- */

dialog {
  width: min(420px, calc(100% - 32px));
  border: 0; border-radius: var(--radius);
  padding: 20px;
  background: var(--surface); color: var(--text);
  box-shadow: 0 20px 60px rgb(0 0 0 / 0.3);
}
dialog::backdrop { background: rgb(0 0 0 / 0.4); }
dialog .stack { justify-items: stretch; width: 100%; }
.kv { display: flex; justify-content: space-between; gap: 12px; }
.kv span { color: var(--muted); }
.safety { display: grid; gap: 6px; padding: 12px; border-radius: 12px; background: var(--bg); text-align: center; }

.toast {
  position: fixed; z-index: 20;
  left: 50%; bottom: calc(88px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  padding: 10px 16px; border-radius: 12px;
  background: #0f172a; color: #fff; font-size: 0.9rem;
  box-shadow: var(--shadow);
  overflow-wrap: anywhere;
}

/* ---------- recordings ---------- */

#btn-space.active { background: var(--brand-soft); }
.tool-btn {
  flex: none;
  width: 44px; height: 44px; border-radius: 50%;
  border: 0; background: transparent; color: var(--muted);
  display: grid; place-items: center; cursor: pointer;
}
.tool-btn svg { width: 22px; height: 22px; fill: currentColor; }
#rec-voice { touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
#rec-voice.recording { background: var(--danger); transform: scale(1.15); transition: transform 0.15s; }
.rec-bar {
  flex: 1;
  display: flex; align-items: center; gap: 10px;
  min-height: 44px; padding: 0 12px;
  font-variant-numeric: tabular-nums;
}
.rec-bar .fine { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.rec-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--danger); animation: pulse 1s infinite; flex: none; }

.media-slot { display: flex; align-items: center; gap: 10px; min-width: 180px; }
.media-slot audio { width: min(260px, 60vw); height: 40px; }
.media-slot video { width: min(280px, 62vw); max-height: 50vh; border-radius: 12px; background: #000; }
.media-slot .play {
  flex: none;
  width: 40px; height: 40px; border-radius: 50%;
  border: 0; cursor: pointer;
  display: grid; place-items: center;
  background: rgb(255 255 255 / 0.25); color: inherit;
}
.msg.theirs .media-slot .play { background: var(--brand-soft); color: var(--brand); }
.media-slot .play svg { width: 22px; height: 22px; fill: currentColor; }
.media-slot .play:disabled { opacity: 0.5; }
.media-label { font-weight: 600; font-variant-numeric: tabular-nums; }

.space { flex: 1; overflow-y: auto; padding: 16px; }
.space-head { display: grid; gap: 4px; margin-bottom: 12px; }
.space-list { display: grid; gap: 10px; grid-template-columns: repeat(auto-fill, minmax(min(280px, 100%), 1fr)); }
.space-card {
  display: grid; gap: 8px;
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.space-card .play { background: var(--brand-soft); color: var(--brand); }
.space-who { font-size: 0.8rem; color: var(--muted); }

/* ---------- call: walkie-talkie + network blame ---------- */

.call-blame { font-size: 0.85rem; padding: 3px 10px; border-radius: 999px; background: rgb(248 113 113 / 0.25); }
.walkie { position: relative; display: grid; gap: 12px; justify-items: center; max-width: 340px; text-align: center; font-size: 0.9rem; opacity: 0.95; }
.walkie p { margin: 0; }
.walkie-btn {
  font: inherit; font-weight: 700;
  padding: 18px 28px; border-radius: 999px;
  border: 0; cursor: pointer;
  background: #fff; color: #0f172a;
  touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
}
.walkie-btn.recording { background: var(--danger); color: #fff; }

/* ---------- video note recorder ---------- */

.recorder {
  position: fixed; inset: 0; z-index: 15;
  background: #000; color: #fff;
  display: flex; flex-direction: column; align-items: center; justify-content: flex-end;
  padding: 16px 16px calc(32px + env(safe-area-inset-bottom));
}
.recorder video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
#recorder-preview { transform: scaleX(-1); }
#recorder-review { object-fit: contain; }
.recorder-time {
  position: absolute; top: calc(16px + env(safe-area-inset-top)); left: 50%; transform: translateX(-50%);
  padding: 4px 12px; border-radius: 999px; background: rgb(0 0 0 / 0.5);
  font-variant-numeric: tabular-nums;
}
.round.rec span { width: 26px; height: 26px; border-radius: 50%; background: var(--danger); transition: all 0.15s; }
.round.rec.on span { border-radius: 6px; width: 22px; height: 22px; }
