:root,
html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0d0f12;
  --bg-elev: #161a20;
  --bg-elev-2: #1d232b;
  --bg-topbar: rgba(13,15,18,0.85);
  --border: #262d36;
  --text: #e7edf3;
  --text-dim: #9aa6b2;
  --accent: #4ea1ff;
  --accent-2: #66d9a3;
  --danger: #ff6464;
  --warn: #ffb860;
  --shadow: 0 8px 24px rgba(0,0,0,0.35);
  --toggle-bg: #1d232b;
  --toggle-thumb: #e7edf3;
  --btn-hover: #232a33;
  --btn-primary-hover: #6db4ff;
  --radius: 12px;
  --space: 16px;
}

html[data-theme="light"] {
  color-scheme: light;
  --bg: #f4f6fa;
  --bg-elev: #ffffff;
  --bg-elev-2: #eef2f7;
  --bg-topbar: rgba(244,246,250,0.92);
  --border: #d8dee6;
  --text: #15181d;
  --text-dim: #5b6573;
  --accent: #2a73d6;
  --accent-2: #1e9b6a;
  --danger: #d83a3a;
  --warn: #c97a14;
  --shadow: 0 8px 24px rgba(20,30,50,0.08);
  --toggle-bg: #d8dee6;
  --toggle-thumb: #15181d;
  --btn-hover: #dde4ee;
  --btn-primary-hover: #1f5cb8;
}

* { 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 Neue",
    Arial, "Noto Sans", sans-serif;
  font-size: 15px;
  line-height: 1.45;
  min-height: 100vh;
  -webkit-tap-highlight-color: transparent;
}

a { color: var(--accent); text-decoration: none; word-break: break-all; }
a:hover { text-decoration: underline; }

.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 20px;
  background: var(--bg-topbar);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(8px);
}

.logo { display: flex; align-items: center; gap: 10px; font-weight: 700; }
.logo-icon {
  width: 22px;
  height: 22px;
  border-radius: 6px;
  display: block;
  transition: filter 0.25s ease, box-shadow 0.25s ease;
}
/* Tint the favicon slightly red while no session is bound, slightly green once bound. */
body[data-bound="false"] .logo-icon {
  filter: hue-rotate(140deg) saturate(0.85) brightness(1.05);
  box-shadow: 0 0 7px rgba(255, 100, 100, 0.55);
}
body[data-bound="true"] .logo-icon {
  filter: hue-rotate(-95deg) saturate(0.85) brightness(1.05);
  box-shadow: 0 0 7px rgba(102, 217, 163, 0.55);
}

.status { display: flex; align-items: center; gap: 12px; }
.timer { font-variant-numeric: tabular-nums; color: var(--text-dim); }

.pill {
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.02em;
  border: 1px solid var(--border);
}
.pill-wait { color: var(--warn); border-color: rgba(255,184,96,0.4); }
.pill-bound { color: var(--accent-2); border-color: rgba(102,217,163,0.4); }
.pill-ended { color: var(--danger); border-color: rgba(255,100,100,0.4); }
.pill-expiring { color: var(--warn); border-color: rgba(255,184,96,0.4); }

.layout {
  display: grid;
  grid-template-columns: 1fr;
  gap: var(--space);
  padding: var(--space);
  max-width: 460px;
  margin: 0 auto;
}

body[data-bound="true"] .layout {
  max-width: none;
  width: 70%;
  margin: 0 auto;
  padding: 8px 0;
}

/* State-driven visibility: only the relevant card is shown. */
body[data-bound="false"] #feed-card { display: none; }
body[data-bound="true"]  #qr-card  { display: none; }
body[data-bound="false"] .menu     { display: none; }

/* Messenger-like full-viewport layout when a session is active. */
body[data-bound="true"] {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  min-height: 0;
  overflow: hidden;
}
body[data-bound="true"] .topbar { flex: 0 0 auto; }
body[data-bound="true"] .layout {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}
body[data-bound="true"] #feed-card {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  max-width: none;
  display: flex;
  flex-direction: column;
  padding: 12px 16px;
}
body[data-bound="true"] .card {
  max-width: 100%;
}
body[data-bound="true"] .feed {
  flex: 1 1 auto;
  max-height: none;
  min-height: 0;
}
body[data-bound="true"] .composer { flex: 0 0 auto; }

.qr-card, .feed-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}

.qr-card h2, .feed-card h2 {
  margin: 0 0 8px;
  font-size: 18px;
  text-align: center;
}

