:root {
  --bg: #0f1115;
  --surface: #171a21;
  --surface-2: #1f2430;
  --border: #2a3040;
  --text: #e8eaf0;
  --muted: #8b93a7;
  --accent: #4f8cff;
  --accent-press: #3a76e8;
  --success: #34c77b;
  --error: #ff6b6b;
  --radius: 14px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

body {
  max-width: 560px;
  margin: 0 auto;
  padding: 20px 16px calc(20px + env(safe-area-inset-bottom));
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 0.82rem; }
.error { color: var(--error); font-size: 0.9rem; }

/* Header */
.app-header { margin-bottom: 18px; }
.logo {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--accent);
}
.logo h1 {
  margin: 0;
  font-size: 1.35rem;
  letter-spacing: -0.02em;
  color: var(--text);
}
.tagline {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 0.88rem;
  line-height: 1.45;
}

/* Tabs */
.tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px;
  margin-bottom: 18px;
}
.tab {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 0.95rem;
  font-weight: 600;
  padding: 10px 0;
  border-radius: 10px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.tab.active {
  background: var(--accent);
  color: #fff;
}

/* Panels */
.panel { display: none; flex-direction: column; gap: 14px; }
.panel.active { display: flex; }

.field-label {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

textarea {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 1rem;
  line-height: 1.5;
  padding: 14px;
  resize: vertical;
  font-family: inherit;
}
textarea:focus {
  outline: none;
  border-color: var(--accent);
}

.send-meta {
  display: flex;
  justify-content: flex-end;
  font-size: 0.8rem;
  margin-top: -8px;
}

/* Method toggle (QR / Sound) */
.method-toggle {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(0, 1fr));
  gap: 8px;
}
.method {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 600;
  padding: 10px 8px;
  border-radius: 10px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
}
.method.active {
  border-color: var(--accent);
  color: var(--text);
  background: rgba(79, 140, 255, 0.12);
}
.method-icon { font-size: 1.05rem; }

/* Sound transmit */
.sound-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px 18px;
  background: var(--surface);
}
.sound-area p { margin: 0; text-align: center; line-height: 1.5; }

/* Emoji sending */
.emoji-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px 18px;
  background: var(--surface);
}
.emoji-area p { margin: 0; text-align: center; line-height: 1.5; }
.emoji-big {
  font-size: 84px;
  line-height: 1.2;
  user-select: all;
  cursor: pointer;
}

.sound-visual {
  display: flex;
  align-items: center;
  gap: 5px;
  height: 44px;
}
.sound-visual span {
  width: 5px;
  height: 8px;
  border-radius: 3px;
  background: var(--accent);
  opacity: 0.35;
}
.sound-visual.playing span {
  animation: eq 0.7s ease-in-out infinite alternate;
  opacity: 0.9;
}
.sound-visual.listening span { background: var(--success); }
.sound-visual span:nth-child(1)  { animation-delay: -0.10s; }
.sound-visual span:nth-child(2)  { animation-delay: -0.35s; }
.sound-visual span:nth-child(3)  { animation-delay: -0.60s; }
.sound-visual span:nth-child(4)  { animation-delay: -0.15s; }
.sound-visual span:nth-child(5)  { animation-delay: -0.50s; }
.sound-visual span:nth-child(6)  { animation-delay: -0.25s; }
.sound-visual span:nth-child(7)  { animation-delay: -0.65s; }
.sound-visual span:nth-child(8)  { animation-delay: -0.05s; }
.sound-visual span:nth-child(9)  { animation-delay: -0.45s; }
.sound-visual span:nth-child(10) { animation-delay: -0.30s; }
@keyframes eq {
  from { height: 8px; }
  to   { height: 38px; }
}

.speed-toggle {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  width: 100%;
  max-width: 320px;
}
.speed {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 600;
  padding: 8px 6px;
  border-radius: 10px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
}
.speed small {
  font-size: 0.68rem;
  font-weight: 500;
  opacity: 0.75;
}
.speed.active {
  border-color: var(--accent);
  color: var(--text);
  background: rgba(79, 140, 255, 0.12);
}

#listen-live.got-piece .listen-indicator {
  border-color: var(--success);
}

