:root {
  --bg: #0f172a;
  --panel: #1e293b;
  --panel-2: #273449;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --good: #22c55e;
  --ok: #eab308;
  --bad: #ef4444;
  --radius: 14px;
  --border: var(--border);
  --on-accent: #04121c;
  --bubble-me: #0c4a6e;
  --bubble-me-sub: #bae6fd;
  --active: var(--active);
  --banner: #0c4a6e;
  --banner-warn: #713f12;
  --toast: var(--border);
  --toast-error: #7f1d1d;
  --overlay: var(--overlay);
  --dot-off: #475569;
  --pulse: var(--pulse);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
#app { max-width: 640px; margin: 0 auto; min-height: 100%; display: flex; flex-direction: column; }
main { padding: 0 16px 24px; }
.stack > * + * { margin-top: 14px; }
h1 { margin: 0 0 4px; font-size: 28px; }
h2 { font-size: 18px; margin: 18px 0 10px; }
h3 { margin: 0 0 10px; font-size: 16px; }
a { color: var(--accent); text-decoration: none; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
[hidden] { display: none !important; }

input, select, button { font: inherit; color: inherit; }
input, select {
  width: 100%; padding: 12px; margin: 6px 0 12px;
  background: var(--bg); border: 1px solid var(--border); border-radius: 10px;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
label { display: block; color: var(--muted); font-size: 14px; }
label.check { display: flex; gap: 8px; align-items: center; margin-bottom: 12px; }
label.check input { width: auto; margin: 0; }
button {
  padding: 12px 16px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--panel-2); cursor: pointer; min-height: 44px;
}
button:disabled { opacity: 0.4; cursor: default; }
button.primary { background: var(--accent); color: var(--on-accent); border: 0; font-weight: 600; width: 100%; }
button.danger { background: transparent; border-color: var(--bad); color: var(--bad); width: 100%; }
button.link { background: none; border: 0; color: var(--accent); width: 100%; margin-top: 8px; }
button.small { padding: 6px 10px; min-height: 34px; font-size: 14px; }

.card { background: var(--panel); border-radius: var(--radius); padding: 16px; }
.card.narrow { max-width: 420px; width: calc(100% - 32px); margin: 12vh auto 0; }

header.top {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 14px 16px; position: sticky; top: 0; background: var(--bg); z-index: 2;
}
header.top nav { display: flex; gap: 16px; }

.banner { margin: 0 16px 8px; padding: 10px 12px; border-radius: 10px; background: var(--banner); font-size: 14px; }
.banner.warn { background: var(--banner-warn); }

ul.contacts, ul.users { list-style: none; margin: 0; padding: 0; }
ul.contacts li, ul.users li {
  display: flex; align-items: center; gap: 12px; padding: 12px; margin-bottom: 8px;
  background: var(--panel); border-radius: var(--radius);
}
ul.users li { justify-content: space-between; }
ul.users li.disabled { opacity: 0.5; }
ul.contacts .who { flex: 1; min-width: 0; }
ul.contacts .name { font-weight: 600; }
.dot { width: 12px; height: 12px; border-radius: 50%; background: var(--dot-off); flex: none; }
.dot.online { background: var(--good); box-shadow: 0 0 0 3px rgba(34, 197, 94, 0.2); }
.dot.busy { background: var(--ok); }
.call-btn { width: 52px; height: 52px; border-radius: 50%; background: var(--good); border: 0; font-size: 22px; padding: 0; }
.tag { font-size: 11px; background: var(--border); padding: 2px 6px; border-radius: 6px; margin-left: 6px; }
.row { display: flex; gap: 10px; align-items: center; justify-content: center; }

#overlay {
  position: fixed; inset: 0; background: var(--overlay); z-index: 10;
  display: flex; align-items: center; justify-content: center; padding: 24px;
}
.sheet { background: var(--panel); border-radius: 20px; padding: 28px 22px; width: 100%; max-width: 380px; text-align: center; }
.sheet.ringing .row { gap: 56px; margin-top: 22px; }
.avatar {
  width: 96px; height: 96px; border-radius: 50%; margin: 0 auto 12px; display: grid; place-items: center;
  background: #0369a1; font-size: 40px; font-weight: 700;
}
.pulse { animation: pulse 1.6s ease-out infinite; }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 var(--pulse); } 100% { box-shadow: 0 0 0 28px rgba(56, 189, 248, 0); } }
.round { width: 70px; height: 70px; border-radius: 50%; border: 0; font-size: 28px; padding: 0; }
.round.accept { background: var(--good); }
.round.decline { background: var(--bad); color: white; }

