:root {
  color-scheme: dark;
  --bg: #0b0d10;
  --panel: #11141a;
  --panel-2: #171b22;
  --line: #242a35;
  --text: #eff3f8;
  --muted: #9aa4b2;
  --accent: #76b900;
  --accent-2: #99e625;
  --danger: #ff6363;
  --shadow: 0 18px 70px rgba(0, 0, 0, 0.28);
  --glow: 0 0 42px rgba(118, 185, 0, 0.22);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  min-width: 320px;
  height: 100%;
  overflow: hidden;
}

body {
  margin: 0;
  display: grid;
  grid-template-rows: auto 1fr;
  background:
    radial-gradient(circle at 20% 10%, rgba(153, 230, 37, 0.13), transparent 16rem),
    radial-gradient(circle at 85% 5%, rgba(118, 185, 0, 0.18), transparent 20rem),
    radial-gradient(circle at 55% 100%, rgba(37, 99, 235, 0.12), transparent 22rem),
    var(--bg);
  color: var(--text);
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background-image:
    linear-gradient(rgba(255,255,255,.025) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255,255,255,.025) 1px, transparent 1px);
  background-size: 44px 44px;
  mask-image: linear-gradient(to bottom, rgba(0,0,0,.55), transparent 78%);
}

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

button {
  touch-action: manipulation;
}

.sidebar {
  z-index: 5;
  border-bottom: 1px solid var(--line);
  background: linear-gradient(180deg, rgba(20, 25, 32, 0.94), rgba(10, 12, 16, 0.92));
  backdrop-filter: blur(16px);
  padding: max(12px, env(safe-area-inset-top)) 12px 12px;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: center;
}

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

.brand strong,
.brand span {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.brand span,
.meta span,
.topbar p {
  color: var(--muted);
  font-size: 12px;
}

.logo,
.empty-logo {
  width: 38px;
  height: 38px;
  flex: 0 0 auto;
  border-radius: 13px;
  display: grid;
  place-items: center;
  background: conic-gradient(from 210deg, #99e625, #76b900, #1b2e05, #99e625);
  color: white;
  font-weight: 900;
  box-shadow: var(--glow);
}

.new-chat,
.tab,
.save-agent,
.attach-button,
.composer button,
.prompts button,
.settings-actions button {
  min-height: 44px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  border-radius: 14px;
  padding: 11px 13px;
  cursor: pointer;
}

.new-chat {
  white-space: nowrap;
}

.tabs {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.tab {
  width: 100%;
  text-align: center;
}

.tab.active {
  border-color: rgba(118, 185, 0, 0.75);
  background: rgba(118, 185, 0, 0.14);
}

.new-chat:hover,
.tab:hover,
.save-agent:hover,
.attach-button:hover,
.prompts button:hover,
.settings-actions button:hover {
  border-color: rgba(118, 185, 0, 0.55);
}

.status,
.meta {
  display: none;
}

#statusDot {
  width: 9px;
  height: 9px;
  border-radius: 999px;
  background: #777;
}

#statusDot.ok {
  background: var(--accent-2);
  box-shadow: 0 0 14px rgba(153, 230, 37, 0.8);
}

#statusDot.err {
  background: var(--danger);
}

