:root {
  --bg: #0a0c10;
  --surface: #12151c;
  --surface-2: #191d27;
  --surface-3: #1f2430;
  --border: rgba(255, 255, 255, 0.07);
  --border-strong: rgba(255, 255, 255, 0.16);
  --text: #edeff4;
  --text-dim: #939aad;
  --text-faint: #5c6272;
  --accent: #ff5a1f;
  --accent-bright: #ff7a3d;
  --accent-soft: rgba(255, 90, 31, 0.14);
  --amber: #ffc24b;
  --success: #34d399;
  --danger: #fb7185;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 18px;
  --radius-pill: 999px;
  --font-display: "Space Grotesk", "Inter", sans-serif;
  --font-body: "Inter", -apple-system, BlinkMacSystemFont, sans-serif;
  --sidebar-w: 288px;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  font-family: var(--font-body);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

.app {
  display: flex;
  height: 100vh;
  height: 100dvh;
  height: var(--app-vh, 100dvh);
  animation: app-in 0.5s ease both;
  position: relative;
  overflow: hidden;
}

@keyframes app-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

@media (prefers-reduced-motion: reduce) {
  .app { animation: none; }
}

.icon-btn {
  background: transparent;
  border: 1px solid var(--border-strong);
  color: var(--text-dim);
  width: 36px;
  height: 36px;
  min-width: 36px;
  padding: 0;
  border-radius: var(--radius-md);
  box-shadow: none;
}

.icon-btn svg { width: 17px; height: 17px; }

.icon-btn:hover {
  color: var(--text);
  border-color: var(--text-faint);
  background: var(--surface-2);
  transform: none;
  box-shadow: none;
  filter: none;
}

.menu-btn { display: none; }
.sidebar-close { display: none; margin-left: auto; }

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(4, 5, 8, 0.6);
  backdrop-filter: blur(2px);
  z-index: 20;
  opacity: 0;
  transition: opacity 0.2s ease;
}

/* ---------- Sidebar ---------- */

.sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 22px 20px;
  padding-top: max(22px, env(safe-area-inset-top, 0px));
  padding-bottom: max(22px, env(safe-area-inset-bottom, 0px));
  display: flex;
  flex-direction: column;
  gap: 20px;
  overflow-y: auto;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  padding-bottom: 4px;
}

.brand-mark {
  width: 38px;
  height: 38px;
  min-width: 38px;
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(155deg, var(--accent), var(--amber));
  color: #10090a;
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.08) inset;
  animation: mark-pulse 3.2s ease-in-out infinite;
}

.brand-mark svg { width: 20px; height: 20px; }

@keyframes mark-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255, 90, 31, 0.35), 0 0 0 1px rgba(255, 255, 255, 0.08) inset; }
  50% { box-shadow: 0 0 0 6px rgba(255, 90, 31, 0), 0 0 0 1px rgba(255, 255, 255, 0.08) inset; }
}

@media (prefers-reduced-motion: reduce) {
  .brand-mark { animation: none; }
}

.brand-text h1 {
  font-family: var(--font-display);
  font-size: 17px;
  font-weight: 600;
  margin: 0;
  letter-spacing: -0.01em;
}

.brand-text p {
  margin: 2px 0 0;
  font-size: 12px;
  color: var(--text-faint);
}

.field {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.field label {
  font-size: 12.5px;
  font-weight: 500;
  color: var(--text-dim);
}

input, select, textarea {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius-md);
  padding: 11px 13px;
  font-size: 14px;
  font-family: var(--font-body);
  outline: none;
  transition: border-color 0.15s ease, background 0.15s ease;
}

input::placeholder, textarea::placeholder { color: var(--text-faint); }

input:focus, select:focus, textarea:focus {
  border-color: var(--accent);
  background: var(--surface-3);
}

.select-wrap {
  position: relative;
}

.select-wrap select {
  width: 100%;
  appearance: none;
  padding-right: 34px;
}