.listen-indicator {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 26px 18px;
  background: var(--surface);
}
.listen-indicator p { margin: 0; text-align: center; }

#listen-live {
  display: flex;
  flex-direction: column;
  gap: 14px;
  align-items: stretch;
}
#listen-live .ghost-btn { align-self: center; }

.method-toggle.sub {
  max-width: 360px;
  align-self: center;
  width: 100%;
}
.method-toggle.sub .method {
  font-size: 0.8rem;
  padding: 8px 6px;
}

/* Wi-Fi (WebRTC) panels */
.wifi-area {
  display: flex;
  flex-direction: column;
  gap: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px 18px;
  background: var(--surface);
}
.wifi-area > p { margin: 0; text-align: center; line-height: 1.5; }

.wifi-step {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}
.wifi-step h3 {
  margin: 0;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 8px;
  align-self: flex-start;
}
.step-num {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 0.75rem;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.wifi-step p { margin: 0; text-align: center; }

.qr-frame.small {
  padding: 12px;
}
.qr-frame.small img {
  width: min(60vw, 220px);
  height: min(60vw, 220px);
  image-rendering: pixelated;
}

.video-wrap.mini {
  width: 100%;
  max-width: 320px;
  aspect-ratio: 1 / 1;
}

.progress {
  width: 100%;
  height: 8px;
  background: var(--surface-2);
  border-radius: 999px;
  overflow: hidden;
}
.progress .bar {
  height: 100%;
  width: 0%;
  background: var(--accent);
  border-radius: 999px;
  transition: width 0.15s ease-out;
}

/* QR display */
.qr-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}
.qr-frame {
  background: #fff;
  padding: 16px;
  border-radius: var(--radius);
  line-height: 0;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.45);
}
#qr-img {
  width: min(78vw, 340px);
  height: min(78vw, 340px);
  image-rendering: pixelated;
}

.qr-controls {
  display: flex;
  align-items: center;
  gap: 14px;
}
.qr-progress {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 6px 14px;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}

.icon-btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  width: 40px;
  height: 40px;
  border-radius: 50%;
  font-size: 1.2rem;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
}
.icon-btn:hover { background: var(--surface-2); }
.qr-progress .icon-btn {
  width: 30px;
  height: 30px;
  font-size: 0.85rem;
  border: none;
  background: transparent;
}

/* Buttons */
.primary-btn {
  appearance: none;
  border: none;
  background: var(--accent);
  color: #fff;
  font-size: 1rem;
  font-weight: 600;
  padding: 13px 26px;
  border-radius: var(--radius);
  cursor: pointer;
  transition: background 0.15s;
}
.primary-btn:hover { background: var(--accent-press); }
.primary-btn.copied { background: var(--success); }

.ghost-btn {
  appearance: none;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.95rem;
  font-weight: 600;
  padding: 12px 24px;
  border-radius: var(--radius);
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
}
.ghost-btn:hover { color: var(--text); border-color: var(--muted); }

/* Empty states */
.empty-state {
  text-align: center;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  padding: 34px 22px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}
.empty-state p { margin: 0; line-height: 1.5; }

/* Scanner */
.video-wrap {
  position: relative;
  border-radius: var(--radius);
  overflow: hidden;
  background: #000;
  aspect-ratio: 3 / 4;
}
.video-wrap video {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.scan-box {
  position: absolute;
  inset: 12%;
  border: 2px solid rgba(255, 255, 255, 0.75);
  border-radius: 18px;
  box-shadow: 0 0 0 999px rgba(0, 0, 0, 0.35);
  pointer-events: none;
}
.scan-box.flash { border-color: var(--success); }

#scan-live {
  display: flex;
  flex-direction: column;
  gap: 14px;
  align-items: center;
}
#scan-live .ghost-btn { align-self: center; }

.scan-progress {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.scan-progress p { margin: 0; }
.chunk-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.chip {
  width: 26px;
  height: 26px;
  border-radius: 7px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.7rem;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-variant-numeric: tabular-nums;
}
.chip.got {
  background: var(--success);
  border-color: var(--success);
  color: #06301b;
}

/* Result */
#scan-result {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.result-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
}
.result-head h2 { margin: 0; font-size: 1.1rem; }
.result-actions {
  display: flex;
  gap: 10px;
}
.result-actions .primary-btn { flex: 1; }

