:root {
  --bg: #0e1621;
  --panel: #17212b;
  --panel2: #242f3d;
  --border: #2b3945;
  --text: #f5f5f5;
  --muted: #8b98a5;
  --accent: #2aabee;
  --bubble-out: #2b5278;
  --bubble-in: #182533;
  --chat-bg: #0e1621;
  --online: #4dcd5e;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #ffffff;
    --panel: #ffffff;
    --panel2: #f0f2f5;
    --border: #e6e6e6;
    --text: #000000;
    --muted: #707579;
    --accent: #2481cc;
    --bubble-out: #effdde;
    --bubble-in: #ffffff;
    --chat-bg: #8daeca;
  }
}

*, *::before, *::after { box-sizing: border-box; }

[hidden] { display: none !important; }

html { height: 100%; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

body.app-auth {
  height: 100%;
  overflow: hidden;
}

body.app-guest {
  min-height: 100%;
  overflow: auto;
}

/* Layout */
.app-shell {
  display: flex;
  height: 100dvh;
  max-height: 100dvh;
}

.sidebar {
  width: 320px;
  min-width: 260px;
  max-width: 38vw;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}

.sidebar-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.75rem;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.sidebar-nav {
  display: flex;
  gap: 0.5rem;
  margin-left: auto;
}

.sidebar-nav a {
  color: var(--accent);
  text-decoration: none;
  font-size: 0.85rem;
}

.sidebar-nav a.active { font-weight: 700; }

.link-btn {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 0.85rem;
  padding: 0;
}

.logout-form { margin: 0; }

.sidebar-list {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.sidebar-empty { padding: 1rem; font-size: 0.9rem; }

.sidebar-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.65rem 0.75rem;
  text-decoration: none;
  color: inherit;
  border-bottom: 1px solid var(--border);
}

.sidebar-item:hover,
.sidebar-item.active { background: var(--panel2); }

.sidebar-item-text { min-width: 0; flex: 1; }

.sidebar-item-name {
  display: block;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar-item-sub {
  display: block;
  font-size: 0.8rem;
  color: var(--muted);
}

.avatar {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  flex-shrink: 0;
}

.avatar.sm { width: 44px; height: 44px; font-size: 0.95rem; }

.main {
  flex: 1;
  min-width: 0;
  overflow-y: auto;
  background: var(--bg);
}

.main-chat {
  overflow: hidden;
  display: flex;
  flex-direction: column;
  padding: 0;
}

.main-guest {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
  padding: 1rem;
}

.login-card {
  width: 100%;
  max-width: 400px;
  padding: 1.25rem;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
}

.page-content { padding: 1rem; max-width: 720px; }

.page-back {
  display: none;
  color: var(--accent);
  text-decoration: none;
  font-size: 0.95rem;
  margin-bottom: 0.75rem;
}

.mobile-only { display: none; }

.chat-placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--muted);
}

