:root {
  --bg: #f5f6f8;
  --fg: #1d2330;
  --muted: #6b7280;
  --accent: #1a56db;
  --user: #e1ebff;
  --bot: #ffffff;
  --border: #dde1e7;
  --err: #b42318;
  --hover: #eef1f6;
  --sidebar-w: 260px;
  --tap: 44px;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--fg);
  font-size: 15px;
  -webkit-text-size-adjust: 100%;
}
a { color: var(--accent); }
/* 16px form fields stop iOS from zooming in on focus. */
input, select, textarea { font: inherit; font-size: 16px; }
[hidden] { display: none !important; }

/* Login / 403 */
body.center { display: grid; place-items: center; padding: 16px; min-height: 100dvh; }
.card {
  background: #fff; border: 1px solid var(--border); border-radius: 12px;
  padding: 32px; max-width: 420px; width: 100%; text-align: center;
}
.card h1 { margin-top: 0; font-size: 1.5rem; }
.btn {
  display: inline-block; background: var(--accent); color: #fff; text-decoration: none;
  padding: 12px 18px; border-radius: 8px; font-weight: 600; margin-top: 8px;
}

/* Top bar (chat + admin) */
.topbar {
  display: flex; align-items: center; gap: 12px; padding: 8px 16px; min-height: 52px;
  padding-left: max(16px, env(safe-area-inset-left)); padding-right: max(16px, env(safe-area-inset-right));
  background: #fff; border-bottom: 1px solid var(--border);
}
.topbar .brand { font-weight: 700; font-size: 1.05rem; color: var(--fg); text-decoration: none; white-space: nowrap; }
/* "ChatWith" + Crane logo, which stands in for the word "Crane". */
.brand { display: inline-flex; align-items: center; gap: 6px; }
.brand-logo { height: 22px; width: auto; display: block; }
.brand-lg { justify-content: center; font-size: 1.6rem; gap: 10px; }
.brand-lg .brand-logo { height: 34px; }
.topbar .spacer { flex: 1; }
.icon-btn {
  min-width: var(--tap); min-height: var(--tap); border: 0; background: none; border-radius: 8px;
  font-size: 1.25rem; cursor: pointer; color: var(--fg);
}
.icon-btn:hover { background: var(--hover); }
.usermenu { position: relative; }
.usermenu summary {
  list-style: none; cursor: pointer; color: var(--muted); padding: 8px 10px; border-radius: 8px;
  min-height: var(--tap); display: flex; align-items: center;
}
.usermenu summary::-webkit-details-marker { display: none; }
.usermenu summary::after { content: "▾"; margin-left: 6px; }
.usermenu summary:hover { background: var(--hover); }
.user-name { max-width: 28vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.usermenu .menu {
  position: absolute; right: 0; top: calc(100% + 4px); z-index: 30; min-width: 220px;
  background: #fff; border: 1px solid var(--border); border-radius: 10px; padding: 6px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .12); display: flex; flex-direction: column;
}
.usermenu .menu a, .menu-email { padding: 10px 12px; border-radius: 6px; text-decoration: none; }
.usermenu .menu a:hover { background: var(--hover); }
.menu-email { font-size: .85em; border-bottom: 1px solid var(--border); border-radius: 0; margin-bottom: 4px; word-break: break-all; }
.only-mobile { display: none; }

/* Chat layout */
body.chat { display: flex; flex-direction: column; height: 100vh; height: 100dvh; overflow: hidden; }
.layout { flex: 1; display: flex; min-height: 0; }
.sidebar {
  width: var(--sidebar-w); flex: none; background: #fff; border-right: 1px solid var(--border);
  display: flex; flex-direction: column; min-height: 0;
}
.new-conv {
  margin: 12px; min-height: var(--tap); border: 1px solid var(--border); background: #fff; border-radius: 8px;
  font: inherit; font-weight: 600; color: var(--accent); cursor: pointer;
}
.new-conv:hover { background: var(--hover); }
.sidebar nav { flex: 1; overflow-y: auto; padding: 0 8px 12px; -webkit-overflow-scrolling: touch; }
.conv-list { list-style: none; margin: 0; padding: 0; }
.conv-list li { display: flex; align-items: stretch; border-radius: 8px; }
.conv-list li:hover { background: var(--hover); }
.conv-list li.active { background: var(--user); }
.conv-open {
  flex: 1; min-width: 0; text-align: left; border: 0; background: none; font: inherit; cursor: pointer;
  padding: 8px 10px; min-height: var(--tap); display: flex; flex-direction: column; justify-content: center; color: var(--fg);
}
.conv-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-date { font-size: .78em; }
.conv-del {
  border: 0; background: none; cursor: pointer; min-width: 40px; opacity: 0; border-radius: 8px; font-size: .95rem;
}
.conv-list li:hover .conv-del, .conv-list li.active .conv-del, .conv-del:focus-visible { opacity: .7; }
.conv-del:hover { opacity: 1; background: #fde8e8; }
.conv-list li.pending .conv-date { color: var(--accent); }
.conv-list li.pending .conv-date::before {
  content: ""; display: inline-block; width: .7em; height: .7em; margin-right: 5px; vertical-align: -1px;
  border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.conv-del:disabled { visibility: hidden; }
.conv-empty { padding: 8px 10px; }
.backdrop { position: fixed; inset: 0; background: rgba(15, 23, 42, .4); z-index: 19; }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; }
#log {
  flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 12px;
  -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
}
.msg { max-width: min(900px, 100%); padding: 10px 14px; border-radius: 12px; line-height: 1.45; overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; background: var(--user); white-space: pre-wrap; max-width: min(720px, 88%); }
.msg.bot { align-self: flex-start; background: var(--bot); border: 1px solid var(--border); width: min(900px, 100%); min-width: 0; }
.msg.bot.highlight { outline: 2px solid var(--accent); }
.final { white-space: pre-wrap; margin: 6px 0; }
.status { color: var(--muted); font-style: italic; font-size: 0.9em; }
.thinking::after { content: ""; display: inline-block; width: 1.2em; text-align: left; animation: dots 1.2s steps(4) infinite; }
@keyframes dots { 0% { content: ""; } 25% { content: "."; } 50% { content: ".."; } 75% { content: "..."; } }
.info { color: var(--muted); font-size: .9em; background: #f3f4f6; border-radius: 6px; padding: 6px 10px; margin: 6px 0; }

details.meta { margin: 6px 0; font-size: 0.9em; }
details.meta summary { cursor: pointer; color: var(--muted); padding: 4px 0; }
.mono {
  font-family: ui-monospace, Consolas, monospace; font-size: 0.85em; white-space: pre-wrap; overflow-wrap: anywhere;
  background: #f3f4f6; padding: 8px; border-radius: 6px; margin: 4px 0;
}

.table-wrap { margin: 8px 0; min-width: 0; }
.table-scroll { overflow: auto; max-height: 420px; max-width: 100%; -webkit-overflow-scrolling: touch; border-radius: 6px; }
.table-scroll table { border-collapse: collapse; font-size: 0.9em; min-width: 100%; }
.table-scroll th, .table-scroll td { border: 1px solid var(--border); padding: 4px 8px; text-align: left; white-space: nowrap; }
.table-scroll th { background: #f3f4f6; position: sticky; top: 0; }
.table-bar { display: flex; flex-wrap: wrap; gap: 6px; justify-content: space-between; align-items: center; margin-top: 4px; font-size: 0.85em; }
.muted { color: var(--muted); }
button.small, button.chip, a.button-link {
  border: 1px solid var(--border); background: #fff; border-radius: 6px; padding: 6px 12px; cursor: pointer;
  font: inherit; color: var(--fg); text-decoration: none; display: inline-block;
}
button.small.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.chip { border-radius: 999px; margin: 4px 6px 0 0; color: var(--accent); text-align: left; }
button.chip:disabled { cursor: default; }
.suggestions { display: flex; flex-wrap: wrap; }
.chart { width: 100%; margin: 8px 0; min-width: 0; }
.err { color: var(--err); margin: 6px 0; }

/* Feedback under each answer */
.fb { margin-top: 8px; border-top: 1px solid var(--border); padding-top: 6px; }
.fb-row { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; font-size: .9em; }
.fb-row > .muted:first-child { margin-right: 4px; }
.fb-btn {
  min-width: var(--tap); min-height: 36px; border: 1px solid transparent; background: none; border-radius: 8px;
  cursor: pointer; font-size: 1.05rem; filter: grayscale(1); opacity: .7;
}
.fb-btn:hover { background: var(--hover); opacity: 1; }
.fb-btn[aria-pressed="true"] { filter: none; opacity: 1; border-color: var(--border); background: var(--hover); }
.fb-state { margin-left: 4px; }
.fb-form { display: flex; flex-direction: column; gap: 8px; margin-top: 8px; max-width: 520px; }
.fb-form select, .fb-form textarea, .review select, .review textarea {
  width: 100%; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: #fff;
}
.fb-form textarea, .review textarea { resize: vertical; }
.fb-actions { display: flex; gap: 8px; justify-content: flex-end; align-items: center; flex-wrap: wrap; }

form#ask {
  display: flex; gap: 8px; padding: 10px 16px; background: #fff; border-top: 1px solid var(--border);
  padding-bottom: max(10px, env(safe-area-inset-bottom));
}
form#ask textarea {
  flex: 1; min-width: 0; resize: none; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; max-height: 40vh;
}
form#ask button {
  background: var(--accent); color: #fff; border: 0; border-radius: 8px; padding: 0 18px; min-height: var(--tap);
  font-weight: 600; cursor: pointer;
}
form#ask button:disabled { opacity: 0.5; cursor: default; }

