:root {
  --bg: #090b10;
  --bg-soft: #10131b;
  --panel: #151925;
  --panel-2: #1b2130;
  --border: #2a3142;
  --text: #eef1f8;
  --muted: #95a0b8;
  --accent: #7c6cff;
  --accent-2: #4cc9f0;
  --accent-soft: rgba(124, 108, 255, 0.14);
  --user: linear-gradient(135deg, #2a3550, #223049);
  --assistant: #141a27;
  --success: #3ecf8e;
  --danger: #ff6b6b;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.35);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  --app-height: 100dvh;
}

* { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
}

html, body {
  margin: 0;
  min-height: 100%;
  width: 100%;
  font-family: Inter, "Segoe UI", system-ui, sans-serif;
  background:
    radial-gradient(circle at 10% 0%, rgba(124, 108, 255, 0.12), transparent 28%),
    radial-gradient(circle at 90% 10%, rgba(76, 201, 240, 0.08), transparent 24%),
    var(--bg);
  color: var(--text);
}

body {
  min-height: 100dvh;
}

button, input, textarea { font: inherit; }

.mobile-only { display: none; }

.app {
  display: grid;
  grid-template-columns: 300px 1fr;
  min-height: 100dvh;
  width: 100%;
  overflow: clip;
}

.sidebar-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(2px);
  z-index: 30;
}

.sidebar {
  border-right: 1px solid var(--border);
  background: rgba(9, 11, 16, 0.94);
  backdrop-filter: blur(16px);
  padding: 18px 14px calc(18px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  gap: 14px;
  overflow: hidden;
  min-height: 100dvh;
}

.sidebar-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

.brand { display: flex; gap: 12px; align-items: center; }

.brand-icon {
  width: 42px;
  height: 42px;
  border-radius: 14px;
  display: grid;
  place-items: center;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: white;
  font-weight: 700;
  box-shadow: 0 8px 24px rgba(124, 108, 255, 0.28);
}

.brand h1, .chat-header h2 {
  margin: 0;
  font-size: 1.05rem;
  letter-spacing: -0.02em;
}

.brand p, .chat-header p, .composer-hint {
  margin: 3px 0 0;
  color: var(--muted);
  font-size: 0.82rem;
}

.sidebar-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 1;
  min-height: 0;
}

.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.section-head h3 {
  margin: 0;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.btn-link {
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 1rem;
  padding: 4px;
}

.btn-link:hover { color: var(--text); }

.history-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow-y: auto;
  min-height: 100px;
  padding-right: 2px;
}

.history-item {
  width: 100%;
  text-align: left;
  border: 1px solid var(--border);
  background: var(--panel);
  color: inherit;
  border-radius: 12px;
  padding: 10px 12px;
  cursor: pointer;
}

