:root {
  --bg: #0a0a0b;
  --panel: #111113;
  --panel-2: #18181b;
  --panel-3: #202024;
  --line: #26262b;
  --text: #f5f5f7;
  --muted: #9b9ba5;
  --brand1: #FF6A35;
  --brand2: #F04478;
  --grad: linear-gradient(135deg, #FF6A35 0%, #F04478 100%);
  --link: #ff8a5c;
  --danger: #ff6b6b;
  --radius: 14px;
  --sidebar-w: 300px;
  --thread-w: 800px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans Bengali", sans-serif;
  font-size: 16px;
  line-height: 1.5;
  overscroll-behavior: none;
}

.hidden { display: none !important; }
.spacer { flex: 1; }

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
::selection { background: rgba(240, 68, 120, .4); }

/* Icons: always vertically centred with their label */
.ic { width: 18px; height: 18px; flex: none; display: block; }
.lbl { line-height: 1; display: block; }
.logo-sm { width: 28px; height: 28px; flex: none; display: block; }
.logo-lg { width: 56px; height: 56px; display: block; margin: 0 auto; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: 40px;
  padding: 0 14px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel-2);
}
.btn.wide { width: 100%; justify-content: flex-start; }
.btn.primary { background: var(--grad); color: #fff; border-color: transparent; font-weight: 600; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); border-color: rgba(255, 107, 107, .35); background: transparent; }
.btn:active { opacity: .8; }
.btn:disabled { opacity: .5; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  border: 0;
  background: transparent;
  color: var(--text);
  flex: none;
}
.icon-btn:active, .icon-btn:hover { background: var(--panel-2); }

/* Login */
.screen { min-height: 100dvh; display: flex; align-items: center; justify-content: center; padding: 24px; }
.login-card { width: 100%; max-width: 340px; display: flex; flex-direction: column; gap: 12px; }
.brand { font-size: 24px; font-weight: 700; text-align: center; margin: 4px 0 10px; letter-spacing: .2px; }
.login-card input {
  height: 46px;
  padding: 0 14px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel);
  outline: none;
}
.login-card input:focus, .field-input:focus { border-color: var(--brand2); }
.login-card .btn { height: 46px; }
.err { color: var(--danger); font-size: 14px; min-height: 20px; text-align: center; }

/* Layout */
.app { position: fixed; inset: 0; display: flex; height: 100dvh; }

.sidebar {
  width: var(--sidebar-w);
  background: var(--panel);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  padding-top: var(--safe-top);
  z-index: 30;
}
.side-brand { display: flex; align-items: center; gap: 10px; padding: 16px 16px 6px; }
.brand-name { font-weight: 700; font-size: 17px; }
.side-head { padding: 10px 12px; display: flex; flex-direction: column; gap: 10px; }
.side-foot { padding: 12px; padding-bottom: calc(12px + var(--safe-bottom)); border-top: 1px solid var(--line); }

.search {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 38px;
  padding: 0 10px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--muted);
}
.search:focus-within { border-color: var(--brand2); }
.search input { flex: 1; min-width: 0; border: 0; outline: 0; background: transparent; height: 100%; font-size: 15px; }
.search input::-webkit-search-cancel-button { display: none; }

.session-list { flex: 1; overflow-y: auto; padding: 0 8px; -webkit-overflow-scrolling: touch; }
.session {
  position: relative;
  display: flex;
  align-items: center;
  gap: 2px;
  min-height: 44px;
  padding: 0 4px 0 12px;
  border-radius: 10px;
  margin-bottom: 2px;
}
.session:hover { background: var(--panel-2); }
.session.active { background: var(--panel-3); }
.session.active::before {
  content: "";
  position: absolute;
  left: 0;
  top: 10px;
  bottom: 10px;
  width: 3px;
  border-radius: 3px;
  background: var(--grad);
}
.session .title {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  line-height: 1;
  cursor: pointer;
  padding: 14px 0;
}
.session .mini {
  width: 32px;
  height: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 0;
  background: transparent;
  color: var(--muted);
  border-radius: 8px;
}
.session .mini .ic { width: 16px; height: 16px; }
.session .mini:hover, .session .mini:active { background: var(--line); color: var(--text); }
.list-empty { color: var(--muted); font-size: 14px; padding: 12px; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; height: 100dvh; }

