/*
 * Talk With Jamie v2 stylesheet.
 *
 * A quiet messaging layout: cool grey-blue surfaces, one slate-blue
 * accent for the user's side of the conversation, small radii and
 * hairline borders. Both themes come from the custom properties
 * below, so components never restyle themselves per theme.
 *
 * Contents:
 *   1. Tokens and themes
 *   2. Base elements
 *   3. Buttons, inputs and notices
 *   4. Chat shell
 *   5. Messages and composer
 *   6. Auth screen
 *   7. Admin and context views
 */

/* 1. Tokens and themes */

:root {
  color-scheme: dark;
  --bg: #10151d;
  --surface: #171e29;
  --surface-raised: #1d2634;
  --ink: #e7ebf1;
  --muted: #93a1b5;
  --line: #2b3545;
  --accent: #38618f;
  --accent-strong: #46739f;
  --accent-contrast: #ffffff;
  --danger-ink: #e79b93;
  --danger-line: #6b3630;
  --ok-ink: #93c9a5;
  --ok-line: #2f5a3d;
  --focus: #7fa7cf;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  --mono: ui-monospace, "Cascadia Code", Consolas, Menlo, monospace;
  --radius: 10px;
}

:root[data-theme="light"] {
  color-scheme: light;
  --bg: #f2f4f7;
  --surface: #ffffff;
  --surface-raised: #e9edf2;
  --ink: #1d2634;
  --muted: #5d6b80;
  --line: #d4dae3;
  --accent: #2e4d75;
  --accent-strong: #223b5b;
  --accent-contrast: #ffffff;
  --danger-ink: #8f2c20;
  --danger-line: #d9aaa3;
  --ok-ink: #22633a;
  --ok-line: #a9cdb4;
  --focus: #2e4d75;
}

/* 2. Base elements */

* {
  box-sizing: border-box;
}

html,
body,
#app {
  min-height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  line-height: 1.45;
}

img {
  display: block;
}

button,
input,
textarea,
select {
  font: inherit;
}

button {
  cursor: pointer;
}

button:focus-visible,
input:focus-visible,
textarea:focus-visible,
select:focus-visible,
a:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

a {
  color: var(--ink);
  text-underline-offset: 0.2em;
}

.hide {
  display: none !important;
}

.muted {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
}

/* 3. Buttons, inputs and notices */

.btn,
.ghost,
.danger {
  min-height: 42px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  padding: 9px 14px;
  font-weight: 600;
}

.btn {
  background: var(--accent);
  color: var(--accent-contrast);
}

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

.ghost {
  background: none;
  color: var(--ink);
  border-color: var(--line);
}

.ghost:hover {
  background: var(--surface-raised);
}

.danger {
  background: none;
  color: var(--danger-ink);
  border-color: var(--danger-line);
}

.danger:hover {
  background: var(--surface-raised);
}

.link-button {
  display: inline-block;
  text-decoration: none;
}

.small {
  padding: 7px 10px;
  font-size: 13px;
}

.full-width-action {
  width: 100%;
  margin-top: 10px;
}

.input,
select,
.composer textarea,
.admin-composer textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
  padding: 11px 13px;
}

.error-box,
.ok-box {
  margin: 10px 0;
  padding: 12px;
  border-radius: var(--radius);
  white-space: pre-wrap;
  background: var(--surface);
}

.error-box {
  color: var(--danger-ink);
  border: 1px solid var(--danger-line);
}

.ok-box {
  color: var(--ok-ink);
  border: 1px solid var(--ok-line);
}

/* 4. Chat shell */

/* Flex lets .messages take the height left by the header and toolbar. */
.app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

.header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}

.avatar {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  object-fit: cover;
  background: var(--surface-raised);
}

.title {
  min-width: 0;
  flex: 1;
}

.title h1 {
  margin: 0;
  font-size: 17px;
  line-height: 1.1;
}

.title p {
  margin: 3px 0 0;
  color: var(--muted);
  font-size: 12px;
}

.chat-toolbar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.65rem 1rem;
  border-bottom: 1px solid var(--line);
  background: var(--bg);
}

.chat-toolbar label {
  color: var(--muted);
  font-size: 0.85rem;
}

.chat-toolbar select {
  min-width: 0;
  max-width: 28rem;
  flex: 1;
}

.auth-hint {
  margin: 10px 2px 0;
}

/* 5. Messages and composer */

.messages {
  overflow: auto;
  flex: 1;
  min-height: 0;
  padding: 18px 14px 122px;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  gap: 8px;
  background: var(--bg);
}

.row {
  display: flex;
}

.row.me {
  justify-content: flex-end;
}

.row.them {
  justify-content: flex-start;
}

.bubble {
  max-width: min(82vw, 520px);
  border-radius: var(--radius);
  padding: 10px 14px;
  white-space: pre-wrap;
  line-height: 1.4;
}

.bubble.me {
  background: var(--accent);
  color: var(--accent-contrast);
  border-bottom-right-radius: 3px;
}

.bubble.them {
  background: var(--surface);
  color: var(--ink);
  border: 1px solid var(--line);
  border-bottom-left-radius: 3px;
}

