:root {
  color-scheme: light;
  --bg: #f5f7fb;
  --panel: #ffffff;
  --panel-soft: #f9fafc;
  --ink: #111827;
  --muted: #667085;
  --line: #d8dee8;
  --line-soft: #edf0f5;
  --accent: #057d7c;
  --accent-soft: #e8f7f2;
  --blue: #2563eb;
  --blue-soft: #edf4ff;
  --danger: #e11d48;
  --danger-soft: #fff1f2;
  --queued: #f97316;
  --queued-soft: #fff7ed;
  --success: #16a34a;
  --shadow: 0 10px 24px rgba(15, 23, 42, 0.08);
  --keyboard-offset: 0px;
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  letter-spacing: 0;
}

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

button {
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  color: var(--ink);
  padding: 0 14px;
  cursor: pointer;
}

button.primary,
button[type="submit"] {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

button:disabled {
  color: var(--muted);
  cursor: not-allowed;
  opacity: 0.65;
}

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

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  color: var(--ink);
  padding: 10px 12px;
}

textarea {
  resize: vertical;
}

h1,
h2,
h3,
p {
  margin-top: 0;
}

.login-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
}

.login-panel {
  width: min(360px, calc(100vw - 32px));
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 24px;
  box-shadow: var(--shadow);
}

.login-panel h1 {
  margin: 0 0 20px;
  font-size: 24px;
}

.login-panel form,
.dialog-body {
  display: grid;
  gap: 14px;
}

.error {
  color: var(--danger);
  margin: 0;
}

.app-shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: minmax(300px, 360px) minmax(0, 1fr);
  background: var(--bg);
}

.nav-drawer {
  display: block;
  position: static;
  width: auto;
  height: auto;
  max-width: none;
  max-height: none;
  margin: 0;
  padding: 0;
  border: 0;
  background: transparent;
}

.nav-drawer::backdrop {
  display: none;
}

.sidebar {
  height: 100vh;
  border-right: 1px solid var(--line);
  background: var(--panel);
  padding: 18px;
  display: grid;
  align-content: start;
  gap: 20px;
  overflow-y: auto;
}

.app-topbar,
.section-heading,
.dialog-body header,
.dialog-body footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.brand-mark {
  width: 34px;
  height: 34px;
  border-radius: 12px;
  background: var(--accent-soft);
  border: 1px solid #b6ebe2;
  position: relative;
}

.brand-mark::before {
  content: "";
  position: absolute;
  inset: 8px;
  border: 3px solid var(--accent);
  border-radius: 8px;
}

.brand-mark::after {
  content: "";
  position: absolute;
  width: 16px;
  height: 4px;
  left: 9px;
  top: 15px;
  border-radius: 999px;
  background: var(--accent);
}

.brand-copy {
  min-width: 0;
  flex: 1;
  display: grid;
  gap: 4px;
}

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

.connection-pill {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  width: fit-content;
  min-height: 28px;
  color: var(--muted);
  white-space: nowrap;
  font-size: 13px;
}

.connection-pill::before {
  content: "";
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: #d0d5dd;
}

.connection-pill.online::before {
  background: #22c55e;
}

.sidebar h2 {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  text-transform: uppercase;
}

#new-session {
  min-height: 34px;
  border-radius: 999px;
  color: var(--accent);
  border-color: #b7e5de;
  background: var(--accent-soft);
  font-weight: 700;
}

.list {
  display: grid;
  gap: 8px;
}

.list-item {
  width: 100%;
  min-height: 64px;
  padding: 12px;
  border-radius: 12px;
  text-align: left;
  display: grid;
  gap: 4px;
  position: relative;
  background: #fff;
}

.list-item strong {
  font-size: 15px;
  line-height: 1.15;
}

.list-item.active {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--accent);
}

.session-dot {
  position: absolute;
  top: 14px;
  right: 14px;
  width: 10px;
  height: 10px;
  border-radius: 999px;
  background: #d0d5dd;
}

.session-dot.online {
  background: var(--accent);
}

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

.status-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 6px;
  background: #d0d5dd;
}

.status-dot.online {
  background: var(--success);
}

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

.mobile-header {
  display: none;
}

.session-header {
  min-height: 86px;
  padding: 16px 22px;
  border-bottom: 3px solid var(--accent);
  background: var(--panel-soft);
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 14px;
}

.session-icon {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  background: var(--accent);
  color: #fff;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 26px;
  font-weight: 800;
  box-shadow: inset 0 0 0 4px #dbeafe;
}

