:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --panel-2: #f9fafb;
  --line: #e3e6ea;
  --text: #1c2026;
  --muted: #6b7380;
  --accent: #2563eb;
  --accent-ink: #ffffff;
  --accent-soft: #e8efff;
  --mine: #2563eb;
  --mine-ink: #ffffff;
  --theirs: #eef0f3;
  --ok: #16a34a;
  --off: #9aa1ab;
  --warn: #d97706;
  --danger: #dc2626;
  --radius: 10px;
  --shadow: 0 12px 40px rgba(15, 23, 42, .18);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1216;
    --panel: #161a20;
    --panel-2: #1b2028;
    --line: #262c35;
    --text: #e6e9ee;
    --muted: #8b94a1;
    --accent: #4f8cff;
    --accent-soft: #1b2a45;
    --mine: #3b74e6;
    --theirs: #232932;
    --off: #5d6570;
    --shadow: 0 12px 40px rgba(0, 0, 0, .5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Ubuntu, Cantarell, "Noto Sans", sans-serif;
}
svg { fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.center { text-align: center; justify-content: center; }
code { font-size: 12px; background: var(--panel-2); padding: 1px 4px; border-radius: 4px; }

/* ---------- layout ---------- */
.app {
  display: grid;
  grid-template-columns: 260px 1fr;
  grid-template-rows: 52px 1fr;
  height: 100vh;
}
.topbar {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; white-space: nowrap; font-weight: 700; letter-spacing: .2px; color: var(--accent); }
.brand span { color: var(--text); }
.me {
  margin-left: 12px; display: flex; align-items: baseline; gap: 8px; min-width: 0;
  border: 1px solid transparent; background: transparent; color: var(--text); font: inherit;
  padding: 4px 8px; border-radius: 8px; cursor: pointer;
}
.me:hover { background: var(--panel-2); border-color: var(--line); }
.me-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.me-id { color: var(--muted); font: 12px ui-monospace, "Cascadia Mono", "DejaVu Sans Mono", monospace; white-space: nowrap; }
.top-actions { margin-left: auto; display: flex; align-items: center; gap: 8px; }

.conn { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); padding: 4px 10px; border-radius: 999px; background: var(--panel-2); border: 1px solid var(--line); }
.conn i { width: 8px; height: 8px; border-radius: 50%; background: var(--off); }
.conn[data-state="on"] i { background: var(--ok); }
.conn[data-state="connecting"] i { background: var(--warn); animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .3; } }

/* ---------- sidebar ---------- */
.sidebar { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.side-home {
  display: flex; align-items: center; gap: 8px; margin: 10px 8px 0; padding: 9px 10px;
  border: none; border-radius: 8px; background: transparent; color: var(--text);
  font: inherit; font-weight: 600; cursor: pointer; text-align: left;
}
.side-home:hover { background: var(--panel-2); }
.side-home.active { background: var(--accent-soft); color: var(--accent); }
.side-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 14px 8px; }
.side-head h2 { font-size: 12px; text-transform: uppercase; letter-spacing: .8px; color: var(--muted); margin: 0; }
.peers { list-style: none; margin: 0; padding: 4px 8px; overflow-y: auto; flex: 1; }
.peers:empty { flex: 0; padding: 0; }
.peer {
  display: grid;
  grid-template-columns: 10px 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  border-radius: 8px;
  cursor: pointer;
}
.peer:hover { background: var(--panel-2); }
.peer.active { background: var(--accent-soft); }
.peer .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--off); }
.peer.online .dot { background: var(--ok); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 20%, transparent); }
.peer .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.peer .sub { display: block; font-size: 12px; color: var(--muted); font-weight: 400; }
.badge { min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px; background: var(--accent); color: var(--accent-ink); font-size: 11px; font-weight: 700; display: grid; place-items: center; }
.side-empty { padding: 16px; text-align: center; }
.side-empty p { margin: 4px 0; }

/* ---------- chat ---------- */
.chat { display: flex; min-height: 0; min-width: 0; }

