:root {
  color-scheme: light;
  --bg: #f4f1eb;
  --surface: #fffdf8;
  --surface-2: #f9f7f0;
  --ink: #1c242b;
  --muted: #64717b;
  --line: #ded9ce;
  --accent: #176b87;
  --accent-2: #0f566d;
  --danger: #b33d3d;
  --user: #e7f2f5;
  --assistant: #fff;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  overflow: hidden;
}

button,
input,
textarea {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
}

.app {
  min-height: 100vh;
}

.auth-screen {
  display: grid;
  min-height: 100vh;
  place-items: center;
  padding: 24px;
}

.auth-card {
  width: min(420px, 100%);
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  box-shadow: 0 24px 70px rgba(28, 36, 43, 0.12);
  padding: 28px;
}

.brand {
  margin-bottom: 22px;
}

.brand h1 {
  margin: 0;
  font-size: 30px;
  line-height: 1.1;
}

.brand p {
  margin: 8px 0 0;
  color: var(--muted);
}

.field {
  display: grid;
  gap: 7px;
  margin-bottom: 14px;
}

.field span {
  color: #34424b;
  font-size: 14px;
  font-weight: 650;
}

.field input,
.composer textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  color: var(--ink);
  outline: 0;
}

.field input {
  height: 44px;
  padding: 0 13px;
}

.field input:focus,
.composer textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(23, 107, 135, 0.14);
}

.actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 18px;
}

.btn {
  min-height: 42px;
  border-radius: 10px;
  background: #ece8df;
  color: var(--ink);
  font-weight: 700;
}

.btn:hover {
  filter: brightness(0.98);
}

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

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

.btn.ghost {
  background: transparent;
  border: 1px solid var(--line);
}

.btn.danger {
  color: var(--danger);
}

.notice {
  min-height: 22px;
  margin: 12px 0 0;
  color: var(--danger);
  font-size: 14px;
}

.chat-app {
  display: grid;
  grid-template-columns: 292px minmax(0, 1fr);
  height: 100vh;
}

.sidebar {
  display: flex;
  flex-direction: column;
  min-height: 0;
  border-right: 1px solid var(--line);
  background: var(--surface-2);
  padding: 16px;
}

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 14px;
}

.sidebar-title h2 {
  margin: 0;
  font-size: 20px;
}

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

.sessions {
  display: grid;
  gap: 8px;
  min-height: 0;
  overflow: auto;
  padding-right: 2px;
}

.session-item {
  width: 100%;
  min-height: 44px;
  border-radius: 10px;
  background: transparent;
  color: var(--ink);
  text-align: left;
  padding: 9px 11px;
}

.session-item:hover {
  background: #efeae0;
}

.session-item.active {
  background: #ddecef;
  color: #0e4e64;
}

.nav-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 12px;
}

.btn.active {
  background: #ddecef;
  border-color: #b9d9e1;
  color: #0e4e64;
}

.session-title {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 700;
}

.session-meta {
  color: var(--muted);
  font-size: 12px;
}

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

.main {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  min-width: 0;
  height: 100vh;
  background: var(--surface);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  border-bottom: 1px solid var(--line);
  padding: 16px 20px;
}

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

.topbar p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 14px;
}

.messages {
  min-height: 0;
  overflow-y: auto;
  padding: 22px min(5vw, 56px);
}

.empty {
  display: grid;
  height: 100%;
  place-items: center;
  color: var(--muted);
  text-align: center;
}

.empty h2 {
  color: var(--ink);
  margin: 0 0 8px;
}

.message {
  display: flex;
  margin-bottom: 18px;
}

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

.bubble {
  width: fit-content;
  max-width: min(760px, 88%);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 13px 15px;
  background: var(--assistant);
  box-shadow: 0 8px 20px rgba(28, 36, 43, 0.04);
  white-space: pre-wrap;
  line-height: 1.55;
}

.message.user .bubble {
  background: var(--user);
  border-color: #c8e1e8;
}

.composer {
  border-top: 1px solid var(--line);
  background: rgba(255, 253, 248, 0.92);
  padding: 14px 20px 18px;
}

.composer-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 104px;
  gap: 10px;
  max-width: 940px;
  margin: 0 auto;
}

.composer textarea {
  min-height: 52px;
  max-height: 180px;
  resize: vertical;
  padding: 13px 14px;
}

.status {
  max-width: 940px;
  min-height: 20px;
  margin: 8px auto 0;
  padding: 0 4px;
  color: var(--muted);
  font-size: 13px;
}

.api-panel {
  min-height: 0;
  overflow: auto;
  padding: 24px min(5vw, 56px);
}

.api-hero {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(260px, 420px);
  gap: 18px;
  align-items: start;
  border-bottom: 1px solid var(--line);
  padding-bottom: 20px;
}

.api-hero h1 {
  margin: 0;
  font-size: 28px;
}

.api-hero p {
  max-width: 620px;
  margin: 7px 0 0;
  color: var(--muted);
}

