:root {
  --bg: #f3f5f7;
  --card: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --primary: #0f766e;
  --primary-press: #0b5e58;
  --primary-soft: #d5f5ef;
  --danger: #dc2626;
  --ok: #16a34a;
  --warn: #d97706;
  --radius: 16px;
  --shadow: 0 1px 2px rgb(15 23 42 / 0.04), 0 4px 16px rgb(15 23 42 / 0.05);
  --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1114;
    --card: #141c21;
    --text: #e5e7eb;
    --muted: #94a3b8;
    --line: #243038;
    --primary: #14b8a6;
    --primary-press: #0f9a8b;
    --primary-soft: #12332f;
    --shadow: none;
  }
}

* {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

html,
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

body {
  min-height: 100dvh;
  overscroll-behavior-y: none;
}

body.has-actionbar main {
  padding-bottom: calc(150px + var(--safe-b));
}

button,
a,
label,
input {
  touch-action: manipulation;
}

main {
  max-width: 880px;
  margin: 0 auto;
  padding: 16px 16px 8px;
  display: grid;
  gap: 22px;
}

h2 {
  font-size: 15px;
  font-weight: 700;
  margin: 0;
}

h3 {
  margin: 0 0 4px;
  font-size: 17px;
  line-height: 1.35;
}

.muted {
  color: var(--muted);
}

.small {
  font-size: 13px;
}

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

/* ---------- top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--card) 88%, transparent);
  -webkit-backdrop-filter: saturate(1.6) blur(14px);
  backdrop-filter: saturate(1.6) blur(14px);
  border-bottom: 1px solid var(--line);
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 800;
  font-size: 17px;
  letter-spacing: -0.01em;
}

.status {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  background: var(--bg);
  border-radius: 999px;
  padding: 4px 10px;
}

.status .dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--warn);
}

.status[data-state="online"] .dot {
  background: var(--ok);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 25%, transparent);
}

.status[data-state="offline"] .dot {
  background: var(--danger);
}

/* ---------- me ---------- */
.me {
  appearance: none;
  width: 100%;
  font: inherit;
  color: inherit;
  text-align: left;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  cursor: pointer;
}

.me:active {
  background: var(--bg);
}

.me-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
  flex: 1;
}

.me-name {
  font-weight: 700;
  font-size: 16px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.me-edit {
  color: var(--primary);
  font-weight: 600;
  font-size: 14px;
}

.device-icon {
  flex: none;
  width: 44px;
  height: 44px;
  border-radius: 13px;
  display: grid;
  place-items: center;
  background: var(--primary-soft);
  color: var(--primary);
}

.device-icon svg {
  width: 24px;
  height: 24px;
}

/* ---------- peers ---------- */
.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 10px;
  min-height: 28px;
}

.count {
  color: var(--muted);
  font-weight: 600;
}

.room-badge {
  appearance: none;
  font: inherit;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 4px 10px;
  cursor: pointer;
}

.room-badge.active {
  color: var(--primary);
  border-color: var(--primary);
}

.tip {
  margin: -4px 0 10px;
}

.peers {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 12px;
}

.peer {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  text-align: center;
  padding: 18px 12px 12px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  transition: border-color 0.15s, transform 0.12s;
}

.peer:active {
  transform: scale(0.98);
}

.peer.dragover {
  border-color: var(--primary);
  transform: scale(1.03);
}

.peer .device-icon {
  width: 60px;
  height: 60px;
  border-radius: 18px;
}

.peer .device-icon svg {
  width: 32px;
  height: 32px;
}

.peer-name {
  font-weight: 700;
  line-height: 1.3;
  word-break: break-word;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.peer-mode {
  font-size: 12px;
  color: var(--muted);
}

.peer-mode .p2p {
  color: var(--ok);
  font-weight: 600;
}

.peer-actions {
  display: flex;
  gap: 8px;
  width: 100%;
}

.peer-actions .btn {
  flex: 1;
}

.peer-actions .btn.icon {
  flex: none;
}

.empty {
  text-align: center;
  padding: 28px 16px 22px;
  background: var(--card);
  border: 1px dashed var(--line);
  border-radius: var(--radius);
}

.empty p {
  margin: 4px 0;
}

.empty-title {
  font-weight: 700;
}

.empty .btn {
  margin-top: 12px;
}

.radar {
  position: relative;
  width: 72px;
  height: 72px;
  margin: 0 auto 14px;
}

.radar span {
  position: absolute;
  inset: 0;
  border: 2px solid var(--primary);
  border-radius: 50%;
  opacity: 0;
  animation: radar 2.4s infinite ease-out;
}

.radar span:nth-child(2) {
  animation-delay: 0.8s;
}

.radar span:nth-child(3) {
  animation-delay: 1.6s;
}

.radar i {
  position: absolute;
  inset: 30px;
  border-radius: 50%;
  background: var(--primary);
}

@keyframes radar {
  0% {
    transform: scale(0.2);
    opacity: 0.9;
  }
  100% {
    transform: scale(1);
    opacity: 0;
  }
}

/* ---------- lists ---------- */
.list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 8px;
}

