:root {
  --ink: #17261c;
  --ink-soft: #4b5a45;
  --paper: #f3eee0;
  --paper-raised: #fffcf5;
  --panel: #ece3c9;
  --line: #d9cca4;
  --green-deep: #133423;
  --green: #1f6f4a;
  --green-bright: #3c9468;
  --gold: #b8842a;
  --gold-bright: #d9ac52;
  --user-bubble: #fbf1d9;
  --assistant-bubble: #163c27;
  --assistant-ink: #f2ecd8;
  --danger: #a3402f;
  --shadow: 0 8px 24px -12px rgba(19, 52, 35, 0.35);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --ink: #eef2e9;
    --ink-soft: #b3c0ac;
    --paper: #0e1911;
    --paper-raised: #172518;
    --panel: #142118;
    --line: #2a3a2b;
    --green-deep: #0b1e13;
    --green: #2f8a5e;
    --green-bright: #57c98c;
    --gold: #d9ac52;
    --gold-bright: #eec87b;
    --user-bubble: #23301e;
    --assistant-bubble: #12291a;
    --assistant-ink: #eef2e9;
    --shadow: 0 8px 28px -12px rgba(0, 0, 0, 0.55);
  }
}
:root[data-theme="dark"] {
  --ink: #eef2e9;
  --ink-soft: #b3c0ac;
  --paper: #0e1911;
  --paper-raised: #172518;
  --panel: #142118;
  --line: #2a3a2b;
  --green-deep: #0b1e13;
  --green: #2f8a5e;
  --green-bright: #57c98c;
  --gold: #d9ac52;
  --gold-bright: #eec87b;
  --user-bubble: #23301e;
  --assistant-bubble: #12291a;
  --assistant-ink: #eef2e9;
  --shadow: 0 8px 28px -12px rgba(0, 0, 0, 0.55);
}

* { box-sizing: border-box; }
html { height: 100%; -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: "Mukta", "Noto Sans", system-ui, sans-serif;
  min-height: 100vh;
  min-height: 100dvh; /* iOS/Android address-bar-safe viewport height; vh above is the fallback for browsers without dvh */
  display: flex;
  flex-direction: column;
  overflow-x: hidden; /* safety net: nothing should ever force horizontal scroll, but this stops it from being visible if something slips through */
}
h1, h2, h3 { text-wrap: balance; font-family: "Yatra One", "Mukta", system-ui, sans-serif; font-weight: 400; margin: 0; }
::selection { background: var(--gold-bright); color: var(--green-deep); }
[hidden] { display: none !important; }

