:root {
  --bg: #0b1220;
  --fg: #eef2f7;
  --muted: #8a97ab;
  --accent: #4dd0e1;
  --danger: #ff5c5c;
  --warn: #f5b342;
  --ok: #4caf50;
  --panel: #131c2e;
  --border: #22314b;
  --tap: 56px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--fg);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}

.app {
  max-width: 480px;
  margin: 0 auto;
  padding: 32px 20px;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.landing-page {
  position: relative;
  padding-top: max(60px, calc(env(safe-area-inset-top) + 36px));
  padding-bottom: max(32px, env(safe-area-inset-bottom));
}
.landing-brand { text-align: center; }
.landing-actions { margin-top: auto; }
.join-modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  overflow-y: auto;
  padding: max(190px, calc(env(safe-area-inset-top) + 160px)) 18px max(24px, env(safe-area-inset-bottom));
  background: rgba(0, 0, 0, 0.72);
}
.join-modal[hidden] { display: none; }
.join-sheet {
  width: min(100%, 400px);
  padding: 26px 24px 24px;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--bg);
}
.join-title { margin: 0 0 24px; text-align: center; font-size: 28px; line-height: 1.1; }
.join-actions { display: grid; grid-template-columns: 1fr 2fr; gap: 9px; margin-top: 20px; }
.join-actions button { min-width: 0; }
.join-actions #btn-join-cancel { padding-left: 10px; padding-right: 10px; font-size: 15px; }
.nearby-section { margin-top: 24px; padding-top: 20px; border-top: 1px solid var(--border); }
.nearby-heading { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.nearby-heading h2 { margin: 0; font-size: 17px; }
.nearby-heading span { flex: 0 0 auto; color: var(--accent); font-size: 12px; font-weight: 700; text-transform: uppercase; }
.nearby-find { min-height: 48px; margin-top: 13px; padding: 12px 14px; font-size: 15px; }
.nearby-status { min-height: 18px; margin-top: 10px; color: var(--muted); font-size: 13px; line-height: 1.4; text-align: center; }
.nearby-results { display: grid; gap: 7px; margin-top: 9px; }
button.nearby-broadcast {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
  min-height: 56px;
  padding: 10px 12px;
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 11px;
  background: var(--panel);
  text-align: left;
}
.nearby-broadcast-info { display: grid; min-width: 0; gap: 3px; }
.nearby-broadcast-info strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.nearby-broadcast-info span { color: var(--muted); font-size: 12px; }
.nearby-code { color: var(--accent); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 18px; font-weight: 700; letter-spacing: .08em; }
.nearby-privacy { margin: 11px 8px 0; color: var(--muted); font-size: 11px; line-height: 1.4; text-align: center; }
.settings-button {
  position: absolute;
  top: max(18px, env(safe-area-inset-top));
  right: 20px;
  display: grid;
  width: 40px;
  height: 40px;
  place-items: center;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 50%;
  background: var(--panel);
  text-decoration: none;
}
.settings-button svg { width: 21px; height: 21px; fill: currentColor; }
.settings-button:active { color: var(--accent); transform: scale(.96); }
.settings-button:focus-visible, .back-link:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 4px;
  border-radius: 4px;
}

.brand {
  font-size: 28px;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--fg);
}
.brand .dot { color: var(--accent); }
.tagline {
  color: var(--muted);
  margin-top: 8px;
  font-size: 15px;
  line-height: 1.5;
}

.spacer { flex: 1; }

button, .btn {
  appearance: none;
  border: none;
  background: var(--accent);
  color: #04202a;
  font-size: 18px;
  font-weight: 600;
  padding: 18px 20px;
  border-radius: 14px;
  min-height: var(--tap);
  cursor: pointer;
  width: 100%;
  transition: transform 0.06s ease, opacity 0.15s ease;
}
button:active { transform: scale(0.98); }
button:disabled { opacity: 0.5; cursor: not-allowed; }
button.secondary {
  background: var(--panel);
  color: var(--fg);
  border: 1px solid var(--border);
}
button.danger { background: var(--danger); color: white; }
button.big {
  font-size: 22px;
  padding: 22px 20px;
  border-radius: 18px;
}

.stack > * + * { margin-top: 14px; }
.stack-lg > * + * { margin-top: 24px; }

