@layer reset, tokens, base, specimen;

@layer reset {
  *, *::before, *::after { box-sizing: border-box; }
  html { color-scheme: light dark; }
  body, h1, h2, h3, p { margin: 0; }
  button, input, select, textarea { font: inherit; }
  img, svg { display: block; max-width: 100%; }
}

@layer tokens {
  :root {
    --background: #ffffff;
    --foreground: #0a0a0a;
    --card: #ffffff;
    --card-foreground: #0a0a0a;
    --popover: #ffffff;
    --popover-foreground: #0a0a0a;
    --primary: #171717;
    --primary-foreground: #fafafa;
    --secondary: #f5f5f5;
    --secondary-foreground: #171717;
    --muted: #f5f5f5;
    --muted-foreground: #737373;
    --accent: #f5f5f5;
    --accent-foreground: #171717;
    --destructive: #e7000b;
    --destructive-foreground: #ffffff;
    --border: #e5e5e5;
    --input: #e5e5e5;
    --ring: #a1a1a1;
    --radius: 0.625rem;
    --shadow-xs: 0 1px 2px 0 rgb(0 0 0 / 0.05);
    --shadow-sm: 0 1px 3px 0 rgb(0 0 0 / 0.1), 0 1px 2px -1px rgb(0 0 0 / 0.1);
  }

  @media (prefers-color-scheme: dark) {
    :root {
      --background: #0a0a0a;
      --foreground: #fafafa;
      --card: #171717;
      --card-foreground: #fafafa;
      --popover: #262626;
      --popover-foreground: #fafafa;
      --primary: #e5e5e5;
      --primary-foreground: #171717;
      --secondary: #262626;
      --secondary-foreground: #fafafa;
      --muted: #262626;
      --muted-foreground: #a1a1a1;
      --accent: #404040;
      --accent-foreground: #fafafa;
      --destructive: #ff6467;
      --destructive-foreground: #171717;
      --border: rgb(255 255 255 / 0.1);
      --input: rgb(255 255 255 / 0.15);
      --ring: #737373;
    }
  }
}

@layer base {
  html { min-width: 320px; background: var(--background); }
  body {
    min-height: 100vh;
    background: var(--background);
    color: var(--foreground);
    font-family: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    font-size: 14px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
  }
  :focus-visible { outline: 2px solid var(--ring); outline-offset: 2px; }
  ::selection { background: var(--primary); color: var(--primary-foreground); }
}

@layer specimen {
  .specimen {
    width: min(100%, 960px);
    min-height: 100vh;
    margin-inline: auto;
    padding: clamp(24px, 5vw, 64px);
    display: grid;
    align-content: start;
    gap: 28px;
  }
  .specimen-header { display: grid; gap: 5px; }
  .specimen-header h1 { font-size: 24px; line-height: 1.25; letter-spacing: -0.025em; font-weight: 650; }
  .specimen-header > p:last-child { max-width: 65ch; color: var(--muted-foreground); }
  .specimen-kicker { color: var(--muted-foreground); font-size: 12px; font-weight: 600; letter-spacing: .08em; text-transform: uppercase; }
  .atom-stage {
    min-height: 360px;
    border: 1px solid var(--border);
    border-radius: calc(var(--radius) + 4px);
    background-color: var(--background);
    background-image: radial-gradient(var(--border) .7px, transparent .7px);
    background-size: 16px 16px;
    padding: clamp(24px, 6vw, 64px);
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    gap: 20px;
    overflow: visible;
  }
  .sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
  }
  @media (max-width: 540px) {
    .specimen { padding: 20px 14px; }
    .atom-stage { min-height: 320px; padding: 24px 16px; }
  }
  @media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
  }
}

