:root {
  --ink: #15130F;
  --ink-soft: #1E1B15;
  --panel: #242019;
  --line: #3A3327;
  --paper: #EFE6D4;
  --paper-dim: #B8AD96;
  --turmeric: #E8A23D;
  --turmeric-soft: #4A3A1F;
  --peacock: #2C8A84;
  --peacock-soft: #173430;
  --danger: #C8584A;
  --radius: 14px;
  --font-display: Georgia, "Tiro Devanagari Hindi", "Noto Serif", serif;
  --font-body: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }

/* The hidden attribute must always win, no matter what display value a
   class sets elsewhere. Without this, elements like .chat-wrap and
   .dropzone-wrap (which set display:flex) stay visible even when JS sets
   their `hidden` attribute, because an author rule's display:flex beats the
   browser default [hidden]{display:none} regardless of selector specificity. */
[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--ink);
  color: var(--paper);
  font-family: var(--font-body);
  -webkit-font-smoothing: antialiased;
}

body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh;
}

button { font-family: inherit; }

::selection { background: var(--turmeric); color: var(--ink); }

:focus-visible {
  outline: 2px solid var(--turmeric);
  outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    transition-duration: 0.001ms !important;
  }
}

/* ---------- Top bar ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
  background: var(--ink-soft);
  flex-shrink: 0;
}

.brand {
  display: flex;
  align-items: baseline;
  gap: 10px;
  min-width: 0;
}

.brand-mark {
  font-family: var(--font-display);
  font-size: 21px;
  font-weight: 700;
  letter-spacing: 0.2px;
  color: var(--turmeric);
  white-space: nowrap;
}

.brand-doc {
  font-size: 13px;
  color: var(--paper-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

.status-pill {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  padding: 5px 10px;
  border-radius: 999px;
  background: var(--panel);
  border: 1px solid var(--line);
  color: var(--paper-dim);
  white-space: nowrap;
}

.status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--paper-dim);
  flex-shrink: 0;
}
.status-dot.ready { background: var(--peacock); box-shadow: 0 0 6px var(--peacock); }
.status-dot.busy { background: var(--turmeric); animation: pulse 1.2s ease-in-out infinite; }
.status-dot.off { background: #6b6253; }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.35; }
}

.icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--paper);
  cursor: pointer;
  font-size: 16px;
}
.icon-btn:hover { border-color: var(--turmeric); }

/* ---------- Main area ---------- */

main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

/* Drop zone (pre-load state) */

.dropzone-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.dropzone {
  width: 100%;
  max-width: 480px;
  border: 1.5px dashed var(--line);
  border-radius: 20px;
  padding: 48px 28px;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.2s ease, background 0.2s ease;
  background: var(--ink-soft);
  position: relative;
  overflow: hidden;
}

.dropzone.drag-over {
  border-color: var(--turmeric);
  background: var(--turmeric-soft);
}

.dropzone-glyph {
  width: 56px;
  height: 56px;
  margin: 0 auto 18px;
  position: relative;
}

.dropzone-glyph svg { width: 100%; height: 100%; }

.dropzone h1 {
  font-family: var(--font-display);
  font-size: 22px;
  margin: 0 0 8px;
  color: var(--paper);
}

.dropzone p {
  margin: 0 0 4px;
  color: var(--paper-dim);
  font-size: 14px;
  line-height: 1.5;
}

.dropzone .hint {
  font-size: 12px;
  color: var(--paper-dim);
  opacity: 0.75;
  margin-top: 14px;
}

.scan-line {
  position: absolute;
  left: 0; right: 0; top: 0;
  height: 2px;
  background: linear-gradient(90deg, transparent, var(--turmeric), transparent);
  transform: translateY(-10px);
  opacity: 0;
}

.dropzone.scanning .scan-line {
  animation: scan 1.1s ease-in-out 1;
  opacity: 1;
}