input[type="text"], input[type="tel"], input[type="number"] {
  width: 100%;
  font-size: 32px;
  padding: 18px 20px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--fg);
  text-align: center;
  letter-spacing: 0.4em;
  font-weight: 600;
}
input:focus { outline: 2px solid var(--accent); }

select {
  width: 100%;
  min-height: 48px;
  padding: 11px 42px 11px 14px;
  border: 1px solid var(--border);
  border-radius: 11px;
  background: var(--bg);
  color: var(--fg);
  font: inherit;
  font-size: 16px;
  font-weight: 600;
}
select:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
select:disabled { opacity: 0.5; }

.section-title { font-size: 18px; font-weight: 700; }
.music-panel {
  padding: 18px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
}
.file-picker {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: var(--tap);
  padding: 14px 18px;
  color: var(--accent);
  border: 1px dashed var(--accent);
  border-radius: 12px;
  font-weight: 600;
  cursor: pointer;
}
.file-picker.disabled { opacity: 0.45; cursor: default; }
.file-picker input { display: none; }
.track-list { display: grid; gap: 8px; }
.track-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  align-items: center;
  gap: 8px;
  padding: 10px;
  background: var(--bg);
  border-radius: 10px;
}
.track-row.built-in { grid-template-columns: minmax(0, 1fr) auto; }
.track-info { min-width: 0; }
.track-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.track-meta { margin-top: 3px; color: var(--muted); font-size: 12px; }
.empty-state { padding: 12px; color: var(--muted); text-align: center; font-size: 14px; }
button.compact { width: auto; min-height: 40px; padding: 9px 13px; font-size: 14px; border-radius: 10px; }
button.icon-btn { width: 40px; min-height: 40px; padding: 4px; color: var(--muted); background: transparent; font-size: 25px; }
.music-controls { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.now-playing {
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--panel);
  text-align: center;
}
.now-playing-label { display: block; margin-bottom: 4px; color: var(--accent); font-size: 12px; text-transform: uppercase; letter-spacing: .08em; }
.now-playing-title { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.latency-panel {
  margin-top: 14px;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--panel);
}
.latency-heading { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.latency-value { color: var(--accent); font-weight: 700; font-variant-numeric: tabular-nums; }
.bluetooth-toggle { font-size: 15px; padding: 13px 16px; min-height: 48px; }
.bluetooth-toggle.active { background: var(--accent); color: #04202a; border-color: var(--accent); }
.latency-hint { margin-top: 0; text-align: left; line-height: 1.4; }

.code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 64px;
  font-weight: 700;
  letter-spacing: 0.15em;
  text-align: center;
  padding: 24px 0;
  color: var(--accent);
}

.status-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
}
.status-row .label { color: var(--muted); font-size: 14px; }
.status-row .value { font-size: 18px; font-weight: 600; }

.dot-status {
  display: inline-block;
  width: 14px; height: 14px;
  border-radius: 50%;
  background: var(--muted);
  margin-right: 10px;
  vertical-align: middle;
}
.dot-status.ok { background: var(--ok); box-shadow: 0 0 12px rgba(76,175,80,0.6); }
.dot-status.warn { background: var(--warn); }
.dot-status.err { background: var(--danger); }

.mic-btn {
  width: 180px;
  height: 180px;
  border-radius: 50%;
  margin: 20px auto;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  font-weight: 700;
  background: var(--panel);
  border: 3px solid var(--border);
  color: var(--fg);
}
.mic-btn.live { background: var(--danger); border-color: var(--danger); color: white; box-shadow: 0 0 40px rgba(255,92,92,0.4); }