.history-item:hover { border-color: #46506a; }
.history-item.active { border-color: var(--accent); background: var(--accent-soft); }

.history-item strong {
  display: block;
  font-size: 0.84rem;
  margin-bottom: 3px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-item span {
  display: block;
  font-size: 0.74rem;
  color: var(--muted);
}

.history-empty {
  color: var(--muted);
  font-size: 0.82rem;
  padding: 8px 4px;
}

.sidebar-footer {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.btn-secondary,
.btn-primary,
.btn-ghost {
  border-radius: 12px;
  padding: 11px 14px;
  cursor: pointer;
}

.btn-block { width: 100%; }

.btn-secondary {
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
}

.btn-primary {
  border: none;
  background: linear-gradient(135deg, var(--accent), #5b8def);
  color: white;
  font-weight: 600;
}

.btn-ghost {
  border: 1px solid transparent;
  background: transparent;
  color: var(--muted);
}

.btn-ghost:hover { color: var(--text); border-color: var(--border); }

.chat {
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-height: 100dvh;
  min-width: 0;
  width: 100%;
  overflow: clip;
}

.chat-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: calc(14px + var(--safe-top)) 20px 14px;
  border-bottom: 1px solid var(--border);
  background: rgba(9, 11, 16, 0.72);
  backdrop-filter: blur(12px);
  position: sticky;
  top: 0;
  z-index: 5;
  width: 100%;
  max-width: 100%;
  overflow: hidden;
  flex-shrink: 0;
}

.chat-header-left,
.chat-header-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.chat-header-left {
  flex: 1 1 0;
  min-width: 0;
}

.chat-header-actions {
  flex: 0 1 auto;
  min-width: 0;
  justify-content: flex-end;
}

.chat-header-titles {
  flex: 1;
  min-width: 0;
}

.chat-header-titles h2,
.chat-header-titles p {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.btn-new-chat {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  border-radius: 12px;
  padding: 8px 12px;
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
  transition: border-color 0.15s ease, background 0.15s ease;
}

.btn-new-chat:hover {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.btn-new-chat-icon {
  display: grid;
  place-items: center;
  width: 20px;
  height: 20px;
  border-radius: 6px;
  background: linear-gradient(135deg, var(--accent), #5b8def);
  color: white;
  font-weight: 700;
  font-size: 0.95rem;
}

.btn-new-chat-label {
  font-size: 0.84rem;
  font-weight: 600;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
  flex-shrink: 0;
  display: grid;
  place-items: center;
}

.pill {
  font-size: 0.74rem;
  padding: 6px 10px;
  border-radius: 999px;
  background: rgba(62, 207, 142, 0.12);
  color: var(--success);
  white-space: nowrap;
  flex: 0 1 auto;
  min-width: 0;
  max-width: 11rem;
  overflow: hidden;
  text-overflow: ellipsis;
}

.pill.busy {
  background: rgba(124, 108, 255, 0.15);
  color: #c4bcff;
  animation: pulse 1.4s ease-in-out infinite;
}

.pill.error {
  background: rgba(255, 107, 107, 0.12);
  color: var(--danger);
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.65; }
}

.messages {
  overflow-y: auto;
  overflow-x: hidden;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  scroll-behavior: smooth;
  min-width: 0;
  min-height: 0;
  -webkit-overflow-scrolling: touch;
}

.empty-state {
  margin: auto;
  max-width: 420px;
  text-align: center;
  padding: 28px 16px;
  color: var(--muted);
}

.empty-state.hidden { display: none; }

.empty-icon {
  width: 56px;
  height: 56px;
  margin: 0 auto 14px;
  border-radius: 18px;
  display: grid;
  place-items: center;
  font-size: 1.4rem;
  color: white;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  box-shadow: 0 12px 30px rgba(124, 108, 255, 0.25);
}

.empty-state h3 {
  margin: 0 0 8px;
  color: var(--text);
  font-size: 1.15rem;
}

.empty-state p {
  margin: 0;
  line-height: 1.5;
  font-size: 0.92rem;
}

.message {
  max-width: min(780px, 100%);
  padding: 13px 15px;
  border-radius: 18px;
  line-height: 1.6;
  overflow-wrap: anywhere;
  word-break: break-word;
  animation: fadeIn 0.2s ease;
}

.message-content {
  max-width: 100%;
  overflow-x: auto;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

.message.user {
  align-self: flex-end;
  margin-left: auto;
  width: fit-content;
  max-width: 100%;
  background: var(--user);
  border: 1px solid #3a4a68;
  border-bottom-right-radius: 6px;
}

.message.assistant {
  align-self: flex-start;
  margin-right: auto;
  width: fit-content;
  max-width: 100%;
  background: var(--assistant);
  border: 1px solid var(--border);
  border-bottom-left-radius: 6px;
}

.message.system {
  align-self: center;
  color: var(--muted);
  border: 1px dashed var(--border);
  background: transparent;
  font-size: 0.88rem;
}

.message-content.markdown > :first-child { margin-top: 0; }
.message-content.markdown > :last-child { margin-bottom: 0; }
.message-content.markdown p { margin: 0 0 0.75rem; }
.message-content.markdown h1,
.message-content.markdown h2,
.message-content.markdown h3 {
  margin: 0 0 0.6rem;
  line-height: 1.3;
}
.message-content.markdown h1 { font-size: 1.15rem; }
.message-content.markdown h2 { font-size: 1.05rem; }
.message-content.markdown h3 { font-size: 0.98rem; }
.message-content.markdown ul,
.message-content.markdown ol {
  margin: 0 0 0.75rem;
  padding-left: 1.2rem;
}
.message-content.markdown li { margin: 0.2rem 0; }
.message-content.markdown code {
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 6px;
  padding: 0.1rem 0.35rem;
  font-family: Consolas, Monaco, monospace;
  font-size: 0.9em;
}
.message-content.markdown pre {
  margin: 0 0 0.75rem;
  padding: 12px 14px;
  border-radius: 12px;
  background: #0b0d12;
  border: 1px solid var(--border);
  overflow-x: auto;
  max-width: 100%;
  -webkit-overflow-scrolling: touch;
}
.message-content.markdown pre code {
  background: transparent;
  border: none;
  padding: 0;
  font-size: 0.86rem;
  white-space: pre;
  word-break: normal;
}
.message-content.markdown table {
  display: block;
  max-width: 100%;
  overflow-x: auto;
  border-collapse: collapse;
}
.message-content.markdown strong { font-weight: 700; color: #f5f6fb; }
.message-content.markdown em { font-style: italic; }
.message-content.markdown a {
  color: #9eb6ff;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.message.user .message-content,
.message.system .message-content {
  white-space: pre-wrap;
}

.composer {
  padding: 12px 16px calc(14px + var(--safe-bottom));
  border-top: 1px solid var(--border);
  background: rgba(9, 11, 16, 0.88);
  backdrop-filter: blur(12px);
  width: 100%;
  max-width: 100%;
}

.composer-inner {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: end;
  width: 100%;
  min-width: 0;
}

.composer textarea {
  resize: none;
  min-height: 48px;
  max-height: 160px;
  width: 100%;
  min-width: 0;
  border-radius: 16px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  padding: 12px 14px;
  line-height: 1.45;
}

.composer textarea:focus {
  outline: none;
  border-color: rgba(124, 108, 255, 0.65);
  box-shadow: 0 0 0 3px rgba(124, 108, 255, 0.12);
}

.composer-actions {
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
}

.action-btn {
  width: 48px;
  height: 48px;
  border: none;
  border-radius: 14px;
  color: white;
  cursor: pointer;
  grid-area: 1 / 1;
  display: grid;
  place-items: center;
}

.send-btn {
  background: linear-gradient(135deg, var(--accent), #5b8def);
  box-shadow: 0 8px 20px rgba(124, 108, 255, 0.28);
}

.stop-btn {
  background: linear-gradient(135deg, #ff6b6b, #d64545);
}

.action-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  box-shadow: none;
}

.composer-hint {
  margin: 8px 4px 0;
  text-align: center;
}

.login-page {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}

.login-card {
  width: min(420px, 100%);
  border: 1px solid var(--border);
  background: rgba(21, 25, 37, 0.92);
  border-radius: 20px;
  padding: 24px;
  box-shadow: var(--shadow);
}

.login-brand { margin-bottom: 18px; }

.login-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.login-form label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.88rem;
  color: var(--muted);
}

.login-form input {
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  border-radius: 12px;
  padding: 12px 14px;
}

.login-error {
  margin: 0 0 12px;
  padding: 10px 12px;
  border-radius: 10px;
  background: rgba(255, 107, 107, 0.12);
  color: #ffb4b4;
  font-size: 0.9rem;
}

body.sidebar-open {
  overflow: hidden;
  touch-action: none;
}

@media (max-width: 900px) {
  .mobile-only { display: inline-grid; }

  .app {
    display: block;
    position: relative;
    min-height: var(--app-height);
    height: var(--app-height);
  }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: min(88vw, 320px);
    z-index: 40;
    transform: translateX(-105%);
    transition: transform 0.22s ease;
    box-shadow: var(--shadow);
    min-height: 0;
    height: 100dvh;
  }

  .sidebar.open { transform: translateX(0); }

  .chat {
    width: 100%;
    max-width: 100%;
    min-height: var(--app-height);
    height: var(--app-height);
    overflow: clip;
  }

  .messages { padding: 14px max(12px, env(safe-area-inset-right)) 18px max(12px, env(safe-area-inset-left)); }

  .message {
    max-width: 100%;
    padding: 12px 14px;
  }

  .message.user,
  .message.assistant {
    width: auto;
    max-width: calc(100% - 4px);
  }

  .chat-header {
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
    gap: 8px;
    flex-wrap: nowrap;
  }

  .chat-header.is-busy .chat-header-titles {
    display: none;
  }

  .chat-header-titles p {
    display: none;
  }

  .chat-header-actions {
    flex: 0 1 auto;
    min-width: 0;
    gap: 6px;
  }

  .composer {
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
  }

  .composer-hint { display: block; }

  .btn-new-chat-label { display: none; }

  .btn-new-chat {
    padding: 8px;
    min-width: 40px;
    justify-content: center;
    flex-shrink: 0;
  }

  .pill {
    max-width: 6.5rem;
  }
}

@media (max-width: 480px) {
  .chat-header {
    align-items: center;
  }

  .chat-header-actions {
    margin-left: auto;
  }

  .chat-header-titles h2 {
    font-size: 0.95rem;
  }

  .composer textarea {
    font-size: 16px;
  }
}

@media (max-width: 900px) and (display-mode: standalone) {
  .composer-hint { display: none; }
}