.topbar {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: calc(6px + var(--safe-top)) 8px 6px;
  border-bottom: 1px solid var(--line);
  background: var(--bg);
}
#modelSelect {
  flex: 0 1 320px;
  min-width: 0;
  height: 40px;
  padding: 0 10px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel);
  text-overflow: ellipsis;
  outline: none;
}
#modelSelect:focus { border-color: var(--brand2); }

.messages { flex: 1; overflow-y: auto; padding: 16px 12px 8px; -webkit-overflow-scrolling: touch; }
.empty { text-align: center; margin-top: 22vh; padding: 0 20px; }
.empty-title { font-size: 22px; font-weight: 700; margin-top: 14px; }
.empty-sub { color: var(--muted); margin-top: 6px; font-size: 15px; }

.msg { max-width: var(--thread-w); margin: 0 auto 18px; }
.msg.user { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; }
.msg.user .bubble {
  background: var(--panel-3);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 14px;
  max-width: 88%;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.msg.assistant .bubble { overflow-wrap: anywhere; }
.msg-meta { color: var(--muted); font-size: 12px; margin-bottom: 4px; display: flex; gap: 8px; align-items: center; }
.msg-meta .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--grad); flex: none; }
.msg-meta .lbl { line-height: 1; }
.msg-actions { margin-top: 4px; display: flex; gap: 2px; flex-wrap: wrap; align-items: center; }
.msg-actions .mini {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 30px;
  padding: 0 8px;
  border: 0;
  background: transparent;
  color: var(--muted);
  border-radius: 8px;
  font-size: 13px;
}
.msg-actions .mini .ic { width: 15px; height: 15px; }
.msg-actions .mini:hover, .msg-actions .mini:active { background: var(--panel-2); color: var(--text); }
.msg.error .bubble { color: var(--danger); }

.cursor::after {
  content: "";
  display: inline-block;
  width: 8px;
  height: 16px;
  margin-left: 3px;
  background: var(--brand2);
  vertical-align: text-bottom;
  animation: blink 1s steps(2) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

/* Attachments inside messages */
.attach-list { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; max-width: 88%; }
.thumb { display: block; border-radius: 12px; overflow: hidden; border: 1px solid var(--line); background: var(--panel-2); }
.thumb img { display: block; max-width: min(260px, 100%); max-height: 220px; object-fit: cover; }
.fchip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  max-width: 100%;
  height: 36px;
  padding: 0 10px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  text-decoration: none;
  font-size: 13px;
}
.fchip .ic { width: 16px; height: 16px; color: var(--muted); }
.fchip .nm { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; line-height: 1; }
.fchip .sz { color: var(--muted); line-height: 1; flex: none; }
a.fchip:hover { border-color: var(--brand2); }
.fchip.gone { opacity: .55; }

/* Markdown */
.bubble p { margin: 0 0 10px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble h2, .bubble h3, .bubble h4, .bubble h5 { margin: 14px 0 6px; line-height: 1.3; }
.bubble h2 { font-size: 1.25em; }
.bubble h3 { font-size: 1.12em; }
.bubble h4, .bubble h5 { font-size: 1em; }
.bubble ul, .bubble ol { margin: 0 0 10px; padding-left: 22px; }
.bubble li { margin: 2px 0; }
.bubble blockquote { margin: 0 0 10px; padding: 2px 12px; border-left: 3px solid var(--brand2); color: var(--muted); }
.bubble hr { border: 0; border-top: 1px solid var(--line); margin: 14px 0; }
.bubble a { color: var(--link); }
.bubble code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.9em;
  background: var(--panel-2);
  padding: 1px 5px;
  border-radius: 5px;
}
.table-wrap { overflow-x: auto; margin: 0 0 10px; border: 1px solid var(--line); border-radius: 10px; }
.bubble table { border-collapse: collapse; width: 100%; font-size: 14px; }
.bubble th, .bubble td { padding: 8px 12px; border-bottom: 1px solid var(--line); text-align: left; white-space: nowrap; }
.bubble th { background: var(--panel-2); font-weight: 600; }
.bubble tr:last-child td { border-bottom: 0; }

