:root {
  --bg: #0f1117;
  --bg-elev: #171a23;
  --bg-elev-2: #1f2430;
  --border: #2a2f3a;
  --text: #e7e9ee;
  --text-dim: #9aa1ad;
  --accent: #10b981;
  --accent-dim: #0d9669;
  --user-bubble: #2b3242;
  --danger: #ef4444;
  --radius: 16px;
  --maxw: 820px;
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
}

body {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

/* ----------------------------- Top bar ----------------------------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-elev);
  flex-shrink: 0;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand .logo {
  font-size: 26px;
  color: var(--accent);
  line-height: 1;
}
.brand h1 { font-size: 16px; margin: 0; font-weight: 600; }
.brand .sub { font-size: 12px; color: var(--text-dim); }

.controls { display: flex; align-items: center; gap: 14px; }
.ctrl { display: flex; align-items: center; gap: 7px; }
.ctrl label { font-size: 12px; color: var(--text-dim); white-space: nowrap; }
.ctrl select, .ctrl input {
  background: var(--bg-elev-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 8px;
  font-size: 13px;
}
#langSelect { min-width: 130px; max-height: 120px; }
#modeSelect { min-width: 150px; }
#allowInput { width: 130px; }

/* ----------------------------- Chat ----------------------------- */
.chat {
  flex: 1;
  overflow-y: auto;
  padding: 24px 16px 8px;
  scroll-behavior: smooth;
}

.welcome {
  max-width: var(--maxw);
  margin: 8vh auto 0;
  text-align: center;
  color: var(--text-dim);
}
.welcome-icon { font-size: 46px; color: var(--accent); }
.welcome h2 { color: var(--text); font-size: 22px; margin: 12px 0 4px; }
.hints {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  max-width: 540px;
  margin: 22px auto 0;
}
.hint {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  font-size: 13px;
  text-align: left;
}
.hint b { color: var(--text); }
kbd {
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 5px;
  padding: 1px 6px;
  font-size: 11px;
}

/* ----------------------------- Messages ----------------------------- */
.msg {
  max-width: var(--maxw);
  margin: 0 auto 22px;
  display: flex;
  gap: 14px;
  align-items: flex-start;
  animation: rise .25s ease;
}
@keyframes rise { from { opacity: 0; transform: translateY(8px);} to {opacity:1; transform:none;} }

.avatar {
  width: 30px; height: 30px;
  border-radius: 8px;
  flex-shrink: 0;
  display: grid;
  place-items: center;
  font-size: 15px;
  font-weight: 700;
}
.msg.user .avatar { background: var(--user-bubble); color: var(--text); }
.msg.bot .avatar { background: var(--accent); color: #052b1f; }

.bubble { flex: 1; min-width: 0; padding-top: 3px; }
.msg .role { font-size: 12px; color: var(--text-dim); margin-bottom: 6px; }

.thumb {
  max-width: 280px;
  max-height: 220px;
  border-radius: 12px;
  border: 1px solid var(--border);
  display: block;
  margin-bottom: 8px;
}
.msg.user .filename { font-size: 12px; color: var(--text-dim); }

/* OCR result card */
.result-text {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 14.5px;
  line-height: 1.55;
}
.result-text.empty { color: var(--text-dim); font-style: italic; }

.stats {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 10px 0;
}
.stat {
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 4px 11px;
  font-size: 12px;
  color: var(--text-dim);
}
.stat b { color: var(--text); font-weight: 600; }

.actions { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.action-btn {
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 6px 12px;
  font-size: 12.5px;
  cursor: pointer;
  transition: background .15s;
}
.action-btn:hover { background: #2a3140; }
.action-btn.done { color: var(--accent); border-color: var(--accent-dim); }

.annotated {
  margin-top: 10px;
  max-width: 100%;
  border-radius: 12px;
  border: 1px solid var(--border);
  display: none;
}
.annotated.show { display: block; }

details.blocks { margin-top: 10px; }
details.blocks summary {
  cursor: pointer;
  font-size: 12.5px;
  color: var(--text-dim);
  user-select: none;
}
.block-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
.block-row:last-child { border-bottom: none; }
.block-conf { color: var(--text-dim); white-space: nowrap; }
.block-conf.low { color: var(--danger); }

.err { color: var(--danger); }

/* typing dots */
.typing { display: inline-flex; gap: 4px; padding: 6px 2px; }
.typing span {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--text-dim);
  animation: blink 1.2s infinite both;
}
.typing span:nth-child(2) { animation-delay: .2s; }
.typing span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%,80%,100%{opacity:.2;} 40%{opacity:1;} }

/* ----------------------------- Composer ----------------------------- */
.composer-wrap {
  flex-shrink: 0;
  padding: 8px 16px 14px;
  background: linear-gradient(transparent, var(--bg) 30%);
}
.composer {
  max-width: var(--maxw);
  margin: 0 auto;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px;
  transition: border-color .15s;
}
.composer:focus-within { border-color: var(--accent-dim); }

.attachments { display: flex; flex-wrap: wrap; gap: 8px; padding: 4px 4px 0; }
.attachments:empty { display: none; }
.attach-chip {
  position: relative;
  width: 56px; height: 56px;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--border);
  background: var(--bg-elev-2);
  flex-shrink: 0;
}
.attach-chip img { width: 100%; height: 100%; object-fit: cover; }
.attach-chip .pdf-tag {
  display: grid; place-items: center;
  width: 100%; height: 100%;
  font-size: 11px; color: var(--text-dim);
  text-align: center;
}
.attach-chip .remove {
  position: absolute; top: 2px; right: 2px;
  width: 18px; height: 18px;
  border-radius: 50%;
  background: rgba(0,0,0,.65);
  color: #fff; border: none;
  cursor: pointer; font-size: 12px; line-height: 1;
}

.composer-row { display: flex; align-items: flex-end; gap: 8px; }
.icon-btn, .send-btn {
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  color: var(--text);
  width: 38px; height: 38px;
  border-radius: 10px;
  display: grid; place-items: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: background .15s, opacity .15s;
}
.icon-btn:hover { background: #2a3140; }
.send-btn { background: var(--accent); color: #052b1f; border-color: var(--accent); }
.send-btn:hover:not(:disabled) { background: var(--accent-dim); }
.send-btn:disabled { opacity: .4; cursor: not-allowed; }

#prompt {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--text);
  resize: none;
  font-size: 15px;
  line-height: 1.5;
  padding: 9px 4px;
  max-height: 160px;
  font-family: inherit;
}
#prompt:focus { outline: none; }

.foot-note {
  max-width: var(--maxw);
  margin: 8px auto 0;
  text-align: center;
  font-size: 11px;
  color: var(--text-dim);
}

/* ----------------------------- Drop zone ----------------------------- */
.drop-zone {
  position: fixed; inset: 0;
  background: rgba(15,17,23,.92);
  z-index: 50;
  display: none;
  place-items: center;
  border: 3px dashed var(--accent);
}
.drop-zone.show { display: grid; }
.drop-inner { text-align: center; color: var(--accent); }
.drop-inner p { margin-top: 12px; font-size: 18px; }

/* scrollbar */
.chat::-webkit-scrollbar { width: 10px; }
.chat::-webkit-scrollbar-thumb { background: var(--border); border-radius: 8px; }

@media (max-width: 600px) {
  .hints { grid-template-columns: 1fr; }
  .brand .sub { display: none; }
}