.qr-hint { text-align: center; }

.hint { color: var(--text-dim); margin: 0 0 14px; font-size: 14px; }

.qr-box {
  background: #fff;
  border-radius: var(--radius);
  padding: 12px;
  display: flex; align-items: center; justify-content: center;
  aspect-ratio: 1;
  width: 100%;
  max-width: 320px;
  margin: 0 auto 14px;
}
.qr-box svg { width: 100%; height: auto; display: block; }

.qr-actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-bottom: 8px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center;
  gap: 6px;
  padding: 8px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-elev-2);
  color: var(--text);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
  user-select: none;
}
.btn:hover { background: var(--btn-hover); }
.btn:active { transform: translateY(1px); }
.btn[disabled] { opacity: 0.45; cursor: not-allowed; }
a.btn, a.btn:hover { text-decoration: none; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #ffffff; }
.btn-primary:hover { background: var(--btn-primary-hover); border-color: var(--btn-primary-hover); }
.btn-danger { background: transparent; border-color: rgba(255,100,100,0.5); color: var(--danger); }
.btn-danger:hover { background: rgba(255,100,100,0.08); }

.pin-box { margin-top: 8px; border-top: 1px dashed var(--border); padding-top: 8px; }
.pin-box summary { cursor: pointer; color: var(--text-dim); font-size: 13px; }
.pin-box form { display: flex; gap: 8px; margin-top: 8px; }
.pin-box input {
  flex: 1; min-width: 0;
  background: var(--bg-elev-2); border: 1px solid var(--border);
  color: var(--text); padding: 8px 10px; border-radius: 8px;
}

.feed-toolbar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px;
  margin-bottom: 12px; justify-content: space-between;
}
.feed-toolbar .actions { display: flex; flex-wrap: wrap; gap: 8px; }

.feed {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-height: 60vh;
  overflow-y: auto;
  scroll-behavior: smooth;
  padding-right: 4px;
}
.feed::-webkit-scrollbar { width: 8px; }
.feed::-webkit-scrollbar-thumb { background: var(--bg-elev-2); border-radius: 8px; }

.empty { color: var(--text-dim); text-align: center; padding: 30px 10px; font-size: 14px; }

.card {
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 6px 12px 5px;
  display: flex; flex-direction: column; gap: 0;
  max-width: min(95%, 900px);
  word-wrap: break-word;
}
.card.dir-in  { align-self: flex-start; border-bottom-left-radius: 4px; }
.card.dir-out {
  align-self: flex-end;
  border-bottom-right-radius: 4px;
  background: rgba(78,161,255,0.14);
  border-color: rgba(78,161,255,0.35);
}
html[data-theme="light"] .card.dir-out {
  background: rgba(42,115,214,0.12);
  border-color: rgba(42,115,214,0.35);
}

.card-body {
  word-wrap: break-word;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
  line-height: 1.35;
}
.card-link { font-weight: 500; }
.card-time {
  float: right;
  margin: 6px 0 0 10px;
  font-size: 10px;
  color: var(--text-dim);
  white-space: nowrap;
  user-select: none;
  line-height: 1;
  position: relative;
  bottom: -2px;
}

.file-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.file-icon {
  width: 36px; height: 36px; border-radius: 8px;
  background: rgba(78,161,255,0.12); color: var(--accent);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700;
}
.file-info { flex: 1; min-width: 0; }
.file-name {
  font-weight: 500;
  font-size: 13px;
  line-height: 1.3;
  word-break: break-all;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  line-clamp: 1;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}
.file-sub  {
  font-size: 11px;
  line-height: 1.3;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.file-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.file-thumb {
  background: var(--bg-elev-2);
  padding: 0;
  overflow: hidden;
  text-decoration: none;
}
.file-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

/* Image lightbox modal */
.img-modal { position: fixed; inset: 0; z-index: 80; display: flex; align-items: center; justify-content: center; }
.img-modal[hidden] { display: none; }
.img-modal-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,0.78); }
.img-modal-card {
  position: relative;
  max-width: min(96vw, 1200px);
  max-height: 92vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}
.img-modal-card img {
  max-width: 100%;
  max-height: 80vh;
  object-fit: contain;
  border-radius: 10px;
  box-shadow: 0 12px 50px rgba(0,0,0,0.6);
  background: #000;
}
.img-modal-close {
  position: absolute;
  top: -10px;
  right: -10px;
  width: 36px; height: 36px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--bg-elev);
  color: var(--text);
  font-size: 20px;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.dl-group { position: relative; display: inline-flex; }