/* File mode (encrypted cloud link) */
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
}
.file-area,
.file-recv {
  display: flex;
  flex-direction: column;
  gap: 14px;
  align-items: center;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px 18px;
  background: var(--surface);
}
.file-area > p,
.file-recv > p { margin: 0; text-align: center; line-height: 1.5; }
.file-recv h2 { margin: 0; font-size: 1.05rem; }
.file-drop {
  appearance: none;
  width: 100%;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--muted);
  font: inherit;
  padding: 26px 14px;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s;
  text-align: center;
  line-height: 1.5;
}
.file-drop:hover,
.file-drop.dragover { border-color: var(--accent); color: var(--text); }
.file-drop small { display: block; font-size: 0.8rem; }
.file-link-area {
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: center;
  width: 100%;
}
.file-link-row {
  display: flex;
  gap: 8px;
  width: 100%;
}
.file-link-row input {
  flex: 1;
  min-width: 0;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--muted);
  font-size: 0.8rem;
  padding: 9px 12px;
  outline: none;
}

/* Hotspot join-QR helper */
.hotspot-helper {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 10px 14px;
}
.hotspot-helper summary {
  cursor: pointer;
  font-size: 0.9rem;
  color: var(--muted);
  user-select: none;
}
.hotspot-helper summary:hover { color: var(--text); }
.hotspot-helper[open] summary { color: var(--text); margin-bottom: 10px; }
.hotspot-helper > *:not(summary) { margin: 10px 0 0; }
.hot-fields {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.hot-fields input {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font: inherit;
  font-size: 0.9rem;
  padding: 9px 12px;
  outline: none;
}
.hot-fields input:focus { border-color: var(--accent); }
.hotspot-helper .qr-frame.small { margin: 12px auto 0; }
@media (max-width: 480px) {
  .hot-fields { grid-template-columns: 1fr; }
}

/* Footer */
.app-footer {
  margin-top: auto;
  padding-top: 26px;
  text-align: center;
}
.badge {
  font-size: 0.75rem;
  color: var(--muted);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 5px 12px;
}
.badge.ok { color: var(--success); border-color: rgba(52, 199, 123, 0.4); }

/* Wide screens: fluid full-width layout, controls left / output right.
   Kept at the end of the file so it wins over the base component rules. */
@media (min-width: 940px) {
  body {
    max-width: none;
    padding: 32px 48px calc(24px + env(safe-area-inset-bottom));
  }
  .app-header {
    display: flex;
    align-items: baseline;
    gap: 18px;
    margin-bottom: 22px;
  }
  .tagline { margin: 0; }

  .panel.active {
    display: grid;
    grid-template-columns: minmax(360px, 500px) minmax(0, 1fr);
    gap: 14px 44px;
    align-items: start;
    align-content: start;
  }
  .panel.active > * { grid-column: 1; min-width: 0; }

  /* Send: output areas fill the right column */
  #panel-send.active > #qr-area,
  #panel-send.active > #sound-area,
  #panel-send.active > #emoji-area,
  #panel-send.active > #wifi-send-area,
  #panel-send.active > #file-area,
  #panel-send.active > #send-empty {
    grid-column: 2;
    grid-row: 1 / span 6;
  }

  /* Receive: progress and result fill the right column */
  #panel-receive.active > #recv-progress {
    grid-column: 2;
    grid-row: 1;
  }
  #panel-receive.active > #scan-result {
    grid-column: 2;
    grid-row: 2 / span 6;
  }

  #qr-img {
    width: min(38vw, 460px);
    height: min(38vw, 460px);
  }

  /* Keep helper paragraphs readable inside the wide right column */
  .qr-area p,
  .sound-area p,
  .emoji-area p,
  .wifi-area > p,
  .file-area > p,
  .file-recv > p,
  .empty-state p {
    max-width: 64ch;
    text-align: center;
    margin-inline: auto;
  }

  .empty-state { padding: 60px 24px; justify-content: center; }
}