.status {
  margin-top: 4px;
  font-family: var(--mono);
  font-size: 10.5px;
  opacity: 0.75;
  text-align: right;
}

.typing {
  display: flex;
  gap: 6px;
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.typing span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--muted);
  animation: typing-pulse 1.1s infinite ease-in-out;
}

.typing span:nth-child(2) {
  animation-delay: 0.13s;
}

.typing span:nth-child(3) {
  animation-delay: 0.26s;
}

@keyframes typing-pulse {
  0%,
  80%,
  100% {
    opacity: 0.35;
  }

  40% {
    opacity: 1;
  }
}

@media (prefers-reduced-motion: reduce) {
  .typing span {
    animation: none;
    opacity: 0.7;
  }
}

.composer {
  position: sticky;
  bottom: 0;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: end;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  background: var(--bg);
  border-top: 1px solid var(--line);
}

.composer textarea {
  min-height: 46px;
  max-height: 160px;
  resize: none;
}

.composer button {
  height: 46px;
  padding-inline: 18px;
}

/* 6. Auth screen */

.center {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 18px;
  background: var(--bg);
}

.card {
  width: min(520px, 100%);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 22px;
}

.card h1 {
  margin: 0 0 8px;
  font-size: 24px;
}

.stack {
  display: grid;
  gap: 10px;
  margin-top: 14px;
}

.auth-main {
  min-height: calc(100vh - 80px);
  display: grid;
  place-items: center;
  padding: clamp(2rem, 7vh, 4rem) 1rem;
  overflow: auto;
}

.auth-main > .row.them {
  display: none;
}

.auth-main .card {
  width: min(520px, calc(100vw - 2rem));
  margin: 0 auto;
}

.tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  margin: 14px 0;
}

.tabs button {
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: none;
  color: var(--muted);
  font-weight: 600;
}

.tabs button:hover {
  background: var(--surface-raised);
}

.tabs button.active-tab {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-contrast);
}

.auth-main .full-width-action {
  margin-top: 2px;
  background: none;
  border: 1px solid var(--line);
  color: var(--ink);
}

.auth-main .full-width-action:hover {
  background: var(--surface-raised);
}

/* 7. Admin and context views */

.admin {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 300px 1fr;
  background: var(--bg);
  color: var(--ink);
}

.side {
  overflow: auto;
  padding: 14px;
  background: var(--surface);
  border-right: 1px solid var(--line);
}

.brand {
  display: flex;
  gap: 10px;
  align-items: center;
  margin-bottom: 12px;
  padding: 12px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.brand img {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  object-fit: cover;
}

.brand small {
  display: block;
  color: var(--muted);
}

.side-actions,
.list {
  display: grid;
  gap: 8px;
}

.side-actions {
  margin-bottom: 12px;
}

.item {
  width: 100%;
  padding: 11px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--ink);
  text-align: left;
}

.item.active {
  border-color: var(--accent);
  background: var(--surface-raised);
}

.item span {
  display: block;
  font-weight: 700;
}

.item small {
  display: block;
  overflow: hidden;
  margin-top: 3px;
  color: var(--muted);
  text-overflow: ellipsis;
  white-space: nowrap;
}

.main {
  min-width: 0;
  display: grid;
  grid-template-rows: auto auto 1fr auto;
}

.top {
  padding: 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.top h1 {
  margin: 0;
  font-size: 22px;
}

.top p {
  margin: 4px 0 0;
  color: var(--muted);
}

.grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
  padding: 14px;
  border-bottom: 1px solid var(--line);
}

.grid label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
}

.wide {
  grid-column: 1 / -1;
}

.history {
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 14px;
}

.admin-bubble {
  max-width: 900px;
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  white-space: pre-wrap;
}

.admin-bubble.ai {
  align-self: flex-end;
  border-color: var(--accent);
}

.admin-bubble.user {
  align-self: flex-start;
}

.tools,
.actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.tools {
  margin-bottom: 12px;
}

.actions {
  align-items: center;
  margin-top: 8px;
}

.admin-composer {
  padding: 12px;
  background: var(--surface);
  border-top: 1px solid var(--line);
}

.admin-composer textarea {
  min-height: 90px;
}

.context-main {
  grid-template-rows: auto 1fr;
}

.context-panel {
  overflow: auto;
  display: grid;
  align-content: start;
  gap: 14px;
  padding: 18px;
}

.context-panel h2 {
  margin: 0 0 8px;
  font-size: 18px;
}

.context-panel p {
  line-height: 1.55;
}

.context-panel ul {
  margin-bottom: 0;
}

.context-panel code {
  padding: 2px 6px;
  border-radius: 5px;
  font-family: var(--mono);
  background: var(--surface-raised);
}

@media (max-width: 860px) {
  .admin {
    grid-template-columns: 1fr;
  }

  .side {
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .grid {
    grid-template-columns: 1fr 1fr;
  }
}

@media (max-width: 560px) {
  .grid {
    grid-template-columns: 1fr;
  }

  .header {
    gap: 8px;
  }

  .small {
    padding: 7px 9px;
    font-size: 12px;
  }

  .admin {
    font-size: 15px;
  }
}