.session-title-block {
  min-width: 0;
  display: grid;
  gap: 8px;
}

.session-title-block > strong {
  font-size: 22px;
  line-height: 1.1;
}

.session-meta {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

.chip,
.badge {
  border-radius: 999px;
  background: #eef2f6;
  color: #344054;
  padding: 4px 10px;
  font-size: 13px;
}

.chip.label {
  background: transparent;
  padding-left: 0;
  padding-right: 0;
  color: var(--muted);
}

.chip.success,
.badge.success {
  border: 1px solid #b7e4d0;
  background: #e9f8f2;
  color: #087568;
}

.chip.queued,
.badge.queued {
  background: var(--queued-soft);
  color: var(--queued);
  font-weight: 700;
}

.chip.danger,
.badge.danger {
  background: var(--danger-soft);
  color: var(--danger);
}

.transcript {
  min-height: 0;
  overflow-y: auto;
  padding: 18px 22px;
  display: grid;
  align-content: start;
  gap: 0;
  background: #fff;
}

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

.message {
  display: grid;
  grid-template-columns: 42px minmax(0, 1fr);
  gap: 14px;
  padding: 18px 0;
  border-bottom: 1px solid var(--line-soft);
}

.message-avatar {
  width: 42px;
  height: 42px;
  border-radius: 999px;
  display: grid;
  place-items: center;
  font-weight: 800;
  color: #fff;
  background: #082033;
}

.message-avatar.user {
  background: var(--accent);
}

.message-main {
  min-width: 0;
  display: grid;
  gap: 10px;
}

.message-header {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
  color: var(--muted);
  font-size: 12px;
}

.message-header strong {
  color: var(--accent);
  font-size: 16px;
}

.message-time {
  margin-left: auto;
  color: var(--muted);
}

.message-content {
  overflow-wrap: anywhere;
  font-size: 15px;
  line-height: 1.5;
}

.message-content.text {
  white-space: pre-wrap;
}

.message-content.markdown {
  white-space: normal;
}

.message.assistant .message-content:not(:empty) {
  width: fit-content;
  max-width: 100%;
  padding: 8px 12px;
  border: 1px solid #b7e4d0;
  border-radius: 10px;
  background: #effbf5;
}

.message.assistant .message-content.text:not(:empty) {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.render-toggle {
  display: inline-flex;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
}

.render-toggle button {
  min-height: 24px;
  padding: 0 8px;
  border: 0;
  border-radius: 0;
  background: transparent;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
}

.render-toggle button.active {
  background: var(--accent);
  color: #fff;
}

.markdown-body {
  display: grid;
  gap: 8px;
}

.markdown-body > * {
  margin: 0;
}

.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4,
.markdown-body h5,
.markdown-body h6 {
  color: var(--ink);
  line-height: 1.2;
}

.markdown-body h1 {
  font-size: 1.35em;
}

.markdown-body h2 {
  font-size: 1.22em;
}

.markdown-body h3 {
  font-size: 1.1em;
}

.markdown-body ul,
.markdown-body ol {
  display: grid;
  gap: 4px;
  padding-left: 1.25rem;
}

.markdown-body blockquote {
  border-left: 3px solid #9fd8bf;
  padding-left: 10px;
  color: var(--muted);
}

.markdown-body code {
  border: 1px solid #cfe6da;
  border-radius: 6px;
  background: #fff;
  padding: 1px 5px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.92em;
}

.markdown-body pre {
  max-width: 100%;
  overflow-x: auto;
  border: 1px solid #cfe6da;
  border-radius: 8px;
  background: #fff;
  padding: 10px;
}

.markdown-body pre code {
  border: 0;
  background: transparent;
  padding: 0;
  white-space: pre;
}

.markdown-body table {
  display: block;
  max-width: 100%;
  overflow-x: auto;
  border-collapse: collapse;
}

.markdown-body th,
.markdown-body td {
  border: 1px solid #cfe6da;
  padding: 5px 7px;
  text-align: left;
}

.markdown-body a {
  color: var(--accent);
  font-weight: 700;
}

.tool-log {
  display: grid;
  gap: 6px;
}

.tool-log summary {
  cursor: pointer;
  width: fit-content;
  border: 1px solid #bbf7d0;
  border-radius: 999px;
  background: #f0fdf4;
  color: #15803d;
  padding: 4px 10px;
  font-size: 12px;
  font-weight: 700;
}

.tool-row {
  border: 1px solid #cfe0ff;
  border-radius: 10px;
  background: var(--blue-soft);
  padding: 8px 10px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
  color: #1f2937;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.tool-row.tool-started {
  color: #1d4ed8;
  font-weight: 700;
}

.tool-row.tool-status {
  border-color: #bbf7d0;
  background: #f0fdf4;
  color: #15803d;
  font-family: inherit;
  font-weight: 700;
}

.tool-row.tool-finished {
  width: fit-content;
  color: var(--muted);
  background: #fff;
}

.artifact-grid,
.pending-attachments {
  display: grid;
  gap: 8px;
}

.artifact-card {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px;
  display: grid;
  grid-template-columns: minmax(74px, 112px) minmax(0, 1fr);
  align-items: center;
  gap: 10px;
  background: #fbfcfd;
}

.artifact-preview {
  min-width: 0;
}

.artifact-preview img,
.artifact-preview video {
  width: 100%;
  aspect-ratio: 4 / 3;
  object-fit: cover;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
}

.artifact-preview audio,
.artifact-preview iframe {
  max-width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
}

.artifact-summary {
  min-width: 0;
  display: grid;
  gap: 6px;
}

.artifact-summary strong {
  overflow-wrap: anywhere;
}

.artifact-meta {
  color: var(--muted);
  font-size: 13px;
}

.artifact-kind {
  width: fit-content;
  border: 1px solid #b7e4d0;
  border-radius: 8px;
  background: #e9f8f2;
  color: #087568;
  padding: 2px 8px;
  font-size: 12px;
  text-transform: capitalize;
}

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

.text-preview {
  max-height: 220px;
  overflow: auto;
  margin: 0;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  white-space: pre-wrap;
}

#jump-latest {
  position: absolute;
  right: 22px;
  bottom: calc(var(--composer-height, 84px) + 12px);
  z-index: 5;
  min-height: 34px;
  padding: 0 14px;
  border-radius: 999px;
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.14);
  background: #fff;
}

