:root {
  --bg0: #050810;
  --bg1: #0a1224;
  --glass: rgba(14, 22, 42, 0.72);
  --glass-border: rgba(120, 160, 255, 0.18);
  --text: #e8eefc;
  --muted: #8b9cc7;
  --accent: #5ee7ff;
  --accent2: #a78bfa;
  --open: #f1f5f9;
  --ack: #34d399;
  --inv: #94a3b8;
  --disc: #f87171;
  --shadow: 0 24px 80px rgba(0, 0, 0, 0.45);
  --radius: 16px;
  --font: "Segoe UI", system-ui, -apple-system, sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  font-family: var(--font);
  color: var(--text);
  background: radial-gradient(1200px 600px at 10% -10%, rgba(94, 231, 255, 0.12), transparent),
    radial-gradient(900px 500px at 90% 0%, rgba(167, 139, 250, 0.14), transparent),
    linear-gradient(180deg, var(--bg0), var(--bg1));
  background-attachment: fixed;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background-image: linear-gradient(rgba(255, 255, 255, 0.02) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 255, 255, 0.02) 1px, transparent 1px);
  background-size: 48px 48px;
  mask-image: radial-gradient(circle at center, black, transparent 80%);
}

.shell {
  max-width: 1440px;
  margin: 0 auto;
  padding: 20px 16px 48px;
  position: relative;
}

.topbar {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}

.brand h1 {
  margin: 0;
  font-size: clamp(1.4rem, 3vw, 2rem);
  letter-spacing: 0.04em;
  background: linear-gradient(90deg, var(--accent), var(--accent2));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.brand p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 0.9rem;
}

.glass {
  background: var(--glass);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius);
  backdrop-filter: blur(18px);
  box-shadow: var(--shadow);
}

.controls {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
  padding: 16px;
  margin-bottom: 16px;
}

.controls label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

input,
select,
button {
  font: inherit;
}

input,
select {
  width: 100%;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(0, 0, 0, 0.25);
  color: var(--text);
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
}

input:focus,
select:focus {
  border-color: rgba(94, 231, 255, 0.5);
  box-shadow: 0 0 0 3px rgba(94, 231, 255, 0.12);
}

.actions {
  display: flex;
  align-items: end;
  gap: 8px;
}

.btn {
  padding: 10px 16px;
  border-radius: 10px;
  border: 1px solid rgba(94, 231, 255, 0.35);
  background: linear-gradient(135deg, rgba(94, 231, 255, 0.18), rgba(167, 139, 250, 0.18));
  color: var(--text);
  cursor: pointer;
  transition: transform 0.15s, opacity 0.15s;
}

.btn:hover {
  transform: translateY(-1px);
}

.btn:active {
  transform: translateY(0);
}

.btn.ghost {
  background: transparent;
  border-color: rgba(255, 255, 255, 0.12);
}

.assistant-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.assistant-toggle-icon {
  font-size: 0.85rem;
  opacity: 0.7;
  transition: opacity 0.2s, transform 0.2s;
}

.assistant-toggle.active {
  border-color: rgba(94, 231, 255, 0.45);
  background: rgba(94, 231, 255, 0.1);
  box-shadow: 0 0 20px rgba(94, 231, 255, 0.12);
}

.assistant-toggle.active .assistant-toggle-icon {
  opacity: 1;
  transform: rotate(90deg);
}

.stats {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 16px;
}

.pill {
  padding: 8px 14px;
  border-radius: 999px;
  border: 1px solid var(--glass-border);
  background: rgba(255, 255, 255, 0.04);
  font-size: 0.85rem;
}

.pill strong {
  margin-right: 6px;
}

.pill.open strong {
  color: var(--open);
}
.pill.ack strong {
  color: var(--ack);
}
.pill.invoiced strong {
  color: var(--inv);
}
.pill.discrepancy strong {
  color: var(--disc);
}

