/* ================= Colores ================= */
:root {
  --frame-1: #0d47a1;
  --frame-2: #1e88e5;
  --frame-3: #64b5f6;
  --bar: #a9bcd0;
  --bar-text: #ffffff;
  --panel: #ffffff;
  --panel-alt: #f4f7fb;
  --line: #d5dee9;
  --thead: #dbe6f3;
  --thead-text: #1c3f66;
  --text: #1b1f24;
  --muted: #6b7785;
  --system: #6b7785;
  --btn: #f2f4f7;
  --btn-border: #b9c3cf;
  --btn-hover: #e3e9f1;
  --menu-1: #eef2f7;
  --menu-2: #d9e1ea;
  --accent: #1565c0;
  --ok: #2e9d3a;
  --danger: #d32f2f;
  --scroll: #9fb3c8;
}

body.night {
  --frame-1: #0a1f3d;
  --frame-2: #123563;
  --frame-3: #1b4b85;
  --bar: #24364d;
  --panel: #151b24;
  --panel-alt: #1b2330;
  --line: #2a3546;
  --thead: #1f2b3b;
  --thead-text: #a9c4e6;
  --text: #e3e8ef;
  --muted: #8b98a8;
  --system: #8b98a8;
  --btn: #1f2836;
  --btn-border: #36445a;
  --btn-hover: #2a3647;
  --menu-1: #1d2633;
  --menu-2: #18202b;
  --accent: #64a8ff;
  --scroll: #36445a;
}

/* ================= Base ================= */
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: Verdana, Tahoma, "Segoe UI", sans-serif;
  font-size: 13px;
  color: var(--text);
  overflow: hidden;
  background: var(--frame-1);
}
button { font: inherit; color: inherit; cursor: pointer; }

.app {
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: 0 8px 8px;
  background: linear-gradient(135deg, var(--frame-1) 0%, var(--frame-2) 55%, var(--frame-3) 100%);
}