header {
  background: var(--green-deep);
  color: var(--paper-raised);
  border-bottom: 3px solid var(--gold);
  padding: 14px 20px;
  display: flex;
  align-items: center;
  gap: 14px;
}
.arch { flex: 0 0 auto; width: 40px; height: 40px; }
.arch path { fill: none; stroke: var(--gold-bright); stroke-width: 2.4; }
.arch circle { fill: var(--gold-bright); }
.brand { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.brand h1 { font-size: clamp(1.15rem, 1rem + 1vw, 1.6rem); color: var(--paper-raised); letter-spacing: 0.02em; }
.brand p { margin: 0; font-size: 0.78rem; color: var(--gold-bright); letter-spacing: 0.04em; text-transform: uppercase; }
.header-link {
  font-size: 0.8rem;
  color: var(--gold-bright);
  text-decoration: none;
  border: 1px solid var(--gold);
  border-radius: 999px;
  padding: 7px 13px;
  background: none;
  cursor: pointer;
  white-space: nowrap;
}
.header-link:hover { background: var(--green); }
#helpToggleBtn { margin-left: auto; } /* anchors the right-aligned cluster (status + Help + Pay); nothing else needs its own auto margin */
.header-link-pay { background: var(--gold); color: var(--green-deep); border-color: var(--gold); font-weight: 700; }
.header-link-pay:hover { background: var(--gold-bright); }

.header-status { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.usage-bubble {
  font-size: 0.76rem; color: var(--paper-raised); background: rgba(255,255,255,0.12);
  border: 1px solid var(--gold); border-radius: 999px; padding: 5px 11px; white-space: nowrap;
}
.expiry-badge {
  font-size: 0.72rem; font-weight: 700; color: var(--green-deep); background: var(--gold-bright);
  border-radius: 999px; padding: 4px 10px; white-space: nowrap;
}

main.chat {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  max-width: 760px;
  width: 100%;
  margin: 0 auto;
}

.user-name-label { font-size: 0.8rem; color: var(--gold-bright); white-space: nowrap; }

.auth-gate {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 20px;
}
.auth-card {
  width: 100%;
  max-width: 360px;
  background: var(--paper-raised);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 24px 22px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.auth-title { font-size: 1.4rem; color: var(--ink); text-align: center; }
.auth-subtitle { margin: 0; font-size: 0.85rem; color: var(--ink-soft); text-align: center; line-height: 1.5; }
.auth-form { display: flex; flex-direction: column; gap: 10px; }
.auth-form input {
  font: inherit; font-size: 0.9rem; padding: 10px 12px; border-radius: 8px; border: 1px solid var(--line);
  background: var(--paper); color: var(--ink); width: 100%;
}
.auth-form button[type="submit"] {
  background: var(--green); color: var(--paper-raised); border: none; border-radius: 8px;
  padding: 10px 14px; font: inherit; font-weight: 600; cursor: pointer; margin-top: 4px;
}
.auth-form button[type="submit"]:hover:not(:disabled) { background: var(--green-bright); }
.auth-form button[type="submit"]:disabled { opacity: 0.6; cursor: not-allowed; }
.auth-toggle {
  background: none; border: none; color: var(--ink-soft); font: inherit; font-size: 0.85rem;
  cursor: pointer; text-decoration: underline; padding: 4px 0; text-align: center;
}
.banner {
  margin: 12px 20px 0;
  padding: 10px 14px;
  border-radius: 10px;
  background: var(--panel);
  border: 1px solid var(--line);
  color: var(--ink-soft);
  font-size: 0.85rem;
}
.usage-status {
  margin: 10px 20px 0;
  padding: 6px 14px;
  font-size: 0.78rem;
  color: var(--ink-soft);
}
.help-panel {
  margin: 12px 20px 0; padding: 12px 14px; border-radius: 10px; background: var(--paper-raised);
  border: 1px solid var(--line); display: flex; flex-direction: column; gap: 8px;
}
.help-panel-label { margin: 0; font-size: 0.82rem; color: var(--ink-soft); line-height: 1.5; }
.help-panel textarea {
  font: inherit; font-size: 0.9rem; padding: 9px 11px; border-radius: 8px; border: 1px solid var(--line);
  background: var(--paper); color: var(--ink); resize: vertical; width: 100%;
}
.help-panel-actions { display: flex; gap: 10px; align-items: center; }
.help-panel-actions button[type="submit"] {
  background: var(--green); color: var(--paper-raised); border: none; border-radius: 8px;
  padding: 8px 14px; font: inherit; font-weight: 600; cursor: pointer;
}
.help-panel-actions button[type="submit"]:hover:not(:disabled) { background: var(--green-bright); }

.pay-panel {
  margin: 12px 20px 0; padding: 12px 14px; border-radius: 10px; background: var(--paper-raised);
  border: 1px solid var(--line); display: flex; flex-direction: column; gap: 10px;
}
.pay-panel-label { margin: 0; font-size: 0.82rem; color: var(--ink-soft); line-height: 1.5; }
.pay-panel .pay-btn { width: 100%; text-align: left; }
.help-panel-actions button[type="submit"]:disabled { opacity: 0.6; cursor: not-allowed; }
.help-panel-actions button[type="button"] {
  background: none; border: none; color: var(--ink-soft); font: inherit; font-size: 0.85rem;
  cursor: pointer; text-decoration: underline; padding: 4px 0;
}

.messages { flex: 1; overflow-y: auto; padding: 22px 20px; display: flex; flex-direction: column; gap: 14px; }
.load-more-btn {
  align-self: center; margin-bottom: 4px; font: inherit; font-size: 0.8rem; background: var(--panel); color: var(--ink-soft);
  border: 1px solid var(--line); border-radius: 999px; padding: 6px 14px; cursor: pointer;
}
.load-more-btn:hover { border-color: var(--gold); color: var(--green-deep); }
.msg { max-width: 82%; display: flex; flex-direction: column; gap: 4px; }
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.assistant { align-self: flex-start; align-items: flex-start; }
.msg.owner { align-self: flex-start; align-items: flex-start; }
.msg.owner .bubble-label { margin: 0 0 3px; font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--gold); }
.bubble { padding: 12px 15px; border-radius: 14px; line-height: 1.55; font-size: 0.95rem; box-shadow: var(--shadow); }
.bubble-p { margin: 0 0 8px; }
.bubble-p:last-child { margin-bottom: 0; }
.msg.user .bubble { background: var(--user-bubble); color: var(--ink); border-bottom-right-radius: 4px; border: 1px solid var(--line); }
.msg.assistant .bubble { background: var(--assistant-bubble); color: var(--assistant-ink); border-bottom-left-radius: 4px; }
.msg.owner .bubble { background: var(--panel); color: var(--ink); border: 1px solid var(--gold); border-bottom-left-radius: 4px; }
.bubble b, .bubble strong { color: var(--gold-bright); }
.msg.user .bubble b, .msg.user .bubble strong { color: var(--gold); }
.bubble ul { margin: 6px 0 0; padding-left: 1.15em; }
.bubble .thinking { display: inline-flex; gap: 4px; align-items: center; }
.bubble .thinking span { width: 6px; height: 6px; border-radius: 50%; background: var(--gold-bright); animation: bounce 1.1s infinite ease-in-out; }
.bubble .thinking span:nth-child(2) { animation-delay: 0.15s; }
.bubble .thinking 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; } }
@media (prefers-reduced-motion: reduce) { .bubble .thinking span { animation: none; } }

