:root {
  --bg: #f5f5f7;
  --surface: #ffffff;
  --border: #e2e4e8;
  --text: #1a1a1f;
  --muted: #6b7280;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --sidebar-w: 280px;
  --content-pad-x: clamp(0.75rem, 3vw, 1.75rem);
  --content-pad-bottom: clamp(0.75rem, 2vw, 1.25rem);
  --composer-min-h: 3.5rem;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  color: var(--text);
  background: var(--bg);
}

/* Login */
.page-login {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 1rem;
}

.login-card {
  background: var(--surface);
  padding: 2rem;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  width: min(400px, 100%);
}

.login-card h1 {
  margin: 0 0 0.25rem;
  font-size: 1.5rem;
}

.muted {
  color: var(--muted);
  font-size: 0.9rem;
}

.error {
  color: #b91c1c;
  background: #fef2f2;
  padding: 0.5rem 0.75rem;
  border-radius: 8px;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  margin-top: 1.25rem;
}

.login-form label {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.85rem;
}

.login-form input {
  padding: 0.65rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 1rem;
}

.login-form button,
.send-btn,
.btn-new {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 0.65rem 1rem;
  font-size: 1rem;
  cursor: pointer;
}

.login-form button:hover,
.send-btn:hover:not(:disabled),
.btn-new:hover {
  background: var(--accent-hover);
}

/* Chat layout */
.page-chat {
  height: 100dvh;
  overflow: hidden;
}

.chat-app {
  display: flex;
  height: 100%;
}

.sidebar {
  width: var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.sidebar-header {
  padding: 0.75rem;
  border-bottom: 1px solid var(--border);
}

.btn-new {
  width: 100%;
}

.session-list {
  list-style: none;
  margin: 0;
  padding: 0.5rem;
  overflow-y: auto;
  flex: 1;
}

.session-list li {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  margin-bottom: 0.25rem;
}

.session-list button.session-item {
  flex: 1;
  text-align: left;
  border: none;
  background: transparent;
  padding: 0.6rem 0.75rem;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.9rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.session-list button.session-item.active,
.session-list button.session-item:hover {
  background: #eef2ff;
}

.session-list .btn-delete {
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  padding: 0.25rem;
  font-size: 1.1rem;
}

.sidebar-footer {
  padding: 0.75rem;
  border-top: 1px solid var(--border);
  font-size: 0.85rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
}

.link,
.link-btn {
  color: var(--accent);
  text-decoration: none;
  background: none;
  border: none;
  cursor: pointer;
  font: inherit;
  padding: 0;
}

.logout-form {
  margin: 0;
}

.main-pane {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.main-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
  padding: 0.85rem var(--content-pad-x);
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.main-header h2 {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.messages {
  flex: 1;
  width: 100%;
  overflow-y: auto;
  padding: 1rem var(--content-pad-x) 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}

.msg {
  width: 100%;
  max-width: 100%;
  padding: 0.95rem 1.1rem;
  border-radius: var(--radius);
  line-height: 1.6;
  font-size: 1rem;
  white-space: pre-wrap;
  word-break: break-word;
}

.msg.user {
  align-self: flex-end;
  width: auto;
  max-width: min(100%, 42rem);
  background: #dbeafe;
}

.msg.assistant {
  align-self: stretch;
  background: var(--surface);
  border: 1px solid var(--border);
}

.msg .attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.5rem;
}

.msg .attachments img {
  max-width: 200px;
  max-height: 160px;
  border-radius: 8px;
  object-fit: cover;
}

.composer-wrap {
  width: 100%;
  border-top: 1px solid var(--border);
  background: var(--surface);
  padding: 0.85rem var(--content-pad-x) var(--content-pad-bottom);
  padding-bottom: max(var(--content-pad-bottom), env(safe-area-inset-bottom, 0px));
}

.preview-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}

.preview-chip {
  position: relative;
  font-size: 0.8rem;
  background: var(--bg);
  padding: 0.25rem 1.5rem 0.25rem 0.5rem;
  border-radius: 6px;
}

.preview-chip button {
  position: absolute;
  right: 2px;
  top: 0;
  border: none;
  background: none;
  cursor: pointer;
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 0.65rem;
  width: 100%;
  max-width: none;
  margin: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.65rem 0.75rem;
  background: #fafafa;
  box-shadow: var(--shadow);
}

.composer-toolbar {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.attach-btn,
.voice-btn,
.btn-icon {
  border: none;
  background: transparent;
  cursor: pointer;
  font-size: 1.2rem;
  padding: 0.35rem;
  border-radius: 6px;
}

.voice-btn.listening {
  background: #fee2e2;
}

.composer textarea {
  flex: 1;
  width: 100%;
  min-width: 0;
  border: none;
  background: transparent;
  resize: none;
  font: inherit;
  font-size: 1rem;
  min-height: var(--composer-min-h);
  max-height: 220px;
  line-height: 1.5;
  padding: 0.35rem 0.25rem;
}

.composer textarea:focus {
  outline: none;
}

.send-btn {
  padding: 0.6rem 1.15rem;
  min-height: 2.75rem;
  align-self: stretch;
  flex-shrink: 0;
}

.send-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.composer-hint {
  margin: 0.4rem 0 0;
  font-size: 0.8rem;
}

.btn-icon.mobile-only,
.sidebar-backdrop.mobile-only {
  display: none;
}

/* Admin */
.page-admin {
  padding: 1.5rem;
  max-width: 1100px;
  margin: 0 auto;
}

.admin-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.5rem;
}

.admin-actions {
  display: flex;
  gap: 1rem;
  align-items: center;
}

.admin-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
  margin-bottom: 1.5rem;
}

.admin-form {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.admin-form input,
.admin-form select,
.reset-form input {
  padding: 0.5rem;
  border: 1px solid var(--border);
  border-radius: 6px;
}

.user-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

.user-table th,
.user-table td {
  border-bottom: 1px solid var(--border);
  padding: 0.5rem;
  text-align: left;
  vertical-align: top;
}

.ops {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}

.ops button.danger {
  color: #b91c1c;
}

@media (min-width: 1200px) {
  :root {
    --content-pad-x: 2rem;
    --composer-min-h: 3.75rem;
  }
}

@media (max-width: 768px) {
  :root {
    --content-pad-x: 0.75rem;
    --composer-min-h: 3.25rem;
  }

  .main-header h2 {
    font-size: 0.95rem;
  }

  .msg {
    padding: 0.85rem 0.95rem;
    font-size: 0.95rem;
  }

  .msg.user {
    max-width: 100%;
  }

  .composer {
    gap: 0.5rem;
    padding: 0.55rem 0.6rem;
  }

  .send-btn {
    min-height: 2.5rem;
    padding: 0.55rem 0.85rem;
    font-size: 0.95rem;
  }

  .attach-btn,
  .voice-btn,
  .btn-icon {
    font-size: 1.35rem;
    padding: 0.4rem;
  }

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 40;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    box-shadow: var(--shadow);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .sidebar-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.35);
    z-index: 30;
  }

  .btn-icon.mobile-only {
    display: inline-flex;
  }
}