/* Chat */
.chat-page {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
  background: var(--chat-bg);
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.6rem 0.75rem;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.chat-back {
  color: var(--accent);
  text-decoration: none;
  font-size: 1.4rem;
  line-height: 1;
  padding: 0.2rem 0.4rem;
  display: none;
}

.chat-header-main {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  min-width: 0;
}

.chat-title {
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-sub {
  font-size: 0.8rem;
  color: var(--muted);
}

.chat-messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 0.75rem;
}

.messages {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.message-row { display: flex; width: 100%; }
.message-row-user { justify-content: flex-end; }
.message-row-bot { justify-content: flex-start; }
.message-row-system { justify-content: center; }

.bubble {
  max-width: min(88%, 480px);
  padding: 0.5rem 0.65rem 0.35rem;
  border-radius: 14px;
  word-break: break-word;
}

.message-row-user .bubble {
  background: var(--bubble-out);
  border-bottom-right-radius: 4px;
}

.message-row-bot .bubble {
  background: var(--bubble-in);
  border-bottom-left-radius: 4px;
}

.message-body {
  margin: 0;
  white-space: pre-wrap;
  line-height: 1.35;
  font-size: 0.95rem;
}

.bubble-time {
  display: block;
  text-align: right;
  font-size: 0.68rem;
  color: var(--muted);
  margin-top: 0.2rem;
}

.message-image img {
  max-width: 100%;
  border-radius: 8px;
  display: block;
}

.file-attachment { font-size: 0.9rem; margin-bottom: 0.25rem; }

.command-status {
  display: block;
  font-size: 0.7rem;
  color: var(--muted);
  margin-top: 0.15rem;
}

.quick-commands {
  display: flex;
  gap: 0.35rem;
  overflow-x: auto;
  padding: 0.35rem 0.75rem 0;
  background: var(--panel);
  flex-shrink: 0;
}

.quick-cmd {
  flex-shrink: 0;
  border: none;
  background: var(--panel2);
  color: var(--accent);
  border-radius: 999px;
  padding: 0.25rem 0.6rem;
  font-size: 0.8rem;
  cursor: pointer;
}

.chat-input {
  flex-shrink: 0;
  padding: 0.45rem 0.5rem calc(0.45rem + env(safe-area-inset-bottom, 0px));
  background: var(--panel);
  border-top: 1px solid var(--border);
}

.hidden-form {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
}

.input-row {
  display: flex;
  align-items: flex-end;
  gap: 0.35rem;
  margin: 0;
}

.icon-btn, .send-btn {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  background: transparent;
  cursor: pointer;
  flex-shrink: 0;
  font-size: 1.2rem;
  padding: 0;
}

.send-btn {
  background: var(--accent);
  color: #fff;
  font-size: 1rem;
}

.input-row textarea {
  flex: 1;
  min-height: 40px;
  max-height: 120px;
  margin: 0;
  resize: none;
  border: none;
  border-radius: 18px;
  padding: 0.55rem 0.75rem;
  background: var(--panel2);
  color: var(--text);
  font-size: 1rem;
}

.input-row textarea:focus {
  outline: 2px solid var(--accent);
}

.load-more {
  display: block;
  margin: 0 auto 0.5rem;
  border: none;
  background: rgba(127,127,127,0.25);
  color: var(--accent);
  border-radius: 999px;
  padding: 0.3rem 0.75rem;
  font-size: 0.8rem;
  cursor: pointer;
}

/* Inbox / settings */
.inbox-list { display: flex; flex-direction: column; gap: 0.5rem; }

.event-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.75rem;
}

.event-card header {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  font-size: 0.85rem;
  color: var(--muted);
}

.event-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.5rem;
}

.bot-list-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.75rem;
  margin-bottom: 0.5rem;
}

.badge {
  background: #e53935;
  color: #fff;
  border-radius: 999px;
  padding: 0 0.35rem;
  font-size: 0.7rem;
}

.muted { color: var(--muted); }
.error { color: #e53935; }

.status-online { color: var(--online); }

#offline-banner {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background: #e53935;
  color: #fff;
  text-align: center;
  padding: 0.35rem;
  font-size: 0.85rem;
  z-index: 1000;
}

/* Mobile */
@media (max-width: 768px) {
  .mobile-only { display: inline-block; }

  .page-chat .sidebar-hidden-mobile { display: none; }
  .page-chat .chat-back { display: block; }
  .page-chat .main-chat { width: 100%; flex: 1; }

  .page-inbox .sidebar,
  .page-settings .sidebar,
  .page-bots .main:not(.main-chat) {
    display: none;
  }

  .page-inbox .main,
  .page-settings .main,
  .page-bots .sidebar {
    width: 100%;
    max-width: none;
    flex: 1;
  }

  .sidebar { width: 100%; max-width: none; }
  .chat-placeholder { display: none; }
}

@media (min-width: 769px) {
  .chat-back { display: none !important; }
  .page-back { display: none !important; }
}

/* Forms (no Pico on app pages) */
.login-card label,
.page-content label {
  display: block;
  margin-bottom: 0.75rem;
  font-size: 0.9rem;
}

.login-card input,
.page-content input[type="text"],
.page-content input[type="email"],
.page-content input[type="password"] {
  width: 100%;
  margin-top: 0.25rem;
  padding: 0.5rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel2);
  color: var(--text);
  font-size: 1rem;
}

.login-card button,
.page-content button[type="submit"] {
  width: 100%;
  padding: 0.55rem 1rem;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-size: 1rem;
  cursor: pointer;
}

.page-content section { margin-bottom: 1.5rem; }

.page-content h1 { margin-top: 0; font-size: 1.35rem; }
.page-content h2 { font-size: 1.1rem; }

/* Pico reset for our forms */
.chat-input button,
.chat-input textarea,
.input-row button,
.input-row textarea {
  margin-bottom: 0;
}