.examples { padding: 0 20px 14px; }
.examples-label { margin: 0 0 8px; font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--ink-soft); }
.chip-row { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  font: inherit; font-size: 0.82rem; background: var(--panel); color: var(--ink);
  border: 1px solid var(--line); border-radius: 999px; padding: 7px 13px; cursor: pointer; text-align: left;
}
.chip:hover { border-color: var(--gold); color: var(--green-deep); }

form.composer { display: flex; gap: 10px; padding: 14px 20px 18px; border-top: 1px solid var(--line); background: var(--paper); }
form.composer textarea {
  flex: 1; resize: none; font: inherit; font-size: 0.95rem; padding: 11px 14px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--paper-raised); color: var(--ink); min-height: 46px; max-height: 140px;
}
form.composer textarea:focus { outline: 2px solid var(--green-bright); outline-offset: 1px; }
form.composer button {
  background: var(--gold); color: var(--green-deep); border: none; border-radius: 12px; padding: 0 20px;
  font: inherit; font-weight: 700; cursor: pointer; flex: 0 0 auto;
}
form.composer button:hover:not(:disabled) { background: var(--gold-bright); }
form.composer button:disabled { opacity: 0.5; cursor: not-allowed; }

.payment-prompt { margin-top: 6px; max-width: 82%; align-self: flex-start; display: flex; flex-direction: column; gap: 8px; }
.payment-prompt-note { margin: 0; font-size: 0.85rem; color: var(--ink-soft); }
.pay-btn {
  align-self: flex-start; background: var(--gold); color: var(--green-deep); border: none; border-radius: 10px;
  padding: 10px 16px; font: inherit; font-weight: 700; cursor: pointer;
}
.pay-btn:hover:not(:disabled) { background: var(--gold-bright); }
.pay-btn:disabled { opacity: 0.6; cursor: not-allowed; }

.pooja-options { margin-top: 6px; max-width: 82%; align-self: flex-start; }
.pooja-options-label { margin: 0 0 6px; font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--ink-soft); }
.pooja-chip.active { background: var(--green); color: var(--paper-raised); border-color: var(--green); }
.pooja-detail { margin-top: 10px; padding: 12px 14px; background: var(--paper-raised); border: 1px solid var(--line); border-radius: 12px; display: flex; flex-direction: column; gap: 10px; }
.pooja-desc { margin: 0; font-size: 0.88rem; line-height: 1.55; color: var(--ink); }
.link-card {
  display: flex; flex-direction: column; gap: 3px; text-decoration: none;
  padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--panel);
}
.link-card:hover { border-color: var(--gold); }
.link-card-title { font-weight: 600; font-size: 0.88rem; color: var(--green); }
.link-card-domain { font-size: 0.76rem; color: var(--ink-soft); }

main.admin { flex: 1; max-width: 720px; width: 100%; margin: 0 auto; padding: 24px 20px 60px; display: flex; flex-direction: column; gap: 20px; }
main.admin .hint { font-size: 0.88rem; color: var(--ink-soft); line-height: 1.55; margin: 0; max-width: 62ch; }
.book-list { display: flex; flex-direction: column; gap: 10px; }
.book-card { background: var(--paper-raised); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; display: flex; flex-direction: column; gap: 6px; box-shadow: var(--shadow); }
.book-card .row { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: 4px 10px; }
.book-card .title { font-weight: 600; font-size: 0.98rem; color: var(--ink); overflow-wrap: anywhere; min-width: 0; }
.book-card .meta { font-size: 0.78rem; color: var(--ink-soft); white-space: nowrap; }
.book-card .preview { font-size: 0.85rem; color: var(--ink-soft); line-height: 1.5; max-height: 4.6em; overflow: hidden; }
.book-card .preview.expanded { max-height: none; }

