*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0f1117;
  --bg2: #1a1d27;
  --bg3: #242736;
  --border: #2e3148;
  --accent: #6c63ff;
  --accent-hover: #5a52e0;
  --danger: #e05252;
  --text: #e8e9f0;
  --text-muted: #8b8fa8;
  --radius: 8px;
}

body { background: var(--bg); color: var(--text); font-family: 'Segoe UI', system-ui, sans-serif; height: 100vh; overflow: hidden; }
.hidden { display: none !important; }
.error { color: var(--danger); font-size: 13px; margin-top: 8px; }

/* ── Login ── */
.screen { display: flex; align-items: center; justify-content: center; height: 100vh; }
.login-box {
  background: var(--bg2); border: 1px solid var(--border); border-radius: 12px;
  padding: 48px 40px; width: 360px; display: flex; flex-direction: column; gap: 16px;
}
.login-box h1 { font-size: 24px; font-weight: 700; text-align: center; }
.login-box .subtitle { color: var(--text-muted); text-align: center; font-size: 13px; margin-top: -8px; }
.login-box input {
  background: var(--bg3); border: 1px solid var(--border); border-radius: var(--radius);
  color: var(--text); padding: 12px 14px; font-size: 15px; outline: none;
}
.login-box input:focus { border-color: var(--accent); }

/* ── Layout ── */
#main-layout { display: flex; height: 100vh; }

.sidebar {
  width: 240px; background: var(--bg2); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; flex-shrink: 0; overflow: hidden;
}
.logo { font-size: 16px; font-weight: 700; color: var(--accent); padding: 20px 20px 16px; }

.nav-link {
  display: flex; align-items: center; gap: 10px; padding: 10px 20px;
  color: var(--text-muted); text-decoration: none; font-size: 14px; transition: all 0.15s;
}
.nav-link:hover { background: var(--bg3); color: var(--text); }
.nav-link.active { background: var(--bg3); color: var(--text); border-right: 2px solid var(--accent); }
.nav-link .icon { font-size: 15px; }

/* ── Chat section in sidebar ── */
.nav-chat-section { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.nav-chat-header { display: flex; align-items: center; }
.nav-link-chat { flex: 1; }
.btn-new-chat {
  background: transparent; border: none; color: var(--text-muted); font-size: 20px;
  padding: 0 14px 0 4px; cursor: pointer; line-height: 1; transition: color 0.15s;
}
.btn-new-chat:hover { color: var(--accent); }

/* ── Chat tree (topics + chats) ── */
.chat-tree { flex: 1; overflow-y: auto; padding-bottom: 8px; }

.topic-group { border-bottom: 1px solid rgba(46,49,72,0.5); }

.topic-header {
  display: flex; align-items: center; gap: 6px;
  padding: 7px 12px 7px 16px; cursor: pointer;
  color: var(--text-muted); font-size: 12px; font-weight: 600;
  text-transform: uppercase; letter-spacing: 0.04em;
  user-select: none; transition: color 0.12s;
}
.topic-header:hover { color: var(--text); }
.topic-arrow {
  font-size: 9px; transition: transform 0.15s; flex-shrink: 0;
  display: inline-block;
}
.topic-group.open .topic-arrow { transform: rotate(90deg); }
.topic-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.topic-chats { display: none; }
.topic-group.open .topic-chats { display: block; }

.chat-item {
  display: flex; align-items: flex-start; gap: 6px;
  padding: 7px 10px 7px 28px; cursor: pointer;
  color: var(--text-muted); font-size: 13px;
  transition: all 0.12s; border-right: 2px solid transparent;
  word-break: break-word; line-height: 1.45;
}
.chat-item:hover { background: var(--bg3); color: var(--text); }
.chat-item.active { background: var(--bg3); color: var(--text); border-right-color: var(--accent); }
.chat-item-name { flex: 1; }
.chat-item-del {
  flex-shrink: 0; background: none; border: none; color: transparent;
  font-size: 13px; cursor: pointer; padding: 0 2px; line-height: 1;
  transition: color 0.12s;
}
.chat-item:hover .chat-item-del { color: var(--text-muted); }
.chat-item-del:hover { color: var(--danger) !important; }

/* ── Sidebar bottom ── */
.sidebar-bottom { padding: 16px 20px; border-top: 1px solid var(--border); }
.btn-logout {
  width: 100%; background: transparent; border: 1px solid var(--border); border-radius: var(--radius);
  color: var(--text-muted); padding: 9px; font-size: 13px; cursor: pointer; transition: all 0.15s;
}
.btn-logout:hover { border-color: var(--danger); color: var(--danger); }

/* ── Content ── */
.content { flex: 1; overflow-y: auto; padding: 32px; display: flex; flex-direction: column; }
.page-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 24px; }
.page-header h2 { font-size: 22px; font-weight: 600; }

