:root {
  color-scheme: light dark;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 16px;
  --background: #f7f7f4;
  --surface: #ffffff;
  --surface-muted: #eef1ed;
  --text: #17211b;
  --muted: #667069;
  --border: #d7ddd8;
  --accent: #2f6b4f;
  --accent-strong: #24543e;
  --accent-text: #ffffff;
  --danger: #a32a2a;
  --new-bg: #e9ecea;
  --new-text: #4d5851;
  --ack-bg: #dce9ff;
  --ack-text: #24518c;
  --status-bg: #fff0cb;
  --status-text: #765512;
  --ask-bg: #e8e0ff;
  --ask-text: #5b3d99;
  --done-bg: #d9f2e4;
  --done-text: #256541;
  --shadow: 0 10px 30px rgb(20 34 26 / 8%);
}

* {
  box-sizing: border-box;
}

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

body {
  background: var(--background);
  color: var(--text);
}

button,
textarea {
  font: inherit;
}

button,
.brand,
summary {
  -webkit-tap-highlight-color: transparent;
}

button {
  min-height: 44px;
}

button:focus-visible,
textarea:focus-visible,
summary:focus-visible,
.brand:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--accent) 45%, transparent);
  outline-offset: 2px;
}

[hidden] {
  display: none !important;
}

.app-shell {
  min-height: 100%;
}

.site-header {
  align-items: center;
  background: color-mix(in srgb, var(--background) 92%, transparent);
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  height: calc(60px + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) max(16px, env(safe-area-inset-right)) 0 max(16px, env(safe-area-inset-left));
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand {
  color: var(--text);
  font-size: 1.2rem;
  font-weight: 750;
  letter-spacing: -0.03em;
  line-height: 44px;
  text-decoration: none;
}

.account {
  align-items: center;
  display: flex;
  gap: 10px;
  min-width: 0;
}

#account-name {
  background: var(--surface-muted);
  border-radius: 999px;
  color: var(--muted);
  font-size: 0.8rem;
  padding: 6px 10px;
  max-width: 32vw;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.link-button {
  background: transparent;
  border: 0;
  color: var(--muted);
  cursor: pointer;
  font-size: 0.82rem;
  padding: 0 4px;
  white-space: nowrap;
  text-decoration: none;
  text-underline-offset: 3px;
}

.signed-out {
  align-items: center;
  display: flex;
  flex-direction: column;
  gap: 16px;
  justify-content: center;
  min-height: calc(100dvh - 60px - env(safe-area-inset-top));
  padding: 24px max(20px, env(safe-area-inset-right)) max(24px, env(safe-area-inset-bottom)) max(20px, env(safe-area-inset-left));
}

.welcome-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 24px;
  box-shadow: var(--shadow);
  max-width: 440px;
  padding: 32px 24px;
  text-align: center;
  width: 100%;
}

.welcome-card img {
  height: 48px;
  width: 48px;
}

.welcome-card h1 {
  font-size: 2rem;
  letter-spacing: -0.04em;
  margin: 14px 0 8px;
}

.welcome-subtitle {
  color: var(--muted);
  line-height: 1.5;
  margin: 0 0 28px;
}

.welcome-card .primary-button {
  max-width: 320px;
  width: 100%;
}

.primary-button,
.secondary-button,
.send-button {
  border-radius: 12px;
  cursor: pointer;
  font-weight: 650;
}

.primary-button,
.send-button {
  background: var(--accent);
  border: 1px solid var(--accent);
  color: var(--accent-text);
}

.primary-button {
  min-width: 140px;
  padding: 10px 24px;
}

.primary-button:hover,
.send-button:hover {
  background: var(--accent-strong);
}

.signed-in {
  margin: 0 auto;
  max-width: 900px;
  width: 100%;
}

.tabs {
  background: var(--surface-muted);
  border: 1px solid var(--border);
  border-radius: 999px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  height: 52px;
  margin: 12px max(14px, env(safe-area-inset-right)) 12px max(14px, env(safe-area-inset-left));
  padding: 3px;
  position: sticky;
  top: calc(60px + env(safe-area-inset-top));
  z-index: 9;
}

.tab {
  background: transparent;
  border: 0;
  border-radius: 999px;
  color: var(--muted);
  cursor: pointer;
  font-weight: 650;
  padding: 8px 16px;
}

.tab[aria-selected="true"] {
  background: var(--accent);
  color: var(--accent-text);
}

.chat-panel {
  display: flex;
  flex-direction: column;
  height: calc(100dvh - 136px - env(safe-area-inset-top));
  min-height: 0;
}