.incall { display: flex; flex-direction: column; height: 100vh; height: 100dvh; padding: 12px 16px 16px; }
.incall header { display: flex; justify-content: space-between; align-items: flex-start; }
.incall .peer { font-size: 22px; font-weight: 700; }
.incall .right { text-align: right; }
.latency { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }
.latency.good { color: var(--good); }
.latency.ok { color: var(--ok); }
.latency.slow { color: var(--bad); }
.status { font-size: 13px; margin: 6px 0 2px; }
.status.ok { color: var(--good); }
.status.warn { color: var(--ok); }
.status.bad { color: var(--bad); }
.langs { margin-bottom: 8px; }
.feed {
  flex: 1; overflow-y: auto; background: var(--panel); border-radius: var(--radius);
  padding: 12px; -webkit-overflow-scrolling: touch;
}
.bubble { max-width: 88%; margin: 0 0 10px; padding: 8px 12px; border-radius: 14px; }
.bubble .who { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.bubble.peer { background: var(--panel-2); }
.bubble.peer .tgt { font-size: 19px; }
.bubble.peer .src { font-size: 13px; color: var(--muted); }
.bubble.me { background: var(--bubble-me); margin-left: auto; }
.bubble.me .src { font-size: 16px; }
.bubble.me .tgt { font-size: 13px; color: var(--bubble-me-sub); }
.bubble .tgt:empty, .bubble .src:empty { display: none; }
.info { min-height: 18px; margin: 6px 0; }
.timer { display: inline; font-variant-numeric: tabular-nums; }
.controls { display: flex; gap: 10px; align-items: center; justify-content: space-between; padding-top: 8px; }
.ctl { flex: 1; font-size: 15px; }
.ctl.on { background: var(--active); border-color: var(--accent); }

.toast {
  position: fixed; left: 16px; right: 16px; bottom: calc(16px + env(safe-area-inset-bottom)); z-index: 20;
  max-width: 560px; margin: 0 auto; padding: 12px 14px; border-radius: 12px;
  background: var(--toast); opacity: 0; transform: translateY(20px); transition: all 0.2s; pointer-events: none;
}
.toast.show { opacity: 1; transform: none; }
.toast.error { background: var(--toast-error); }

.meter { height: 10px; background: var(--bg); border-radius: 6px; overflow: hidden; margin: 6px 0; }
.meter > div { height: 100%; background: var(--good); }
.meter.warn > div { background: var(--ok); }
.meter.blocked > div { background: var(--bad); }
.warn-text { color: var(--ok); }
.bad-text { color: var(--bad); }
.scroll { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: 13px; }
th, td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { color: var(--muted); font-weight: 500; }
.linkarea input { font-size: 13px; }
ul.users li { flex-wrap: wrap; }
.linkpanel { flex-basis: 100%; margin-top: 4px; padding-top: 10px; border-top: 1px solid var(--border); }
.linkpanel input { font-size: 13px; margin: 6px 0 8px; }
.linkpanel .row { justify-content: flex-start; flex-wrap: wrap; }

.tabs { display: flex; gap: 8px; margin: 14px 0 10px; }
.tab { flex: 1; background: var(--panel); position: relative; }
.tab.active { background: var(--active); border-color: var(--accent); font-weight: 600; }
.badge { display: inline-block; min-width: 20px; padding: 0 6px; margin-left: 6px; border-radius: 10px; background: var(--bad); color: white; font-size: 12px; line-height: 20px; }
.icon-btn { width: 40px; height: 40px; min-height: 40px; padding: 0; border-radius: 50%; background: transparent; border: 1px solid var(--border); }
ul.history .dir { width: 22px; text-align: center; font-size: 18px; color: var(--muted); }
ul.history .dir.missed { color: var(--bad); }
ul.history li.missed .name { color: var(--bad); }
.adminbar { margin: 0 16px; }
.adminbar .banner { margin: 0 0 8px; display: flex; gap: 10px; align-items: center; justify-content: space-between; }
.provider-switch { justify-content: flex-start; margin-bottom: 4px; }
.callpanel { background: var(--panel); border-radius: var(--radius); padding: 12px; margin-top: 8px; }
.callpanel input[type="range"] { padding: 0; margin: 8px 0 12px; accent-color: var(--accent); }
.controls .ctl { font-size: 13px; padding: 10px 6px; }
.sheet.summary { text-align: left; }
.sumrow { display: flex; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid var(--border); }
.sheet.summary button { width: 100%; margin-top: 16px; }
.card input[type="range"] { padding: 0; margin: 8px 0 12px; accent-color: var(--accent); }
.net { font-size: 12px; margin-top: 2px; }
.net.good { color: var(--good); }
.net.slow { color: var(--ok); }
.net.poor { color: var(--bad); }

/* ---------- themes: palette x light/dark (Night is the original look) ---------- */
html[data-palette="pastel"][data-mode="light"] {
  --bg: #fff5fa; --panel: #ffffff; --panel-2: #fde7f3; --text: #3b2340; --muted: #8a6f8f;
  --accent: #e879b9; --on-accent: #ffffff; --border: #f3d3e6; --active: #f9a8d4;
  --bubble-me: #ede9fe; --bubble-me-sub: #6d28d9; --banner: #fce7f3; --banner-warn: #fef3c7;
  --toast: #3b2340; --toast-error: #be123c; --overlay: rgba(59, 35, 64, 0.55); --dot-off: #d4c4d3;
  --pulse: rgba(232, 121, 185, 0.5); color-scheme: light;
}
html[data-palette="pastel"][data-mode="dark"] {
  --bg: #241826; --panel: #33213a; --panel-2: #402a48; --text: #fbe7f5; --muted: #c7a7c6;
  --accent: #f9a8d4; --on-accent: #3b0a2a; --border: #4f3657; --active: #6b2f5c;
  --bubble-me: #4c2a63; --bubble-me-sub: #e9d5ff; --banner: #5b2a52; --banner-warn: #6b4a14;
  --toast: #4f3657; --overlay: rgba(20, 10, 22, 0.88); --dot-off: #6b5570; --pulse: rgba(249, 168, 212, 0.5);
}
html[data-palette="warm"][data-mode="light"] {
  --bg: #fff7f0; --panel: #ffffff; --panel-2: #ffe8d6; --text: #3d2418; --muted: #8f6b5a;
  --accent: #fb7f5a; --on-accent: #ffffff; --border: #f6d6c2; --active: #fdba9a;
  --bubble-me: #ffe1d1; --bubble-me-sub: #c2410c; --banner: #ffedd5; --banner-warn: #fef3c7;
  --toast: #3d2418; --toast-error: #b91c1c; --overlay: rgba(61, 36, 24, 0.55); --dot-off: #e2cbbd;
  --pulse: rgba(251, 127, 90, 0.5); color-scheme: light;
}
html[data-palette="warm"][data-mode="dark"] {
  --bg: #24170f; --panel: #33221a; --panel-2: #402c22; --text: #ffeee3; --muted: #c9a594;
  --accent: #fb923c; --on-accent: #2b1305; --border: #553a2c; --active: #7c3d1d;
  --bubble-me: #5a2e1c; --bubble-me-sub: #fed7aa; --banner: #5a2e1c; --banner-warn: #6b4a14;
  --toast: #553a2c; --overlay: rgba(24, 14, 8, 0.88); --dot-off: #6e5546; --pulse: rgba(251, 146, 60, 0.5);
}
html[data-palette="calm"][data-mode="light"] {
  --bg: #f2fbf8; --panel: #ffffff; --panel-2: #dff5ee; --text: #17343a; --muted: #5f8189;
  --accent: #2fb8a5; --on-accent: #ffffff; --border: #cfe9e2; --active: #99e2d6;
  --bubble-me: #dbeefe; --bubble-me-sub: #0369a1; --banner: #e0f2fe; --banner-warn: #fef3c7;
  --toast: #17343a; --toast-error: #b91c1c; --overlay: rgba(23, 52, 58, 0.5); --dot-off: #c3d8d4;
  --pulse: rgba(47, 184, 165, 0.5); color-scheme: light;
}
html[data-palette="calm"][data-mode="dark"] {
  --bg: #0f1f22; --panel: #172d31; --panel-2: #1e3a3f; --text: #e3f7f3; --muted: #94b8b4;
  --accent: #5eead4; --on-accent: #04221f; --border: #2b4a4f; --active: #155e63;
  --bubble-me: #164e63; --bubble-me-sub: #a5f3fc; --banner: #164e63; --banner-warn: #6b4a14;
  --toast: #2b4a4f; --overlay: rgba(6, 16, 18, 0.88); --dot-off: #45605f; --pulse: rgba(94, 234, 212, 0.5);
}
html[data-mode="light"] .round.decline { color: white; }
html[data-mode="light"] .bubble.me { color: var(--text); }
html[data-mode="light"] .toast { color: #ffffff; }
html[data-mode="light"] .banner.warn { color: #713f12; }

/* ---------- photos / avatars ---------- */
.avatar-img, .avatar-initial { border-radius: 50%; object-fit: cover; flex: none; display: inline-grid; place-items: center; color: white; font-weight: 700; }
.avatar-wrap { position: relative; flex: none; }
.avatar-wrap .dot { position: absolute; right: -1px; bottom: -1px; width: 14px; height: 14px; border: 2px solid var(--panel); }
.me-chip { display: flex; align-items: center; gap: 10px; color: var(--text); }
.peer-head { display: flex; align-items: center; gap: 12px; }
.ring-avatar { width: 112px; height: 112px; margin: 0 auto 12px; border-radius: 50%; }
.sheet-avatar { display: grid; place-items: center; margin: 4px 0 12px; }
.contact-sheet { text-align: left; }
.contact-sheet h2 { text-align: center; margin-top: 0; }
.contact-sheet .row { margin: 4px 0 12px; }
.contact-sheet .row button { flex: 1; }
.contact-sheet > button { width: 100%; margin-top: 6px; }
ul.history li { gap: 10px; }

/* ---------- look settings ---------- */
.swatches { display: flex; flex-wrap: wrap; gap: 8px; margin: 6px 0 14px; }
.swatch { flex: 1 1 40%; font-size: 14px; }
.swatch.selected { border: 2px solid var(--accent); font-weight: 600; }
.swatch[data-swatch="night"] { background: #1e293b; color: #e2e8f0; }
.swatch[data-swatch="pastel"] { background: linear-gradient(135deg, #fde7f3, #ede9fe); color: #3b2340; }
.swatch[data-swatch="warm"] { background: linear-gradient(135deg, #ffe8d6, #fecaca); color: #3d2418; }
.swatch[data-swatch="calm"] { background: linear-gradient(135deg, #dff5ee, #dbeefe); color: #17343a; }

/* ---------- playful style ---------- */
html[data-style="playful"] { --radius: 22px; }
html[data-style="playful"] body {
  background:
    radial-gradient(circle at 10% 0%, color-mix(in srgb, var(--accent) 18%, transparent) 0, transparent 40%),
    radial-gradient(circle at 100% 30%, color-mix(in srgb, var(--accent) 12%, transparent) 0, transparent 35%),
    var(--bg);
  background-attachment: fixed;
}
html[data-style="playful"] button { border-radius: 999px; transition: transform 0.12s ease; }
html[data-style="playful"] button:active { transform: scale(0.94); }
html[data-style="playful"] .avatar-img,
html[data-style="playful"] .avatar-initial { box-shadow: 0 0 0 3px var(--panel), 0 0 0 5px var(--accent); }
html[data-style="playful"] .bubble { border-radius: 20px; }
html[data-style="playful"] .bubble.peer { border-bottom-left-radius: 6px; }
html[data-style="playful"] .bubble.me { border-bottom-right-radius: 6px; }
html[data-style="playful"] .sheet.ringing { position: relative; overflow: hidden; }
html[data-style="playful"] .sheet.ringing::before,
html[data-style="playful"] .sheet.ringing::after {
  content: "💕"; position: absolute; font-size: 28px; bottom: -30px; animation: floatup 3s ease-in infinite; opacity: 0.8;
}
html[data-style="playful"] .sheet.ringing::before { left: 18%; }
html[data-style="playful"] .sheet.ringing::after { right: 18%; content: "💖"; animation-delay: 1.4s; }
@keyframes floatup { from { transform: translateY(0) scale(0.8); opacity: 0; } 20% { opacity: 0.9; } to { transform: translateY(-260px) scale(1.2); opacity: 0; } }
html[data-style="playful"] h2::after { content: " ✨"; }
@media (prefers-reduced-motion: reduce) {
  html[data-style="playful"] .sheet.ringing::before, html[data-style="playful"] .sheet.ringing::after { animation: none; display: none; }
}