.admin-section { border-top: 1px solid var(--line); padding-top: 18px; display: flex; flex-direction: column; gap: 12px; }
.admin-section h2 { font-size: 1.05rem; color: var(--green); }
.chat-list, .help-message-list { display: flex; flex-direction: column; gap: 10px; }
.chat-card, .help-message-card {
  background: var(--paper-raised); border: 1px solid var(--line); border-radius: 10px;
  padding: 12px 14px; display: flex; flex-direction: column; gap: 6px; box-shadow: var(--shadow);
}
.chat-id { font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace; font-size: 0.85rem !important; }
.help-message-text { margin: 0; font-size: 0.88rem; color: var(--ink); line-height: 1.5; }
.help-message-contact { margin: 0; font-size: 0.8rem; color: var(--ink-soft); }
.reply-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-top: 4px; }
.reply-row input {
  flex: 1; min-width: 160px; font: inherit; font-size: 0.85rem; padding: 7px 10px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--paper); color: var(--ink);
}
.reply-status { font-size: 0.78rem; color: var(--ink-soft); }
.book-card .actions, .chat-card .actions { display: flex; gap: 14px; flex-wrap: wrap; }
.book-card button, .chat-card button { background: none; border: none; font: inherit; font-size: 0.78rem; cursor: pointer; padding: 2px 0; text-decoration: underline; }
.toggle-btn { color: var(--green); }
.remove-btn { color: var(--danger); }
.empty-note { font-size: 0.85rem; color: var(--ink-soft); font-style: italic; }

.add-book { border-top: 1px solid var(--line); padding-top: 18px; }
.add-book h2 { font-size: 1.05rem; color: var(--green); margin-bottom: 10px; }
.add-book form { display: flex; flex-direction: column; gap: 8px; }
.add-book label { font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--ink-soft); }
.add-book input, .add-book textarea {
  font: inherit; font-size: 0.9rem; padding: 9px 11px; border-radius: 8px; border: 1px solid var(--line);
  background: var(--paper-raised); color: var(--ink); resize: vertical;
}
.add-book input:focus, .add-book textarea:focus { outline: 2px solid var(--green-bright); outline-offset: 1px; }
.add-book textarea { min-height: 130px; }
.add-book button[type="submit"] {
  align-self: flex-start; background: var(--green); color: var(--paper-raised); border: none; border-radius: 8px;
  padding: 9px 16px; font: inherit; font-weight: 600; cursor: pointer;
}
.add-book button[type="submit"]:hover { background: var(--green-bright); }
.add-book .status { font-size: 0.8rem; min-height: 1.2em; color: var(--green); }
.add-book .status.error { color: var(--danger); }
.index-stat { font-size: 0.78rem; color: var(--ink-soft); }

.login-card {
  max-width: 360px; margin: 15vh auto 0; padding: 28px 24px; background: var(--paper-raised);
  border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 14px;
}
.login-card h2 { color: var(--green); }
.login-card input {
  font: inherit; font-size: 0.95rem; padding: 10px 12px; border-radius: 8px; border: 1px solid var(--line);
  background: var(--paper); color: var(--ink);
}
.login-card input:focus { outline: 2px solid var(--green-bright); outline-offset: 1px; }
.login-card button {
  background: var(--green); color: var(--paper-raised); border: none; border-radius: 8px; padding: 10px 16px;
  font: inherit; font-weight: 600; cursor: pointer;
}
.login-card button:hover { background: var(--green-bright); }
.login-card .error { color: var(--danger); font-size: 0.85rem; min-height: 1.2em; }

/* Mobile phones — tightened spacing, wider message/chip columns, and
   16px inputs (below 16px, iOS Safari auto-zooms the whole page on focus,
   which is jarring in a chat UI where the composer gets focused a lot). */
@media (max-width: 480px) {
  header { padding: 10px 12px; gap: 8px 10px; flex-wrap: wrap; }
  .brand { flex: 1 1 auto; }
  .brand h1 { font-size: 1.05rem; }
  .brand p { font-size: 0.68rem; }
  .header-status { flex-basis: 100%; order: 3; margin-left: 0; }
  #helpToggleBtn { margin-left: auto; }
  .header-link { padding: 6px 10px; font-size: 0.75rem; white-space: nowrap; }

  .banner { margin: 10px 12px 0; }
  .messages { padding: 14px 12px; gap: 12px; }
  .msg, .pooja-options { max-width: 92%; }
  .bubble { padding: 10px 12px; font-size: 0.92rem; }

  .examples { padding: 0 12px 12px; }
  .chip { font-size: 0.8rem; padding: 6px 11px; }

  form.composer { padding: 10px 12px 14px; gap: 8px; }
  form.composer textarea { font-size: 16px; padding: 10px 12px; }
  form.composer button { padding: 0 14px; font-size: 0.92rem; }

  main.admin { padding: 16px 12px 40px; gap: 16px; }
  .book-card { padding: 10px 12px; }
  .add-book textarea { min-height: 100px; }
  .add-book input, .add-book textarea { font-size: 16px; }

  .login-card { max-width: 100%; margin: 8vh auto 0; padding: 22px 18px; }
  .login-card input { font-size: 16px; }

  .auth-card { max-width: 100%; margin: 8vh auto 0; padding: 22px 18px; }
  .auth-form input { font-size: 16px; }
}