.tabs {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
}

.tab {
  padding: 10px 18px;
  border-radius: 999px;
  border: 1px solid transparent;
  background: rgba(255, 255, 255, 0.04);
  color: var(--muted);
  cursor: pointer;
}

.tab.active {
  color: var(--text);
  border-color: rgba(94, 231, 255, 0.4);
  background: rgba(94, 231, 255, 0.1);
}

.meta-line {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
  color: var(--muted);
  font-size: 0.85rem;
  margin-bottom: 16px;
}

.refresh-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--ack);
  box-shadow: 0 0 12px var(--ack);
  animation: pulse 2s infinite;
}

.refresh-dot.loading {
  background: var(--accent);
  box-shadow: 0 0 12px var(--accent);
}

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

.groups {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.po-card {
  overflow: hidden;
  transition: border-color 0.2s, opacity 0.2s, box-shadow 0.2s;
}

.po-card:hover {
  border-color: rgba(94, 231, 255, 0.28);
}

.po-card.dragging {
  opacity: 0.72;
  border-color: rgba(167, 139, 250, 0.55);
  box-shadow: 0 16px 48px rgba(94, 231, 255, 0.18);
}

.po-card.drop-target {
  border-color: rgba(94, 231, 255, 0.5);
  box-shadow: inset 0 0 0 1px rgba(94, 231, 255, 0.35);
}

.po-header {
  display: grid;
  grid-template-columns: auto auto 1fr auto;
  gap: 10px;
  align-items: center;
  padding: 14px 16px;
  user-select: none;
}

.chevron-btn,
.drag-handle {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(255, 255, 255, 0.05);
  color: var(--text);
  cursor: pointer;
  transition: transform 0.2s, border-color 0.2s, background 0.2s;
  touch-action: none;
}

.chevron-btn:hover,
.drag-handle:hover {
  border-color: rgba(94, 231, 255, 0.35);
  background: rgba(94, 231, 255, 0.08);
}

.chevron {
  font-size: 1.1rem;
  line-height: 1;
  transition: transform 0.2s;
}

.po-card.expanded .chevron {
  transform: rotate(90deg);
}

.drag-handle {
  cursor: grab;
  font-size: 1rem;
  letter-spacing: -0.12em;
  color: var(--muted);
}

.drag-handle:active,
.po-card.dragging .drag-handle {
  cursor: grabbing;
  color: var(--accent);
  border-color: rgba(167, 139, 250, 0.45);
  background: rgba(167, 139, 250, 0.12);
}

.po-title {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 16px;
  align-items: center;
}

.po-title .po-num {
  font-size: 1.05rem;
  font-weight: 600;
  letter-spacing: 0.02em;
}

.po-title .sub {
  color: var(--muted);
  font-size: 0.85rem;
}

.badge {
  display: inline-flex;
  align-items: center;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  border: 1px solid transparent;
}

.badge.open {
  color: #0f172a;
  background: var(--open);
}
.badge.ack {
  color: #052e1a;
  background: var(--ack);
}
.badge.invoiced {
  color: #1e293b;
  background: var(--inv);
}
.badge.discrepancy {
  color: #450a0a;
  background: var(--disc);
}

.table-wrap {
  overflow-x: auto;
  border-top: 1px solid rgba(255, 255, 255, 0.06);
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
}

th,
td {
  padding: 10px 12px;
  text-align: left;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
  white-space: nowrap;
  vertical-align: middle;
}

tbody tr {
  height: 44px;
}

th {
  color: var(--muted);
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  position: sticky;
  top: 0;
  background: rgba(8, 12, 24, 0.95);
}

tr.row-open td {
  background: rgba(241, 245, 249, 0.04);
}
tr.row-ack td {
  background: rgba(52, 211, 153, 0.08);
}
tr.row-invoiced td {
  background: rgba(148, 163, 184, 0.1);
  color: #cbd5e1;
}
tr.row-discrepancy td {
  background: rgba(248, 113, 113, 0.12);
}

.remarks {
  width: 52px;
  min-width: 52px;
  max-width: 52px;
  text-align: center;
  vertical-align: middle;
  padding: 6px 4px !important;
  white-space: nowrap;
}

.remarks-empty {
  color: var(--muted);
  opacity: 0.45;
}

.remarks-btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  padding: 0;
  border-radius: 10px;
  border: 1px solid rgba(94, 231, 255, 0.22);
  background: rgba(94, 231, 255, 0.08);
  color: var(--accent);
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.2s, border-color 0.2s, background 0.2s;
}

