:root {
  --bg: #212121;
  --bg-sidebar: #171717;
  --bg-elevated: #2f2f2f;
  --border: #3d3d3d;
  --text: #ececec;
  --text-muted: #a0a0a0;
  --accent: #10a37f;
  --accent-hover: #0d8f6e;
  --user-bubble: #2f2f2f;
  --assistant-bubble: #1e1e1e;
  --link: #7dd3fc;
  --link-hover: #bae6fd;
  --link-user: #6ee7b7;
  --link-user-hover: #a7f3d0;
}

* {
  box-sizing: border-box;
}

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

.chat-app {
  display: flex;
  min-height: 100vh;
}

.chat-sidebar {
  width: 260px;
  flex-shrink: 0;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}

.sidebar-top {
  flex-shrink: 0;
  padding: 0.75rem;
  border-bottom: 1px solid var(--border);
}

.new-chat-form {
  margin: 0;
  display: block;
}

.btn-new-chat {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  font-size: 0.9rem;
  cursor: pointer;
  text-align: left;
}

.btn-new-chat:hover {
  background: var(--bg-elevated);
}

.btn-new-chat-icon {
  font-size: 1.1rem;
  font-weight: 600;
  opacity: 0.85;
}

.sidebar-chats {
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem;
}

.sidebar-footer {
  padding: 0.75rem;
  border-top: 1px solid var(--border);
}

.sidebar-staff-link {
  font-size: 0.85rem;
  color: var(--text-muted);
  text-decoration: none;
}

.sidebar-staff-link:hover {
  color: var(--link);
}

.sidebar-chat-row {
  display: flex;
  align-items: center;
  gap: 0.15rem;
  margin-bottom: 2px;
  border-radius: 8px;
}

.sidebar-chat-row:hover {
  background: var(--bg-elevated);
}

.sidebar-chat-row:has(.sidebar-chat-link.is-active) {
  background: var(--bg-elevated);
}

.sidebar-chat-link {
  display: block;
  flex: 1;
  min-width: 0;
  padding: 0.55rem 0.35rem 0.55rem 0.65rem;
  border-radius: 8px;
  color: var(--text);
  text-decoration: none;
  font-size: 0.875rem;
}

.sidebar-chat-link:hover {
  background: transparent;
}

.sidebar-chat-row:hover .sidebar-chat-link:not(.is-active),
.sidebar-chat-row:has(.sidebar-chat-link.is-active) .sidebar-chat-link {
  background: transparent;
}

.sidebar-chat-link.is-active {
  background: transparent;
  font-weight: 600;
}

.sidebar-chat-title {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sidebar-chat-delete-form {
  margin: 0;
  flex-shrink: 0;
}

.sidebar-chat-delete {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0.45rem;
  margin: 0;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  line-height: 0;
  transition: color 0.15s ease, background 0.15s ease;
}

.sidebar-chat-delete:hover {
  color: #f87171;
  background: rgba(248, 113, 113, 0.12);
}

.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  max-width: 900px;
  margin: 0 auto;
  width: 100%;
  padding: 0 1rem 1rem;
}

.chat-main--empty {
  justify-content: center;
}

.chat-main-header {
  display: flex;
  align-items: flex-start;
  gap: 0.75rem;
  padding: 1rem 0;
  flex-shrink: 0;
}

.chat-main-header-text {
  min-width: 0;
  flex: 1;
}

.sidebar-toggle {
  display: none;
  flex-shrink: 0;
  align-items: center;
  justify-content: center;
  width: 2.25rem;
  height: 2.25rem;
  margin-top: 0.1rem;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-elevated);
  color: var(--text);
  cursor: pointer;
}

.sidebar-toggle:hover {
  background: var(--bg-sidebar);
}

.header-new-chat-form {
  display: none;
  margin: 0;
}

.header-new-chat {
  display: none;
  align-items: center;
  gap: 0.25rem;
  padding: 0.4rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-elevated);
  color: var(--text);
  font-size: 0.8rem;
  cursor: pointer;
  white-space: nowrap;
}

.header-new-chat:hover {
  background: var(--bg-sidebar);
}

.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 150;
  border: none;
  padding: 0;
  margin: 0;
  background: rgba(0, 0, 0, 0.45);
  cursor: pointer;
}

.chat-brand {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 600;
  color: var(--text);
}

