:root {
  --navy: #1d4873;
  --navy-dark: #12324f;
  --cyan: #05bdfd;
  --bg: #f4f7fa;
  --surface: #ffffff;
  --text: #17242f;
  --text-muted: #5b6b78;
  --border: #dfe6ec;
  --up: #1a9c5b;
  --down: #d13b3b;
  --radius: 14px;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", Roboto, -apple-system, BlinkMacSystemFont, sans-serif;
}

body {
  height: 100dvh;
  overflow: hidden;
  position: relative;
}

/* ---------- App shell: sidebar + main panel, ChatGPT-style layout ---------- */

.app {
  display: flex;
  height: 100%;
}

.sidebar {
  width: 268px;
  flex-shrink: 0;
  background: linear-gradient(180deg, var(--navy), var(--navy-dark));
  color: #dceaf5;
  display: flex;
  flex-direction: column;
  z-index: 30;
  transition: transform 0.2s ease;
}

.sidebar__brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 16px 14px 10px;
  flex-shrink: 0;
}

.sidebar__logo {
  height: 30px;
  background: #fff;
  border-radius: 6px;
  padding: 4px 7px;
  flex-shrink: 0;
}

.sidebar__brand-text {
  font-weight: 700;
  font-size: 13.5px;
  line-height: 1.25;
  color: #fff;
}

.sidebar__newchat {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 10px 14px;
  padding: 9px 12px;
  border-radius: 10px;
  border: 1px solid rgba(255,255,255,0.25);
  background: rgba(255,255,255,0.06);
  color: #fff;
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease;
}
.sidebar__newchat:hover { background: rgba(255,255,255,0.16); }
.sidebar__newchat:active { transform: scale(0.98); }

.sidebar__label {
  padding: 10px 16px 4px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: #8fb3cc;
  flex-shrink: 0;
}

.sidebar__history {
  flex: 1;
  overflow-y: auto;
  padding: 0 8px;
}

.sidebar__empty {
  padding: 16px 10px;
  font-size: 13px;
  color: #8fb3cc;
  text-align: center;
}

.sidebar__item {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 6px;
  padding: 9px 10px;
  border-radius: 8px;
  cursor: pointer;
  color: #dceaf5;
}
.sidebar__item:hover { background: rgba(255,255,255,0.1); }
.sidebar__item--active { background: rgba(255,255,255,0.16); }

.sidebar__item-text { min-width: 0; }