.remarks-btn:hover,
.remarks-btn[aria-expanded="true"] {
  transform: translateY(-1px);
  border-color: rgba(94, 231, 255, 0.45);
  background: rgba(94, 231, 255, 0.14);
  box-shadow: 0 0 18px rgba(94, 231, 255, 0.22);
}

.remarks-btn.warn {
  border-color: rgba(248, 113, 113, 0.35);
  background: rgba(248, 113, 113, 0.12);
  color: #fca5a5;
}

.remarks-btn.warn:hover,
.remarks-btn.warn[aria-expanded="true"] {
  border-color: rgba(248, 113, 113, 0.55);
  background: rgba(248, 113, 113, 0.18);
  box-shadow: 0 0 18px rgba(248, 113, 113, 0.28);
}

.remarks-icon {
  width: 18px;
  height: 18px;
}

.remarks-dot {
  position: absolute;
  top: 5px;
  right: 5px;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 8px rgba(94, 231, 255, 0.8);
}

.remarks-btn.warn .remarks-dot {
  background: #f87171;
  box-shadow: 0 0 8px rgba(248, 113, 113, 0.9);
}

.remarks-backdrop {
  position: fixed;
  inset: 0;
  z-index: 1090;
  background: rgba(5, 8, 16, 0.55);
  backdrop-filter: blur(2px);
}

.remarks-backdrop[hidden] {
  display: none !important;
}

.remarks-popover {
  position: fixed;
  z-index: 1100;
  width: min(420px, calc(100vw - 24px));
  max-height: 70vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.remarks-popover[hidden] {
  display: none !important;
}

.remarks-popover--sheet {
  left: 12px !important;
  right: 12px;
  bottom: 12px;
  top: auto !important;
  width: auto;
  max-width: none;
  max-height: min(70vh, calc(100vh - 24px));
}

.remarks-popover-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 12px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

.remarks-popover-title {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.remarks-popover-close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 36px;
  min-height: 36px;
  padding: 0;
  font-size: 1.25rem;
  line-height: 1;
}

.remarks-popover-body {
  padding: 12px 14px 14px;
  overflow-y: auto;
  max-height: calc(70vh - 52px);
  font-size: 0.82rem;
  line-height: 1.5;
  color: var(--text);
  white-space: pre-wrap;
  overflow-wrap: break-word;
  word-wrap: break-word;
  word-break: break-word;
}

.empty {
  text-align: center;
  padding: 48px 16px;
  color: var(--muted);
}

.error-banner {
  margin-bottom: 16px;
  padding: 12px 16px;
  border-radius: 12px;
  border: 1px solid rgba(248, 113, 113, 0.4);
  background: rgba(248, 113, 113, 0.12);
  color: #fecaca;
}

.assistant-panel {
  position: fixed;
  right: 16px;
  bottom: 16px;
  width: min(420px, calc(100vw - 32px));
  max-height: min(620px, calc(100vh - 32px));
  display: flex;
  flex-direction: column;
  z-index: 1000;
  overflow: hidden;
  transition: opacity 0.2s, transform 0.2s;
}

.assistant-panel[hidden] {
  display: none !important;
}

body:not(.assistant-open) .shell {
  max-width: 100%;
}

.assistant-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

.assistant-header h2 {
  margin: 0;
  font-size: 1rem;
  background: linear-gradient(90deg, var(--accent), var(--accent2));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.assistant-header p {
  margin: 4px 0 0;
  font-size: 0.75rem;
  color: var(--muted);
}

.assistant-close {
  min-width: 36px;
  padding: 4px 10px;
  font-size: 1.25rem;
  line-height: 1;
}

.assistant-messages {
  flex: 1;
  overflow-y: auto;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 200px;
  max-height: 420px;
}

.msg {
  padding: 10px 12px;
  border-radius: 12px;
  font-size: 0.85rem;
  line-height: 1.45;
  max-width: 95%;
  overflow-wrap: break-word;
  word-break: break-word;
}

.msg.bot {
  align-self: stretch;
  width: 100%;
  max-width: 100%;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.08);
  color: var(--text);
}

.msg.user {
  align-self: flex-end;
  max-width: 95%;
  background: rgba(94, 231, 255, 0.12);
  border: 1px solid rgba(94, 231, 255, 0.25);
}

.msg.bot em {
  color: var(--accent);
  font-style: normal;
}

.msg.bot strong {
  color: #fff;
  font-weight: 600;
}

/* Rich assistant answer */
.answer-rich {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: min(380px, 52vh);
  overflow-y: auto;
  overflow-x: hidden;
  padding-right: 2px;
  scrollbar-width: thin;
}

.answer-summary-pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  align-self: flex-start;
  padding: 6px 12px;
  border-radius: 999px;
  background: linear-gradient(135deg, rgba(94, 231, 255, 0.14), rgba(167, 139, 250, 0.12));
  border: 1px solid rgba(94, 231, 255, 0.28);
  margin-bottom: 2px;
}

