:root {
  --bg: #f5f7fb;
  --surface: #ffffff;
  --surface-soft: #f8f9fc;
  --surface-muted: #eef2f8;
  --text: #0f172a;
  --text-muted: #5b6478;
  --line: #d8dfeb;
  --line-strong: #c5d0e2;
  --brand: #2563eb;
  --brand-soft: #e9f0ff;
  --ok: #0f9d58;
  --danger: #dc2626;
  --shadow-sm: 0 2px 8px rgba(15, 23, 42, 0.06);
  --shadow-md: 0 8px 28px rgba(15, 23, 42, 0.08);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  font-family: "Inter", "Segoe UI", sans-serif;
  color: var(--text);
  background: linear-gradient(180deg, #f8faff 0%, var(--bg) 40%, var(--bg) 100%);
}

.hidden {
  display: none !important;
}

/* Login */
.login-wrap {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.login-card {
  width: min(440px, 100%);
  padding: 28px;
  border-radius: 20px;
  border: 1px solid var(--line);
  background: var(--surface);
  box-shadow: var(--shadow-md);
  display: grid;
  gap: 14px;
}

.login-card h1 {
  margin: 0;
  font-size: 28px;
  line-height: 1.1;
}

.login-card p {
  margin: 0;
  color: var(--text-muted);
}

/* Base controls */
label {
  display: grid;
  gap: 6px;
  font-size: 13px;
  color: var(--text-muted);
}

input,
textarea,
select,
button {
  font: inherit;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 12px;
  background: var(--surface);
}

input:focus,
textarea:focus,
select:focus,
button:focus {
  outline: 2px solid #bfd5ff;
  outline-offset: 0;
  border-color: #7ea5f9;
}

button {
  cursor: pointer;
  transition: 140ms ease;
}

button:hover {
  border-color: var(--line-strong);
  transform: translateY(-1px);
}

.error {
  color: var(--danger);
  min-height: 20px;
}

.info {
  color: var(--text-muted);
  min-height: 20px;
}

/* App shell */
.app {
  min-height: 100dvh;
  height: 100dvh;
  display: grid;
  grid-template-columns: 300px minmax(0, 1fr);
}

.sidebar {
  min-height: 0;
  overflow: hidden;
  padding: 16px;
  border-right: 1px solid var(--line);
  background: var(--surface-soft);
  display: grid;
  grid-template-rows: auto 1fr auto;
  gap: 14px;
}

.sidebar-top,
.sidebar-bottom {
  display: grid;
  gap: 8px;
}

#newChatBtn,
#logoutBtn,
#openAdminBtn {
  width: 100%;
  text-align: left;
}

#newChatBtn {
  border-color: #9cbcf7;
  background: var(--brand-soft);
  color: #1f4ec6;
  font-weight: 600;
}

.user-meta {
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 12px;
  background: var(--surface);
  box-shadow: var(--shadow-sm);
}

.user-meta strong {
  font-size: 14px;
}

.user-meta span {
  display: block;
  margin-top: 2px;
  color: var(--text-muted);
  font-size: 12px;
}

.conversation-list {
  min-height: 0;
  overflow: auto;
  display: grid;
  gap: 6px;
  padding-right: 4px;
  align-content: start;
  justify-items: stretch;
}

.conversation-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 28px;
  gap: 6px;
  align-items: center;
}

.conversation-item {
  text-align: left;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  padding: 7px 10px;
  font-size: 13px;
  line-height: 1.25;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conversation-item.active {
  border-color: #88aaf2;
  background: var(--brand-soft);
  color: #1d4ec6;
}

.conversation-delete {
  width: 28px;
  height: 28px;
  border-radius: 9px;
  padding: 0;
  display: grid;
  place-items: center;
  font-size: 18px;
  line-height: 1;
  color: var(--text-muted);
  background: var(--surface);
}

.conversation-delete:hover {
  color: var(--danger);
  border-color: #f0b6b6;
  background: #fff3f3;
}

/* Chat area */
.chat-main {
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  display: grid;
  grid-template-rows: auto 1fr auto;
  background: var(--surface);
}

.chat-head {
  min-height: 68px;
  padding: 14px 24px;
  border-bottom: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.chat-head h2 {
  margin: 0;
  font-size: 18px;
  font-weight: 650;
}

#citationBadge {
  color: var(--text-muted);
  font-size: 12px;
  text-align: right;
  max-width: 50%;
  overflow-wrap: anywhere;
}

.messages {
  min-height: 0;
  overflow: auto;
  display: grid;
  align-content: start;
  gap: 12px;
  padding: 22px;
  background: linear-gradient(180deg, #fafcff 0%, #f7f9fd 100%);
}

.msg {
  width: fit-content;
  max-width: min(900px, 100%);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 12px 14px;
  line-height: 1.45;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  box-shadow: var(--shadow-sm);
}

.msg.user {
  margin-left: auto;
  border-color: #9cbcf7;
  background: #edf3ff;
}

.msg.assistant {
  background: var(--surface);
}

.composer {
  border-top: 1px solid var(--line);
  background: var(--surface);
  padding: 14px 18px 18px;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
}

#promptInput {
  min-height: 56px;
  max-height: 180px;
  resize: vertical;
}

#sendBtn {
  width: 110px;
  font-weight: 600;
  border-color: #9cbcf7;
  background: var(--brand);
  color: white;
}

#sendBtn:hover {
  border-color: #7ea5f9;
  background: #1f56d8;
}

/* Admin workspace */
.admin {
  display: none;
}

.app.admin-open {
  grid-template-columns: 300px minmax(0, 1fr);
}

.app.admin-open .chat-main {
  display: none;
}

.app.admin-open .admin {
  min-width: 0;
  min-height: 0;
  overflow: auto;
  display: grid !important;
  align-content: start;
  gap: 18px;
  grid-template-columns: repeat(2, minmax(320px, 1fr));
  padding: 20px;
  background: var(--bg);
}

.app.admin-open .admin > h3 {
  grid-column: 1 / -1;
  margin: 0;
  font-size: 24px;
}

.card {
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--surface);
  box-shadow: var(--shadow-sm);
  padding: 14px;
  display: grid;
  gap: 10px;
}

.card h4 {
  margin: 0;
  font-size: 16px;
}

.row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.list {
  border: 1px solid var(--line);
  border-radius: 12px;
  max-height: 220px;
  overflow: auto;
}

.list-item {
  padding: 10px;
  border-bottom: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.list-item:last-child {
  border-bottom: 0;
}

.list-item span {
  min-width: 0;
  overflow-wrap: anywhere;
}

/* Breakpoints */
@media (max-width: 1200px) {
  .app {
    grid-template-columns: 260px minmax(0, 1fr);
  }

  .app.admin-open .admin {
    grid-template-columns: 1fr;
    padding: 14px;
  }
}

@media (max-width: 860px) {
  .app {
    grid-template-columns: 1fr;
    height: auto;
    min-height: 100dvh;
  }

  .sidebar {
    border-right: 0;
    border-bottom: 1px solid var(--line);
    grid-template-rows: auto;
  }

  .conversation-list {
    max-height: 180px;
  }

  .chat-head,
  .messages {
    padding-left: 14px;
    padding-right: 14px;
  }

  .composer {
    grid-template-columns: 1fr;
    padding: 12px;
  }

  #sendBtn {
    width: 100%;
  }

  .app.admin-open {
    grid-template-columns: 1fr;
  }

  .app.admin-open .sidebar {
    display: none;
  }

  .app.admin-open .admin {
    grid-template-columns: 1fr;
    padding: 12px;
  }
}