.code { margin: 0 0 10px; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; background: #0e0e10; }
.code-h {
  display: flex;
  align-items: center;
  gap: 2px;
  height: 36px;
  padding: 0 6px 0 12px;
  background: var(--panel-2);
  color: var(--muted);
  font-size: 12px;
}
.code-h .tag { flex: 1; line-height: 1; }
.code-h button {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 28px;
  padding: 0 8px;
  border: 0;
  background: transparent;
  color: var(--muted);
  border-radius: 6px;
  font-size: 12px;
}
.code-h button:hover, .code-h button:active { background: var(--panel-3); color: var(--text); }
.code-h button .ic { width: 14px; height: 14px; }
.code pre { margin: 0; padding: 12px; overflow-x: auto; }
.code pre code { background: transparent; padding: 0; font-size: 13px; white-space: pre; }

/* Composer */
.composer { width: 100%; max-width: calc(var(--thread-w) + 24px); margin: 0 auto; padding: 6px 12px calc(10px + var(--safe-bottom)); }
.box {
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 20px;
  padding: 6px 6px 6px 10px;
  transition: border-color .15s;
}
.box:focus-within { border-color: #4b3340; }
.box.drag { border-color: var(--brand2); }
#input {
  display: block;
  width: 100%;
  resize: none;
  max-height: 200px;
  min-height: 40px;
  padding: 8px 6px;
  border: 0;
  background: transparent;
  outline: none;
  line-height: 1.45;
}
.row { display: flex; align-items: center; gap: 4px; }
.row .icon-btn { width: 38px; height: 38px; color: var(--muted); }
.send {
  width: 38px;
  height: 38px;
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  border: 0;
  background: var(--grad);
  color: #fff;
}
.send:disabled { opacity: .45; }
.send .ic { width: 19px; height: 19px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; padding: 4px 4px 2px 0; }
.chips:empty { display: none; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 34px;
  max-width: 100%;
  padding: 0 4px 0 8px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  font-size: 13px;
}
.chip .ic { width: 15px; height: 15px; color: var(--muted); }
.chip .nm { min-width: 0; max-width: 180px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; line-height: 1; }
.chip .st { color: var(--muted); line-height: 1; }
.chip.bad { border-color: rgba(255, 107, 107, .5); }
.chip .x {
  width: 26px;
  height: 26px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--muted);
}
.chip .x .ic { width: 14px; height: 14px; color: inherit; }
.chip .x:hover { background: var(--panel-3); color: var(--text); }
.chip img { width: 22px; height: 22px; object-fit: cover; border-radius: 5px; flex: none; }

.drop-overlay {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  background: rgba(10, 10, 11, .82);
  border: 2px dashed var(--brand2);
  font-size: 18px;
  font-weight: 600;
  pointer-events: none;
}
.drop-overlay .ic { width: 22px; height: 22px; }

/* Modal / settings */
.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(0, 0, 0, .65);
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.modal-card {
  width: 100%;
  max-width: 560px;
  height: min(92dvh, 760px);
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px 18px 0 0;
  padding: 12px 16px calc(12px + var(--safe-bottom));
}
.modal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; }
.modal-title { font-size: 18px; font-weight: 700; }

.tabs { display: flex; gap: 4px; padding: 4px; background: var(--bg); border-radius: 12px; margin-bottom: 8px; }
.tab {
  flex: 1;
  height: 36px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: var(--muted);
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.tab.active { background: var(--panel-3); color: var(--text); }
.tab-body { flex: 1; overflow-y: auto; padding: 0 2px; -webkit-overflow-scrolling: touch; }

.field-label { display: block; margin: 14px 0 6px; color: var(--muted); font-size: 13px; }
.pane select, .pane textarea, .pane input[type="number"], .pane input[type="text"] {
  width: 100%;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--bg);
  outline: none;
}
.pane select:focus, .pane textarea:focus, .pane input:focus { border-color: var(--brand2); }
.pane select { height: 42px; padding-top: 0; padding-bottom: 0; }
.pane textarea { resize: vertical; min-height: 100px; }
.help { color: var(--muted); font-size: 13px; margin-top: 6px; }
.modal-actions { display: flex; justify-content: space-between; gap: 10px; margin-top: 18px; }
.modal-actions.wrap { flex-wrap: wrap; justify-content: flex-start; }
.version { color: var(--muted); font-size: 12px; text-align: center; padding-top: 8px; }