.answer-summary-num {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--accent);
  line-height: 1;
}

.answer-summary-label {
  font-size: 0.78rem;
  color: var(--text);
  text-transform: capitalize;
}

.answer-section-title {
  margin: 4px 0 2px;
  font-size: 0.8rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: var(--accent2);
  text-transform: uppercase;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  padding-bottom: 4px;
}

.answer-para {
  margin: 0;
  font-size: 0.84rem;
  line-height: 1.5;
}

.answer-bullet {
  padding: 6px 8px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.03);
  font-size: 0.82rem;
  line-height: 1.45;
}

.disc-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.disc-list--scroll {
  max-height: 280px;
  overflow-y: auto;
  padding-right: 2px;
}

.disc-card {
  padding: 8px 10px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.07);
  border-left: 3px solid rgba(255, 255, 255, 0.2);
  font-size: 0.78rem;
  line-height: 1.4;
}

.disc-card--qty {
  border-left-color: #f59e0b;
  background: rgba(245, 158, 11, 0.06);
}

.disc-card--rate {
  border-left-color: #f87171;
  background: rgba(248, 113, 113, 0.07);
}

.disc-card--transport {
  border-left-color: #60a5fa;
  background: rgba(96, 165, 250, 0.07);
}

.disc-card--default {
  border-left-color: rgba(94, 231, 255, 0.45);
}

.disc-card-row {
  display: grid;
  grid-template-columns: minmax(0, 1.1fr) minmax(0, 1.2fr) minmax(0, 1.4fr);
  gap: 6px 8px;
  align-items: start;
}

.disc-card-po {
  font-weight: 600;
  color: var(--accent);
  background: none;
  border: none;
  padding: 0;
  font: inherit;
  text-align: left;
  cursor: pointer;
  text-decoration: underline;
  text-decoration-color: rgba(94, 231, 255, 0.35);
  text-underline-offset: 2px;
}

.disc-card-po:hover {
  color: #fff;
}

.disc-card-item {
  color: var(--text);
  font-family: ui-monospace, "Cascadia Code", monospace;
  font-size: 0.74rem;
  opacity: 0.92;
  overflow-wrap: anywhere;
}