#jump-latest[hidden] {
  display: none;
}

#jump-latest::after {
  content: " v";
}

.composer {
  border-top: 1px solid var(--line);
  background: var(--panel);
  padding: 14px 22px;
  display: grid;
  gap: 10px;
}

.composer-row {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto auto auto auto;
  gap: 10px;
  align-items: end;
}

#message-input {
  min-height: 48px;
  max-height: 160px;
  border-radius: 16px;
  font-size: 16px;
}

dialog {
  width: min(520px, calc(100vw - 32px));
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 0;
}

.dialog-body {
  padding: 18px;
}

.dialog-body h2 {
  margin: 0;
  font-size: 18px;
}

@media (max-width: 760px) {
  html,
  body {
    height: 100%;
    overflow: hidden;
  }

  .app-shell {
    height: calc(100dvh - var(--keyboard-offset));
    min-height: 0;
    grid-template-columns: 1fr;
    overflow: hidden;
  }

  .nav-drawer:not([open]) {
    display: none;
  }

  .nav-drawer[open] {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(340px, 88vw);
    max-width: none;
    height: 100dvh;
    max-height: none;
    margin: 0;
    border: 0;
    border-radius: 0;
    background: #fff;
    box-shadow: 18px 0 48px rgba(15, 23, 42, 0.2);
  }

  .nav-drawer::backdrop {
    display: block;
    background: rgba(15, 23, 42, 0.32);
  }

  .sidebar {
    height: 100%;
    border-right: 0;
    padding: max(18px, env(safe-area-inset-top)) 14px max(18px, env(safe-area-inset-bottom));
    gap: 14px;
  }

  .app-topbar {
    min-height: 58px;
  }

  .brand-copy h1 {
    font-size: 17px;
  }

  .connection-pill {
    font-size: 13px;
  }

  .section-heading {
    min-height: 34px;
  }

  .machine-section {
    display: grid;
  }

  .main {
    height: 100%;
    min-height: 0;
    grid-template-rows: 44px minmax(0, 1fr) auto;
    overflow: hidden;
  }

  .mobile-header {
    height: 44px;
    min-height: 44px;
    display: grid;
    grid-template-columns: 40px minmax(0, 1fr) auto 34px;
    align-items: center;
    gap: 8px;
    padding: max(0px, env(safe-area-inset-top)) 10px 0;
    border-bottom: 1px solid var(--line-soft);
    background: #fff;
  }

  .mobile-menu-button,
  .mobile-overflow-button {
    width: 34px;
    min-width: 34px;
    height: 34px;
    min-height: 34px;
    padding: 0;
    border: 0;
    background: transparent;
    position: relative;
  }

  .mobile-menu-button span,
  .mobile-menu-button::before,
  .mobile-menu-button::after {
    content: "";
    display: block;
    width: 18px;
    height: 2px;
    margin: 4px auto;
    border-radius: 999px;
    background: var(--ink);
  }

  #mobile-title {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 15px;
  }

  .mobile-status-dot {
    width: 9px;
    height: 9px;
    border-radius: 999px;
    background: #d0d5dd;
  }

  .mobile-status-dot.success {
    background: var(--success);
  }

  .mobile-status-dot.queued {
    background: var(--queued);
  }

  .mobile-status-dot.danger {
    background: var(--danger);
  }

  .mobile-overflow-button::before {
    content: "";
    position: absolute;
    left: 14px;
    top: 8px;
    width: 5px;
    height: 5px;
    border-radius: 999px;
    background: var(--ink);
    box-shadow: 0 8px 0 var(--ink), 0 16px 0 var(--ink);
  }

  .session-header {
    display: none;
  }

  .transcript {
    min-height: 0;
    padding: 0 12px 10px;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }

  .message {
    grid-template-columns: minmax(0, 1fr);
    gap: 0;
    padding: 12px 0;
  }

  .message-avatar {
    display: none;
  }

  .message-main {
    gap: 7px;
  }

  .message-header {
    gap: 6px;
    font-size: 11px;
  }

  .render-toggle button {
    min-height: 22px;
    padding: 0 7px;
    font-size: 10px;
  }

  .message-header::before {
    content: "U";
    display: inline-grid;
    place-items: center;
    width: 20px;
    height: 20px;
    border-radius: 999px;
    background: var(--accent);
    color: #fff;
    font-size: 10px;
    font-weight: 800;
  }

  .message.assistant .message-header::before {
    content: "AI";
    background: #082033;
  }

  .message-header strong {
    font-size: 14px;
  }

  .message-content {
    font-size: 13px;
    line-height: 1.38;
  }

  .message.assistant .message-content:not(:empty) {
    padding: 7px 9px;
    border-radius: 9px;
    font-size: 13px;
  }

  .badge {
    padding: 2px 7px;
    font-size: 11px;
  }

  .tool-log {
    gap: 5px;
  }

  .tool-log summary {
    padding: 3px 8px;
    font-size: 11px;
  }

  .tool-row {
    padding: 5px 7px;
    border-radius: 9px;
    font-size: 10px;
  }

  .artifact-card {
    grid-template-columns: 78px minmax(0, 1fr);
    gap: 8px;
    padding: 7px;
    border-radius: 12px;
    background: #fff;
  }

  .artifact-summary {
    gap: 4px;
  }

  .artifact-summary strong {
    font-size: 13px;
  }

  .artifact-meta,
  .artifact-actions a,
  .artifact-kind {
    font-size: 11px;
  }

  #jump-latest {
    right: 12px;
    bottom: calc(var(--composer-height, 60px) + env(safe-area-inset-bottom) + 8px);
    min-height: 30px;
    padding: 0 10px;
    font-size: 12px;
  }

  .composer {
    position: relative;
    z-index: 4;
    padding: 7px max(9px, env(safe-area-inset-right)) max(9px, env(safe-area-inset-bottom)) max(9px, env(safe-area-inset-left));
  }

  .composer-row {
    grid-template-columns: 40px minmax(0, 1fr) repeat(4, 38px);
    gap: 7px;
    align-items: center;
  }

  #attach,
  #send,
  #queue,
  #steer,
  #stop {
    width: 38px;
    min-width: 38px;
    height: 40px;
    min-height: 40px;
    padding: 0;
    border-radius: 14px;
    font-size: 0;
  }

  #attach {
    width: 40px;
    min-width: 40px;
  }

  #attach::before {
    content: "+";
    font-size: 22px;
    font-weight: 800;
    color: var(--ink);
  }

  #message-input {
    min-height: 40px;
    max-height: 88px;
    padding: 9px 12px;
    border-radius: 15px;
    font-size: 16px;
  }

  #send::before {
    content: ">";
    font-size: 26px;
  }

  #queue::before {
    content: "Q";
    font-size: 13px;
    font-weight: 800;
  }

  #steer::before {
    content: "S";
    font-size: 13px;
    font-weight: 800;
  }

  #stop::before {
    content: "X";
    font-size: 13px;
    font-weight: 800;
  }

  dialog#new-session-dialog {
    width: min(360px, calc(100vw - 24px));
  }
}
