/* =============================================================================
   FEEDBACK DASHBOARD — v11. Conversation detail lives in a native <dialog>
   element. No layout gymnastics — <dialog> handles modal positioning + backdrop,
   and the body has a plain max-height / overflow-y: auto that cannot fail.
   ========================================================================== */

:root {
  --bg:       #f3f6f8;
  --panel:    #ffffff;
  --panel-2:  #f1f4f7;
  --panel-3:  #e9eef2;
  --line:     #d9e1e7;
  --line-2:   #e6ecef;
  --ink-900:  #0d2c38;
  --ink-700:  #314c58;
  --ink-500:  #5d7380;
  --ink-300:  #a9b7bf;
  --ink-200:  #cdd6dc;
  --teal-50:  #eef9fa;
  --teal-100: #def2f3;
  --teal-300: #7fcfd4;
  --teal-500: #3faab1;
  --teal-600: #2a8a94;
  --teal-700: #1f6c74;
  --teal-900: #0f4349;
  --good:     #1f7a44;
  --good-bg:  #dff1e5;
  --good-bd:  #bfe0cb;
  --bad:      #b5261b;
  --bad-bg:   #fdecea;
  --bad-bd:   #f5c6c2;
  --warn:     #8a5a10;
  --warn-bg:  #fcf1dc;
  --warn-bd:  #f0dbac;
  --shadow-sm: 0 1px 2px rgba(13,44,56,.04), 0 1px 3px rgba(13,44,56,.06);
  --shadow-md: 0 12px 40px rgba(13,44,56,.18);
  --shadow-lg: 0 24px 60px rgba(13,44,56,.28);
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  font-family: Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 13.5px;
  line-height: 1.45;
  color: var(--ink-900);
  -webkit-font-smoothing: antialiased;
  background:
    radial-gradient(1200px 400px at 20% -20%, var(--teal-50), transparent 60%),
    radial-gradient(900px 300px at 100% 0%,  #eaf3f4, transparent 60%),
    var(--bg);
  min-height: 100vh;
}

/* =============================================================================
   HEADER
   ========================================================================== */
.hd {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 14px 24px;
  background: rgba(255,255,255,.92);
  backdrop-filter: saturate(1.2) blur(6px);
  -webkit-backdrop-filter: saturate(1.2) blur(6px);
  border-bottom: 1px solid var(--line);
}
.hd-left { display: flex; align-items: center; gap: 14px; min-width: 0; }
.logo {
  width: 40px; height: 40px; border-radius: 11px;
  background: linear-gradient(135deg, var(--teal-600), var(--teal-900));
  color: #fff; font-weight: 800;
  display: grid; place-items: center;
  letter-spacing: .5px;
  box-shadow: var(--shadow-sm);
  flex-shrink: 0;
}
.title { font-weight: 800; font-size: 16px; letter-spacing: -.01em; }
.sub   { color: var(--ink-500); font-size: 12px; margin-top: 2px; }
.hd-right { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.stat {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 5px 12px;
  font-size: 12px;
  color: var(--ink-700);
  box-shadow: var(--shadow-sm);
  display: inline-flex;
  align-items: center;
  gap: 5px;
  white-space: nowrap;
}
.stat b { color: var(--ink-900); font-weight: 700; }

/* =============================================================================
   FILTERS
   ========================================================================== */
.filters {
  position: sticky;
  top: 66px;
  z-index: 9;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 24px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  box-shadow: var(--shadow-sm);
}
.filters label {
  display: flex; flex-direction: column; gap: 2px;
  font-size: 10.5px; color: var(--ink-500);
  font-weight: 700; text-transform: uppercase; letter-spacing: .05em;
}
.filters input, .filters select {
  font: inherit;
  color: var(--ink-900);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 7px 10px;
  min-width: 180px;
}
.filters input:focus, .filters select:focus {
  outline: none;
  border-color: var(--teal-500);
  background: var(--panel);
}
.filters .spacer { flex: 1; }

.btn {
  appearance: none;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 9px 14px;
  background: var(--panel);
  color: var(--ink-900);
  font-weight: 600;
  font-size: 12.5px;
  cursor: pointer;
  transition: background .12s ease, border-color .12s ease, transform .08s ease;
  box-shadow: var(--shadow-sm);
}
.btn:hover { background: var(--panel-2); }
.btn:active { transform: translateY(1px); }
.btn.primary {
  background: var(--teal-600);
  color: #fff;
  border-color: var(--teal-600);
}
.btn.primary:hover { background: var(--teal-700); border-color: var(--teal-700); }

/* =============================================================================
   TABLE — plain block, always full width
   ========================================================================== */
.table-pane {
  margin: 16px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow-sm);
  overflow: auto;
}

.feedback-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 12.8px;
  table-layout: fixed;
}
.feedback-table col.col-when    { width: 150px; }
.feedback-table col.col-clinic  { width: 70px; }
.feedback-table col.col-mrn     { width: 120px; }
.feedback-table col.col-type    { width: 120px; }
.feedback-table col.col-rating  { width: 80px; }
.feedback-table col.col-snippet { width: auto; }
.feedback-table col.col-comment { width: 24%; }
.feedback-table thead th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--panel-2);
  font-weight: 700;
  color: var(--ink-700);
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: .06em;
  padding: 11px 14px;
  border-bottom: 1px solid var(--line);
  text-align: left;
}
.feedback-table tbody td {
  padding: 10px 14px;
  border-bottom: 1px solid var(--line-2);
  vertical-align: middle;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.feedback-table tbody tr {
  cursor: pointer;
  transition: background .12s ease;
}
.feedback-table tbody tr:hover { background: var(--teal-50); }
.feedback-table tbody tr.active { background: var(--teal-100); }
.feedback-table tbody tr.active td:first-child {
  box-shadow: inset 3px 0 0 var(--teal-600);
}
.feedback-table td.when-cell { color: var(--ink-700); white-space: nowrap; }
.feedback-table .mrn {
  font-family: "JetBrains Mono", ui-monospace, monospace;
  font-size: 11.5px;
  color: var(--ink-700);
  background: var(--panel-2);
  padding: 2px 6px;
  border-radius: 6px;
}
.feedback-table .rating-pill {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  font-weight: 700;
}
.feedback-table .rating-pill .star { color: var(--warn); font-size: 12px; }
.feedback-table .empty { text-align: center; color: var(--ink-500); padding: 36px; }
.feedback-table .empty.error { color: var(--bad); }

/* =============================================================================
   TYPE BADGES
   ========================================================================== */
.badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--ink-700);
  text-transform: capitalize;
  letter-spacing: .02em;
}
.badge.neutral         { background: var(--panel-2); color: var(--ink-700); border-color: var(--line); }
.badge.t-helpful       { background: var(--good-bg); color: var(--good);    border-color: var(--good-bd); }
.badge.t-unhelpful     { background: var(--bad-bg);  color: var(--bad);     border-color: var(--bad-bd); }
.badge.t-hallucination { background: var(--bad-bg);  color: var(--bad);     border-color: var(--bad-bd); }
.badge.t-accuracy      { background: var(--teal-100); color: var(--teal-700); border-color: var(--teal-300); }
.badge.t-completeness  { background: #e5efec;        color: #2e6e5f;         border-color: #bed6ce; }
.badge.t-citation      { background: #ece6f7;        color: #5c43a0;         border-color: #cec2ea; }

/* =============================================================================
   NATIVE <dialog> — modal for the conversation detail
   ========================================================================== */
dialog.convo-dialog {
  /* Reset the browser default styling — not all defaults are friendly. */
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow-lg);
  background: var(--panel);
  color: var(--ink-900);
  max-width: min(720px, 92vw);
  width: 100%;
  /* Explicit height — not just max-height — so the flex split below can
     compute correctly. Without this, the dialog sizes to content and the
     overflow never engages. */
  height: 85vh;
  max-height: 85vh;
  overflow: hidden;              /* inner panes handle scroll */
  display: flex;
  flex-direction: column;
}
dialog.convo-dialog::backdrop {
  background: rgba(13, 44, 56, 0.45);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
}
dialog.convo-dialog:not([open]) { display: none; }

.convo-hd {
  flex: 0 0 auto;
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
  background: linear-gradient(180deg, var(--panel) 0%, var(--panel-2) 100%);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.convo-title { font-weight: 800; font-size: 16px; letter-spacing: -.01em; }
.convo-meta  { font-size: 12.5px; color: var(--ink-500); margin-top: 4px; }
.convo-meta code {
  font-family: "JetBrains Mono", ui-monospace, monospace;
  background: var(--panel-2);
  padding: 1px 5px;
  border-radius: 5px;
  font-size: 11.5px;
}
.icon-btn {
  appearance: none;
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 8px;
  width: 32px; height: 32px;
  cursor: pointer;
  color: var(--ink-500);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  flex-shrink: 0;
}
.icon-btn:hover { background: var(--panel-2); color: var(--ink-900); }

/* The scroll region. Dialog has explicit height; body fills remaining space.
   Use plain block layout (NOT flex) inside the body — otherwise flex-shrink
   on the cards compresses them to fit the body height instead of scrolling. */
.convo-body {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: scroll !important;
  overflow-x: hidden;
  padding: 16px 18px 24px;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}
.convo-body > * {
  margin-bottom: 14px;
}
.convo-body > *:last-child {
  margin-bottom: 0;
}
.convo-body::-webkit-scrollbar { width: 12px; }
.convo-body::-webkit-scrollbar-track { background: var(--panel-2); }
.convo-body::-webkit-scrollbar-thumb {
  background: var(--ink-200);
  border-radius: 999px;
  border: 3px solid var(--panel-2);
}
.convo-body::-webkit-scrollbar-thumb:hover { background: var(--ink-300); }

body.modal-open { overflow: hidden; }  /* lock background scroll while dialog is up */

/* =============================================================================
   CONVERSATION CARDS inside the modal
   ========================================================================== */
.msg-block {
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
  background: var(--panel);
  box-shadow: var(--shadow-sm);
  word-wrap: break-word;
  overflow-wrap: anywhere;
}
.msg-block.user {
  border-color: var(--ink-200);
  background: linear-gradient(180deg, var(--panel) 0%, #f8fbfc 100%);
}
.msg-block.assistant {
  border-color: var(--teal-300);
  background: linear-gradient(180deg, #fcffff 0%, var(--teal-50) 100%);
}
.msg-hd {
  padding: 10px 14px;
  background: var(--panel-2);
  font-size: 11.5px;
  color: var(--ink-700);
  font-weight: 700;
  border-bottom: 1px solid var(--line);
  text-transform: uppercase;
  letter-spacing: .06em;
  display: flex;
  gap: 8px;
  align-items: center;
}
.msg-block.assistant .msg-hd {
  background: linear-gradient(90deg, var(--teal-100), var(--teal-50));
  color: var(--teal-900);
  border-bottom-color: var(--teal-300);
}
.msg-hd .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--ink-300); }
.msg-block.assistant .msg-hd .dot { background: var(--teal-500); }

.msg-body {
  padding: 14px 16px;
  font-size: 14px;
  line-height: 1.6;
  color: var(--ink-900);
  word-wrap: break-word;
  overflow-wrap: anywhere;
}
.msg-body p { margin: 0 0 10px; }
.msg-body p:last-child { margin: 0; }
.msg-body code {
  background: var(--panel-2);
  padding: 1px 5px;
  border-radius: 5px;
  font-size: 12px;
  font-family: "JetBrains Mono", ui-monospace, monospace;
}
.msg-body pre {
  background: var(--panel-2);
  padding: 10px;
  border-radius: 8px;
  overflow-x: auto;
  font-size: 12px;
}
.msg-body ul, .msg-body ol { padding-left: 20px; margin: 4px 0 8px; }
.msg-body li { margin: 2px 0; }
.msg-body:empty::before {
  content: "(no paired user message)";
  color: var(--ink-300);
  font-style: italic;
}
.msg-body * { max-width: 100%; }
.msg-body img, .msg-body table { max-width: 100%; height: auto; }
.msg-body sup.cite-chip {
  font-size: 10.5px;
  background: var(--teal-100);
  color: var(--teal-700);
  border: 1px solid var(--teal-300);
  border-radius: 6px;
  padding: 1px 5px;
  margin-left: 2px;
  font-weight: 700;
  vertical-align: super;
}

.cites {
  padding: 10px 14px 12px;
  border-top: 1px dashed var(--teal-300);
  background: rgba(255,255,255,.55);
}
.cites-hd {
  font-size: 10.5px;
  color: var(--teal-700);
  text-transform: uppercase;
  letter-spacing: .06em;
  margin-bottom: 6px;
  font-weight: 700;
}
.cites ul { margin: 0; padding-left: 18px; font-size: 12.2px; color: var(--ink-700); }
.cites li { margin: 3px 0; }
.cite-i { color: var(--teal-700); font-weight: 800; margin-right: 4px; }

.feedback-list ul {
  margin: 0;
  padding: 10px 12px;
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.fb-li {
  border: 1px solid var(--line);
  border-left: 3px solid var(--ink-200);
  border-radius: 10px;
  padding: 8px 12px;
  background: var(--panel);
}
.fb-li.helpful       { border-left-color: var(--good); background: #f7fcf9; }
.fb-li.unhelpful,
.fb-li.hallucination { border-left-color: var(--bad);  background: #fef8f7; }
.fb-li.accuracy      { border-left-color: var(--teal-600); background: #f5fbfc; }
.fb-li.completeness  { border-left-color: #2e6e5f; background: #f5faf8; }
.fb-li.citation      { border-left-color: #5c43a0; background: #f8f5fc; }

.fb-top {
  display: flex; align-items: center; gap: 8px;
  font-size: 11.5px; color: var(--ink-500); flex-wrap: wrap;
}
.fb-top .rating {
  background: var(--warn-bg);
  color: var(--warn);
  padding: 2px 8px;
  border-radius: 999px;
  font-weight: 700;
  border: 1px solid var(--warn-bd);
  font-size: 11px;
}
.fb-top .when { margin-left: auto; font-size: 11px; }
.fb-text {
  margin-top: 6px;
  font-size: 12.8px;
  color: var(--ink-900);
  white-space: pre-wrap;
  line-height: 1.5;
}
.cites.cites-fb {
  margin-top: 8px;
  padding: 6px 10px 8px;
  border-top: 1px dashed var(--line);
  background: rgba(255,255,255,.6);
  border-radius: 6px;
}
.cites.cites-fb .cites-hd {
  color: var(--ink-500);
  font-weight: 600;
}
.cites.cites-fb ul { font-size: 11.8px; }

/* Clickable citation entry — only when has-source="1". */
.cites li.cite-clickable { cursor: pointer; }
.cites li.cite-clickable:hover {
  background: var(--teal-50);
  border-radius: 4px;
}
.cites li.cite-clickable .cite-label {
  border-bottom: 1px dotted var(--teal-500);
}
.cites li.cite-no-source {
  color: var(--ink-500);
  font-style: italic;
}

/* ---- Document viewer dialog (citation S3 preview) ---- */
.doc-viewer-dialog {
  border: none;
  padding: 0;
  border-radius: 14px;
  width: min(92vw, 1100px);
  max-width: 92vw;
  height: min(90vh, 820px);
  max-height: 90vh;
  background: var(--panel);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
}
/* Use grid only when the dialog is open — closed <dialog> needs display:none.
   Grid gives the body row a definite height (1fr), which is what the iframe's
   height:100% needs to resolve. Flex layout collapses percentage heights here
   and the PDF viewer ends up unscrollable. */
.doc-viewer-dialog[open] {
  display: grid;
  grid-template-rows: auto 1fr;
}
.doc-viewer-dialog::backdrop {
  background: rgba(13, 44, 56, 0.55);
}
.doc-viewer-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}
.doc-viewer-body {
  position: relative;
  min-height: 0;
  background: var(--panel-2);
}
.doc-viewer-body iframe {
  display: block;
  width: 100%;
  height: 100%;
  border: 0;
  background: var(--panel);
}
.doc-viewer-body .empty {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 24px;
  background: var(--panel-2);
  z-index: 1;
}
/* The [hidden] HTML attribute sets display:none, but our rule above overrides
   that via specificity. Re-assert it explicitly so loading/error overlays
   actually disappear and don't intercept scroll on the iframe behind them. */
.doc-viewer-body .empty[hidden] { display: none; }

/* =============================================================================
   STATES
   ========================================================================== */
.empty { padding: 24px; color: var(--ink-500); text-align: center; font-size: 12.5px; }
.empty.error { color: var(--bad); }

.skeleton-row td { padding: 12px 14px; }
.skeleton {
  display: inline-block;
  height: 10px;
  border-radius: 6px;
  background: linear-gradient(90deg, var(--panel-2) 0%, var(--panel-3) 50%, var(--panel-2) 100%);
  background-size: 200% 100%;
  animation: shine 1.2s linear infinite;
  width: 100%;
}
@keyframes shine {
  from { background-position: 0% 0%; }
  to   { background-position: -200% 0%; }
}
