:root {
  --bg: #0b0d08;
  --bg-soft: #131909;
  --glass: rgba(14, 20, 8, 0.62);
  --glass-strong: rgba(12, 17, 6, 0.9);
  --line: rgba(162, 178, 82, 0.24);
  --line-soft: rgba(138, 155, 64, 0.18);
  --text: #ece5ce;
  --muted: #a89c78;
  --accent: #d4921a;
  --accent-2: #7ab83e;
  --primary: #c4781a;
  --primary-strong: #9a5c0e;
  --shadow: 0 22px 70px rgba(0, 0, 0, 0.55);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html { height: 100%; -webkit-text-size-adjust: 100%; }

body {
  font-family: "Outfit", sans-serif;
  color: var(--text);
  background: radial-gradient(circle at 10% 10%, #1c2910 0%, #0b0d08 42%, #070905 100%);
  height: 100%;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

/* ─── Canvas & overlays ─── */
#bg3d { position: fixed; inset: 0; width: 100%; height: 100%; z-index: 0; }

.noise, .vignette { position: fixed; inset: 0; pointer-events: none; z-index: 1; }
.noise {
  opacity: 0.12; mix-blend-mode: soft-light;
  background-image:
    radial-gradient(circle at 20% 30%, rgba(255,255,255,0.08) 0, transparent 30%),
    radial-gradient(circle at 80% 60%, rgba(255,255,255,0.08) 0, transparent 28%);
}
.vignette { background: radial-gradient(circle at center, transparent 35%, rgba(3,6,10,0.72) 100%); }

/* ─── Shell ─── */
.app-shell {
  position: relative; z-index: 2;
  max-width: 1400px; margin: 0 auto; padding: 22px;
  display: grid; grid-template-columns: 350px 1fr; gap: 20px;
  height: 100%;
}

.glass {
  background: linear-gradient(155deg, rgba(22,28,12,0.82), rgba(12,16,6,0.74));
  border: 1px solid var(--line);
  border-radius: 24px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
}

/* ─── Sidebar ─── */
.sidebar { padding: 22px; display: flex; flex-direction: column; gap: 14px; overflow-y: auto; }
.sidebar-backdrop {
  display: none; position: fixed; inset: 0; z-index: 90;
  background: rgba(3,5,1,0.78); backdrop-filter: blur(3px);
  opacity: 0; transition: opacity 0.28s ease;
}
.sidebar-backdrop.visible { display: block; opacity: 1; }

.brand-logo {
  display: block; width: 160px; max-width: 100%; margin-bottom: 10px;
  filter: brightness(0.9) sepia(0.15) saturate(1.3); opacity: 0.9;
}
.brand h1 { margin: 8px 0; font-family: "Sora", sans-serif; font-size: 1.8rem; letter-spacing: 0.01em; }
.brand p { color: var(--muted); font-size: 0.94rem; line-height: 1.4; }

.badge {
  display: inline-block; font-size: 0.66rem; letter-spacing: 0.18em;
  padding: 7px 10px; border-radius: 999px; color: #f0d89a;
  background: linear-gradient(120deg, rgba(140,88,14,0.72), rgba(200,140,20,0.28));
  border: 1px solid rgba(200,152,40,0.48); font-weight: 700;
}

.panel {
  border: 1px solid var(--line-soft); border-radius: 16px; padding: 14px;
  background: linear-gradient(145deg, rgba(18,24,8,0.86), rgba(14,19,6,0.56));
}
.panel h2 {
  margin: 0 0 10px; font-size: 0.86rem; font-family: "Sora", sans-serif;
  text-transform: uppercase; letter-spacing: 0.1em; color: #d4c08a;
}

.switch-row { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; font-size: 0.9rem; color: #e8dfc0; }
.switch-row input { accent-color: var(--accent-2); }

.panel input[type="text"] {
  width: 100%; border: 1px solid rgba(138,155,64,0.32); border-radius: 12px;
  padding: 10px 12px; font-family: inherit; background: rgba(8,10,4,0.68); color: var(--text);
}

.blob-list { margin-top: 10px; max-height: 220px; overflow: auto; display: grid; gap: 8px; font-size: 0.81rem; }
.blob-item { border: 1px solid rgba(120,140,50,0.32); border-radius: 9px; padding: 10px; background: rgba(10,13,5,0.72); color: #e0d8b0; }
.blob-item a { color: #c8a040; text-decoration: none; }

.kpi-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.kpi-grid div { border: 1px solid rgba(110,130,40,0.22); border-radius: 12px; padding: 10px; background: rgba(7,10,3,0.6); }
.kpi-grid span { display: block; font-size: 0.72rem; color: #8e8060; margin-bottom: 5px; }
.kpi-grid strong { font-family: "Sora", sans-serif; font-size: 0.95rem; color: #e8d898; }

.btn {
  border-radius: 12px; padding: 10px 14px; font-weight: 600; cursor: pointer;
  border: 1px solid transparent; transition: transform 0.16s ease, box-shadow 0.16s ease;
  font-family: inherit; font-size: 0.88rem;
}
.btn:hover { transform: translateY(-1px); }
.btn.secondary {
  width: 100%; background: linear-gradient(120deg, rgba(100,80,14,0.38), rgba(80,100,16,0.2));
  color: #e0d0a0; border-color: rgba(160,140,40,0.4);
}

/* ─── Chat area ─── */
.chat-area {
  display: flex; flex-direction: column; min-height: 0; overflow: hidden;
}

.chat-header {
  padding: 18px 22px; border-bottom: 1px solid rgba(140,155,60,0.2);
  display: flex; align-items: center; gap: 12px; flex-shrink: 0;
}
.chat-header-brand { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; }
.chat-logo { width: 36px; height: 36px; border-radius: 10px; object-fit: cover; display: none; }
.chat-header-text { flex: 1; min-width: 0; }
.chat-header h2 { font-family: "Sora", sans-serif; letter-spacing: 0.02em; font-size: 1.1rem; }
.chat-header p { color: var(--muted); font-size: 0.88rem; margin-top: 2px; }

.status-wrap { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }
.status-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--accent-2);
  box-shadow: 0 0 6px rgba(122,184,62,0.5);
  animation: statusPulse 2s infinite;
}
.status-label { font-size: 0.76rem; color: var(--muted); font-weight: 600; letter-spacing: 0.05em; text-transform: uppercase; }

@keyframes statusPulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.6; transform: scale(0.85); }
}

/* ─── Messages ─── */
.messages {
  flex: 1; min-height: 0; padding: 22px; overflow-y: auto;
  display: flex; flex-direction: column; gap: 14px; scroll-behavior: smooth;
}
.messages::-webkit-scrollbar { width: 4px; }
.messages::-webkit-scrollbar-track { background: transparent; }
.messages::-webkit-scrollbar-thumb { background: rgba(168,156,120,0.25); border-radius: 4px; }

.message-item {
  max-width: min(86%, 760px); padding: 14px 16px; border-radius: 18px;
  animation: rise 0.3s cubic-bezier(0.16,1,0.3,1);
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
.message-item.user {
  margin-left: auto;
  background: linear-gradient(130deg, rgba(140,80,14,0.32), rgba(90,50,8,0.48));
  border: 1px solid rgba(200,148,40,0.35);
  border-radius: 18px 18px 6px 18px;
}
.message-item.assistant {
  margin-right: auto;
  background: linear-gradient(145deg, rgba(22,28,10,0.82), rgba(14,18,6,0.86));
  border: 1px solid rgba(122,140,50,0.25);
  border-radius: 18px 18px 18px 6px;
}

.role { font-size: 0.7rem; letter-spacing: 0.12em; text-transform: uppercase; color: #c8a84a; margin-bottom: 5px; font-weight: 600; }
.content { white-space: pre-wrap; line-height: 1.52; }
.content.prose { white-space: normal; line-height: 1.65; }
.content.prose p { margin: 0 0 0.65em; }
.content.prose p:last-child { margin-bottom: 0; }
.content.prose strong { color: #e0c070; }
.content.prose em { color: var(--muted); font-style: italic; }

.content.prose h1, .content.prose h2, .content.prose h3 {
  font-family: "Sora", sans-serif; font-weight: 700; color: var(--accent); margin: 0.9em 0 0.45em; line-height: 1.3;
}
.content.prose h1 { font-size: 1.15rem; }
.content.prose h2 { font-size: 1.05rem; }
.content.prose h3 { font-size: 0.97rem; color: var(--accent-2); }

.content.prose ul, .content.prose ol { padding-left: 1.2em; margin: 0.4em 0 0.7em; }
.content.prose li { margin-bottom: 0.3em; }

.content.prose code {
  background: rgba(180,130,20,0.14); border: 1px solid rgba(180,130,20,0.28);
  padding: 0.1em 0.38em; border-radius: 5px;
  font-family: "Fira Code","Consolas",monospace; font-size: 0.85em; color: #e0c070;
}
.content.prose blockquote {
  border-left: 3px solid var(--accent); padding: 0.5em 1em; margin: 0.65em 0;
  background: rgba(160,110,14,0.1); border-radius: 0 8px 8px 0; color: var(--muted); font-size: 0.92em;
}
.content.prose table { width: 100%; border-collapse: collapse; font-size: 0.88em; margin: 0.7em 0; }
.content.prose th {
  background: rgba(180,120,20,0.18); color: var(--accent); padding: 6px 10px;
  text-align: left; border-bottom: 1px solid rgba(180,130,20,0.32); font-weight: 600;
}
.content.prose td { padding: 6px 10px; border-bottom: 1px solid rgba(110,130,40,0.18); }
.content.prose tr:last-child td { border-bottom: none; }
.content.prose a { color: var(--accent-2); text-decoration: underline; text-underline-offset: 3px; }

/* ─── Typing indicator ─── */
.typing-indicator {
  display: flex; gap: 5px; align-items: center;
  padding: 10px 14px;
  background: linear-gradient(145deg, rgba(22,28,10,0.82), rgba(14,18,6,0.86));
  border: 1px solid rgba(122,140,50,0.3); border-radius: 18px 18px 18px 6px;
  max-width: 72px; margin: 0 22px 10px; animation: rise 0.28s ease;
}
.typing-indicator[hidden] { display: none; }
.typing-indicator span {
  width: 7px; height: 7px; background: var(--accent); border-radius: 50%;
  animation: typingBounce 1.2s infinite ease-in-out;
}
.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }

@keyframes typingBounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.45; }
  30% { transform: translateY(-6px); opacity: 1; }
}

/* ─── Composer ─── */
.composer {
  flex-shrink: 0; padding: 12px 16px; border-top: 1px solid rgba(120,140,48,0.15);
  background: linear-gradient(180deg, rgba(12,16,6,0.96) 0%, rgba(8,11,4,0.92) 100%);
}

.composer-input-row {
  display: flex; align-items: flex-end; gap: 8px;
  background: rgba(6,8,3,0.72); border: 1px solid rgba(130,148,52,0.32);
  border-radius: 22px; padding: 6px 8px 6px 14px;
  transition: border-color 0.2s ease;
}
.composer-input-row:focus-within { border-color: rgba(212,146,26,0.5); }

.attach-icon {
  display: flex; align-items: center; justify-content: center;
  width: 36px; height: 36px; cursor: pointer; flex-shrink: 0;
  color: var(--muted); transition: color 0.2s;
  border-radius: 50%;
}
.attach-icon:hover, .attach-icon:active { color: var(--accent); }

.composer textarea {
  flex: 1; min-width: 0; border: none; background: transparent;
  color: var(--text); font-family: inherit; font-size: 0.95rem;
  padding: 8px 0; line-height: 1.45; resize: none;
  max-height: 140px; outline: none;
}
.composer textarea::placeholder { color: #807050; }

.send-btn {
  display: flex; align-items: center; justify-content: center;
  width: 38px; height: 38px; border-radius: 50%; border: none; cursor: pointer;
  background: linear-gradient(135deg, var(--primary), #e8a030);
  color: #0e0a04; flex-shrink: 0;
  transition: transform 0.15s, box-shadow 0.15s;
  box-shadow: 0 4px 14px rgba(196,120,26,0.35);
}
.send-btn:hover { transform: scale(1.06); }
.send-btn:active { transform: scale(0.95); }

.files-bar {
  display: flex; align-items: center; gap: 8px; padding: 6px 14px;
  font-size: 0.8rem; color: var(--accent);
}
.files-bar[hidden] { display: none; }
.clear-files {
  border: none; background: none; color: var(--muted); font-size: 1.1rem;
  cursor: pointer; padding: 2px 6px; line-height: 1;
}

/* ─── Composer toolbar (visible on mobile) ─── */
.composer-toolbar {
  display: none; /* shown on mobile */
  align-items: center; justify-content: space-between;
  padding: 6px 2px 0;
}

.toolbar-btn {
  display: flex; align-items: center; gap: 5px; border: none; background: none;
  color: var(--muted); font-size: 0.78rem; cursor: pointer; padding: 6px 8px;
  border-radius: 8px; font-family: inherit; transition: background 0.15s;
}
.toolbar-btn:active { background: rgba(168,156,120,0.12); }

.toggle-pills { display: flex; gap: 6px; }
.pill {
  display: flex; align-items: center; gap: 4px;
  padding: 5px 10px; border-radius: 999px; font-size: 0.72rem; font-weight: 600;
  background: rgba(14,18,6,0.6); border: 1px solid var(--line-soft);
  color: var(--muted); cursor: pointer; transition: all 0.2s;
  -webkit-user-select: none; user-select: none;
}
.pill input { display: none; }
.pill:has(input:checked) {
  color: #f0d898; border-color: rgba(200,152,40,0.5);
  background: linear-gradient(120deg, rgba(140,88,14,0.35), rgba(80,100,16,0.2));
}

/* ─── Hamburger ─── */
.menu-btn {
  display: none; flex-direction: column; justify-content: center; gap: 5px;
  width: 42px; height: 42px; padding: 10px;
  border-radius: 12px; border: 1px solid rgba(130,148,48,0.25);
  background: rgba(16,20,7,0.6); cursor: pointer; flex-shrink: 0;
  transition: background 0.2s;
}
.menu-btn:active { background: rgba(30,38,14,0.8); }
.menu-btn span { display: block; height: 2px; border-radius: 2px; background: var(--accent); transition: all 0.25s cubic-bezier(0.4,0,0.2,1); }
.menu-btn.open span:nth-child(1) { transform: translateY(7px) rotate(45deg); }
.menu-btn.open span:nth-child(2) { opacity: 0; transform: scaleX(0); }
.menu-btn.open span:nth-child(3) { transform: translateY(-7px) rotate(-45deg); }

@keyframes rise {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ─── Tablet: ≤1100px ─── */
@media (max-width: 1100px) {
  .app-shell { grid-template-columns: 300px 1fr; padding: 16px; gap: 14px; }
}

/* ─────────────────────────────────────────────────── */
/* ─── MOBILE: ≤768px — iPhone 17 Pro Max optimised ─ */
/* ─────────────────────────────────────────────────── */
@media (max-width: 768px) {
  html, body { height: 100%; overflow: hidden; }
  body { background: var(--bg); }

  #bg3d, .noise, .vignette { display: none; }

  .menu-btn { display: flex; }

  .app-shell {
    display: flex; flex-direction: column;
    padding: 0; gap: 0; height: 100%;
    max-width: 100%;
  }

  /* ── Sidebar: left drawer ── */
  .sidebar {
    position: fixed; inset: 0 auto 0 0;
    width: min(84vw, 340px); z-index: 100;
    border-radius: 0 24px 24px 0; border-left: none;
    transform: translateX(-105%);
    transition: transform 0.32s cubic-bezier(0.4,0,0.2,1);
    overflow-y: auto; -webkit-overflow-scrolling: touch;
    padding: 20px 18px;
    padding-top: calc(20px + var(--safe-top));
    background: linear-gradient(170deg, rgba(14,18,7,0.98), rgba(8,11,4,0.99));
  }
  .sidebar.open { transform: translateX(0); }

  .brand-logo { width: 120px; }
  .brand h1 { font-size: 1.4rem; }
  .blob-list { max-height: 160px; }

  /* ── Chat area ── */
  .chat-area {
    flex: 1; min-height: 0;
    border-radius: 0; border: none;
    background: linear-gradient(170deg, rgba(14,18,7,0.98), rgba(8,11,4,0.99));
    box-shadow: none;
  }

  /* ── Header: compact with logo ── */
  .chat-header {
    display: flex; align-items: center; gap: 10px;
    padding: 10px 14px;
    padding-top: calc(10px + var(--safe-top));
    border-bottom: 1px solid rgba(130,148,52,0.15);
    background: rgba(14,18,7,0.92);
    backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  }
  .chat-logo { display: block; width: 34px; height: 34px; border-radius: 8px; }
  .chat-header h2 { font-size: 1rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .chat-header p { display: none; }

  /* ── Messages ── */
  .messages {
    flex: 1; min-height: 0; padding: 14px 10px 8px;
    gap: 10px; -webkit-overflow-scrolling: touch;
  }
  .typing-indicator { margin: 0 10px 8px; }

  .message-item {
    max-width: 92%; padding: 12px 14px; border-radius: 16px;
  }
  .message-item.user { border-radius: 16px 16px 4px 16px; }
  .message-item.assistant { border-radius: 16px 16px 16px 4px; }

  .role { font-size: 0.65rem; margin-bottom: 3px; }
  .content, .content.prose { font-size: 0.93rem; line-height: 1.58; }

  .content.prose table { display: block; overflow-x: auto; -webkit-overflow-scrolling: touch; white-space: nowrap; }
  .content.prose th, .content.prose td { white-space: normal; min-width: 80px; }
  .content.prose blockquote { padding: 0.4em 0.75em; font-size: 0.9em; }

  /* ── Composer: modern floating bar ── */
  .composer {
    padding: 8px 10px;
    padding-bottom: calc(8px + var(--safe-bottom));
    border-top: 1px solid rgba(120,140,48,0.12);
    background: rgba(10,13,5,0.97);
  }

  .composer-input-row {
    border-radius: 20px; padding: 4px 6px 4px 12px;
  }

  .attach-icon { width: 40px; height: 40px; }

  .composer textarea {
    font-size: 16px; /* prevent iOS zoom */
    min-height: 40px; max-height: 110px; padding: 10px 0;
  }

  .send-btn { width: 40px; height: 40px; }

  .composer-toolbar { display: flex; }

  .panel input[type="text"] { font-size: 16px; }
}

/* ─── Very small phones: ≤380px ─── */
@media (max-width: 380px) {
  .chat-header h2 { font-size: 0.88rem; }
  .message-item { max-width: 96%; padding: 10px 11px; }
  .content.prose h1 { font-size: 1rem; }
  .content.prose h2 { font-size: 0.95rem; }
  .pill { padding: 4px 8px; font-size: 0.68rem; }
}

/* ─── Landscape phones ─── */
@media (max-width: 768px) and (max-height: 500px) {
  .chat-header { padding: 6px 14px; padding-top: calc(6px + var(--safe-top)); }
  .messages { padding: 8px 10px 4px; gap: 8px; }
  .composer { padding: 6px 10px; padding-bottom: calc(6px + var(--safe-bottom)); }
  .composer textarea { max-height: 80px; }
}