.chat-heading {
  align-items: center;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-shrink: 0;
  justify-content: space-between;
  padding: 6px max(14px, env(safe-area-inset-right)) 8px max(14px, env(safe-area-inset-left));
}

.thread-label {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 650;
  margin: 0;
}

.chat-actions {
  display: flex;
  gap: 8px;
}

.chat-actions .secondary-button {
  font-size: 0.82rem;
  min-height: 38px;
  padding: 6px 10px;
}

.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 8px max(14px, env(safe-area-inset-right)) 18px max(14px, env(safe-area-inset-left));
  scroll-behavior: smooth;
}

.messages .chat-empty {
  font-size: 0.94rem;
  line-height: 1.5;
  margin: 0 auto;
  max-width: 340px;
  padding: 36px 12px;
}

.messages:has(.message-row) .chat-empty {
  display: none;
}

.message-row {
  display: flex;
  margin: 10px 0;
}

.message-row.user {
  justify-content: flex-end;
}

.message {
  border-radius: 12px;
  font-size: 15px;
  line-height: 1.45;
  max-width: 85%;
  overflow-wrap: anywhere;
  padding: 10px 14px;
  white-space: pre-wrap;
}

.message.user {
  background: var(--accent);
  border-bottom-right-radius: 0;
  color: var(--accent-text);
}

.message.assistant {
  background: var(--surface);
  border: 1px solid var(--border);
  border-bottom-left-radius: 0;
}

.message.seat {
  background: var(--status-bg);
  border: 1px solid color-mix(in srgb, var(--status-text) 25%, var(--border));
  border-bottom-left-radius: 0;
  display: grid;
  gap: 5px;
}

.message-label {
  color: var(--status-text);
  display: block;
  font-size: 0.76rem;
  line-height: 1.3;
}

.message.streaming:empty::after {
  animation: pulse 1.2s ease-in-out infinite;
  background: radial-gradient(circle, var(--muted) 3px, transparent 3.5px) 0 center / 12px 12px repeat-x;
  content: "";
  display: block;
  height: 22px;
  width: 36px;
}

.message-row.error {
  justify-content: center;
}

.message.error {
  background: transparent;
  color: var(--danger);
  font-size: 0.9rem;
  padding: 6px 10px;
  text-align: center;
}