.dl-group .dl-main {
  border-top-right-radius: 0;
  border-bottom-right-radius: 0;
  border-right: 0;
}
.dl-group .dl-more {
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
  padding: 8px 8px;
  min-width: 30px;
  font-size: 12px;
}
.dl-menu {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  min-width: 180px;
}

.composer {
  margin-top: 14px;
  display: flex; flex-direction: column; gap: 8px;
}
.composer textarea {
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 10px;
  padding: 10px 12px;
  resize: vertical;
  min-height: 56px;
  font-family: inherit; font-size: 14px;
}
.composer textarea:focus { outline: none; border-color: var(--accent); }
.composer-actions { display: flex; gap: 8px; justify-content: flex-end; }

.footer {
  text-align: center;
  color: var(--text-dim);
  font-size: 12px;
  padding: 20px;
}

.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%);
  background: var(--bg-elev-2); border: 1px solid var(--border);
  padding: 10px 14px; border-radius: 10px; font-size: 14px;
  box-shadow: 0 6px 20px rgba(0,0,0,0.4);
  opacity: 0; pointer-events: none; transition: opacity 0.2s;
  z-index: 100;
}
.toast.show { opacity: 1; }

progress { width: 100%; height: 6px; }

/* Theme toggle */
.theme-toggle {
  appearance: none;
  background: transparent;
  border: 0;
  padding: 0;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  -webkit-tap-highlight-color: transparent;
}
.theme-toggle:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; border-radius: 999px; }
.theme-toggle-track {
  position: relative;
  width: 52px;
  height: 28px;
  border-radius: 999px;
  background: var(--toggle-bg);
  border: 1px solid var(--border);
  display: inline-flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 6px;
  transition: background 0.2s;
}
.theme-toggle-icon {
  font-size: 13px;
  line-height: 1;
  color: var(--text-dim);
  user-select: none;
}
.theme-toggle-icon-sun { color: var(--warn); }
.theme-toggle-icon-moon { color: var(--accent); }
.theme-toggle-thumb {
  position: absolute;
  top: 50%;
  left: 2px;
  transform: translateY(-50%);
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--toggle-thumb);
  box-shadow: 0 2px 6px rgba(0,0,0,0.25);
  transition: left 0.2s ease, background 0.2s ease;
}
html[data-theme="light"] .theme-toggle-thumb { left: calc(100% - 24px); }

/* Topbar grouping (logo + small QR) */
.topbar-left { display: flex; align-items: center; gap: 12px; min-width: 0; }

/* Header QR thumbnail (visible only after binding) */
.header-qr {
  appearance: none;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 3px;
  width: 36px;
  height: 36px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: border-color 0.15s, transform 0.05s;
}
.header-qr:hover { border-color: var(--accent); }
.header-qr:active { transform: translateY(1px); }
.header-qr-svg { display: inline-flex; width: 100%; height: 100%; }
.header-qr-svg svg { width: 100%; height: 100%; display: block; }

/* Kebab dropdown */
.menu { position: relative; }
.menu-button {
  appearance: none;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
  width: 36px; height: 36px;
  border-radius: 10px;
  padding: 0;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  line-height: 1;
}
.menu-button:hover { background: var(--bg-elev-2); }
.menu-button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.menu-list {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  min-width: 200px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow);
  padding: 6px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  z-index: 20;
}
.menu-list[hidden] { display: none; }
.menu-item {
  appearance: none;
  background: transparent;
  border: 0;
  color: var(--text);
  text-align: left;
  font-size: 14px;
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-family: inherit;
}
.menu-item:hover { background: var(--bg-elev-2); }
.menu-item-danger { color: var(--danger); }
.menu-item-danger:hover { background: rgba(255,100,100,0.1); }

/* QR modal */
.qr-modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.qr-modal[hidden] { display: none; }
.qr-modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.55);
}
.qr-modal-card {
  position: relative;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 22px 20px 18px;
  max-width: 380px;
  width: 100%;
  box-shadow: var(--shadow);
}
.qr-modal-card h2 { margin: 0 0 12px; font-size: 16px; text-align: center; }
.qr-modal-card .hint { text-align: center; margin: 12px 0 0; }
.qr-modal-close {
  position: absolute;
  top: 6px; right: 6px;
  width: 32px; height: 32px;
  border-radius: 8px;
  background: transparent;
  border: 0;
  color: var(--text);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
}
.qr-modal-close:hover { background: var(--bg-elev-2); }
