:root,
[data-theme="dark"] {
  --bg: #0f1115;
  --bg-elevated: #1a1d23;
  --bg-bubble-other: #1a1d23;
  --border: #2a2e36;
  --text: #e8e8e8;
  --text-secondary: #9a9a9a;
  --accent: #ff7a3c;
  --accent-text: #1a0d05;
  --danger: #ff5c5c;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

[data-theme="light"] {
  --bg: #f5f5f5;
  --bg-elevated: #ffffff;
  --bg-bubble-other: #ffffff;
  --border: #e2e2e2;
  --text: #1a1a1a;
  --text-secondary: #6b6b6b;
  --accent: #e8632a;
  --accent-text: #ffffff;
  --danger: #d23c3c;
}

* {
  box-sizing: border-box;
}

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

button,
input {
  font-family: inherit;
}

#app {
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.hidden {
  display: none !important;
}

/* ---------- Barra superior ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(12px + var(--safe-top)) 16px 12px;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.topbar h1 {
  font-size: 18px;
  margin: 0;
  font-weight: 700;
  color: var(--accent);
}

.icon-btn {
  background: none;
  border: none;
  color: var(--text);
  font-size: 22px;
  padding: 6px 10px;
  cursor: pointer;
  line-height: 1;
}

.icon-btn:focus-visible,
button:focus-visible,
input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ---------- Vistas ---------- */
.view {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  min-height: 0;
}

/* ---------- Login ---------- */
.login-screen {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
  gap: 16px;
  text-align: center;
}

.login-screen .app-name {
  font-size: 28px;
  font-weight: 800;
  color: var(--accent);
  margin: 0 0 8px;
}

.login-form {
  width: 100%;
  max-width: 320px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.field {
  width: 100%;
  padding: 14px 16px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text);
  font-size: 16px;
}

.btn-primary {
  width: 100%;
  padding: 14px 16px;
  border-radius: 12px;
  border: none;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
}

.btn-primary:disabled {
  opacity: 0.5;
}

.btn-secondary {
  width: 100%;
  padding: 12px 16px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  font-size: 15px;
  cursor: pointer;
}

.error-msg {
  color: var(--danger);
  font-size: 14px;
  min-height: 18px;
}

/* ---------- Lista de contactos ---------- */
.contact-item,
.conversation-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  background: none;
  border-left: none;
  border-right: none;
  border-top: none;
  width: 100%;
  text-align: left;
  color: var(--text);
}

.contact-item:active,
.conversation-item:active {
  background: var(--bg-elevated);
}

.avatar {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  color: #fff;
  flex-shrink: 0;
  font-size: 16px;
}

.contact-meta {
  flex: 1;
  min-width: 0;
}

.contact-name {
  font-weight: 600;
  font-size: 16px;
}

.contact-preview {
  font-size: 13px;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.empty-state {
  padding: 40px 24px;
  text-align: center;
  color: var(--text-secondary);
}

.unread-badge {
  min-width: 22px;
  height: 22px;
  padding: 0 6px;
  border-radius: 11px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 12px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

/* ---------- Chat ---------- */
.chat-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(10px + var(--safe-top)) 12px 10px;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.chat-header .contact-name {
  font-size: 16px;
}

.messages-list {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.bubble {
  max-width: 78%;
  padding: 10px 14px;
  border-radius: 16px;
  font-size: 15px;
  line-height: 1.35;
  word-wrap: break-word;
  white-space: pre-wrap;
}

.bubble.mine {
  align-self: flex-end;
  background: var(--accent);
  color: var(--accent-text);
  border-bottom-right-radius: 4px;
}

.bubble.theirs {
  align-self: flex-start;
  background: var(--bg-bubble-other);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}

.bubble-time {
  font-size: 11px;
  opacity: 0.7;
  margin-top: 4px;
  display: block;
}

.composer {
  display: flex;
  gap: 8px;
  padding: 10px;
  padding-bottom: calc(10px + var(--safe-bottom));
  background: var(--bg-elevated);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.composer textarea {
  flex: 1;
  resize: none;
  max-height: 100px;
  padding: 10px 14px;
  border-radius: 18px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.3;
}

.send-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 18px;
  cursor: pointer;
  flex-shrink: 0;
}

.send-btn:disabled {
  opacity: 0.5;
}

/* ---------- Configuración ---------- */
.settings-section {
  padding: 16px;
  border-bottom: 1px solid var(--border);
}

.settings-section h2 {
  font-size: 14px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-secondary);
  margin: 0 0 12px;
}

.theme-toggle {
  display: flex;
  gap: 8px;
}

.theme-option {
  flex: 1;
  padding: 12px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text);
  cursor: pointer;
  font-size: 14px;
}

.theme-option.active {
  border-color: var(--accent);
  color: var(--accent);
  font-weight: 700;
}

.banner {
  margin: 12px 16px 0;
  padding: 10px 14px;
  border-radius: 10px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  font-size: 14px;
  color: var(--text-secondary);
}