.broadcast-page {
  height: 100vh;
  height: 100svh;
  min-height: 0;
  overflow: hidden;
  padding: max(10px, env(safe-area-inset-top)) 16px max(10px, env(safe-area-inset-bottom));
}
.broadcast-page > * + * { margin-top: 7px; }
.broadcast-brand { text-align: center; }
.broadcast-brand .brand { font-size: 25px; line-height: 1.15; }
.share-code .tagline { margin-top: 0; text-align: center; line-height: 1.15; }
.broadcast-page .code {
  padding: 0;
  font-size: 50px;
  line-height: 1.05;
}
.waveform {
  height: 46px;
  flex: 0 0 46px;
  overflow: hidden;
  background: transparent;
}
.waveform canvas { display: block; width: 100%; height: 100%; }
.broadcast-page .mic-btn {
  width: 104px;
  height: 104px;
  min-height: 104px;
  margin: 12px auto 14px;
  padding: 8px;
  border-width: 2px;
  font-size: 17px;
  line-height: 1.15;
}
.broadcast-page .mic-btn.live { box-shadow: 0 0 24px rgba(255,92,92,0.32); }
.status-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 7px; }
.status-grid .status-row {
  min-width: 0;
  min-height: 49px;
  padding: 9px 11px;
  border-radius: 11px;
}
.status-grid .status-value { display: flex; min-width: 0; align-items: center; }
.status-grid .dot-status { width: 10px; height: 10px; margin-right: 6px; flex: 0 0 auto; }
.status-grid .value { overflow: hidden; font-size: 15px; text-overflow: ellipsis; white-space: nowrap; }
.broadcast-music { padding: 11px; border-radius: 11px; }
.music-select-label {
  display: block;
  margin-bottom: 6px;
  font-size: 15px;
  font-weight: 700;
}
.music-now-playing { margin-top: 8px; }
.music-current { display: flex; min-width: 0; align-items: center; gap: 8px; margin-bottom: 7px; }
.music-current .now-playing-label { margin: 0; flex: 0 0 auto; }
.music-current-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; font-weight: 600; }
.broadcast-music .music-controls { grid-template-columns: repeat(3, 1fr); gap: 6px; }
.broadcast-music button.compact { width: 100%; min-height: 38px; padding: 7px 8px; }
.broadcast-music #music-status:empty { display: none; }
.broadcast-visibility {
  min-height: 44px;
  padding: 10px 14px;
  border-radius: 11px;
  font-size: 15px;
}
.broadcast-visibility.active { color: #04202a; border-color: var(--accent); background: var(--accent); }
.visibility-status { color: var(--danger); font-size: 12px; line-height: 1.35; text-align: center; }
.visibility-status:empty { display: none; }
.broadcast-page .spacer { min-height: 0; }
.broadcast-page #end-btn { min-height: 52px; padding: 13px 16px; font-size: 17px; border-radius: 12px; }
.broadcast-page .error:empty { display: none; }
.landing-page .landing-footer { margin-top: 50px; }

html.pwa-standalone .broadcast-page {
  padding-bottom: max(2px, calc(env(safe-area-inset-bottom) - 12px));
}
html.pwa-standalone .broadcast-page #end-btn {
  min-height: 56px;
  padding-top: 14px;
  padding-bottom: 14px;
}

.settings-page {
  max-width: 620px;
  padding-top: max(22px, calc(env(safe-area-inset-top) + 12px));
  padding-bottom: max(36px, calc(env(safe-area-inset-bottom) + 24px));
  gap: 18px;
}
.settings-header {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  min-height: 42px;
}
.settings-header .brand { font-size: 23px; text-align: center; }
.settings-header-spacer { min-width: 1px; }
.back-link { color: var(--accent); font-size: 15px; font-weight: 650; text-decoration: none; }
.settings-title-block h1 { margin: 6px 0 5px; font-size: 30px; line-height: 1.1; }
.settings-title-block p,
.settings-card p { margin: 0; color: var(--muted); font-size: 14px; line-height: 1.45; }
.settings-card {
  padding: 18px;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--panel);
}
.settings-card h2 { margin: 0 0 4px; font-size: 18px; }
.settings-card-heading,
.library-summary { display: flex; align-items: flex-start; justify-content: space-between; gap: 14px; }
.connection-badge,
.library-limit {
  flex: 0 0 auto;
  padding: 5px 8px;
  border: 1px solid rgba(77, 208, 225, 0.34);
  border-radius: 999px;
  color: var(--accent);
  background: rgba(77, 208, 225, 0.08);
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .04em;
}
.library-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; margin-top: 17px; }
.library-actions .file-picker { min-height: 52px; padding: 11px 12px; text-align: center; }
.library-actions .primary-picker { color: #04202a; border-style: solid; background: var(--accent); }
.library-actions .folder-picker { color: var(--fg); border-color: var(--border); border-style: solid; background: var(--bg); }
.settings-note { margin-top: 14px !important; }
.supported-formats { margin-top: 10px !important; color: var(--accent) !important; font-size: 12px !important; font-weight: 650; text-align: center; }
.library-card { padding-bottom: 12px; }
.settings-status { min-height: 20px; margin-top: 11px; color: var(--accent); font-size: 13px; line-height: 1.4; }
.settings-status:empty { min-height: 0; margin-top: 0; }
.settings-status.has-error { color: var(--danger); }
.library-sync-progress {
  height: 5px;
  margin-top: 8px;
  overflow: hidden;
  border-radius: 999px;
  background: var(--bg);
}
.library-sync-progress > div {
  width: 0;
  height: 100%;
  border-radius: inherit;
  background: var(--accent);
  transition: width .15s linear;
}
.library-track-list { display: grid; gap: 9px; margin-top: 12px; }
.library-track-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
  padding: 12px;
  border-radius: 12px;
  background: var(--bg);
}
.library-track-actions { display: flex; gap: 5px; }
.library-track-actions button.compact { min-height: 34px; padding: 7px 9px; font-size: 12px; }
.library-track-actions .danger-text { color: var(--danger); }
.library-empty { padding: 28px 12px; color: var(--muted); text-align: center; font-size: 14px; }
#library-preview { width: 100%; height: 44px; margin-top: 12px; }
.settings-privacy { margin: 0 10px; color: var(--muted); font-size: 12px; line-height: 1.45; text-align: center; }