.item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow);
  transition: border-color 0.15s, background 0.15s;
}

.item.selected {
  border-color: var(--primary);
  background: color-mix(in srgb, var(--primary-soft) 45%, var(--card));
}

.item-body {
  flex: 1;
  min-width: 0;
}

.item-title {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.item-sub {
  font-size: 12px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.item-sub.error {
  color: var(--danger);
  white-space: normal;
}

.item-sub .saved {
  color: var(--ok);
  font-weight: 600;
}

.item-actions {
  display: flex;
  gap: 6px;
  flex: none;
}

.pick {
  flex: none;
  width: 24px;
  height: 24px;
  margin: 0;
  accent-color: var(--primary);
}

.thumb {
  flex: none;
  position: relative;
  width: 52px;
  height: 52px;
  border-radius: 10px;
  overflow: hidden;
  background: var(--primary-soft);
  color: var(--primary);
  display: grid;
  place-items: center;
  font-size: 11px;
  font-weight: 800;
  border: 0;
  padding: 0;
  cursor: pointer;
}

.thumb img,
.thumb video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.thumb .play {
  position: absolute;
  inset: auto 4px 4px auto;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: rgb(0 0 0 / 0.55);
  display: grid;
  place-items: center;
}

.thumb .play::after {
  content: "";
  margin-left: 2px;
  border-left: 6px solid #fff;
  border-top: 4px solid transparent;
  border-bottom: 4px solid transparent;
}

.text-msg {
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 14px;
  margin-top: 2px;
  max-height: 7.5em;
  overflow: auto;
}

.bar {
  height: 6px;
  border-radius: 99px;
  background: var(--line);
  overflow: hidden;
  margin: 6px 0 3px;
}

.bar > span {
  display: block;
  height: 100%;
  width: 0;
  background: var(--primary);
  transition: width 0.15s linear;
}

.pct {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  font-size: 13px;
  color: var(--primary);
}

/* ---------- buttons, inputs ---------- */
.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--text);
  border-radius: 12px;
  min-height: 44px;
  padding: 0 16px;
  font: inherit;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: transform 0.1s, background 0.15s;
}

.btn.primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

.btn.primary:active {
  background: var(--primary-press);
}

.btn.ghost {
  background: transparent;
}

.btn.danger {
  color: var(--danger);
}

.btn.sm {
  min-height: 36px;
  padding: 0 12px;
  font-size: 14px;
  border-radius: 10px;
}

.btn.icon {
  width: 44px;
  padding: 0;
}

.btn.icon.sm {
  width: 36px;
}

.btn svg {
  width: 20px;
  height: 20px;
}

.btn:active {
  transform: scale(0.97);
}

.btn[hidden] {
  display: none;
}

.link {
  appearance: none;
  border: 0;
  background: none;
  color: var(--primary);
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  padding: 4px 0;
  cursor: pointer;
}

input:not([type="checkbox"]),
textarea {
  width: 100%;
  font: inherit;
  font-size: 16px; /* >= 16px để iOS không tự zoom khi focus */
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 11px 12px;
  outline: none;
}

input:not([type="checkbox"]):focus,
textarea:focus {
  border-color: var(--primary);
}

textarea {
  resize: vertical;
}

.row {
  display: flex;
  gap: 8px;
  margin: 12px 0;
}

.field {
  display: grid;
  gap: 6px;
  margin: 12px 0;
}

.check {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 12px;
  margin-top: 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg);
  cursor: pointer;
}

.check input {
  flex: none;
  width: 22px;
  height: 22px;
  margin: 1px 0 0;
  accent-color: var(--primary);
}

.check > span {
  display: grid;
  gap: 2px;
  font-size: 14px;
}

.check[hidden] {
  display: none;
}

/* ---------- dialogs ---------- */
dialog {
  width: min(460px, calc(100vw - 32px));
  max-height: calc(100dvh - 40px);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 20px;
  background: var(--card);
  color: var(--text);
  overflow: auto;
}

dialog::backdrop {
  background: rgb(0 0 0 / 0.45);
}

.sheet-handle {
  display: none;
}

.incoming-head {
  display: flex;
  gap: 12px;
  align-items: center;
  margin-bottom: 12px;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 16px;
}

.file-names {
  list-style: none;
  margin: 0;
  padding: 4px 12px;
  max-height: 34dvh;
  overflow: auto;
  font-size: 14px;
  background: var(--bg);
  border-radius: 12px;
}

.file-names li {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 7px 0;
  border-bottom: 1px solid var(--line);
}

.file-names li:last-child {
  border-bottom: 0;
}