@keyframes scan {
  0% { transform: translateY(0); opacity: 0; }
  15% { opacity: 1; }
  100% { transform: translateY(220px); opacity: 0; }
}

input[type="file"] { display: none; }

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

.chat-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.chat-thread {
  flex: 1;
  overflow-y: auto;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.msg {
  max-width: 78%;
  padding: 12px 14px;
  border-radius: var(--radius);
  font-size: 14.5px;
  line-height: 1.55;
  white-space: pre-wrap;
}

.msg.user {
  align-self: flex-end;
  background: var(--turmeric);
  color: var(--ink);
  border-bottom-right-radius: 4px;
}

.msg.assistant {
  align-self: flex-start;
  background: var(--panel);
  border: 1px solid var(--line);
  border-bottom-left-radius: 4px;
}

.msg.system-note {
  align-self: center;
  background: transparent;
  color: var(--paper-dim);
  font-size: 12.5px;
  text-align: center;
  max-width: 90%;
}

.msg .source-tag {
  display: block;
  margin-top: 8px;
  font-size: 11px;
  color: var(--peacock);
  opacity: 0.9;
}

.typing {
  display: inline-flex;
  gap: 4px;
  align-items: center;
}
.typing span {
  width: 5px; height: 5px;
  border-radius: 50%;
  background: var(--paper-dim);
  animation: bounce 1.2s infinite ease-in-out;
}
.typing span:nth-child(2) { animation-delay: 0.15s; }
.typing span:nth-child(3) { animation-delay: 0.3s; }
@keyframes bounce {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.5; }
  40% { transform: translateY(-4px); opacity: 1; }
}

/* ---------- Composer ---------- */

.composer {
  flex-shrink: 0;
  display: flex;
  gap: 10px;
  padding: 12px 14px;
  border-top: 1px solid var(--line);
  background: var(--ink-soft);
}

.composer textarea {
  flex: 1;
  resize: none;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--paper);
  padding: 12px 14px;
  font-size: 14.5px;
  font-family: inherit;
  max-height: 120px;
  min-height: 44px;
}
.composer textarea:focus { border-color: var(--turmeric); }

.send-btn {
  flex-shrink: 0;
  width: 44px;
  height: 44px;
  border-radius: 12px;
  border: none;
  background: var(--turmeric);
  color: var(--ink);
  font-size: 17px;
  cursor: pointer;
}
.send-btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* ---------- Settings drawer ---------- */

.drawer-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.5);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
  z-index: 20;
}
.drawer-backdrop.open { opacity: 1; pointer-events: auto; }

.drawer {
  position: fixed;
  top: 0; right: 0;
  width: min(360px, 92vw);
  height: 100%;
  background: var(--ink-soft);
  border-left: 1px solid var(--line);
  z-index: 21;
  transform: translateX(100%);
  transition: transform 0.25s ease;
  display: flex;
  flex-direction: column;
  padding: 20px;
  overflow-y: auto;
}
.drawer.open { transform: translateX(0); }

.drawer h2 {
  font-family: var(--font-display);
  font-size: 18px;
  margin: 0 0 18px;
  color: var(--turmeric);
}

.field-label {
  font-size: 12.5px;
  color: var(--paper-dim);
  margin: 16px 0 6px;
  display: block;
}

.field-label:first-of-type { margin-top: 0; }

.drawer input[type="text"],
.drawer input[type="password"],
.drawer-select {
  width: 100%;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--paper);
  font-size: 13.5px;
}

.drawer-select {
  appearance: none;
  -webkit-appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--paper-dim) 50%), linear-gradient(135deg, var(--paper-dim) 50%, transparent 50%);
  background-position: calc(100% - 16px) center, calc(100% - 11px) center;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  cursor: pointer;
}