@media (max-width: 520px) {
  .settings-page { padding-left: 15px; padding-right: 15px; }
  .library-track-row { grid-template-columns: 1fr; }
  .library-track-actions { display: grid; grid-template-columns: repeat(3, 1fr); }
  .library-track-actions button.compact { width: 100%; }
}

@media (max-width: 360px) {
  .library-actions { grid-template-columns: 1fr; }
}

@media (max-height: 700px) {
  .join-modal { padding-top: max(88px, calc(env(safe-area-inset-top) + 68px)); }
  .broadcast-page { padding-top: max(7px, env(safe-area-inset-top)); padding-bottom: max(7px, env(safe-area-inset-bottom)); }
  .broadcast-page > * + * { margin-top: 5px; }
  .broadcast-brand .brand { font-size: 22px; }
  .broadcast-page .code { font-size: 43px; }
  .waveform { height: 38px; flex-basis: 38px; }
  .broadcast-page .mic-btn { width: 88px; height: 88px; min-height: 88px; margin: 12px auto; font-size: 15px; }
  .status-grid .status-row { min-height: 44px; padding: 7px 9px; }
  .broadcast-music { padding: 9px; }
  .music-select-label { margin-bottom: 4px; }
  .broadcast-music select { min-height: 42px; padding-top: 8px; padding-bottom: 8px; }
  .music-now-playing { margin-top: 6px; }
  .music-current { margin-bottom: 5px; }
  .broadcast-music button.compact { min-height: 34px; padding: 5px 7px; }
  .broadcast-visibility { min-height: 38px; padding: 7px 12px; font-size: 14px; }
  .broadcast-page #end-btn { min-height: 48px; padding: 10px 14px; }
  html.pwa-standalone .broadcast-page #end-btn { min-height: 52px; padding-top: 11px; padding-bottom: 11px; }
}

.hint {
  color: var(--muted);
  font-size: 14px;
  text-align: center;
  margin-top: 12px;
}

.error {
  color: var(--danger);
  font-size: 14px;
  margin-top: 8px;
  text-align: center;
}

.diag-overlay {
  position: fixed;
  right: 12px;
  bottom: 12px;
  width: min(360px, calc(100vw - 24px));
  max-height: 60vh;
  overflow: auto;
  background: rgba(10,15,25,0.95);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  z-index: 999;
  display: none;
}
.diag-overlay.visible { display: block; }
.diag-overlay table { width: 100%; border-collapse: collapse; }
.diag-overlay td { padding: 4px 6px; border-bottom: 1px solid var(--border); }
.diag-overlay td.k { color: var(--muted); }
.diag-overlay .actions { margin-top: 10px; display: flex; gap: 8px; }
.diag-overlay button { font-size: 12px; padding: 6px 10px; min-height: auto; }

.subs {
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 8px;
  padding: 20px 0;
}
.subs .n { font-size: 48px; font-weight: 700; color: var(--accent); }
.subs .l { color: var(--muted); font-size: 15px; }

.listener-waveform {
  height: 60px;
  overflow: hidden;
}
.listener-waveform canvas { display: block; width: 100%; height: 100%; }