/* ── Buttons ── */
.btn-primary {
  background: var(--accent); color: #fff; border: none; border-radius: var(--radius);
  padding: 10px 20px; font-size: 14px; font-weight: 500; cursor: pointer; transition: background 0.15s;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-secondary {
  background: var(--bg3); color: var(--text); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 10px 20px; font-size: 14px; cursor: pointer;
}
.btn-secondary:hover { border-color: var(--accent); }
.btn-danger {
  background: var(--danger); color: #fff; border: none; border-radius: var(--radius);
  padding: 10px 20px; font-size: 14px; cursor: pointer;
}
.btn-danger-sm {
  background: transparent; border: none; color: var(--text-muted); font-size: 16px;
  cursor: pointer; padding: 4px 8px; border-radius: 4px; transition: all 0.15s;
}
.btn-danger-sm:hover { color: var(--danger); background: rgba(224,82,82,0.1); }
.btn-icon {
  background: transparent; border: none; color: var(--text-muted); font-size: 15px;
  cursor: pointer; padding: 4px 7px; border-radius: 4px; transition: color 0.15s;
}
.btn-icon:hover { color: var(--accent); }

/* ── Cards ── */
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 16px; }
.card {
  background: var(--bg2); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 20px; display: flex; flex-direction: column; gap: 12px; transition: border-color 0.15s;
}
.card:hover { border-color: var(--accent); }
.card-name { font-size: 16px; font-weight: 600; word-break: break-all; }
.card-meta { font-size: 12px; color: var(--text-muted); }
.card-desc { font-size: 13px; color: var(--text-muted); }
.card-actions { display: flex; gap: 8px; margin-top: auto; }
.btn-sm { padding: 7px 14px; font-size: 13px; border-radius: 6px; border: none; cursor: pointer; }
.btn-open { background: var(--accent); color: #fff; }
.btn-open:hover { background: var(--accent-hover); }
.btn-delete { background: var(--bg3); color: var(--danger); border: 1px solid var(--border); }
.delete-name { font-weight: 600; font-size: 16px; word-break: break-all; }
.warning { color: var(--text-muted); font-size: 13px; }
.empty-state { color: var(--text-muted); text-align: center; padding: 60px 0; font-size: 15px; }

/* ── Chat page ── */
#page-chat { padding: 0; overflow: hidden; height: 100%; display: flex; flex-direction: column; }

.chat-topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 20px; border-bottom: 1px solid var(--border); flex-shrink: 0; gap: 12px;
}
.chat-topbar-left { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.chat-topic-badge {
  font-size: 11px; color: var(--accent); font-weight: 600;
  text-transform: uppercase; letter-spacing: 0.05em;
}
.chat-name-title { font-size: 15px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-topbar-actions { display: flex; align-items: center; gap: 4px; flex-shrink: 0; }

.chat-messages {
  flex: 1; overflow-y: auto; padding: 20px 24px;
  display: flex; flex-direction: column; gap: 14px;
}
.chat-msg { max-width: 760px; }
.chat-msg.user { align-self: flex-end; }
.chat-msg .bubble {
  padding: 11px 15px; border-radius: var(--radius); font-size: 14px;
  line-height: 1.65; white-space: pre-wrap; word-break: break-word;
}
.chat-msg.user .bubble { background: var(--accent); color: #fff; }
.chat-msg.assistant .bubble { background: var(--bg2); border: 1px solid var(--border); }
.chat-msg.assistant .role { font-size: 11px; color: var(--text-muted); margin-bottom: 4px; }

/* ── Status bar ── */
.chat-status-bar {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 20px; border-top: 1px solid var(--border);
  background: var(--bg); flex-shrink: 0;
}
.status-spinner {
  font-size: 15px; line-height: 1; width: 18px; text-align: center;
  color: var(--accent); flex-shrink: 0;
}
.status-text {
  font-size: 13px; color: var(--text-muted); flex: 1;
  transition: opacity 0.3s;
}
.status-timer {
  font-size: 12px; color: var(--text-muted); font-variant-numeric: tabular-nums;
  font-family: monospace; flex-shrink: 0;
}

.chat-input-row {
  display: flex; gap: 10px; align-items: flex-end;
  padding: 14px 20px; border-top: 1px solid var(--border); flex-shrink: 0;
}
#chat-input {
  flex: 1; background: var(--bg2); border: 1px solid var(--border); border-radius: var(--radius);
  color: var(--text); padding: 10px 14px; font-size: 14px; resize: none; outline: none; font-family: inherit;
}
#chat-input:focus { border-color: var(--accent); }

.chat-empty-state {
  flex: 1; display: flex; flex-direction: column; align-items: center;
  justify-content: center; gap: 16px; color: var(--text-muted);
}
.chat-empty-state p { font-size: 15px; }

/* ── Modal ── */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,0.6);
  display: flex; align-items: center; justify-content: center; z-index: 100;
}
.modal-box {
  background: var(--bg2); border: 1px solid var(--border); border-radius: 12px;
  padding: 32px; width: 440px; display: flex; flex-direction: column; gap: 14px;
}
.modal-box h3 { font-size: 18px; font-weight: 600; }
.modal-box label { font-size: 13px; color: var(--text-muted); margin-bottom: -6px; }
.modal-box input, .modal-box select, .modal-box textarea {
  background: var(--bg3); border: 1px solid var(--border); border-radius: var(--radius);
  color: var(--text); padding: 10px 12px; font-size: 14px; outline: none; font-family: inherit;
}
.modal-box input:focus, .modal-box select:focus { border-color: var(--accent); }
.modal-box select option { background: var(--bg3); }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 4px; }

/* Topic combo in modals */
.topic-combo { display: flex; flex-direction: column; gap: 8px; }
.topic-combo-or { font-size: 12px; color: var(--text-muted); text-align: center; }

/* ── Scrollbar ── */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