.model-row.selectable { cursor: pointer; }
.model-row.selectable:hover { border-color: var(--turmeric); }
.model-row.active { border-color: var(--peacock); background: var(--peacock-soft); }
.model-row .row-main { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.model-row .row-action { font-size: 11.5px; color: var(--turmeric); white-space: nowrap; flex-shrink: 0; margin-left: 8px; }

.offline-progress-row { padding: 0 12px 10px; margin-top: -8px; margin-bottom: 8px; }
.offline-progress-row .progress-track { margin-top: 4px; }
.offline-progress-row .meta { font-size: 11px; color: var(--paper-dim); }

.source-chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.source-chip {
  font-size: 10.5px;
  padding: 3px 8px;
  border-radius: 999px;
  background: var(--peacock-soft);
  color: var(--peacock);
  border: 1px solid var(--peacock);
  white-space: nowrap;
}

.msg.error { background: var(--ink-soft); border: 1px solid var(--danger); color: var(--danger); align-self: center; max-width: 90%; font-size: 13px; }

/* ---------- License gate ---------- */

.gate-backdrop {
  position: fixed; inset: 0;
  background: var(--ink);
  z-index: 50;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.gate-backdrop.open { display: flex; }

.gate-card {
  width: 100%;
  max-width: 380px;
  background: var(--ink-soft);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 32px 26px;
  text-align: center;
  position: relative;
}

.gate-message {
  color: var(--paper-dim);
  font-size: 14px;
  line-height: 1.5;
  margin: 14px 0 18px;
}

#gateKeyInput {
  width: 100%;
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--paper);
  font-size: 15px;
  text-align: center;
  letter-spacing: 0.04em;
  font-family: monospace;
  text-transform: uppercase;
}
#gateKeyInput:focus { border-color: var(--turmeric); }

.gate-error {
  color: var(--danger);
  font-size: 12.5px;
  min-height: 16px;
  margin: 10px 0 0;
}

.drawer .small-note {
  font-size: 11.5px;
  color: var(--paper-dim);
  line-height: 1.5;
  margin-top: 6px;
}

.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 16px;
  padding: 12px;
  background: var(--panel);
  border-radius: 10px;
  border: 1px solid var(--line);
}

.toggle-row span { font-size: 13.5px; }

.switch {
  position: relative;
  width: 40px;
  height: 22px;
  flex-shrink: 0;
}
.switch input { opacity: 0; width: 0; height: 0; }
.switch-track {
  position: absolute; inset: 0;
  background: var(--line);
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.2s ease;
}
.switch-track::before {
  content: "";
  position: absolute;
  width: 16px; height: 16px;
  left: 3px; top: 3px;
  background: var(--paper);
  border-radius: 50%;
  transition: transform 0.2s ease;
}
.switch input:checked + .switch-track { background: var(--peacock); }
.switch input:checked + .switch-track::before { transform: translateX(18px); }

.btn {
  margin-top: 18px;
  padding: 11px 16px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--paper);
  font-size: 13.5px;
  cursor: pointer;
  text-align: center;
}
.btn:hover { border-color: var(--turmeric); }
.btn.primary {
  background: var(--turmeric);
  color: var(--ink);
  border: none;
  font-weight: 600;
}
.btn.danger { color: var(--danger); }

.model-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  margin-bottom: 8px;
  font-size: 13px;
}

.model-row .meta { color: var(--paper-dim); font-size: 11.5px; }

.progress-track {
  width: 100%;
  height: 5px;
  background: var(--panel);
  border-radius: 999px;
  overflow: hidden;
  margin-top: 10px;
}
.progress-fill {
  height: 100%;
  background: var(--turmeric);
  width: 0%;
  transition: width 0.2s ease;
}

.drawer-close {
  position: absolute;
  top: 18px; right: 18px;
  background: none;
  border: none;
  color: var(--paper-dim);
  font-size: 18px;
  cursor: pointer;
}

@media (min-width: 760px) {
  .chat-thread { padding: 24px 14%; }
  .composer { padding: 14px 14%; }
  .msg { max-width: 65%; }
}