.app {
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

.view {
  display: none;
  height: 100%;
  min-height: 0;
  min-width: 0;
}

.view.active {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
}

.settings-view.active {
  grid-template-rows: auto minmax(0, 1fr);
}

.topbar {
  padding: 14px 14px 12px;
  border-bottom: 1px solid var(--line);
  display: grid;
  gap: 12px;
  background: linear-gradient(90deg, rgba(255,255,255,.03), transparent);
}

.topbar h1,
.empty h2 {
  margin: 0;
}

.topbar h1 {
  font-size: clamp(1.2rem, 6vw, 1.8rem);
  line-height: 1.1;
}

select,
input {
  width: 100%;
  border: 1px solid var(--line);
  outline: 0;
  background: var(--panel-2);
  color: var(--text);
  border-radius: 14px;
  padding: 12px 14px;
}

.messages {
  min-height: 0;
  overflow: auto;
  padding: 16px 12px 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  overscroll-behavior: contain;
}

.empty {
  margin: auto;
  width: min(100%, 720px);
  text-align: center;
  display: grid;
  gap: 18px;
  justify-items: center;
  padding: 18px 4px;
}

.empty h2 {
  font-size: clamp(1.35rem, 7vw, 2rem);
}

.prompts {
  width: 100%;
  display: grid;
  gap: 10px;
}

.message {
  width: 100%;
  display: grid;
  gap: 7px;
}

.message.user {
  align-self: flex-end;
}

.message.assistant {
  align-self: flex-start;
}

.bubble {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  line-height: 1.55;
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 13px 14px;
  background: linear-gradient(145deg, rgba(23, 27, 34, 0.98), rgba(11, 13, 16, 0.96));
  box-shadow: var(--shadow);
}

.user .bubble {
  background: linear-gradient(145deg, rgba(29, 37, 18, 0.98), rgba(11, 20, 7, 0.96));
  border-color: rgba(118, 185, 0, 0.35);
}

.assistant.loading .bubble {
  display: flex;
  gap: 10px;
  align-items: center;
  border-color: rgba(153, 230, 37, 0.38);
  box-shadow: var(--shadow), var(--glow);
}

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

.typing-spinner i {
  width: 8px;
  height: 8px;
  border-radius: 999px;
  background: var(--accent-2);
  animation: typingPulse 0.9s infinite ease-in-out;
}

.typing-spinner i:nth-child(2) {
  animation-delay: 0.14s;
}

.typing-spinner i:nth-child(3) {
  animation-delay: 0.28s;
}

@keyframes typingPulse {
  0%, 80%, 100% {
    transform: translateY(0);
    opacity: 0.38;
  }
  40% {
    transform: translateY(-5px);
    opacity: 1;
  }
}

.message-attachments {
  margin-top: 12px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.message-attachments span {
  border: 1px solid rgba(153, 230, 37, 0.22);
  border-radius: 999px;
  background: rgba(118, 185, 0, 0.08);
  color: var(--muted);
  padding: 6px 9px;
  font-size: 12px;
}

.role {
  font-size: 12px;
  color: var(--muted);
  padding: 0 6px;
}

.composer {
  margin: 0 10px max(10px, env(safe-area-inset-bottom));
  border: 1px solid var(--line);
  background: linear-gradient(145deg, rgba(17, 20, 26, 0.98), rgba(8, 10, 13, 0.98));
  border-radius: 20px;
  padding: 8px;
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 8px;
  align-items: end;
  box-shadow: var(--shadow), 0 0 0 1px rgba(118, 185, 0, 0.07);
}

.attach-button {
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  padding: 0;
  font-size: 1.35rem;
  font-weight: 700;
}

.attach-button input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.attachments-preview {
  margin: 0 10px 8px;
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 2px 0;
}

.attachment-chip {
  flex: 0 0 auto;
  max-width: min(78vw, 360px);
  display: grid;
  grid-template-columns: auto 1fr auto auto;
  gap: 7px;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(17, 20, 26, 0.96);
  padding: 7px 9px;
  color: var(--muted);
  font-size: 12px;
}

.attachment-chip strong {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text);
}

.attachment-chip button {
  width: 26px;
  height: 26px;
  border: 0;
  border-radius: 999px;
  background: var(--panel-2);
  color: var(--text);
}

textarea {
  min-width: 0;
  min-height: 44px;
  max-height: 35dvh;
  flex: 1;
  resize: none;
  border: 0;
  outline: 0;
  padding: 11px 10px;
  background: transparent;
  color: var(--text);
}

.composer button {
  background: var(--accent);
  color: #061000;
  border-color: transparent;
  font-weight: 800;
}

.composer button:disabled {
  opacity: 0.55;
  cursor: wait;
}

.agent-form {
  min-height: 0;
  overflow: auto;
  padding: 16px 12px max(24px, env(safe-area-inset-bottom));
  display: grid;
  gap: 16px;
}

.agent-form label {
  display: grid;
  gap: 8px;
}

.agent-form label span {
  color: var(--muted);
  font-size: 13px;
}

.agent-form textarea {
  min-height: 140px;
  max-height: none;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--panel);
}