.usage-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.usage-grid div,
.key-reveal,
.api-example,
.key-card {
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
}

.usage-grid div {
  display: grid;
  gap: 4px;
  padding: 13px;
}

.usage-grid span,
.key-reveal span,
.api-example span,
.key-card small,
.key-card span {
  color: var(--muted);
  font-size: 12px;
}

.usage-grid strong {
  font-size: 22px;
}

.api-create {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 150px;
  gap: 10px;
  max-width: 720px;
  margin: 22px 0 14px;
}

.api-create input {
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0 13px;
  outline: 0;
}

.key-reveal,
.api-example {
  display: grid;
  gap: 8px;
  max-width: 940px;
  margin-bottom: 14px;
  padding: 13px;
}

.key-reveal code,
.api-example code {
  overflow-x: auto;
  border-radius: 8px;
  background: #f3f0e8;
  padding: 10px;
  color: #22313a;
}

.key-list {
  display: grid;
  gap: 10px;
  max-width: 940px;
}

.key-card {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 14px;
  align-items: center;
  padding: 13px;
}

.key-card > div {
  display: grid;
  gap: 4px;
}

.key-card > div:last-child {
  grid-auto-flow: column;
  align-items: center;
  gap: 10px;
}

.key-card.revoked {
  opacity: 0.62;
}

.admin-section {
  display: grid;
  gap: 10px;
  margin-top: 22px;
}

.admin-section h2 {
  margin: 0;
  font-size: 18px;
}

.typing {
  display: inline-flex;
  gap: 4px;
  align-items: center;
}

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

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

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

@keyframes pulse {
  0%, 80%, 100% { opacity: 0.35; transform: translateY(0); }
  40% { opacity: 1; transform: translateY(-2px); }
}

@media (max-width: 820px) {
  body {
    overflow: hidden;
  }

  .auth-screen {
    align-items: start;
    min-height: 100dvh;
    padding: 18px 14px;
  }

  .auth-card {
    margin-top: 18px;
    padding: 22px;
    border-radius: 12px;
  }

  .brand h1 {
    font-size: 26px;
  }

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

  .chat-app {
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(0, 1fr);
    height: 100dvh;
    min-height: 0;
  }

  .sidebar {
    display: grid;
    grid-template-columns: 1fr auto;
    grid-template-areas:
      "head new"
      "nav nav"
      "sessions sessions"
      "foot foot";
    gap: 10px;
    height: auto;
    max-height: none;
    border-right: 0;
    border-bottom: 1px solid var(--line);
    padding: 10px;
  }

  .sidebar-head {
    grid-area: head;
    margin: 0;
    min-width: 0;
  }

  .sidebar-title h2 {
    font-size: 18px;
  }

  .sidebar-title p {
    max-width: 180px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .sidebar > .btn.primary {
    grid-area: new;
    min-width: 116px;
    min-height: 38px;
    padding: 0 12px;
  }

  .sessions {
    grid-area: sessions;
    display: flex;
    gap: 8px;
    overflow-x: auto;
    overflow-y: hidden;
    padding: 0 0 2px;
    scrollbar-width: none;
  }

  .nav-actions {
    grid-area: nav;
    margin: 0;
  }

  .sessions::-webkit-scrollbar {
    display: none;
  }

  .session-item {
    flex: 0 0 min(76vw, 260px);
    min-height: 42px;
    padding: 8px 10px;
  }

  .sidebar-foot {
    grid-area: foot;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    margin: 0;
  }

  .sidebar-foot .btn {
    min-height: 38px;
    padding: 0 8px;
    font-size: 14px;
  }

  .main {
    min-height: 0;
    height: auto;
  }

  .topbar {
    padding: 12px 14px;
  }

  .topbar h1 {
    max-width: 94vw;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 19px;
  }

  .topbar p {
    font-size: 12px;
  }

  .messages {
    padding: 14px 12px;
  }

  .bubble {
    max-width: 92%;
    border-radius: 12px;
    padding: 11px 12px;
    font-size: 15px;
    line-height: 1.5;
  }

  .composer {
    padding: 10px 10px calc(10px + env(safe-area-inset-bottom));
  }

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

  .composer textarea {
    min-height: 46px;
    max-height: 120px;
    padding: 11px 12px;
    font-size: 16px;
  }

  .composer .btn {
    min-height: 46px;
  }

  .status {
    min-height: 18px;
    margin-top: 6px;
    font-size: 12px;
  }

  .api-panel {
    padding: 14px 12px;
  }

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

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

  .key-card,
  .key-card > div:last-child {
    grid-template-columns: 1fr;
    grid-auto-flow: row;
    justify-items: start;
  }
}

@media (max-width: 420px) {
  .sidebar {
    grid-template-columns: 1fr;
    grid-template-areas:
      "head"
      "new"
      "nav"
      "sessions"
      "foot";
  }

  .sidebar > .btn.primary {
    width: 100%;
  }

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