:root {
  --bg: #ffffff;
  --surface: #f5f6f8;
  --panel: #ffffff;
  --border: #e2e4e9;
  --text: #14161a;
  --muted: #6b7280;
  --accent: #3b82f6;
  --success: #22c55e;
  --danger: #ef4444;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115;
    --surface: #171a21;
    --panel: #1b1e26;
    --border: #2a2f3a;
    --text: #e6e8eb;
    --muted: #8b93a1;
    --accent: #4f8cff;
    --success: #34d399;
    --danger: #f87171;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
  }
}

:root[data-theme="dark"] {
  --bg: #0f1115;
  --surface: #171a21;
  --panel: #1b1e26;
  --border: #2a2f3a;
  --text: #e6e8eb;
  --muted: #8b93a1;
  --accent: #4f8cff;
  --success: #34d399;
  --danger: #f87171;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}

.icon {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
  display: block;
}

.hidden { display: none !important; }

/* ---------------- Top bar ---------------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  padding-top: calc(12px + env(safe-area-inset-top));
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-bottom: 1px solid var(--border);
}

.topbar-brand { display: flex; align-items: center; gap: 10px; }
.brand-mark {
  width: 30px; height: 30px; border-radius: 8px;
  background: var(--accent); color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 0.95rem;
}
.topbar-title { font-weight: 700; font-size: 1.05rem; }
.topbar-actions { display: flex; gap: 4px; }

.icon-btn {
  width: 38px; height: 38px; border-radius: 10px;
  background: transparent; border: none; color: var(--text);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
}
.icon-btn:active { background: var(--border); }

.icon-moon { display: none; }
:root[data-theme="light"] .icon-sun { display: none; }
:root[data-theme="light"] .icon-moon { display: block; }
.moon-cutout { fill: var(--surface); }

/* ---------------- Main / tab panels ---------------- */

.app-main {
  padding: 16px;
  padding-bottom: calc(84px + env(safe-area-inset-bottom));
  max-width: 720px;
  margin: 0 auto;
}
.tab-panel { display: none; }
.tab-panel.active { display: block; }
.hint { color: var(--muted); font-size: 0.85rem; }

/* ---------------- Buttons ---------------- */

.btn-primary, .btn-secondary {
  border: none;
  border-radius: 10px;
  padding: 10px 16px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-secondary { background: var(--border); color: var(--text); }
.btn-primary:active, .btn-secondary:active { opacity: 0.75; }
.full { width: 100%; display: block; margin-top: 8px; }

/* ---------------- Peer list ---------------- */

.peer-list { list-style: none; padding: 0; margin: 12px 0; display: flex; flex-direction: column; gap: 8px; }
.peer-list li {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: var(--shadow);
}
.peer-list li.hint { box-shadow: none; border-style: dashed; }
.peer-list button {
  background: var(--accent);
  border: none;
  color: #fff;
  padding: 7px 14px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 600;
  font-size: 0.85rem;
}

/* ---------------- Room / QR ---------------- */

.room-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
@media (max-width: 700px) { .room-grid { grid-template-columns: 1fr; } }
.room-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 16px;
  box-shadow: var(--shadow);
}
.room-card h3 { margin-top: 0; }
.room-card input {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 9px 10px;
  border-radius: 8px;
}
.join-row { display: flex; gap: 8px; }
.join-row input { flex: 1; min-width: 0; }
.room-code {
  font-size: 1.6rem;
  letter-spacing: 4px;
  font-weight: 700;
  margin: 10px 0;
}
#roomQr { background: #fff; padding: 8px; border-radius: 8px; }
#qrVideo { width: 100%; max-width: 320px; margin-top: 10px; border-radius: 10px; }

/* ---------------- Bottom nav ---------------- */

.bottomnav {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 30;
  display: flex;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.bottomnav-item {
  flex: 1;
  background: none;
  border: none;
  color: var(--muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 0 6px;
  font-size: 0.72rem;
  cursor: pointer;
  position: relative;
}
.bottomnav-item.active { color: var(--accent); }
.bn-badge {
  position: absolute;
  top: 2px;
  right: calc(50% - 26px);
  background: var(--danger);
  color: #fff;
  font-size: 0.65rem;
  font-weight: 700;
  min-width: 16px;
  height: 16px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 4px;
}

/* ---------------- Settings sheet ---------------- */

.sheet { position: fixed; inset: 0; z-index: 50; display: flex; align-items: flex-end; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, 0.5); }
.sheet-box {
  position: relative;
  width: 100%;
  background: var(--panel);
  border-radius: 18px 18px 0 0;
  padding: 10px 20px calc(20px + env(safe-area-inset-bottom));
  max-width: 480px;
  margin: 0 auto;
}
.sheet-handle { width: 36px; height: 4px; border-radius: 2px; background: var(--border); margin: 4px auto 12px; }
.sheet-box h3 { margin: 0 0 12px; }
.field-label { display: block; font-size: 0.8rem; color: var(--muted); margin-bottom: 4px; }
.sheet-box input#nickname {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 12px;
  border-radius: 10px;
  margin-bottom: 16px;
}
.sheet-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
  border-top: 1px solid var(--border);
  font-size: 0.95rem;
}