.select-caret {
  position: absolute;
  right: 13px;
  top: 50%;
  transform: translateY(-50%);
  width: 11px;
  height: 7px;
  color: var(--text-faint);
  pointer-events: none;
}

button {
  background: var(--accent);
  background: linear-gradient(155deg, var(--accent-bright), var(--accent));
  color: #150a06;
  border: none;
  border-radius: var(--radius-pill);
  padding: 11px 16px;
  font-weight: 600;
  font-family: var(--font-body);
  cursor: pointer;
  font-size: 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: transform 0.12s ease, box-shadow 0.12s ease, filter 0.12s ease;
}

button:hover { filter: brightness(1.06); transform: translateY(-1px); box-shadow: 0 6px 18px rgba(255, 90, 31, 0.22); }
button:active { transform: translateY(0); }

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

button.secondary {
  background: transparent;
  border: 1px solid var(--border-strong);
  color: var(--text-dim);
}

button.secondary svg { width: 15px; height: 15px; }

button.secondary:hover {
  border-color: var(--text-faint);
  background: var(--surface-2);
  color: var(--text);
  filter: none;
  box-shadow: none;
}

.status {
  font-size: 12.5px;
  min-height: 16px;
  color: var(--text-faint);
}

.status.ok { color: var(--success); }
.status.ok::before { content: "●"; margin-right: 6px; font-size: 9px; vertical-align: 1px; }
.status.err { color: var(--danger); }
.status.err::before { content: "●"; margin-right: 6px; font-size: 9px; vertical-align: 1px; }

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.chip {
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  color: var(--text-dim);
  border-radius: var(--radius-pill);
  padding: 5px 12px;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  box-shadow: none;
}

.chip:hover {
  border-color: var(--accent);
  color: var(--text);
  transform: none;
  box-shadow: none;
  filter: none;
}

.chip.active {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent-bright);
}

#btn-delete-provider {
  padding: 9px 14px;
  font-size: 12.5px;
}

.sidebar-footer {
  margin-top: auto;
  font-size: 11.5px;
  color: var(--text-faint);
  text-align: center;
  padding-top: 8px;
  border-top: 1px solid var(--border);
}

/* ---------- Chat area ---------- */

.chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background:
    radial-gradient(ellipse 900px 500px at 50% -10%, rgba(255, 90, 31, 0.06), transparent 60%),
    var(--bg);
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 28px;
  border-bottom: 1px solid var(--border);
  min-height: 52px;
  padding-top: max(14px, env(safe-area-inset-top, 0px));
  position: sticky;
  top: 0;
  z-index: 5;
  flex-shrink: 0;
  background: var(--bg);
}

.chat-header-model {
  font-family: var(--font-display);
  font-size: 13.5px;
  font-weight: 500;
  color: var(--text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
  min-width: 0;
}

.conn-indicator {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-size: 12.5px;
  color: var(--text-faint);
  flex: none;
}

.conn-indicator .dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-faint);
}

.conn-indicator.connected { color: var(--success); }
.conn-indicator.connected .dot {
  background: var(--success);
  box-shadow: 0 0 0 3px rgba(52, 211, 153, 0.18);
}

.conn-indicator.error { color: var(--danger); }
.conn-indicator.error .dot { background: var(--danger); }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 28px 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  max-width: 800px;
  width: 100%;
  margin: 0 auto;
}

.msg {
  max-width: 72%;
  padding: 13px 17px;
  border-radius: var(--radius-lg);
  line-height: 1.55;
  font-size: 14.5px;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.msg.user {
  align-self: flex-end;
  background: var(--surface-3);
  border: 1px solid var(--border-strong);
  border-bottom-right-radius: 4px;
}

.msg.bot {
  align-self: flex-start;
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 2px solid var(--accent);
  border-bottom-left-radius: 4px;
}

.msg.system {
  align-self: center;
  max-width: 480px;
  text-align: center;
  background: transparent;
  border: none;
  color: var(--text-faint);
  font-size: 13.5px;
}

.msg.bot.typing {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 16px 17px;
}

.msg.bot.typing .dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-faint);
  animation: typing-bounce 1.1s ease-in-out infinite;
}