.chat-role-badge {
  margin: 0.25rem 0 0;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.chat-empty {
  text-align: center;
  color: var(--text-muted);
  padding: 2rem;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding-bottom: 1rem;
  display: flex;
  flex-direction: column;
  gap: 1.35rem;
  scroll-behavior: smooth;
}

.message-row {
  display: flex;
  flex-direction: column;
}

.message {
  max-width: min(92%, 44rem);
  border-radius: 14px;
  padding: 0.85rem 1.05rem;
  line-height: 1.6;
  font-size: 0.9375rem;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.22);
  overflow-wrap: break-word;
  word-break: normal;
}

.message-user {
  align-self: flex-end;
  background: var(--user-bubble);
  border: 1px solid var(--border);
}

.message-assistant,
.message-system {
  align-self: stretch;
  max-width: min(100%, 44rem);
  background: var(--assistant-bubble);
  border: 1px solid var(--border);
}

.message-assistant.streaming .message-inner {
  opacity: 0.95;
}

.thinking {
  color: var(--text-muted);
  font-style: italic;
}

.message-body {
  word-wrap: break-word;
  overflow-wrap: anywhere;
}

.message-body p {
  margin: 0 0 0.65rem;
}

.message-body p:last-child {
  margin-bottom: 0;
}

.message-body ul,
.message-body ol {
  margin: 0.35rem 0 0.65rem;
  padding-left: 1.35rem;
}

.message-body li {
  margin: 0.2rem 0;
}

.message-body strong,
.message-markdown strong {
  font-weight: 600;
  color: var(--text);
}

.message-markdown h1,
.message-markdown h2,
.message-markdown h3,
.message-markdown h4 {
  margin: 1.1rem 0 0.5rem;
  line-height: 1.35;
  font-weight: 600;
  color: var(--text);
}

.message-markdown h1 { font-size: 1.25rem; }
.message-markdown h2 { font-size: 1.12rem; border-bottom: 1px solid var(--border); padding-bottom: 0.35rem; }
.message-markdown h3 { font-size: 1.02rem; color: #f0f0f0; }
.message-markdown h4 { font-size: 0.95rem; color: var(--text-muted); }

.message-markdown h1:first-child,
.message-markdown h2:first-child,
.message-markdown h3:first-child {
  margin-top: 0;
}

.message-markdown hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 1rem 0;
}

.message-markdown blockquote {
  margin: 0.65rem 0;
  padding: 0.5rem 0.85rem;
  border-left: 3px solid var(--accent);
  background: rgba(16, 163, 127, 0.08);
  color: var(--text-muted);
  border-radius: 0 6px 6px 0;
}

.message-markdown code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.85em;
  background: rgba(255, 255, 255, 0.06);
  padding: 0.12rem 0.35rem;
  border-radius: 4px;
}

.message-markdown pre {
  margin: 0.65rem 0;
  padding: 0.75rem 0.9rem;
  background: #141414;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow-x: auto;
}

.message-markdown pre code {
  background: none;
  padding: 0;
}

.message-markdown table {
  width: 100%;
  border-collapse: collapse;
  margin: 0.75rem 0;
  font-size: 0.88rem;
}

.message-markdown th,
.message-markdown td {
  border: 1px solid var(--border);
  padding: 0.45rem 0.6rem;
  text-align: left;
  vertical-align: top;
}

.message-markdown th {
  background: var(--bg-elevated);
  font-weight: 600;
}

.message-markdown tr:nth-child(even) td {
  background: rgba(255, 255, 255, 0.02);
}

.message-markdown em {
  color: var(--text-muted);
}

.message-link {
  color: var(--link);
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 3px;
  font-weight: 500;
  transition: color 0.12s ease;
}

.message-link:hover {
  color: var(--link-hover);
}

.message-user .message-link {
  color: var(--link-user);
}

.message-user .message-link:hover {
  color: var(--link-user-hover);
}

.composer-wrap {
  flex-shrink: 0;
  padding-top: 0.5rem;
  border-top: 1px solid var(--border);
}

.composer {
  display: flex;
  gap: 0.75rem;
  align-items: flex-end;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.65rem 0.75rem;
}

.composer-input {
  flex: 1;
  resize: none;
  border: none;
  background: transparent;
  color: var(--text);
  font: inherit;
  line-height: 1.45;
  min-height: 44px;
  max-height: 200px;
}

.composer-input:focus {
  outline: none;
}

.composer-submit {
  padding: 0.5rem 1rem;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  font-size: 0.875rem;
  cursor: pointer;
}

.composer-submit:hover {
  background: var(--accent-hover);
}