/* ---------- tela inicial ---------- */
.home { flex: 1; overflow-y: auto; padding: 32px 24px; display: flex; flex-direction: column; align-items: center; gap: 16px; }
.card { width: min(520px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 20px 22px; }
.card h3 { margin: 0 0 2px; font-size: 15px; }
.card p { margin: 0 0 12px; }
.small { font-size: 12.5px; }
.my-id { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 18px; border-radius: 10px; background: var(--accent-soft); margin-bottom: 8px; }
.my-id span { font: 700 34px/1 ui-monospace, "Cascadia Mono", "DejaVu Sans Mono", monospace; letter-spacing: 2px; color: var(--accent); user-select: all; }
.card .muted.small { margin: 0; }
.connect-row { display: flex; gap: 8px; }
.id-input {
  flex: 1; min-width: 0; font: 700 22px/1 ui-monospace, "Cascadia Mono", "DejaVu Sans Mono", monospace; letter-spacing: 2px;
  padding: 10px 14px; border-radius: 10px; border: 1px solid var(--line); background: var(--panel-2); color: var(--text); outline: none;
}
.id-input:focus { border-color: var(--accent); }
.connect-row .btn { padding: 0 20px; }
.connect-status { display: flex; align-items: center; gap: 10px; margin-top: 12px; padding: 10px 12px; border-radius: 10px; background: var(--panel-2); }
.connect-status #connect-text { flex: 1; }
.connect-status.error { background: color-mix(in srgb, var(--danger) 12%, transparent); color: var(--danger); }
.spinner { width: 16px; height: 16px; border-radius: 50%; border: 2px solid var(--line); border-top-color: var(--accent); animation: spin .8s linear infinite; flex: none; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- chat ---------- */
.chat-view { flex: 1; display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.chat-head { display: flex; align-items: center; gap: 14px; padding: 10px 18px; background: var(--panel); border-bottom: 1px solid var(--line); }
.chat-title { flex: 1; min-width: 0; }
.chat-head h2 { margin: 0; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.peer-status { font-size: 12px; color: var(--muted); }
.switch { display: inline-flex; align-items: center; gap: 8px; font-size: 12.5px; color: var(--muted); cursor: pointer; user-select: none; white-space: nowrap; }
.switch input { position: absolute; opacity: 0; pointer-events: none; }
.switch .track { width: 32px; height: 18px; border-radius: 9px; background: var(--line); position: relative; transition: background .15s; flex: none; }
.switch .thumb { position: absolute; top: 2px; left: 2px; width: 14px; height: 14px; border-radius: 50%; background: #fff; transition: transform .15s; box-shadow: 0 1px 2px rgba(0,0,0,.25); }
.switch input:checked + .track { background: var(--ok); }
.switch input:checked + .track .thumb { transform: translateX(14px); }
.switch input:focus-visible + .track { outline: 2px solid var(--accent); outline-offset: 2px; }

.messages { flex: 1; overflow-y: auto; padding: 18px; display: flex; flex-direction: column; gap: 4px; }
.day { align-self: center; font-size: 11px; color: var(--muted); background: var(--panel); border: 1px solid var(--line); border-radius: 999px; padding: 2px 10px; margin: 10px 0 6px; }
.msg { max-width: min(560px, 75%); padding: 7px 11px 5px; border-radius: 14px; background: var(--theirs); align-self: flex-start; white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere; }
.msg.mine { background: var(--mine); color: var(--mine-ink); align-self: flex-end; border-bottom-right-radius: 4px; }
.msg:not(.mine) { border-bottom-left-radius: 4px; }
.msg .meta { display: block; text-align: right; font-size: 10.5px; opacity: .7; margin-top: 2px; }
.msg.failed { background: color-mix(in srgb, var(--danger) 85%, black); }
.typing { padding: 0 18px 4px; font-size: 12px; color: var(--muted); font-style: italic; }

.composer { display: flex; align-items: flex-end; gap: 6px; padding: 10px 12px; background: var(--panel); border-top: 1px solid var(--line); }
.composer textarea {
  flex: 1; resize: none; max-height: 140px; min-height: 38px;
  padding: 9px 12px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--panel-2); color: var(--text); font: inherit; outline: none;
}
.composer textarea:focus { border-color: var(--accent); }

/* ---------- botões ---------- */
.btn { border: 1px solid transparent; background: var(--accent); color: var(--accent-ink); font: inherit; font-weight: 600; padding: 8px 14px; border-radius: 8px; cursor: pointer; }
.btn:hover { filter: brightness(1.08); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.small { padding: 5px 10px; font-size: 12.5px; }
.btn.ghost { background: transparent; color: var(--text); border-color: var(--line); }
.btn.ghost:hover { background: var(--panel-2); filter: none; }
.btn.danger { background: var(--danger); }
.danger-text { color: var(--danger) !important; }
.icon-btn { width: 36px; height: 36px; display: grid; place-items: center; border: none; border-radius: 8px; background: transparent; color: var(--muted); cursor: pointer; }
.icon-btn:hover:not(:disabled) { background: var(--panel-2); color: var(--text); }
.icon-btn:disabled { opacity: .4; cursor: not-allowed; }

/* ---------- modais ---------- */
dialog { border: none; padding: 0; border-radius: 14px; background: var(--panel); color: var(--text); box-shadow: var(--shadow); width: min(420px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(10, 14, 20, .45); }
.dlg { padding: 20px; display: flex; flex-direction: column; gap: 12px; }
.dlg h2 { margin: 0; font-size: 17px; }
.dlg p { margin: 0; }
.dlg label { display: flex; flex-direction: column; gap: 5px; font-weight: 600; font-size: 13px; }
.dlg input { font: inherit; font-weight: 400; padding: 8px 10px; border-radius: 8px; border: 1px solid var(--line); background: var(--panel-2); color: var(--text); outline: none; }
.dlg input:focus { border-color: var(--accent); }
.dlg small { font-weight: 400; }
.row { display: flex; gap: 8px; align-items: center; }
.row.end { justify-content: flex-end; }
.row.between { justify-content: space-between; }
.req-who { display: flex; flex-direction: column; gap: 2px; padding: 12px 14px; border-radius: 10px; background: var(--accent-soft); }
.req-who strong { font-size: 17px; }
.req-who .muted { font-family: ui-monospace, "Cascadia Mono", "DejaVu Sans Mono", monospace; }
details { border-top: 1px solid var(--line); padding-top: 10px; }
details summary { cursor: pointer; font-weight: 600; font-size: 13px; color: var(--muted); }
details[open] { display: flex; flex-direction: column; gap: 10px; }
details[open] > .btn { align-self: flex-start; }

/* ---------- avisos ---------- */
.toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 50; }
.toast { background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: 9px; box-shadow: var(--shadow); max-width: 320px; animation: in .18s ease-out; }
.toast.error { background: var(--danger); color: #fff; }
@keyframes in { from { transform: translateY(8px); opacity: 0; } }

@media (max-width: 720px) {
  .app { grid-template-columns: 1fr; grid-template-rows: 52px auto 1fr; }
  .sidebar { border-right: none; border-bottom: 1px solid var(--line); max-height: 34vh; }
  .me-name { display: none; }
  .brand span { display: none; }
  .topbar { gap: 8px; }
  .switch span:last-child { display: none; }
  .my-id span { font-size: 26px; }
}

/* ---------- arquivos no chat ---------- */
.icon-text { display: inline-flex; align-items: center; gap: 6px; }
.msg.has-file { min-width: 240px; }
.msg-file { display: flex; flex-direction: column; gap: 6px; }
.msg-text { margin-top: 4px; }
.file-thumb { display: block; max-width: 100%; max-height: 260px; border-radius: 9px; cursor: pointer; background: rgba(0,0,0,.08); }
.file-row { display: flex; align-items: center; gap: 10px; }
.file-icon {
  width: 38px; height: 44px; flex: none; border-radius: 6px; display: grid; place-items: center;
  background: rgba(0,0,0,.12); font: 700 10px/1 ui-monospace, monospace; text-transform: uppercase; letter-spacing: .5px;
}
.msg.mine .file-icon { background: rgba(255,255,255,.2); }
.file-info { flex: 1; min-width: 0; }
.file-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-sub { font-size: 12px; opacity: .8; }
.file-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.file-btn { padding: 4px 9px; font-size: 12px; }
.msg.mine .file-btn { color: inherit; border-color: rgba(255,255,255,.45); }
.msg.mine .file-btn:hover { background: rgba(255,255,255,.15); }
.file-expired { font-size: 12.5px; font-style: italic; opacity: .75; padding: 6px 0; }
.progress { height: 5px; border-radius: 3px; background: rgba(0,0,0,.15); overflow: hidden; }
.msg.mine .progress { background: rgba(255,255,255,.25); }
.progress i { display: block; height: 100%; width: 0; background: var(--accent); transition: width .2s; }
.msg.mine .progress i { background: #fff; }
.msg-file.is-audio audio { width: 260px; max-width: 100%; height: 40px; }

.drop-overlay {
  position: absolute; inset: 0; z-index: 5; display: grid; place-items: center; pointer-events: none;
  background: color-mix(in srgb, var(--accent) 12%, transparent); border: 3px dashed var(--accent); border-radius: 12px; margin: 8px;
}
.drop-overlay div { background: var(--panel); color: var(--accent); font-weight: 700; padding: 12px 20px; border-radius: 10px; box-shadow: var(--shadow); }
.chat-view { position: relative; }

/* gravação de áudio */
.recbar { flex: 1; display: flex; align-items: center; gap: 10px; min-height: 38px; padding: 0 12px; border-radius: 10px; background: var(--panel-2); border: 1px solid var(--line); }
.recbar .btn { margin-left: auto; }
.rec-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--danger); animation: pulse 1s infinite; }
.rec-time { font: 600 14px ui-monospace, monospace; }
.icon-btn.recording { color: var(--danger); background: color-mix(in srgb, var(--danger) 12%, transparent); }

/* ---------- navegador de arquivos remoto ---------- */
dialog.dlg-wide { width: min(980px, calc(100vw - 24px)); height: min(680px, calc(100vh - 24px)); }
.fb { display: flex; flex-direction: column; height: 100%; }
.fb-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 16px 10px; border-bottom: 1px solid var(--line); }
.fb-head h2 { margin: 0; font-size: 16px; }
.fb-toolbar { display: flex; align-items: center; gap: 6px; padding: 8px 12px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.fb-path { flex: 1; min-width: 160px; display: flex; align-items: center; gap: 2px; overflow-x: auto; white-space: nowrap; scrollbar-width: none; padding: 4px 6px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; }
.fb-crumb { border: none; background: transparent; color: var(--text); font: inherit; padding: 2px 6px; border-radius: 5px; cursor: pointer; }
.fb-crumb:hover:not(:disabled) { background: var(--accent-soft); color: var(--accent); }
.fb-sep { color: var(--muted); }
.fb-check { display: inline-flex; align-items: center; gap: 5px; font-size: 12.5px; color: var(--muted); cursor: pointer; white-space: nowrap; }
.fb-body { flex: 1; display: grid; grid-template-columns: 190px 1fr; min-height: 0; }
.fb-roots { border-right: 1px solid var(--line); padding: 8px; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.fb-root { text-align: left; border: none; background: transparent; color: var(--text); font: inherit; padding: 7px 10px; border-radius: 7px; cursor: pointer; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.fb-root:hover { background: var(--panel-2); }
.fb-root.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.fb-main { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.fb-list { flex: 1; overflow-y: auto; padding: 4px 6px; }
.fb-row {
  display: grid; grid-template-columns: 26px minmax(0, 1fr) 80px 130px auto; align-items: center; gap: 10px;
  padding: 6px 8px; border-radius: 7px; cursor: default;
}
.fb-row.dir { cursor: pointer; }
.fb-row:hover, .fb-row:focus-visible { background: var(--panel-2); outline: none; }
.fb-icon { color: var(--muted); display: grid; place-items: center; }
.fb-row.dir .fb-icon { color: var(--accent); }
.fb-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fb-size, .fb-date { font-size: 12px; color: var(--muted); text-align: right; white-space: nowrap; }
.fb-actions { display: flex; gap: 2px; opacity: 0; transition: opacity .1s; }
.fb-row:hover .fb-actions, .fb-row:focus-within .fb-actions { opacity: 1; }
.icon-btn.small { width: 28px; height: 28px; }
.fb-state { display: flex; align-items: center; justify-content: center; gap: 10px; padding: 14px; color: var(--muted); text-align: center; }
.fb-state.loading, .fb-state.error, .fb-state.empty { flex: 1; flex-direction: column; }
.fb-state.error { color: var(--danger); }
.fb-state.info { padding: 6px; font-size: 12px; }
.fb-state.hidden { display: none; }
.fb-list:empty + .fb-state.info { display: none; }
.fb-transfers { border-top: 1px solid var(--line); padding: 6px 12px; max-height: 190px; overflow-y: auto; display: flex; flex-direction: column; gap: 4px; }
.tr-row { display: flex; align-items: center; gap: 10px; padding: 4px 0; }
.tr-dir { width: 22px; height: 22px; border-radius: 50%; display: grid; place-items: center; background: var(--accent-soft); color: var(--accent); font-weight: 700; flex: none; }
.tr-info { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 3px; }
.tr-name { font-weight: 600; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tr-label { font-size: 12px; color: var(--muted); }
.tr-row.error .tr-label { color: var(--danger); }
.tr-row.done .tr-dir { background: color-mix(in srgb, var(--ok) 18%, transparent); color: var(--ok); }
.tr-actions { display: flex; gap: 4px; align-items: center; }

/* aviso: outro PC está acessando seus arquivos */
.fs-banner {
  position: fixed; left: 50%; top: 60px; transform: translateX(-50%); z-index: 40;
  display: flex; align-items: center; gap: 10px; max-width: calc(100vw - 24px);
  padding: 8px 10px 8px 14px; border-radius: 999px; background: var(--warn); color: #1a1205; font-weight: 600; box-shadow: var(--shadow);
}
.fs-banner span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fs-banner .btn { border-radius: 999px; }

@media (max-width: 720px) {
  .fb-body { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .fb-roots { flex-direction: row; overflow-x: auto; border-right: none; border-bottom: 1px solid var(--line); }
  .fb-row { grid-template-columns: 24px minmax(0, 1fr) auto; }
  .fb-size, .fb-date { display: none; }
  .fb-actions { opacity: 1; }
  #btn-files { padding: 5px 8px; }
  .msg.has-file { min-width: 0; }
}