.file-names li span:first-child {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.file-names li span:last-child {
  flex: none;
  color: var(--muted);
}

.room-current {
  display: grid;
  justify-items: center;
  gap: 6px;
  background: var(--primary-soft);
  border-radius: 14px;
  padding: 14px;
  margin: 12px 0;
}

.room-code {
  font-size: 30px;
  font-weight: 800;
  letter-spacing: 0.18em;
  color: var(--primary);
  font-variant-numeric: tabular-nums;
}

/* Điện thoại: hộp thoại thành bottom sheet, nút bấm full ngang. */
@media (max-width: 640px) {
  main {
    gap: 20px;
  }

  .peers {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  dialog.sheet {
    width: 100%;
    max-width: 100%;
    margin: auto 0 0;
    border-radius: 22px 22px 0 0;
    border-bottom: 0;
    padding: 8px 18px calc(18px + var(--safe-b));
    max-height: 92dvh;
  }

  dialog.sheet[open] {
    animation: sheet-up 0.22s ease-out;
  }

  .sheet-handle {
    display: block;
    width: 40px;
    height: 5px;
    border-radius: 99px;
    background: var(--line);
    margin: 0 auto 14px;
  }

  .sheet .actions {
    flex-wrap: nowrap;
  }

  .sheet .actions .btn {
    flex: 1;
    min-height: 50px;
  }

  #dlg-room .actions {
    flex-wrap: wrap;
  }

  #dlg-room .actions .btn {
    flex: 1 1 40%;
  }
}

@keyframes sheet-up {
  from {
    transform: translateY(100%);
  }
  to {
    transform: translateY(0);
  }
}

/* ---------- preview ---------- */
dialog.preview {
  width: 100vw;
  height: 100dvh;
  max-width: 100vw;
  max-height: 100dvh;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: #000;
  color: #fff;
  display: none;
  flex-direction: column;
}

dialog.preview[open] {
  display: flex;
}

.preview-top {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(8px + env(safe-area-inset-top)) 8px 8px 16px;
}

.preview-top .btn {
  color: #fff;
  border-color: transparent;
}

.preview-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 600;
}

.preview-body {
  flex: 1;
  min-height: 0;
  display: grid;
  place-items: center;
}

.preview-body img,
.preview-body video {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

.preview-actions {
  display: flex;
  gap: 8px;
  padding: 12px 16px calc(12px + var(--safe-b));
}

.preview-actions .btn {
  flex: 1;
  min-height: 50px;
}

.preview-actions .btn:not(.primary) {
  background: #1f2937;
  border-color: #374151;
  color: #fff;
}

/* ---------- action bar ---------- */
.actionbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 8;
  background: var(--card);
  border-top: 1px solid var(--line);
  box-shadow: 0 -6px 24px rgb(15 23 42 / 0.08);
  padding: 10px 16px calc(10px + var(--safe-b));
  animation: sheet-up 0.2s ease-out;
}

.actionbar[hidden] {
  display: none;
}

.actionbar-info,
.actionbar-buttons {
  max-width: 848px;
  margin: 0 auto;
}

.actionbar-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 8px;
}

.actionbar-buttons {
  display: flex;
  gap: 8px;
}

.actionbar-buttons .btn:not(.icon) {
  flex: 1;
}

/* ---------- toast, drop overlay ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + var(--safe-b));
  transform: translate(-50%, 20px);
  background: #0f172a;
  color: #fff;
  padding: 11px 16px;
  border-radius: 12px;
  font-size: 14px;
  font-weight: 500;
  opacity: 0;
  pointer-events: none;
  transition: 0.2s;
  width: max-content;
  max-width: calc(100vw - 32px);
  z-index: 30;
}

body.has-actionbar .toast {
  bottom: calc(130px + var(--safe-b));
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

.drop-overlay {
  position: fixed;
  inset: 0;
  display: none;
  place-items: center;
  font-weight: 700;
  color: var(--primary);
  background: color-mix(in srgb, var(--bg) 70%, transparent);
  border: 3px dashed var(--primary);
  pointer-events: none;
  z-index: 4;
}

body.dragging .drop-overlay {
  display: grid;
}

body.dragging .peer {
  z-index: 6;
}

footer {
  text-align: center;
  padding: 12px 16px calc(24px + var(--safe-b));
}

body.has-actionbar footer {
  display: none;
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation: none !important;
    transition: none !important;
  }
}

/* ---------- cài app, file chia sẻ chờ gửi ---------- */
.install {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 10px 12px 12px;
}

.install img {
  flex: none;
  border-radius: 11px;
}

.install-text {
  flex: 1;
  min-width: 0;
  display: grid;
  gap: 2px;
  line-height: 1.35;
}

.install[hidden],
.share-banner[hidden] {
  display: none;
}

.share-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px;
  margin-bottom: 12px;
  border-radius: 14px;
  background: var(--primary-soft);
  color: var(--primary);
  font-weight: 600;
  font-size: 14px;
}

/* Khi chạy như app đã cài: bỏ footer quảng bá, chừa vùng tai thỏ. */
@media (display-mode: standalone) {
  footer {
    display: none;
  }
}