.seg { display: flex; gap: 4px; padding: 4px; background: var(--bg); border: 1px solid var(--line); border-radius: 12px; }
.seg button {
  flex: 1;
  height: 36px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: var(--muted);
  display: flex;
  align-items: center;
  justify-content: center;
}
.seg button.on { background: var(--grad); color: #fff; font-weight: 600; }

.switch-row { display: flex; align-items: center; justify-content: space-between; gap: 14px; padding: 8px 0 2px; }
.switch-title { font-weight: 600; }
.switch { position: relative; width: 46px; height: 28px; flex: none; display: block; }
.switch input { position: absolute; inset: 0; opacity: 0; margin: 0; width: 100%; height: 100%; cursor: pointer; z-index: 1; }
.switch .knob { position: absolute; inset: 0; border-radius: 28px; background: var(--panel-3); border: 1px solid var(--line); transition: background .15s; }
.switch .knob::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 3px;
  width: 20px;
  height: 20px;
  margin-top: -10px;
  border-radius: 50%;
  background: #fff;
  transition: transform .15s;
}
.switch input:checked + .knob { background: var(--grad); border-color: transparent; }
.switch input:checked + .knob::after { transform: translateX(18px); }

.mem-list { display: flex; flex-direction: column; gap: 6px; }
.mem {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 4px 4px 12px;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
}
.mem .t { flex: 1; min-width: 0; overflow-wrap: anywhere; font-size: 14px; }
.mem .src { color: var(--muted); font-size: 11px; line-height: 1; flex: none; }
.mem .icon-btn { width: 32px; height: 32px; color: var(--muted); }
.mem .icon-btn .ic { width: 16px; height: 16px; }
.add-row { display: flex; gap: 8px; margin-top: 10px; }
.add-row input { flex: 1; min-width: 0; }

.meter { height: 10px; border-radius: 10px; background: var(--bg); border: 1px solid var(--line); overflow: hidden; margin-top: 6px; }
.meter-fill { height: 100%; width: 0; background: var(--grad); border-radius: 10px; }
.kv { margin-top: 10px; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.kv .r { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 12px; border-bottom: 1px solid var(--line); font-size: 14px; }
.kv .r:last-child { border-bottom: 0; }
.kv .r .k { color: var(--muted); min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.kv .r .v { text-align: right; flex: none; }
.cards { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 6px; }
.card { border: 1px solid var(--line); border-radius: 12px; padding: 12px; background: var(--bg); }
.card .n { font-size: 20px; font-weight: 700; line-height: 1.2; }
.card .l { color: var(--muted); font-size: 12px; margin-top: 2px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(100px + var(--safe-bottom));
  transform: translateX(-50%);
  background: var(--panel-3);
  border: 1px solid var(--line);
  padding: 8px 14px;
  border-radius: 10px;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s;
  z-index: 60;
  max-width: 90vw;
  text-align: center;
}
.toast.show { opacity: 1; }

/* Mobile drawer */
@media (max-width: 899px) {
  .sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    transform: translateX(-100%);
    transition: transform .22s ease;
    width: min(84vw, 320px);
  }
  .app.drawer .sidebar { transform: translateX(0); }
  .scrim {
    position: fixed;
    inset: 0;
    z-index: 20;
    background: rgba(0, 0, 0, .55);
    opacity: 0;
    pointer-events: none;
    transition: opacity .2s;
  }
  .app.drawer .scrim { opacity: 1; pointer-events: auto; }
  #modelSelect { flex: 1 1 auto; }
  .topbar .spacer { display: none; }
}

@media (min-width: 900px) {
  #menuBtn, .mobile-only { display: none; }
  .messages { padding: 24px 24px 8px; }
  .modal { align-items: center; }
  .modal-card { border-radius: 18px; height: min(86dvh, 720px); }
  .session .mini { opacity: 0; }
  .session:hover .mini, .session.active .mini { opacity: 1; }
  .msg { font-size: 16px; }
}