.composer {
  align-items: flex-end;
  background: color-mix(in srgb, var(--background) 94%, transparent);
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 10px max(12px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  flex-shrink: 0;
  border-top: 1px solid var(--border);
}

.composer textarea {
  flex: 1;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  color: var(--text);
  font-size: 16px;
  line-height: 1.45;
  max-height: 142px;
  min-width: 0;
  min-height: 48px;
  overflow-y: auto;
  padding: 12px 14px;
  resize: none;
  width: 100%;
}

.send-button {
  align-items: center;
  border-radius: 50%;
  display: flex;
  flex: 0 0 48px;
  height: 48px;
  justify-content: center;
  padding: 0;
}

.send-button svg {
  height: 22px;
  width: 22px;
}

button:disabled,
textarea:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

.requests-panel {
  padding: 18px max(14px, env(safe-area-inset-right)) calc(24px + env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
}

.section-heading {
  align-items: center;
  display: flex;
  justify-content: space-between;
  margin-bottom: 14px;
}

.section-heading h1 {
  font-size: 1.35rem;
  margin: 0;
}

.secondary-button {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 14px;
}

.requests-list {
  display: grid;
  gap: 10px;
}

.request {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  overflow: hidden;
}

.request-summary {
  align-items: center;
  cursor: pointer;
  display: grid;
  gap: 10px;
  grid-template-columns: minmax(0, 1fr) auto;
  min-height: 62px;
  padding: 12px;
}

.request-summary::-webkit-details-marker {
  display: none;
}

.request-main {
  display: grid;
  gap: 3px;
  min-width: 0;
}

.request-text {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  font-weight: 650;
  line-height: 1.45;
  overflow: hidden;
  overflow-wrap: anywhere;
}

.request-meta,
.requester,
.request-update time {
  color: var(--muted);
  font-size: 0.82rem;
}

.requester {
  display: block;
  grid-column: 1;
  grid-row: 2;
}

.request-summary.operator .status-badge {
  grid-column: 2;
  grid-row: 1 / span 2;
}

.status-badge {
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 750;
  padding: 5px 8px;
  text-transform: uppercase;
}

.status-new { background: var(--new-bg); color: var(--new-text); }
.status-ack { background: var(--ack-bg); color: var(--ack-text); }
.status-status { background: var(--status-bg); color: var(--status-text); }
.status-ask { background: var(--ask-bg); color: var(--ask-text); }
.status-done { background: var(--done-bg); color: var(--done-text); }

.request-update {
  border-top: 1px solid var(--border);
  padding: 12px;
}

.request-update p {
  line-height: 1.45;
  margin: 6px 0;
  white-space: pre-wrap;
}

.error-line {
  color: var(--danger);
  line-height: 1.4;
  margin: 10px 0;
  text-align: center;
}

.empty-state {
  color: var(--muted);
  padding: 28px 0;
  text-align: center;
}

.history-overlay {
  background: rgb(0 0 0 / 38%);
  inset: 0;
  position: fixed;
  z-index: 30;
}

.history-sheet {
  background: var(--background);
  height: 100dvh;
  margin-left: auto;
  max-width: 520px;
  overflow-y: auto;
  padding: max(18px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(24px, env(safe-area-inset-bottom)) 16px;
  width: 100%;
}

.history-heading {
  align-items: center;
  display: flex;
  justify-content: space-between;
  margin-bottom: 16px;
}

.history-heading h2 {
  font-size: 1.35rem;
  margin: 0;
}

.history-list {
  display: grid;
  gap: 10px;
}

.history-row {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  color: var(--text);
  cursor: pointer;
  display: grid;
  gap: 4px;
  min-height: 72px;
  padding: 12px 14px;
  text-align: left;
}

.history-row.current {
  border-color: var(--accent);
  box-shadow: inset 3px 0 var(--accent);
}

.history-row > span:not(.history-count) {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-count {
  color: var(--muted);
  font-size: 0.78rem;
}

.sr-only {
  clip: rect(0, 0, 0, 0);
  clip-path: inset(50%);
  height: 1px;
  overflow: hidden;
  position: absolute;
  white-space: nowrap;
  width: 1px;
}

@keyframes pulse {
  50% { opacity: 0.45; }
}

@media (min-width: 640px) {
  .site-header {
    padding-left: max(24px, env(safe-area-inset-left));
    padding-right: max(24px, env(safe-area-inset-right));
  }

  .chat-heading,
  .messages,
  .requests-panel {
    padding-left: max(24px, env(safe-area-inset-left));
    padding-right: max(24px, env(safe-area-inset-right));
  }

  .composer {
    padding-left: max(24px, env(safe-area-inset-left));
    padding-right: max(24px, env(safe-area-inset-right));
  }

  .request-summary.operator {
    grid-template-columns: minmax(0, 1fr) minmax(130px, 0.35fr) auto;
  }

  .requester {
    grid-column: auto;
    grid-row: auto;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .request-summary.operator .status-badge {
    grid-column: auto;
    grid-row: auto;
  }
}

@media (prefers-color-scheme: dark) {
  :root {
    --background: #101713;
    --surface: #17211c;
    --surface-muted: #202c25;
    --text: #edf2ee;
    --muted: #a4afa7;
    --border: #324039;
    --accent: #347654;
    --accent-strong: #3c825f;
    --accent-text: #ffffff;
    --danger: #ff9292;
    --new-bg: #303a34;
    --new-text: #c4cec7;
    --ack-bg: #203958;
    --ack-text: #b8d6ff;
    --status-bg: #4c3c1b;
    --status-text: #ffe19b;
    --ask-bg: #392d56;
    --ask-text: #d5c3ff;
    --done-bg: #204330;
    --done-text: #9de0b8;
    --shadow: 0 10px 30px rgb(0 0 0 / 20%);
  }
}

@media (prefers-reduced-motion: reduce) {
  .messages {
    scroll-behavior: auto;
  }

  .message.streaming:empty::after {
    animation: none;
  }
}


.attach-button {
  align-items: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 50%;
  color: var(--muted);
  cursor: pointer;
  display: flex;
  flex: 0 0 48px;
  height: 48px;
  justify-content: center;
  padding: 0;
}

.pending-images,
.composer > .error-line {
  flex-basis: 100%;
}

.pending-images {
  display: flex;
  gap: 8px;
}

.pending-image {
  position: relative;
  width: 64px;
}

.pending-image img {
  border-radius: 10px;
  display: block;
  height: 64px;
  object-fit: cover;
  width: 64px;
}

.remove-image {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 50%;
  color: var(--text);
  cursor: pointer;
  height: 24px;
  min-height: 24px;
  padding: 0;
  position: absolute;
  right: 0;
  top: 0;
  width: 24px;
}

.image-status {
  color: var(--muted);
  display: block;
  font-size: 0.7rem;
  text-align: center;
}

.message-images {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 6px;
}

.message-images a {
  color: inherit;
  max-width: 100%;
}

.message-images img {
  border-radius: 8px;
  display: block;
  max-height: 200px;
  max-width: 100%;
}