.save-agent {
  width: 100%;
  background: var(--accent);
  color: #061000;
  border-color: transparent;
  font-weight: 800;
}

.settings-actions {
  display: grid;
  gap: 10px;
}

.agent-preview {
  margin: 0;
  max-height: 44dvh;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 14px;
  background: #080a0d;
  color: var(--muted);
  line-height: 1.45;
  overflow: auto;
}

.api-keys-panel {
  display: grid;
  gap: 14px;
  border: 1px solid var(--line);
  border-radius: 20px;
  background: linear-gradient(145deg, rgba(255, 255, 255, 0.04), rgba(118, 185, 0, 0.025));
  padding: 14px;
}

.api-keys-panel h2 {
  margin: 0 0 4px;
  font-size: 1.1rem;
}

.api-keys-panel p,
.api-keys-status,
.muted {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
}

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

.api-key-card {
  display: grid;
  gap: 10px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--panel);
  padding: 12px;
}

.api-key-card div {
  min-width: 0;
  display: grid;
  gap: 4px;
}

.api-key-card code {
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--accent-2);
}

.token-box {
  display: block;
  width: 100%;
  overflow-wrap: anywhere;
  border: 1px solid rgba(153, 230, 37, 0.24);
  border-radius: 14px;
  background: rgba(0, 0, 0, 0.24);
  padding: 12px;
  color: var(--accent-2);
}

.api-key-card small {
  color: var(--muted);
}

.api-key-card .key-error {
  color: #ffb1b1;
}

.api-key-card button,
.api-key-add button {
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel-2);
  color: var(--text);
}

.api-key-toggle {
  display: flex !important;
  grid-template-columns: none !important;
  flex-direction: row;
  gap: 8px !important;
  align-items: center;
}

.api-key-toggle input {
  width: auto;
}

.api-key-add {
  display: grid;
  gap: 10px;
}

.api-key-add button {
  background: var(--accent);
  color: #061000;
  border-color: transparent;
  font-weight: 800;
}

@media (min-width: 560px) {
  .sidebar {
    grid-template-columns: 1fr auto;
    padding-inline: 18px;
  }

  .tabs {
    max-width: 360px;
  }

  .topbar {
    grid-template-columns: 1fr auto;
    align-items: center;
    padding: 18px 24px;
  }

  select {
    width: auto;
    min-width: 210px;
  }

  .messages {
    padding: 24px;
  }

  .message {
    width: min(900px, 92%);
  }

  .prompts,
  .settings-actions {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
  }

  .composer {
    margin: 0 24px 22px;
  }

  .attachments-preview {
    margin: 0 24px 8px;
  }

  .agent-form {
    padding: 24px;
    max-width: 980px;
  }

  .api-key-card {
    grid-template-columns: auto 1fr auto;
    align-items: center;
  }

  .api-key-add {
    grid-template-columns: minmax(180px, 0.7fr) minmax(260px, 1fr) auto;
    align-items: center;
  }

  .save-agent {
    width: auto;
  }
}

@media (min-width: 920px) {
  body {
    grid-template-columns: 280px 1fr;
    grid-template-rows: 1fr;
  }

  .sidebar {
    min-height: 100dvh;
    border-right: 1px solid var(--line);
    border-bottom: 0;
    padding: 18px;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 18px;
  }

  .logo {
    width: 42px;
    height: 42px;
  }

  .tabs {
    max-width: none;
    display: grid;
    grid-template-columns: 1fr;
  }

  .tab {
    text-align: left;
  }

  .status {
    margin-top: auto;
    display: flex;
    gap: 8px;
    align-items: center;
    color: var(--muted);
    font-size: 13px;
  }

  .meta {
    display: grid;
    gap: 5px;
    padding: 14px;
    border: 1px solid var(--line);
    border-radius: 16px;
    background: rgba(255, 255, 255, 0.02);
  }

  .view {
    height: 100dvh;
  }

  .messages {
    padding: 28px;
  }

  .composer {
    margin: 0 28px 24px;
  }

  .attachments-preview {
    margin: 0 28px 8px;
  }

  .agent-form {
    padding: 28px;
  }
}

@media (display-mode: standalone) {
  .sidebar {
    padding-top: max(14px, env(safe-area-inset-top));
  }
}