.message-tool .tool-payload {
  margin: 0;
  font-size: 0.8rem;
  white-space: pre-wrap;
  word-break: break-word;
}

.message-tool-calls {
  align-self: flex-start;
  max-width: min(100%, 44rem);
  background: rgba(55, 65, 81, 0.45);
  border: 1px dashed var(--border);
  box-shadow: none;
}

.message-tool-calls .tool-call-block + .tool-call-block {
  margin-top: 0.65rem;
  padding-top: 0.65rem;
  border-top: 1px solid var(--border);
}

.tool-call-heading {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: 0.35rem;
}

.tool-call-name {
  font-size: 0.85rem;
  color: var(--link);
  background: rgba(0, 0, 0, 0.2);
  padding: 0.1rem 0.35rem;
  border-radius: 4px;
}

.tool-payload--compact {
  margin: 0;
  font-size: 0.75rem;
  max-height: 8rem;
  overflow: auto;
}

.flash {
  padding: 0.75rem 1rem;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  color: var(--text);
}

.flash-notice {
  border-left: 3px solid var(--accent);
}

.flash-alert {
  border-left: 3px solid #f87171;
}

/* Staff nav + portal */
.staff-shell {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.staff-shell .chat-app {
  flex: 1;
  min-height: 0;
}

.staff-nav {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.65rem 1rem;
  background: var(--bg-sidebar);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.staff-nav-brand {
  flex-shrink: 0;
}

.staff-nav-logo {
  color: var(--text);
  font-weight: 600;
  text-decoration: none;
}

.staff-nav-links {
  display: flex;
  gap: 0.75rem;
  flex: 1;
  flex-wrap: wrap;
}

.staff-nav-link {
  color: var(--text-muted);
  text-decoration: none;
  font-size: 0.9rem;
}

.staff-nav-link:hover,
.staff-nav-link.is-active {
  color: var(--text);
}

.staff-nav-user {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-left: auto;
}

.staff-nav-meta {
  font-size: 0.85rem;
  color: var(--text-muted);
}

.staff-nav-logout {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 6px;
  padding: 0.35rem 0.65rem;
  font-size: 0.85rem;
  cursor: pointer;
}

.staff-nav-logout:hover {
  color: var(--text);
  border-color: var(--text-muted);
}

.inline-form {
  display: inline;
  margin: 0;
}

.auth-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}

.auth-card {
  width: 100%;
  max-width: 420px;
  background: var(--bg-sidebar);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1.75rem;
}

.auth-title {
  margin: 0 0 0.35rem;
  font-size: 1.35rem;
}

.auth-subtitle {
  margin: 0;
  color: var(--text-muted);
  font-size: 0.9rem;
}

.auth-form {
  margin-top: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.auth-field label {
  display: block;
  margin-bottom: 0.35rem;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.auth-field input {
  width: 100%;
  padding: 0.65rem 0.75rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}

.auth-submit {
  margin-top: 0.25rem;
  padding: 0.7rem 1rem;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: white;
  font-weight: 600;
  cursor: pointer;
}

.auth-submit:hover {
  background: var(--accent-hover);
}

.auth-demo {
  margin-top: 1.5rem;
  padding-top: 1.25rem;
  border-top: 1px solid var(--border);
  font-size: 0.85rem;
  color: var(--text-muted);
}

.auth-demo h2 {
  margin: 0 0 0.5rem;
  font-size: 0.85rem;
  color: var(--text);
}

.auth-demo ul {
  margin: 0;
  padding-left: 1.1rem;
}

.auth-footer {
  margin-top: 1.25rem;
  text-align: center;
}

.auth-link {
  color: var(--link);
  font-size: 0.9rem;
}

.portal-page {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.portal-main {
  flex: 1;
  padding: 2rem 1.5rem 3rem;
  max-width: 960px;
  margin: 0 auto;
  width: 100%;
}

.portal-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 1rem;
  flex-wrap: wrap;
  margin-bottom: 2rem;
}

.portal-kicker {
  margin: 0 0 0.25rem;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--accent);
}

.portal-title {
  margin: 0;
  font-size: 1.75rem;
}

.portal-meta {
  margin: 0.35rem 0 0;
  color: var(--text-muted);
}

.portal-actions {
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.btn-primary,
.btn-secondary {
  display: inline-flex;
  align-items: center;
  padding: 0.6rem 1rem;
  border-radius: 8px;
  text-decoration: none;
  font-size: 0.9rem;
  cursor: pointer;
  border: none;
}

.btn-primary {
  background: var(--accent);
  color: white;
  font-weight: 600;
}

.btn-primary:hover {
  background: var(--accent-hover);
}

.btn-secondary {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
}

.btn-secondary:hover {
  background: var(--bg-elevated);
}

.portal-section {
  margin-bottom: 2rem;
}

.portal-section h2 {
  margin: 0 0 1rem;
  font-size: 1.05rem;
}

.portal-section--highlight {
  padding: 1rem 1.25rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg-sidebar);
}

.portal-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 1rem;
}

.portal-card {
  padding: 1rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg-elevated);
}