@layer app {
  body { padding: 32px 20px; }
  a { color: inherit; text-underline-offset: 4px; }
  a:hover { color: var(--muted-foreground); }
  .container { width: min(100%, 960px); margin: 0 auto; }
  .narrow { max-width: 640px; }
  .chat { max-width: 800px; }
  .container > nav { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 40px; }
  nav ul { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; margin: 0; padding: 0; list-style: none; }
  nav strong { font-size: 16px; font-weight: 650; }
  .page-nav { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 40px; }
  .brand { font-size: 16px; font-weight: 650; text-decoration: none; }
  .nav-links, .action-row { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; }
  .page-header { display: grid; gap: 8px; margin-bottom: 28px; }
  h1, h2, h3 { margin: 0; letter-spacing: -.025em; }
  h1 { font-size: clamp(24px, 4vw, 30px); line-height: 1.2; font-weight: 650; }
  h2 { font-size: 16px; line-height: 1.4; font-weight: 600; }
  p { margin: 0; }
  .muted, small { color: var(--muted-foreground); }
  .stack { display: grid; gap: 20px; }
  .stack-sm { display: grid; gap: 10px; }

  section { display: grid; gap: 12px; margin-top: 32px; }
  article { display: grid; gap: 10px; padding: 16px; border: 1px solid var(--border); border-radius: calc(var(--radius) + 2px); background: var(--card); box-shadow: var(--shadow-xs); }
  article > header { display: flex; justify-content: space-between; gap: 12px; }
  form { display: grid; gap: 16px; }
  label { display: grid; gap: 6px; font-size: 14px; font-weight: 500; }
  input, select, textarea { width: 100%; min-width: 0; border: 1px solid var(--input); border-radius: var(--radius); outline: none; background: transparent; color: var(--foreground); padding: 7px 10px; font: inherit; }
  input, select { height: 36px; }
  textarea { min-height: 96px; resize: vertical; }
  input:focus-visible, select:focus-visible, textarea:focus-visible { border-color: var(--ring); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ring) 45%, transparent); }
  input:disabled, select:disabled, textarea:disabled, button:disabled { cursor: not-allowed; opacity: .5; }
  button, input[type="submit"], [role="button"] { display: inline-flex; width: fit-content; min-height: 36px; align-items: center; justify-content: center; border: 1px solid transparent; border-radius: var(--radius); padding: 7px 12px; background: var(--primary); color: var(--primary-foreground); font: inherit; font-weight: 500; line-height: 20px; text-decoration: none; cursor: pointer; }
  button:hover:not(:disabled), input[type="submit"]:hover:not(:disabled), [role="button"]:hover { background: color-mix(in srgb, var(--primary) 80%, transparent); color: var(--primary-foreground); }
  button.secondary, button.outline { border-color: var(--border); background: transparent; color: var(--foreground); }
  button.secondary:hover:not(:disabled), button.outline:hover:not(:disabled) { background: var(--muted); }
  details { padding: 12px; border: 1px solid var(--border); border-radius: var(--radius); }
  summary { cursor: pointer; font-weight: 500; }
  details > label:first-of-type { margin-top: 14px; }
  details > label { margin-top: 12px; }
  progress { width: 100%; height: 4px; accent-color: var(--primary); }
  code { padding: 1px 4px; border-radius: 4px; background: var(--muted); font-size: .9em; overflow-wrap: anywhere; }
  .tool-command { margin: -4px 0 0; white-space: pre-wrap; overflow-wrap: anywhere; }
  .tool-command code { display: block; padding: 8px 10px; line-height: 1.5; }
  .tool-result { display: grid; gap: 8px; margin-top: 10px; }
  .tool-result-text { display: block; width: fit-content; max-width: 100%; }
  .tool-result-images { display: grid; gap: 10px; }
  .tool-result-image-frame { display: grid; width: fit-content; max-width: 100%; gap: 6px; margin: 0; }
  .tool-result-image { width: auto; max-width: 100%; max-height: 34rem; border: 1px solid var(--border); border-radius: var(--radius); background: var(--muted); object-fit: contain; }
  .tool-result-image-frame figcaption { color: var(--muted-foreground); font-size: 12px; }
  .tool-activity-failed { border-color: color-mix(in srgb, var(--destructive) 45%, var(--border)); }
  .tool-activity-failed > summary { color: var(--destructive); }
  details.select-root { position: relative; width: 100% !important; padding: 0; border: 0; background: transparent; }
  details.select-root > summary { list-style: none; }
  details.select-root > summary::-webkit-details-marker { display: none; }
  .select-root .select-trigger { display: flex; width: 100% !important; min-height: 36px; align-items: center; justify-content: space-between; gap: 8px; border: 1px solid var(--input); border-radius: var(--radius); padding: 7px 10px; cursor: pointer; }
  .select-root .select-content { position: absolute; z-index: 20; top: calc(100% + 5px); width: 100% !important; max-height: 20rem; overflow-y: auto; overflow-x: hidden; -webkit-overflow-scrolling: touch; touch-action: pan-y; border: 1px solid var(--border); border-radius: var(--radius); background: var(--popover); box-shadow: var(--shadow-sm); }
  .select-root .select-trigger > span:last-child { flex: 0 0 auto !important; }
  .select-root .select-item { display: flex !important; width: 100%; justify-content: flex-start; border: 0; padding: 9px 10px; background: transparent; color: var(--popover-foreground); text-align: left; cursor: pointer; }
  .select-root .select-item:hover { background: var(--accent); }
  .select-root .select-item > span { display: flex !important; width: 100%; align-items: baseline; justify-content: flex-start; gap: 8px; text-align: left; }
  .provider-select .select-item small { color: var(--muted-foreground); }
  .provider-select .select-item .provider-select-availability { color: var(--destructive); }
  .provider-select .select-item--disabled { cursor: not-allowed; opacity: .75; }
  .provider-select-input, .runtime-select-input { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }

  .chat-header { display: grid; gap: 8px; padding-bottom: 24px; border-bottom: 1px solid var(--border); }
  .external-chat-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; }
  .external-chat-list article > header { align-items: flex-start; }
  .external-chat-list .status-badge { flex: 0 0 auto; }
  .empty-state { place-items: center; gap: 16px; min-height: 240px; padding: 24px; border: 1px dashed var(--border); border-radius: calc(var(--radius) + 2px); text-align: center; }
  .empty-state p { max-width: 384px; }
  .transcript { gap: 12px; }
  .message { max-width: 85%; }
  .message.user { margin-left: auto; background: var(--secondary); }
  .message.assistant { margin-right: auto; }
  .queued-message { border-style: dashed; }
  .queued-message-status { display: flex; align-items: center; justify-content: flex-end; gap: 6px; margin-top: 8px; color: var(--muted-foreground); font-size: 12px; }
  .queued-message-dot { width: 6px; height: 6px; border-radius: 999px; background: currentColor; }
  .message.agent-monologue { border-style: dashed; background: color-mix(in srgb, var(--muted) 55%, transparent); color: var(--muted-foreground); box-shadow: none; }
  .assistant-working { display: flex; align-items: center; gap: 8px; color: var(--muted-foreground); }
  .activity, .configuration { margin: 0; }
  @keyframes working-spin { to { transform: rotate(360deg); } }
  .composer { margin-top: 32px; padding-top: 24px; border-top: 1px solid var(--border); }
  .composer-attachment-picker { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; }
  .composer-attachment-picker small { flex: 1 1 220px; }
  .attachment-picker-button { gap: 7px; }
  .attachment-picker-button svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
  .attachment-file-input { display: none; }
  .composer-attachment-list, .message-attachments { display: grid; gap: 8px; }
  .composer-attachment-list:empty { display: none; }
  .attachment-card { position: relative; display: flex; width: 100%; max-width: 420px; min-width: 0; align-items: center; gap: 8px; padding: 8px; border: 1px solid var(--border); border-radius: calc(var(--radius) + 6px); background: var(--card); color: var(--card-foreground); }
  .attachment-card[data-state="error"] { border-color: color-mix(in srgb, var(--destructive) 35%, var(--border)); }
  .attachment-media { display: flex; width: 40px; aspect-ratio: 1; flex: 0 0 auto; align-items: center; justify-content: center; border-radius: 10px; background: var(--muted); }
  .attachment-media svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
  .attachment-content { min-width: 0; flex: 1; }
  .attachment-title, .attachment-description { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .attachment-title { color: inherit; font-weight: 500; }
  .attachment-description { margin-top: 2px; color: var(--muted-foreground); font-size: 12px; }
  .attachment-card[data-state="error"] .attachment-description { color: var(--destructive); }
  .attachment-content progress { display: block; height: 3px; margin-top: 5px; }
  .attachment-actions { display: flex; flex: 0 0 auto; }
  button.attachment-action { width: 28px; min-height: 28px; height: 28px; padding: 0; border-radius: 999px; background: transparent; color: var(--foreground); }
  button.attachment-action:hover:not(:disabled) { background: var(--accent); color: var(--accent-foreground); }
  .attachment-action svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
  .attachment-spinner { width: 16px; height: 16px; border: 2px solid color-mix(in srgb, var(--foreground) 20%, transparent); border-top-color: var(--foreground); border-radius: 50%; animation: working-spin .7s linear infinite; }
  .attachment-file-icon { display: none; }
  .attachment-card:not([data-state="uploading"]) .attachment-spinner { display: none; }
  .attachment-card:not([data-state="uploading"]) .attachment-file-icon { display: block; }
  .attachment-card--transcript { max-width: 360px; background: var(--background); }
  .message-attachments { margin-top: 2px; }
  .runtime-controls { display: grid; grid-template-columns: minmax(0, 2fr) minmax(0, 2fr) minmax(0, 1fr); gap: 12px; }
  .runtime-controls label { min-width: 0; }
  .error { border-color: color-mix(in srgb, var(--destructive) 45%, var(--border)); color: var(--destructive); }
  .notice { border-color: color-mix(in srgb, #16a34a 45%, var(--border)); }
  .chat-link { display: block; font-weight: 600; text-decoration: none; }
  .chat-link:hover { text-decoration: underline; }
  .connection-header { display: flex; justify-content: space-between; gap: 1rem; }
  .connection-settings { grid-template-columns: minmax(12rem, 1fr) 7rem auto; align-items: end; gap: .5rem; margin-top: 1rem; }
  .connection-settings input, .connection-settings button { margin: 0; }
  .integration-account { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding-top: 12px; border-top: 1px solid var(--border); }
  .integration-account form { flex: 0 0 auto; }
  .memory-editor { padding: 20px; border: 1px solid var(--border); border-radius: calc(var(--radius) + 2px); background: var(--card); box-shadow: var(--shadow-xs); }
  .field-group { display: grid; gap: 20px; }
  .field-item { display: grid; gap: 8px; }
  .field-label { font-weight: 500; }
  .field-control { width: 100%; min-width: 0; border: 1px solid var(--input); border-radius: 8px; outline: none; background: transparent; color: var(--foreground); font: inherit; }
  .field-textarea { min-height: 12rem; padding: 8px 10px; resize: vertical; }
  .field-control:focus-visible { border-color: var(--ring); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ring) 50%, transparent); }
  .field-control[aria-invalid="true"] { border-color: var(--destructive); box-shadow: 0 0 0 3px color-mix(in srgb, var(--destructive) 20%, transparent); }
  .field-description { color: var(--muted-foreground); }
  .field-error { color: var(--destructive); }
  .field-actions { display: flex; justify-content: flex-end; }
  .field-button { display: inline-flex; width: fit-content; min-height: 36px; align-items: center; justify-content: center; border: 1px solid transparent; border-radius: 8px; padding: 7px 12px; font: inherit; font-weight: 500; cursor: pointer; }
  .field-button--primary { background: var(--primary); color: var(--primary-foreground); }
  .memory-card-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; }
  .status-badge { display: inline-flex; min-height: 22px; align-items: center; border-radius: 999px; padding: 2px 8px; font-size: 12px; font-weight: 600; white-space: nowrap; }
  .status-badge--outline { border: 1px solid var(--border); color: var(--muted-foreground); }
  .status-badge--success { border: 1px solid color-mix(in srgb, #16a34a 35%, var(--border)); background: color-mix(in srgb, #16a34a 10%, transparent); color: color-mix(in srgb, #16a34a 80%, var(--foreground)); }
  @media (max-width: 40rem) {
    body { padding: 20px 14px; }
    .container > nav { margin-bottom: 28px; }
    .connection-settings, .runtime-controls { grid-template-columns: 1fr; }
    .message { max-width: 100%; }
  }
}