.disc-card-issue {
  color: var(--muted);
  overflow-wrap: anywhere;
}

.disc-card-cites {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 6px;
}

.cite-chip {
  display: inline-flex;
  align-items: center;
  padding: 2px 7px;
  border-radius: 999px;
  font-size: 0.66rem;
  line-height: 1.3;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: rgba(0, 0, 0, 0.2);
  color: var(--muted);
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

button.cite-chip {
  cursor: pointer;
  font: inherit;
}

.cite-tracker {
  border-color: rgba(94, 231, 255, 0.35);
  background: rgba(94, 231, 255, 0.1);
  color: var(--accent);
}

.cite-tracker:hover {
  background: rgba(94, 231, 255, 0.2);
  color: #fff;
}

.cite-md {
  border-color: rgba(167, 139, 250, 0.3);
  color: #c4b5fd;
}

.cite-pdf {
  border-color: rgba(248, 113, 113, 0.3);
  color: #fca5a5;
}

.po-highlight {
  outline: 2px solid rgba(94, 231, 255, 0.55);
  outline-offset: 2px;
  box-shadow: 0 0 0 4px rgba(94, 231, 255, 0.12);
}

tr.row-highlight {
  outline: 2px solid rgba(94, 231, 255, 0.7);
  outline-offset: -1px;
  background: rgba(94, 231, 255, 0.12) !important;
}

.msg.loading .typing {
  color: var(--muted);
  animation: pulse 1.2s infinite;
}

.msg-meta {
  margin-top: 8px;
  font-size: 0.7rem;
  color: var(--muted);
  word-break: break-word;
}

.filter-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}

.filter-chip {
  display: inline-block;
  padding: 3px 8px;
  border-radius: 999px;
  font-size: 0.68rem;
  background: rgba(94, 231, 255, 0.1);
  border: 1px solid rgba(94, 231, 255, 0.22);
  color: var(--accent);
}

.assistant-form {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  padding: 12px 14px 14px;
  border-top: 1px solid rgba(255, 255, 255, 0.06);
}

.assistant-form textarea {
  resize: vertical;
  min-height: 44px;
  max-height: 120px;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(0, 0, 0, 0.25);
  color: var(--text);
  font: inherit;
}

.assistant-form textarea:focus {
  border-color: rgba(94, 231, 255, 0.5);
  box-shadow: 0 0 0 3px rgba(94, 231, 255, 0.12);
  outline: none;
}

.assistant-form .btn {
  align-self: end;
}

@media (max-width: 768px) {
  .po-header {
    grid-template-columns: auto auto 1fr;
  }

  .po-header .badge {
    grid-column: 1 / -1;
    justify-self: start;
    margin-left: 74px;
  }

  .chevron-btn,
  .drag-handle {
    width: 36px;
    height: 36px;
  }

  th:nth-child(n + 6):not(:last-child),
  td:nth-child(n + 6):not(:last-child) {
    display: none;
  }

  .remarks {
    display: table-cell;
  }

  .remarks-popover-close {
    min-width: 44px;
    min-height: 44px;
    font-size: 1.4rem;
  }

  .assistant-panel {
    right: 8px;
    bottom: 8px;
    width: calc(100vw - 16px);
    max-height: min(70vh, calc(100vh - 120px));
  }

  .disc-card-row {
    grid-template-columns: 1fr;
    gap: 3px;
  }

  .answer-rich {
    max-height: min(320px, 45vh);
    font-size: 0.88rem;
  }

  .disc-card {
    font-size: 0.82rem;
  }

  .cite-chip {
    font-size: 0.72rem;
    white-space: normal;
    word-break: break-all;
  }

  .assistant-toggle span:last-child {
    display: none;
  }

  .assistant-toggle {
    min-width: 40px;
    justify-content: center;
    padding-inline: 12px;
  }
}