.msg.bot.typing .dot:nth-child(2) { animation-delay: 0.15s; }
.msg.bot.typing .dot:nth-child(3) { animation-delay: 0.3s; }

@keyframes typing-bounce {
  0%, 60%, 100% { opacity: 0.35; transform: translateY(0); }
  30% { opacity: 1; transform: translateY(-3px); }
}

.msg.bot.has-code {
  max-width: 92%;
}

.msg-text {
  white-space: pre-wrap;
  word-wrap: break-word;
}

.msg-text + .code-block,
.code-block + .msg-text,
.code-block + .code-block {
  margin-top: 10px;
}

.code-block {
  background: #0d0f14;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  overflow: hidden;
  font-family: "Space Grotesk", monospace;
}

.code-block-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 7px 10px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
}

.code-lang {
  font-size: 11.5px;
  color: var(--text-faint);
  text-transform: lowercase;
  letter-spacing: 0.02em;
}

.code-actions {
  display: flex;
  gap: 6px;
}

.code-btn {
  background: transparent;
  border: 1px solid var(--border-strong);
  color: var(--text-dim);
  border-radius: var(--radius-pill);
  padding: 4px 11px;
  font-size: 11.5px;
  font-weight: 500;
  cursor: pointer;
  box-shadow: none;
}

.code-btn:hover {
  border-color: var(--text-faint);
  background: var(--surface-3);
  color: var(--text);
  transform: none;
  box-shadow: none;
  filter: none;
}

.code-btn.ok {
  border-color: var(--success);
  color: var(--success);
}

.code-block pre {
  margin: 0;
  padding: 12px 14px;
  overflow-x: auto;
  max-width: 100%;
}

.code-block pre code {
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  font-size: 13px;
  line-height: 1.5;
  color: #d6e2ff;
  white-space: pre;
}

.zip-all-btn {
  margin-top: 4px;
  width: 100%;
  justify-content: center;
}

.chat-input {
  display: flex;
  gap: 10px;
  padding: 14px 18px;
  padding-bottom: max(14px, env(safe-area-inset-bottom, 0px));
  border-top: 1px solid var(--border);
  background: var(--surface);
  max-width: 800px;
  width: 100%;
  margin: 0 auto;
  position: sticky;
  bottom: 0;
  z-index: 5;
  flex-shrink: 0;
}

.chat-input textarea {
  flex: 1;
  resize: none;
  max-height: 140px;
  border-radius: var(--radius-lg);
}

.chat-input button {
  align-self: flex-end;
  padding: 11px 18px;
}

.chat-input button svg { width: 15px; height: 15px; }

/* ---------- Responsive: sidebar becomes a drawer ---------- */

@media (max-width: 860px) {
  .menu-btn { display: inline-flex; }
  .sidebar-close { display: inline-flex; }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 30;
    width: min(84vw, 320px);
    min-width: 0;
    max-width: 320px;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    box-shadow: 0 0 40px rgba(0, 0, 0, 0.5);
  }

  .sidebar.open { transform: translateX(0); }

  .sidebar-overlay.open {
    display: block;
    opacity: 1;
  }

  .chat-area { width: 100%; }

  .msg { max-width: 88%; }
  .msg.bot.has-code { max-width: 96%; }
  .chat-header { padding: 12px 14px; gap: 10px; }
  .messages { padding: 20px 14px; }
  .chat-input { padding: 12px 14px; padding-bottom: max(12px, env(safe-area-inset-bottom, 0px)); }
}

@media (min-width: 861px) {
  .sidebar-overlay { display: none !important; }
}