.sidebar__item-title {
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar__item-date {
  font-size: 11px;
  color: #8fb3cc;
  margin-top: 2px;
}

.sidebar__item-del {
  flex-shrink: 0;
  border: none;
  background: none;
  color: #8fb3cc;
  cursor: pointer;
  font-size: 15px;
  line-height: 1;
  padding: 2px 5px;
  border-radius: 4px;
}
.sidebar__item-del:hover { background: rgba(0,0,0,0.25); color: #ff9b9b; }

.sidebar__footer {
  flex-shrink: 0;
  padding: 8px;
  border-top: 1px solid rgba(255,255,255,0.12);
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.sidebar__footlink {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  border-radius: 8px;
  border: none;
  background: none;
  color: #dceaf5;
  font-size: 13px;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
  text-align: left;
  width: 100%;
}
.sidebar__footlink:hover { background: rgba(255,255,255,0.1); }
.sidebar__footlink[hidden] { display: none; }

.sidebar__backdrop {
  display: none;
}

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

.mobiletop {
  display: none;
  align-items: center;
  gap: 10px;
  background: linear-gradient(135deg, var(--navy), var(--navy-dark));
  color: #fff;
  padding: 10px 12px;
  flex-shrink: 0;
}

.mobiletop__title {
  flex: 1;
  font-weight: 700;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.iconbtn {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: none;
  background: rgba(255,255,255,0.1);
  color: #dceaf5;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.15s ease;
  flex-shrink: 0;
}
.iconbtn:hover { background: rgba(255,255,255,0.2); }
.iconbtn:active { transform: scale(0.94); }

.topbar__status {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: #bcd6ea;
  flex-shrink: 0;
  margin-left: 4px;
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #9aa8b3;
  box-shadow: 0 0 0 3px rgba(255,255,255,0.08);
}
.dot--live { background: var(--cyan); }
.dot--error { background: var(--down); }

.ticker {
  position: relative;
  overflow: hidden;
  padding: 10px 0;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.ticker__track {
  display: flex;
  gap: 10px;
  width: max-content;
  padding: 0 12px;
  animation: ticker-scroll var(--ticker-duration, 60s) linear infinite;
}

.ticker:hover .ticker__track {
  animation-play-state: paused;
}

@keyframes ticker-scroll {
  from { transform: translateX(0); }
  to { transform: translateX(-50%); }
}

.ticker__item {
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 8px 14px;
  border-radius: 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  white-space: nowrap;
  font-size: 13px;
  flex-shrink: 0;
}

.ticker__item--skeleton { color: var(--text-muted); }

.ticker__sym { font-weight: 700; color: var(--navy); }
.ticker__price { font-weight: 600; }
.ticker__change--up { color: var(--up); }
.ticker__change--down { color: var(--down); }

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

.chat__scroll {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

/* Each message row is centered within a ChatGPT-width column, not the full viewport. */
.msg {
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: 100%;
  max-width: 780px;
  margin: 0 auto;
}
.msg--user { align-items: flex-end; }
.msg--bot { align-items: stretch; }

.msg__bubble {
  line-height: 1.6;
  font-size: 14.5px;
  white-space: pre-wrap;
}

.msg--bot .msg__bubble {
  padding: 2px 0;
  color: var(--text);
}

.msg--user .msg__bubble {
  max-width: 78%;
  padding: 10px 16px;
  border-radius: 18px;
  background: var(--navy);
  color: #fff;
  border-top-right-radius: 4px;
}

.msg__table {
  border-collapse: collapse;
  margin: 8px 0;
  font-size: 13px;
  max-width: 100%;
  display: block;
  overflow-x: auto;
}

.msg__table th, .msg__table td {
  border: 1px solid var(--border);
  padding: 5px 10px;
  text-align: left;
  white-space: nowrap;
}

.msg__table thead {
  background: var(--bg);
}

.msg__table th {
  color: var(--navy);
  font-weight: 700;
}

.msg__sources {
  margin-top: 8px;
  font-size: 11.5px;
  color: var(--text-muted);
  border-top: 1px dashed var(--border);
  padding-top: 6px;
}

.msg__sources b { color: var(--navy); }

.msg__file {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 12px;
  border-radius: 999px;
  background: var(--navy);
  color: #fff;
  font-size: 13px;
  font-weight: 600;
  text-decoration: none;
  width: fit-content;
  transition: background 0.15s ease;
}

.msg__file:hover { background: var(--navy-dark); }
.msg__file svg { flex-shrink: 0; }

.typing-dot {
  display: inline-block;
  width: 6px;
  height: 6px;
  margin-right: 3px;
  border-radius: 50%;
  background: var(--navy);
  animation: bounce 1.2s infinite;
}
.typing-dot:nth-child(2) { animation-delay: 0.15s; }
.typing-dot:nth-child(3) { animation-delay: 0.3s; }

@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
  30% { transform: translateY(-4px); opacity: 1; }
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  width: 100%;
  max-width: 780px;
  margin: 0 auto;
  padding: 10px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 26px;
  box-shadow: 0 4px 18px rgba(18,50,79,0.08);
  flex-shrink: 0;
  margin-bottom: max(16px, env(safe-area-inset-bottom));
}

.composer textarea {
  flex: 1;
  resize: none;
  border: none;
  background: none;
  padding: 8px 4px;
  font-size: 16px; /* same iOS auto-zoom-on-focus threshold as the auth form inputs */
  font-family: inherit;
  max-height: 120px;
  outline: none;
  color: var(--text);
}

.composer textarea:focus {
  border-color: var(--cyan);
}

.composer button {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: none;
  background: var(--cyan);
  color: #08324a;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: transform 0.1s ease;
}

.composer button:active { transform: scale(0.92); }
.composer button:disabled { opacity: 0.5; cursor: default; }

/* Sidebar becomes an off-canvas drawer below this width; the mobile top bar with the hamburger
   toggle takes over as the way to reach it. */
@media (max-width: 860px) {
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    transform: translateX(-100%);
  }
  .sidebar--open { transform: translateX(0); }
  .sidebar__backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(10,20,30,0.45);
    z-index: 25;
  }
  .sidebar__backdrop[hidden] { display: none; }
  .mobiletop { display: flex; }
}

@media (max-width: 420px) {
  .mobiletop { padding: 8px 10px; }
  #connText { display: none; }
  .topbar__status { gap: 0; }
  .msg--user .msg__bubble { max-width: 88%; }
  .topbar { padding: 10px 12px; }
  .topbar__logo { height: 26px; padding: 3px 6px; }
  .topbar__title-main { font-size: 13.5px; }
  .adminbtn { padding: 6px 10px; font-size: 12px; }
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1b26;
    --surface: #16232f;
    --text: #e7edf2;
    --text-muted: #93a4b1;
    --border: #26343f;
  }
  .sidebar__logo { background: #fff; }
}

/* ---------- Auth pages (login / signup) ---------- */

.authbody {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
  padding: 24px;
  /* Overrides the base `body { overflow: hidden }` rule (meant for the fixed-height chat shell) —
     these pages must stay scrollable, or the mobile keyboard opening (which can push the card taller
     than the now-shrunk visible viewport) leaves the Sign in button permanently out of reach with no
     way to scroll down to it. This was the actual cause behind "tapping Sign in does nothing" on
     mobile: confirmed via server logs — real iPhone visits loaded the page fine but never once sent
     the login request, meaning the tap never landed on the button at all. */
  height: auto;
  overflow-y: auto;
}

.authcard {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 12px 32px rgba(0,0,0,0.12);
  padding: 32px 28px;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
}

.authcard--success { display: flex; }
.authcard[hidden] { display: none; }

.authcard__logo {
  height: 42px;
  background: #fff;
  border-radius: 6px;
  padding: 6px 10px;
  margin-bottom: 16px;
}

.authcard__title {
  font-size: 17px;
  margin: 0 0 4px;
  color: var(--navy);
}

.authcard__subtitle {
  font-size: 13px;
  color: var(--text-muted);
  margin: 0 0 20px;
}

.authform {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 14px;
  text-align: left;
}

.authform__label {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.authform__optional {
  font-weight: 400;
  color: var(--text-muted);
}

.authform__label input {
  font: inherit;
  /* Must be >=16px — iOS Safari auto-zooms the whole page on focus for any input below that, which
     visually shifts the card and was very likely why the Sign in button became untappable after
     typing a password: the zoom moves the button out from under where the next tap lands. */
  font-size: 16px;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}
.authform__label input:focus {
  outline: none;
  border-color: var(--cyan);
}

.authform__error {
  font-size: 12.5px;
  color: var(--down);
  background: rgba(209,59,59,0.1);
  border-radius: 8px;
  padding: 8px 10px;
}

.authform__submit {
  margin-top: 4px;
  padding: 11px;
  border-radius: 10px;
  border: none;
  background: linear-gradient(135deg, var(--navy), var(--navy-dark));
  color: #fff;
  font-weight: 700;
  font-size: 14px;
  cursor: pointer;
  text-align: center;
  text-decoration: none;
  display: block;
}
.authform__submit:disabled { opacity: 0.6; cursor: default; }
.authform__submit--link { margin-top: 8px; }

.authcard__foot {
  margin-top: 18px;
  font-size: 13px;
  color: var(--text-muted);
}
.authcard__foot a { color: var(--navy); font-weight: 600; text-decoration: none; }

.installbtn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  margin-top: 14px;
  padding: 10px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--navy);
  font-size: 13.5px;
  font-weight: 700;
  cursor: pointer;
}
.installbtn:hover { background: var(--border); }
.installbtn[hidden] { display: none; }
.authcard__foot a:hover { text-decoration: underline; }

/* ---------- Admin page ---------- */

.adminbody {
  /* Overrides the base `body { overflow: hidden }` rule (meant for the fixed-height chat shell) —
     the admin page's content height depends on the user list, so it must be able to scroll. */
  height: auto;
  min-height: 100dvh;
  overflow-y: auto;
  background: var(--bg);
}

/* The admin page kept the pre-redesign topbar markup (brand + title + actions) rather than the
   chat app's sidebar shell — a dedicated nav sidebar doesn't make sense for a single-table admin
   view. These rules only exist here now; index.html's header moved to .sidebar/.mobiletop. */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  background: linear-gradient(135deg, var(--navy), var(--navy-dark));
  color: #fff;
  padding: 12px 20px;
}

.topbar__brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.topbar__logo {
  height: 34px;
  background: #fff;
  border-radius: 6px;
  padding: 4px 8px;
  flex-shrink: 0;
  display: block;
}

.topbar__title-main {
  font-weight: 700;
  font-size: 15px;
  white-space: nowrap;
}

.topbar__actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.adminbtn {
  padding: 7px 14px;
  border-radius: 8px;
  border: 1px solid rgba(255,255,255,0.3);
  background: rgba(255,255,255,0.1);
  color: #fff;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
}
.adminbtn:hover { background: rgba(255,255,255,0.2); }
.adminbtn--ghost { background: transparent; }

.adminmain {
  max-width: 1320px;
  margin: 0 auto;
  padding: 24px 16px 40px;
}

.statgrid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
  margin-bottom: 24px;
}