.portal-card h3 {
  margin: 0 0 0.35rem;
  font-size: 1rem;
}

.portal-card-meta,
.portal-card-hint {
  margin: 0;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.portal-card-hint {
  margin-top: 0.5rem;
}

.portal-copy {
  margin: 0;
  color: var(--text-muted);
  line-height: 1.5;
}

.portal-stats {
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
}

.portal-stat {
  min-width: 100px;
  padding: 1rem;
  border-radius: 10px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
}

.portal-stat span {
  display: block;
  font-size: 1.5rem;
  font-weight: 700;
}

.portal-stat label {
  font-size: 0.8rem;
  color: var(--text-muted);
}

.portal-chat-list {
  margin: 0;
  padding: 0;
  list-style: none;
}

.portal-chat-list li {
  padding: 0.5rem 0;
  border-bottom: 1px solid var(--border);
}

.portal-chat-list a {
  color: var(--link);
  text-decoration: none;
}

.portal-empty {
  color: var(--text-muted);
  margin: 0;
}

/* Parent chat (unauthenticated) */
.parent-shell {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.parent-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.75rem 1rem;
  background: var(--bg-sidebar);
  border-bottom: 1px solid var(--border);
}

.parent-nav-brand {
  min-width: 0;
}

.parent-nav-title {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
}

.parent-nav-subtitle {
  margin: 0.15rem 0 0;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.parent-nav-link {
  flex-shrink: 0;
  color: var(--text-muted);
  text-decoration: none;
  font-size: 0.85rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.35rem 0.65rem;
}

.parent-nav-link:hover {
  color: var(--text);
  border-color: var(--text-muted);
}

.parent-shell .chat-app {
  flex: 1;
  min-height: 0;
}

.chat-app--solo {
  display: flex;
  min-height: 0;
}

.chat-app--solo .chat-main {
  flex: 1;
  max-width: 900px;
}

.chat-welcome {
  margin: auto 0;
  text-align: center;
  color: var(--text-muted);
  padding: 2rem 1rem;
  max-width: 32rem;
  align-self: center;
}

.chat-welcome p {
  margin: 0 0 0.75rem;
  line-height: 1.55;
}

.chat-welcome-hint {
  font-size: 0.9rem;
}

/* Chat responsive layout */
@media (max-width: 767px) {
  .parent-nav {
    padding: 0.5rem 0.75rem;
  }

  .parent-nav-title {
    font-size: 0.95rem;
  }

  .chat-app--solo .chat-main {
    max-width: none;
    padding: 0 0.75rem 1rem;
  }

  .staff-nav {
    position: sticky;
    top: 0;
    z-index: 300;
    padding: 0.5rem 0.75rem;
    gap: 0.5rem;
  }

  .staff-nav-links {
    order: 3;
    width: 100%;
    gap: 0.5rem;
  }

  .staff-nav-user {
    margin-left: 0;
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  .staff-nav-meta {
    font-size: 0.8rem;
  }

  .chat-app {
    position: relative;
  }

  .chat-sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 200;
    width: min(280px, 85vw);
    transform: translateX(-100%);
    transition: transform 0.2s ease;
  }

  .chat-app.sidebar-open .chat-sidebar {
    transform: translateX(0);
    z-index: 400;
  }

  .chat-app.sidebar-open .sidebar-backdrop {
    display: block;
    z-index: 350;
  }

  .sidebar-toggle {
    display: flex;
  }

  .header-new-chat-form {
    display: block;
    margin-left: auto;
  }

  .header-new-chat {
    display: flex;
  }

  .chat-main {
    max-width: none;
    padding: 0 0.75rem 1rem;
  }

  .chat-main-header {
    padding: 0.75rem 0;
  }

  .chat-brand {
    font-size: 0.95rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .message {
    max-width: 100%;
    font-size: 0.9rem;
  }

  .message-user {
    max-width: 92%;
  }

  .composer-wrap {
    padding-bottom: env(safe-area-inset-bottom, 0);
  }
}