.switch-toggle { position: relative; display: inline-block; }
.switch-toggle input { position: absolute; opacity: 0; width: 44px; height: 26px; margin: 0; cursor: pointer; }
.switch-track {
  display: block;
  width: 44px; height: 26px;
  border-radius: 13px;
  background: var(--border);
  transition: background 0.15s;
}
.switch-thumb {
  display: block;
  width: 22px; height: 22px;
  border-radius: 50%;
  background: #fff;
  margin: 2px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  transition: transform 0.15s;
}
.switch-toggle input:checked ~ .switch-track { background: var(--success); }
.switch-toggle input:checked ~ .switch-track .switch-thumb { transform: translateX(18px); }

.debug-row { border-bottom: none; }
.debug-panel { margin-bottom: 12px; }
.debug-log-box {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 10px;
  max-height: 180px;
  overflow-y: auto;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.68rem;
  line-height: 1.5;
  color: var(--muted);
  white-space: pre-wrap;
  word-break: break-word;
}
.debug-log-box .debug-line { color: var(--text); }
.debug-actions { display: flex; gap: 8px; margin-top: 8px; }
.debug-actions button { flex: 1; font-size: 0.8rem; padding: 8px; }

/* ---------------- Incoming invite modal ---------------- */

.modal {
  position: fixed; inset: 0; background: rgba(0, 0, 0, 0.55);
  display: flex; align-items: center; justify-content: center; z-index: 60;
  padding: 20px;
}
.modal-box {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 16px; padding: 20px; max-width: 380px; width: 100%; text-align: center;
  box-shadow: var(--shadow);
}
.modal-actions { display: flex; gap: 10px; margin-top: 14px; }
.modal-actions button { flex: 1; }

.chip-row {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 4px 0 8px;
  -webkit-overflow-scrolling: touch;
}
.chip-thumb-only {
  flex: 0 0 auto;
  width: 56px; height: 56px;
  border-radius: 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  display: flex; align-items: center; justify-content: center;
  overflow: hidden;
  position: relative;
}
.chip-thumb-only img { width: 100%; height: 100%; object-fit: cover; }
.chip-thumb-only .icon.file-icon { width: 24px; height: 24px; color: var(--muted); }
.chip-ext {
  position: absolute; bottom: 2px; left: 2px; right: 2px;
  font-size: 0.55rem; font-weight: 700; text-align: center;
  background: rgba(0, 0, 0, 0.55); color: #fff; border-radius: 4px; padding: 1px 0;
}

/* ---------------- Transfers tab ---------------- */

.transfer-list { display: flex; flex-direction: column; gap: 12px; }
.transfer-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 14px;
  box-shadow: var(--shadow);
}
.transfer-card.done { border-color: var(--success); }
.transfer-card.errored { border-color: var(--danger); }

.transfer-card-head {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
}
.transfer-dir .icon { width: 18px; height: 18px; color: var(--accent); }
.transfer-card.done .transfer-dir .icon { color: var(--success); }
.transfer-card.errored .transfer-dir .icon { color: var(--danger); }
.transfer-status { font-size: 0.88rem; font-weight: 600; flex: 1; }
.transfer-card.errored .transfer-status { color: var(--danger); }
.transfer-card.done .transfer-status { color: var(--success); }

.retry-btn {
  background: var(--accent); color: #fff; border: none;
  border-radius: 8px; padding: 5px 12px; font-size: 0.8rem; font-weight: 600; cursor: pointer;
}

.transfer-card .chip-row { overflow-x: auto; }

.file-chip {
  flex: 0 0 auto;
  width: 92px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.file-thumb {
  width: 92px; height: 92px;
  border-radius: 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  display: flex; align-items: center; justify-content: center;
  overflow: hidden;
  position: relative;
}
.file-thumb img { width: 100%; height: 100%; object-fit: cover; }
.file-thumb .file-icon { width: 30px; height: 30px; color: var(--muted); }
.file-thumb .file-ext {
  position: absolute; bottom: 3px; left: 3px; right: 3px;
  font-size: 0.6rem; font-weight: 700; text-align: center;
  background: rgba(0, 0, 0, 0.55); color: #fff; border-radius: 4px; padding: 1px 0;
}
.file-meta { display: flex; flex-direction: column; gap: 4px; }
.file-name {
  font-size: 0.72rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.file-progress-track { background: var(--border); border-radius: 4px; height: 4px; overflow: hidden; }
.file-progress-fill { background: var(--accent); height: 100%; width: 0%; transition: width 0.1s linear; }
.file-chip.done .file-progress-fill { background: var(--success); width: 100% !important; }
.file-chip.failed .file-progress-fill { background: var(--danger); }
.file-chip.queued .file-thumb { opacity: 0.6; }