.statcard {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
}
.statcard__value { font-size: 24px; font-weight: 800; color: var(--navy); line-height: 1.2; }
.statcard__value--active { color: var(--up); }
.statcard__value--inactive { color: var(--down); }
.statcard__label { font-size: 12px; color: var(--text-muted); margin-top: 2px; }

.adminmain__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 16px;
}
.adminmain__head h2 { margin: 0; color: var(--navy); font-size: 18px; }

.adminmain__head-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.adminbtn--export {
  background: var(--up);
  border-color: var(--up);
  color: #fff;
}
.adminbtn--export:hover { background: #157a48; }

.searchbox {
  font: inherit;
  font-size: 13.5px;
  padding: 8px 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  min-width: 240px;
}
.searchbox:focus { outline: none; border-color: var(--cyan); }

.filterselect {
  font: inherit;
  font-size: 13.5px;
  padding: 8px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}
.filterselect:focus { outline: none; border-color: var(--cyan); }

.admintable-wrap {
  overflow-x: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.admintable__empty {
  padding: 30px 16px;
  text-align: center;
  color: var(--text-muted);
  font-size: 13.5px;
}
.admintable__empty[hidden] { display: none; }

.admintable {
  width: 100%;
  border-collapse: collapse;
  font-size: 13.5px;
  white-space: nowrap;
}
.admintable th, .admintable td {
  padding: 11px 14px;
  text-align: left;
  border-bottom: 1px solid var(--border);
}
.admintable th { color: var(--text-muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: 0.02em; }
.admintable tbody tr:last-child td { border-bottom: none; }
.admintable tbody tr:hover { background: var(--bg); }

.rolepill, .statuspill, .youpill {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 700;
}
.rolepill--admin { background: rgba(5,189,253,0.15); color: #0a7fa8; }
.rolepill--user { background: var(--bg); color: var(--text-muted); }
.statuspill--active { background: rgba(26,156,91,0.15); color: var(--up); }
.statuspill--inactive { background: rgba(209,59,59,0.12); color: var(--down); }
.youpill { background: var(--bg); color: var(--text-muted); font-weight: 600; }

.adminbtn--sm {
  padding: 6px 12px;
  font-size: 12.5px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--navy);
}
.adminbtn--sm:hover { background: var(--border); }
.adminbtn--sm:disabled { opacity: 0.5; cursor: default; }
.adminbtn--sm.adminbtn--danger { color: var(--down); }

.roleselect {
  font: inherit;
  font-size: 12.5px;
  font-weight: 700;
  padding: 4px 8px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--navy);
  cursor: pointer;
}
.roleselect:disabled { opacity: 0.6; cursor: default; }

.resetpill {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  background: rgba(234,179,8,0.15);
  color: #a16207;
  margin-left: 6px;
}

.admintable td .actioncell {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: nowrap;
}

/* Below this width the table rotates into stacked cards — each <td>'s data-label attribute becomes
   its visible row label, since a horizontally-scrolling admin table is awkward on a phone. */
@media (max-width: 720px) {
  .statgrid { grid-template-columns: repeat(2, 1fr); }

  .admintable thead { display: none; }
  .admintable, .admintable tbody, .admintable tr, .admintable td { display: block; width: 100%; }
  .admintable tr {
    padding: 12px 14px;
    border-bottom: 1px solid var(--border);
  }
  .admintable td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    padding: 6px 0;
    border-bottom: none;
    white-space: normal;
    text-align: right;
  }
  .admintable td[data-label]::before {
    content: attr(data-label);
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.02em;
    color: var(--text-muted);
    text-align: left;
  }
  .admintable td:last-child { justify-content: flex-end; }
  .admintable td:last-child .actioncell { justify-content: flex-end; }
}

/* ---------- Password-reset modal ---------- */

.modal__backdrop {
  position: fixed;
  inset: 0;
  background: rgba(10,20,30,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 40;
  padding: 20px;
}
.modal__backdrop[hidden] { display: none; }

.modal {
  width: 100%;
  max-width: 360px;
  background: var(--surface);
  border-radius: var(--radius);
  padding: 24px;
  box-shadow: 0 20px 50px rgba(0,0,0,0.3);
}
.modal__title { margin: 0 0 8px; color: var(--navy); font-size: 16px; }
.modal__text { margin: 0 0 16px; font-size: 13.5px; color: var(--text-muted); line-height: 1.5; }

.modal__pwrow {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
}
.modal__pw {
  flex: 1;
  font-family: "SF Mono", Consolas, Monaco, monospace;
  font-size: 15px;
  font-weight: 700;
  color: var(--navy);
  letter-spacing: 0.02em;
  word-break: break-all;
}