/* Admin */
body.admin { min-height: 100dvh; }
.admin-main { padding: 16px; max-width: 1400px; margin: 0 auto; }
.admin-main h1 { font-size: 1.3rem; margin: 4px 0 8px; }
.hint { margin-top: 0; }
.filters { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; margin: 12px 0; }
.filters label { display: flex; flex-direction: column; gap: 4px; font-size: .85em; color: var(--muted); }
.filters select { padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: #fff; min-height: var(--tap); }
.filters .button-link { min-height: var(--tap); display: inline-flex; align-items: center; }
.fb-table { width: 100%; border-collapse: collapse; background: #fff; border: 1px solid var(--border); border-radius: 8px; }
.fb-table th, .fb-table td { border-bottom: 1px solid var(--border); padding: 8px; text-align: left; vertical-align: top; font-size: .9em; }
.fb-table th { background: #f3f4f6; white-space: nowrap; }
.fb-table td.q { min-width: 180px; }
.fb-table td { overflow-wrap: anywhere; }
.fb-table tr.down td:first-child { box-shadow: inset 3px 0 var(--err); }
.review { display: flex; flex-direction: column; gap: 6px; min-width: 220px; }

.conv-dialog {
  width: min(900px, 96vw); max-height: 90vh; padding: 0; border: 1px solid var(--border); border-radius: 12px;
  background: var(--bg); color: var(--fg);
}
.conv-dialog::backdrop { background: rgba(15, 23, 42, .45); }
.dialog-head {
  display: flex; align-items: center; gap: 8px; justify-content: space-between; padding: 8px 8px 8px 16px;
  background: #fff; border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 1;
}
.dialog-body { padding: 16px; display: flex; flex-direction: column; gap: 12px; }

/* Phones and small tablets */
@media (max-width: 899px) {
  .only-mobile { display: inline-flex; align-items: center; justify-content: center; }
  .only-desktop { display: none; }
  .topbar { gap: 4px; padding-left: max(8px, env(safe-area-inset-left)); padding-right: max(8px, env(safe-area-inset-right)); }
  .sidebar {
    position: fixed; top: 0; bottom: 0; left: 0; z-index: 20; width: min(85vw, 320px);
    transform: translateX(-100%); transition: transform .2s ease, visibility .2s;
    padding-top: env(safe-area-inset-top); padding-bottom: env(safe-area-inset-bottom);
    visibility: hidden; /* also keeps the closed drawer out of the tab order */
  }
  .sidebar.open { transform: none; visibility: visible; box-shadow: 0 0 24px rgba(0, 0, 0, .2); }
  .conv-del { opacity: .6; min-width: var(--tap); }
  #log { padding: 12px; }
  .msg { padding: 10px 12px; }
  .msg.user { max-width: 92%; }
  form#ask { padding-left: 8px; padding-right: 8px; }
  form#ask button { padding: 0 14px; }
  .fb-form { max-width: none; }

  .admin-main { padding: 12px; }
  .filters { flex-wrap: nowrap; overflow-x: auto; padding-bottom: 4px; }
  .filters label, .filters .button-link { flex: none; }
  /* Feedback table becomes one card per row. */
  .fb-table, .fb-table tbody, .fb-table tr, .fb-table td { display: block; width: 100%; }
  .fb-table { border: 0; background: none; }
  .fb-table thead { display: none; }
  .fb-table tr { background: #fff; border: 1px solid var(--border); border-radius: 10px; margin-bottom: 12px; padding: 4px 0; }
  .fb-table tr.down { border-left: 3px solid var(--err); }
  .fb-table tr.down td:first-child { box-shadow: none; }
  .fb-table td { border: 0; padding: 6px 12px; }
  .fb-table td::before { content: attr(data-label); display: block; font-size: .78em; color: var(--muted); margin-bottom: 2px; }
  .review { min-width: 0; }
  .conv-dialog { width: 100vw; max-width: 100vw; height: 100dvh; max-height: 100dvh; margin: 0; border-radius: 0; border: 0; }
  .dialog-body { padding: 12px; }
}
@media (prefers-reduced-motion: reduce) {
  .sidebar { transition: none; }
  .conv-list li.pending .conv-date::before { animation: none; }
}