/* ================= Encabezado ================= */
.top { padding: 6px 12px 8px; color: #fff; }
.top h1 {
  font-family: "Trebuchet MS", "Segoe UI", sans-serif;
  font-size: 28px; font-weight: 700; line-height: 1.1;
  text-shadow: 0 2px 4px rgba(0,0,0,.25);
}
.top p { font-size: 9.5px; letter-spacing: .25em; text-transform: uppercase; font-weight: 700; opacity: .9; }

/* ================= Marco ================= */
.frame {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 1fr 220px;
  gap: 4px;
}

/* ================= Chat ================= */
.chat {
  display: flex; flex-direction: column;
  min-width: 0; min-height: 0;
  background: var(--panel);
  border: 1px solid rgba(0,0,0,.15);
}

.roombar {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  background: var(--bar);
  color: var(--bar-text);
  padding: 6px 14px;
  font-size: 12px;
  white-space: nowrap;
}
.roombar > span:first-child { overflow: hidden; text-overflow: ellipsis; }
.roombar-right { display: flex; align-items: center; gap: 10px; }
.users-toggle {
  display: none;
  background: rgba(255,255,255,.2); border: 1px solid rgba(255,255,255,.5);
  color: #fff; border-radius: 3px; padding: 1px 8px; font-size: 14px;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 8px 12px;
  line-height: 1.65;
  overflow-wrap: anywhere;
}
.messages::-webkit-scrollbar { width: 14px; }
.messages::-webkit-scrollbar-track { background: var(--panel-alt); border-left: 1px solid var(--line); }
.messages::-webkit-scrollbar-thumb { background: var(--scroll); border: 3px solid var(--panel-alt); }

.line { padding: 1px 0; animation: fade .2s ease-out; }
.line .who { font-weight: 700; }
.line .sep { margin: 0 4px; }
.line.size-s .txt { font-size: 11.5px; }
.line.size-l .txt { font-size: 16px; }
.line audio { height: 30px; vertical-align: middle; max-width: min(300px, 70%); margin-left: 2px; }
.system { color: var(--system); font-style: italic; padding: 1px 0; }
.system.welcome { color: var(--accent); }

.typing { height: 18px; padding: 0 12px; font-size: 11px; color: var(--muted); font-style: italic; }

@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

/* ================= Barra inferior ================= */
.toolbar {
  position: relative;
  display: flex; align-items: center; gap: 8px;
  padding: 8px 10px;
  background: var(--panel-alt);
  border-top: 1px solid var(--line);
}
.fmt { display: flex; border: 1px solid var(--btn-border); background: var(--btn); }
.fmt-btn {
  width: 26px; height: 26px; border: none; background: transparent;
  font-size: 12px; position: relative;
}
.fmt-btn + .fmt-btn { border-left: 1px solid var(--btn-border); }
.fmt-btn:hover { background: var(--btn-hover); }
.fmt-btn.on { background: var(--accent); color: #fff; }
.color-btn { font-weight: 700; }
#colorBar { position: absolute; left: 5px; right: 5px; bottom: 3px; height: 3px; background: var(--text); }

.plain-btn {
  border: none; background: none;
  font-weight: 700; padding: 2px 4px;
  display: flex; align-items: center;
}
.plain-btn small { font-size: 9px; margin-left: 2px; }
.emoji-btn { font-size: 24px; line-height: 1; }
.mic-btn svg { width: 24px; height: 24px; fill: var(--muted); }
.mic-btn:hover svg { fill: var(--accent); }
.mic-btn.active svg { fill: var(--danger); animation: blink 1s infinite; }

.input-wrap { flex: 1; position: relative; min-width: 0; }
.input-wrap input {
  width: 100%; height: 30px;
  border: 1px solid var(--btn-border);
  background: var(--panel);
  color: var(--text);
  padding: 0 10px;
  font: inherit; font-size: 13px;
  outline: none;
}
.input-wrap input:focus { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(21,101,192,.15); }
.recording {
  position: absolute; inset: 0;
  display: flex; align-items: center; gap: 8px;
  padding: 0 10px; font-size: 12px;
  background: var(--panel); border: 1px solid var(--danger);
}
.recording[hidden] { display: none; }
.rec-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--danger); animation: blink 1s infinite; }
.rec-label { color: var(--muted); flex: 1; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.link-btn { border: none; background: none; color: var(--danger); font-weight: 700; font-size: 12px; }
@keyframes blink { 50% { opacity: .35; } }

.send-btn {
  height: 30px; padding: 0 16px;
  border: 1px solid var(--btn-border);
  background: linear-gradient(var(--panel), var(--btn));
  font-weight: 700; font-size: 11.5px; letter-spacing: .03em;
}
.send-btn:hover { background: var(--btn-hover); }

/* Menús emergentes */
.pop {
  position: absolute; bottom: 46px; z-index: 30;
  background: var(--panel); border: 1px solid var(--btn-border);
  box-shadow: 0 6px 20px rgba(0,0,0,.2);
  padding: 6px;
}
.pop[hidden] { display: none; }
.color-pop { left: 10px; display: grid; grid-template-columns: repeat(6, 22px); gap: 4px; }
.color-pop button { width: 22px; height: 22px; border: 1px solid rgba(0,0,0,.2); }
.color-pop button.none { grid-column: span 6; width: auto; height: 22px; font-size: 11px; background: var(--btn); }
.color-pop button.sel { outline: 2px solid var(--accent); outline-offset: 1px; }
.size-pop { left: 120px; display: flex; flex-direction: column; min-width: 100px; padding: 4px 0; }
.size-pop button { border: none; background: none; text-align: left; padding: 5px 12px; }
.size-pop button:hover, .size-pop button.sel { background: var(--btn-hover); }
.size-pop [data-size="s"] { font-size: 11px; }
.size-pop [data-size="l"] { font-size: 15px; }
.emoji-pop { left: 150px; display: grid; grid-template-columns: repeat(8, 32px); gap: 2px; max-width: calc(100vw - 40px); }
.emoji-pop button { border: none; background: none; font-size: 20px; height: 32px; border-radius: 4px; }
.emoji-pop button:hover { background: var(--btn-hover); }

/* ================= Lado derecho ================= */
.side {
  display: flex; flex-direction: column;
  min-height: 0;
  background: var(--panel);
  border: 1px solid rgba(0,0,0,.15);
}
.tabs { display: flex; gap: 3px; padding: 5px 5px 0; background: var(--panel-alt); }
.tab {
  flex: 1; padding: 6px 0;
  border: 1px solid var(--line); border-bottom: none;
  border-radius: 6px 6px 0 0;
  background: var(--btn-hover);
  font-weight: 700; color: var(--muted);
}
.tab.active { background: var(--panel); color: var(--text); }
.tab-panel { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.tab-panel[hidden] { display: none; }
.thead {
  display: grid; grid-template-columns: 1fr 82px;
  background: var(--thead); color: var(--thead-text);
  font-weight: 700; font-size: 11.5px;
}
.thead span { padding: 4px 8px; }
.thead span + span { border-left: 1px solid var(--line); }
.rows { list-style: none; overflow-y: auto; flex: 1; }
.rows li {
  display: grid; grid-template-columns: 1fr 82px; align-items: center;
  border-bottom: 1px solid var(--line);
  min-height: 27px;
}
.rows li:hover { background: var(--panel-alt); }
.rows .cell-name { display: flex; align-items: center; gap: 6px; padding: 3px 8px; min-width: 0; }
.rows .cell-name span:last-child { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.rows .smile { font-size: 14px; }
.rows li.me .cell-name { font-weight: 700; }
.status { display: flex; align-items: center; gap: 4px; padding: 3px 6px; font-size: 10px; color: var(--muted); }
.status svg { width: 14px; height: 14px; fill: var(--ok); flex-shrink: 0; }

#roomList li { cursor: pointer; }
#roomList li.current { background: var(--thead); font-weight: 700; cursor: default; }
#roomList .room-count { padding: 3px 8px; font-size: 11px; color: var(--muted); text-align: center; }

.menu { border-top: 1px solid var(--line); }
.menu button {
  width: 100%;
  display: flex; align-items: center; gap: 8px;
  padding: 6px 10px;
  border: none; border-bottom: 1px solid var(--btn-border);
  background: linear-gradient(var(--menu-1), var(--menu-2));
  font-size: 12px; text-align: left;
}
.menu button:hover { filter: brightness(.96); }
.menu .mi { width: 16px; text-align: center; font-size: 13px; }

.backdrop { display: none; }

/* ================= Ventanas ================= */
.modal {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(5, 20, 45, .55);
  display: grid; place-items: center;
  padding: 16px;
}
.modal[hidden] { display: none; }
.dialog {
  width: min(380px, 100%);
  background: var(--panel);
  border: 1px solid var(--btn-border);
  box-shadow: 0 20px 50px rgba(0,0,0,.35);
}
.dialog h3 {
  background: linear-gradient(90deg, var(--frame-1), var(--frame-2));
  color: #fff; font-size: 14px; padding: 9px 14px;
}
.dialog label { display: block; padding: 14px 14px 0; font-weight: 700; font-size: 12px; }
.dialog label input {
  display: block; width: 100%; margin-top: 6px;
  height: 32px; padding: 0 10px;
  border: 1px solid var(--btn-border); background: var(--panel); color: var(--text);
  font: inherit; font-weight: 400; outline: none;
}
.dialog label input:focus { border-color: var(--accent); }
.hint { padding: 6px 14px 0; font-size: 11px; color: var(--muted); }
.error { padding: 6px 14px 0; font-size: 12px; color: var(--danger); min-height: 18px; }
.profile-info { margin: 12px 14px 0; border: 1px solid var(--line); }
.profile-info div { display: flex; justify-content: space-between; gap: 10px; padding: 6px 10px; font-size: 12px; }
.profile-info div + div { border-top: 1px solid var(--line); }
.profile-info span { color: var(--muted); }
.actions { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 14px 14px; }
.btn {
  height: 30px; padding: 0 16px;
  border: 1px solid var(--accent); background: var(--accent); color: #fff;
  font-weight: 700; font-size: 12px;
}
.btn.ghost { background: var(--btn); color: var(--text); border-color: var(--btn-border); }

/* ================= Pantallas chicas ================= */
@media (max-width: 760px) {
  .app { padding: 0 4px 4px; }
  .top { padding: 4px 8px 6px; }
  .top h1 { font-size: 22px; }
  .frame { grid-template-columns: 1fr; }
  .users-toggle { display: block; }
  .side {
    position: fixed; top: 0; right: 0; bottom: 0; z-index: 40;
    width: min(260px, 85vw);
    transform: translateX(100%);
    transition: transform .25s ease;
    box-shadow: -8px 0 24px rgba(0,0,0,.35);
  }
  .side.open { transform: none; }
  .backdrop {
    display: block; position: fixed; inset: 0; z-index: 35;
    background: rgba(0,0,0,.45);
    opacity: 0; pointer-events: none; transition: opacity .25s;
  }
  .backdrop.show { opacity: 1; pointer-events: auto; }
}

@media (max-width: 520px) {
  .top p { display: none; }
  .roombar { padding: 5px 8px; font-size: 11px; }
  #roomCountLabel { display: none; }
  .me-label { max-width: 130px; overflow: hidden; text-overflow: ellipsis; }
  .toolbar { flex-wrap: wrap; gap: 6px; padding: 6px; }
  .input-wrap { order: 10; flex: 1 0 calc(100% - 130px); }
  .mic-btn { order: 11; }
  .send-btn { order: 12; padding: 0 10px; }
  .size-pop { left: 110px; }
  .emoji-pop { left: 6px; grid-template-columns: repeat(7, 32px); }
  .pop { bottom: 80px; }
}
