/* Colors are taken from the original workbook so staff recognise them:
   gold = "you type here", green = highest, red = lowest, purple = above U&C. */
:root {
  --bg: #ffffff;
  --text: #1f1f1f;
  --muted: #6b6b6b;
  --border: #d6d3cc;
  --header-bg: #edebe7;
  --readonly-bg: #f3f2f0;
  --input-bg: #faf1d3;
  --input-border: #d9c78a;
  --brand: #1f3a5f;
  --brand-text: #ffffff;
  --link: #1f5fa8;
  --high-bg: #ddf1e5;
  --high-text: #1e7a4c;
  --low-bg: #fadcda;
  --low-text: #b3302b;
  --above-uc-bg: #ebddf5;
  --above-uc-text: #6d3a9c;
  --focus: #1f5fa8;
  --radius: 4px;
  --font: "Segoe UI", system-ui, -apple-system, Roboto, Arial, sans-serif;
}

/* Anything marked hidden stays hidden, even if a class gives it display: flex. */
[hidden] {
  display: none !important;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.45;
}

a {
  color: var(--link);
}

:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

/* ---------- Layout ---------- */

/* Hidden until a keyboard user tabs to it, then shown at the top left. */
.skip-link {
  position: absolute;
  left: -999px;
  top: 0.5rem;
  z-index: 20;
  padding: 0.5rem 0.9rem;
  background: #fff;
  color: var(--brand);
  border: 2px solid var(--focus);
  border-radius: var(--radius);
}

.skip-link:focus {
  left: 0.5rem;
}

/* main gets focus after each navigation (for screen readers); no outline needed there. */
main:focus {
  outline: none;
}

.app-header {
  display: flex;
  align-items: center;
  gap: 1.5rem;
  padding: 0.5rem 1rem;
  background: var(--brand);
  color: var(--brand-text);
  flex-wrap: wrap;
}

.app-title {
  font-weight: 600;
  font-size: 1.05rem;
  white-space: nowrap;
}

.app-nav {
  display: flex;
  gap: 0.25rem;
  flex-wrap: wrap;
  flex: 1;
}

.app-nav a {
  color: var(--brand-text);
  text-decoration: none;
  padding: 0.35rem 0.65rem;
  border-radius: var(--radius);
  opacity: 0.85;
}

.app-nav a:hover {
  background: rgba(255, 255, 255, 0.12);
  opacity: 1;
}

.app-nav a[aria-current="page"] {
  background: rgba(255, 255, 255, 0.2);
  opacity: 1;
  font-weight: 600;
}

.app-user {
  font-size: 0.875rem;
  opacity: 0.9;
  white-space: nowrap;
}

.app-user a {
  color: inherit;
}

/* A link that looks like a button (e.g. "Sign in with Microsoft"). */
a.button {
  display: inline-block;
  padding: 0.4rem 0.9rem;
  border-radius: var(--radius);
  border: 1px solid var(--brand);
  background: var(--brand);
  color: var(--brand-text);
  text-decoration: none;
}

.sign-in {
  max-width: 32rem;
  margin: 3rem auto;
  text-align: center;
}

main {
  padding: 1rem;
  max-width: 1400px;
  margin: 0 auto;
}

h1 {
  font-size: 1.4rem;
  margin: 0 0 1rem;
}

.app-footer {
  padding: 1rem;
  text-align: center;
  font-size: 0.8rem;
  color: var(--muted);
}

/* ---------- Banner (errors, session expired) ---------- */

.banner {
  padding: 0.6rem 1rem;
  background: var(--low-bg);
  color: var(--low-text);
  border-bottom: 1px solid var(--low-text);
  display: flex;
  gap: 1rem;
  align-items: center;
}

.banner[hidden] {
  display: none;
}

/* ---------- Cards, tables, inputs ---------- */

.card {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  margin-bottom: 1rem;
  background: #fff;
}

.muted {
  color: var(--muted);
}

table.data {
  border-collapse: collapse;
  width: 100%;
}

table.data th,
table.data td {
  border: 1px solid var(--border);
  padding: 0.35rem 0.6rem;
  text-align: left;
}

table.data th {
  background: var(--header-bg);
}

/* Only the header row sticks to the top; row headers (th in tbody) scroll normally. */
table.data thead th {
  position: sticky;
  top: 0;
  z-index: 1;
}

table.data td.num,
table.data th.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

input,
select,
textarea,
button {
  font: inherit;
}

/* Gold = editable, exactly like the spreadsheet. */
input:not([type="checkbox"]):not([type="radio"]):not([type="button"]):not([type="submit"]),
select,
textarea {
  background: var(--input-bg);
  border: 1px solid var(--input-border);
  border-radius: var(--radius);
  padding: 0.3rem 0.45rem;
}

input[readonly] {
  background: var(--readonly-bg);
  border-color: var(--border);
}

button {
  padding: 0.4rem 0.9rem;
  border-radius: var(--radius);
  border: 1px solid var(--brand);
  background: var(--brand);
  color: var(--brand-text);
  cursor: pointer;
}

button.secondary {
  background: #fff;
  color: var(--brand);
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Rate highlight classes (used from Phase 3 onwards). */
.rate-high {
  background: var(--high-bg);
  color: var(--high-text);
}

.rate-low {
  background: var(--low-bg);
  color: var(--low-text);
}

.rate-above-uc {
  background: var(--above-uc-bg);
  color: var(--above-uc-text);
}

.ok {
  color: var(--high-text);
  font-weight: 600;
}

.bad {
  color: var(--low-text);
  font-weight: 600;
}

/* ---------- List pages (Payers, Codes) ---------- */

.page-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 0.75rem;
}

.page-head h1 {
  margin: 0;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 0.75rem;
}

.toolbar input[type="search"] {
  min-width: 16rem;
}

/* The table scrolls inside this box so the sticky header row stays visible. */
.table-wrap {
  overflow: auto;
  max-height: calc(100vh - 220px);
  border: 1px solid var(--border);
}

.table-wrap table.data th:first-child,
.table-wrap table.data td:first-child {
  border-left: 0;
}

table.data tbody tr:hover {
  background: #faf9f6;
}

tr.inactive td {
  color: var(--muted);
}

.badge {
  display: inline-block;
  padding: 0 0.45rem;
  border-radius: 999px;
  background: var(--readonly-bg);
  border: 1px solid var(--border);
  font-size: 0.8rem;
}

button.link {
  background: none;
  border: 0;
  padding: 0;
  color: var(--link);
  text-decoration: underline;
  text-align: left;
  cursor: pointer;
}

button.danger {
  background: var(--low-text);
  border-color: var(--low-text);
  color: #fff;
}

.check {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  cursor: pointer;
}

input.upper {
  text-transform: uppercase;
}

/* ---------- Dialogs and forms ---------- */

dialog.modal {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 1.25rem;
  width: min(560px, calc(100% - 32px));
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}

dialog.modal::backdrop {
  background: rgba(0, 0, 0, 0.35);
}

dialog.modal h2 {
  margin: 0 0 1rem;
  font-size: 1.2rem;
}

.form {
  display: grid;
  gap: 0.85rem;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.85rem;
}

.field {
  display: grid;
  gap: 0.25rem;
}

.field-label {
  font-weight: 600;
  font-size: 0.875rem;
}

.form-error {
  margin: 0;
  padding: 0.5rem 0.75rem;
  background: var(--low-bg);
  color: var(--low-text);
  border-radius: var(--radius);
}

.form-error[hidden] {
  display: none;
}

.modal-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-top: 0.5rem;
}

.spacer {
  flex: 1;
}

.delete-area {
  display: grid;
  gap: 0.25rem;
  max-width: 55%;
}

.confirm-details {
  max-height: 14rem;
  overflow: auto;
  margin: 0 0 0.75rem;
  padding-left: 1.25rem;
  font-variant-numeric: tabular-nums;
}

/* ---------- Enter by Payer ---------- */

.entry-head {
  display: grid;
  grid-template-columns: minmax(16rem, 26rem) 1fr;
  gap: 1rem 2rem;
  align-items: start;
}

.payer-input {
  width: 100%;
  font-size: 1.05rem;
}

.summary {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(11rem, 1fr));
  gap: 0.5rem 1.25rem;
  margin: 0;
}

.summary dt {
  font-size: 0.8rem;
  color: var(--muted);
}

.summary dd {
  margin: 0;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.entry-table {
  font-size: 0.9rem;
}

.entry-table td {
  vertical-align: top;
  padding: 0.25rem 0.5rem;
}

.entry-table td:nth-child(2) {
  min-width: 16rem;
}

.entry-table td:nth-child(7),
.entry-table td:nth-child(8) {
  white-space: nowrap;
}

.entry-table th.input-head {
  background: var(--input-bg);
}

.entry-table td.input-cell {
  padding: 2px 4px;
}

.rate-input {
  width: 6.5rem;
  text-align: right;
}

.date-input {
  width: 9.5rem;
}

.note-input {
  width: 12rem;
}

/* A row that has something typed in it but isn't saved yet. */
.entry-table tr.edited td {
  background: #e8f1fb;
}

.entry-table tr.edited td.input-cell {
  background: #d4e6f8;
}

.entry-table tr.no-rate td:not(.input-cell) {
  color: var(--muted);
}

.entry-table tr.no-rate td:first-child button.link {
  color: var(--link);
}

.entry-table tbody tr {
  cursor: pointer;
}

input.invalid {
  border-color: var(--low-text) !important;
  background: var(--low-bg) !important;
}

input.above-uc {
  border-color: var(--above-uc-text) !important;
  background: var(--above-uc-bg) !important;
  color: var(--above-uc-text);
}

.status-above {
  color: var(--above-uc-text);
  font-weight: 600;
}

.scheduled-note {
  font-size: 0.78rem;
  color: var(--link);
  white-space: nowrap;
}

.scheduled-note::before {
  content: "→ ";
}

/* ---------- Rate history side panel ---------- */

.side-panel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(440px, 100%);
  background: #fff;
  border-left: 1px solid var(--border);
  box-shadow: -8px 0 24px rgba(0, 0, 0, 0.15);
  padding: 1rem 1.25rem;
  overflow: auto;
  z-index: 5;
}

.side-panel[hidden] {
  display: none;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.panel-head h2 {
  margin: 0;
  font-size: 1.15rem;
}

.panel-payer {
  font-weight: 600;
  margin-bottom: 0;
}

.history-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: grid;
  gap: 0.6rem;
}

.history-item {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.6rem 0.75rem;
  display: grid;
  gap: 0.25rem;
}

.history-item.current {
  border-color: var(--high-text);
}

.history-main {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  flex-wrap: wrap;
}

.history-amount {
  font-weight: 700;
  font-size: 1.05rem;
  font-variant-numeric: tabular-nums;
  padding: 0 0.25rem;
  border-radius: var(--radius);
}

.state-badge {
  font-size: 0.75rem;
  padding: 0 0.45rem;
  border-radius: 999px;
  border: 1px solid var(--border);
}

.state-badge.current {
  background: var(--high-bg);
  color: var(--high-text);
  border-color: var(--high-text);
}

.state-badge.scheduled {
  background: #e8f1fb;
  color: var(--link);
  border-color: var(--link);
}

.state-badge.past {
  background: var(--readonly-bg);
  color: var(--muted);
}

.small {
  font-size: 0.8rem;
}

.history-note {
  white-space: pre-wrap;
}

button.danger-link {
  color: var(--low-text);
  justify-self: start;
  font-size: 0.85rem;
}

/* ---------- Rate Lookup ---------- */

.lookup-pickers {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
  gap: 1rem;
}

.lookup-pickers input,
.lookup-pickers select {
  width: 100%;
}

.lookup-grid {
  display: grid;
  grid-template-columns: 3fr 2fr;
  gap: 1rem;
}

.lookup-grid .card,
.history-inline {
  margin-bottom: 1rem;
}

section.card h2 {
  margin: 0 0 0.5rem;
  font-size: 1.15rem;
}

.summary-table th[scope="row"] {
  background: var(--readonly-bg);
  white-space: nowrap;
}

.check-warning {
  color: var(--above-uc-text);
  background: var(--above-uc-bg);
  padding: 0.4rem 0.6rem;
  border-radius: var(--radius);
  font-weight: 600;
}

tr.selected td {
  font-weight: 600;
}

tr.selected td:not(.rate-high):not(.rate-low):not(.rate-above-uc) {
  background: #e8f1fb;
}

.benchmark-badge {
  margin-left: 0.4rem;
  font-weight: normal;
}

.scheduled-cell {
  color: var(--link);
  font-size: 0.85rem;
  white-space: nowrap;
}

.legend {
  display: inline-flex;
  gap: 0.4rem;
  font-size: 0.8rem;
}

.legend-chip {
  padding: 0.1rem 0.5rem;
  border-radius: var(--radius);
}

/* ---------- Fee Matrix ---------- */

.matrix-toolbar {
  align-items: end;
}

.matrix-wrap {
  max-height: calc(100vh - 230px);
}

table.matrix {
  width: auto;
  font-size: 0.85rem;
}

table.matrix th,
table.matrix td {
  padding: 0.2rem 0.4rem;
}

/* Code and description stay visible while scrolling sideways (like Excel's frozen panes). */
table.matrix .col-code,
table.matrix .col-desc {
  position: sticky;
  background: #fff;
  z-index: 1;
}

table.matrix .col-code {
  left: 0;
  min-width: 5rem;
  max-width: 5rem;
  font-weight: 600;
}

table.matrix .col-desc {
  left: 5rem;
  min-width: 14rem;
  max-width: 14rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  /* A divider drawn inside the cell, so it moves with the frozen column
     (collapsed table borders don't follow sticky cells). */
  box-shadow: inset -3px 0 0 var(--input-border);
}

table.matrix thead th {
  z-index: 2;
  vertical-align: bottom;
}

table.matrix thead th.col-code,
table.matrix thead th.col-desc {
  z-index: 3;
  background: var(--header-bg);
}

table.matrix .payer-head {
  min-width: 6.5rem;
  max-width: 6.5rem;
  font-size: 0.72rem;
  font-weight: 600;
  line-height: 1.2;
}

table.matrix .payer-head div {
  display: -webkit-box;
  -webkit-line-clamp: 4;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

table.matrix .col-uc {
  white-space: nowrap;
  background: var(--readonly-bg);
}

table.matrix td.rate-cell {
  cursor: pointer;
  white-space: nowrap;
}

table.matrix td.rate-cell:hover {
  outline: 2px solid var(--focus);
  outline-offset: -2px;
}

.matrix-empty {
  padding: 1rem;
}

/* ---------- Import ---------- */

.import-step1,
.import-mapping {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(15rem, 1fr));
  gap: 0.85rem 1rem;
  align-items: start;
}

.import-step1 input[type="text"],
.import-mapping select {
  width: 100%;
}

.row-input {
  width: 6rem;
}

.sample-wrap {
  margin-top: 1rem;
  max-height: 16rem;
}

.sample-table {
  font-size: 0.8rem;
}

.sample-table th,
.sample-table td {
  white-space: nowrap;
  max-width: 14rem;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sample-table .mapped {
  background: var(--input-bg);
}

.sample-header {
  font-weight: normal;
  color: var(--muted);
}

.mapped-as {
  margin-left: 0.4rem;
  padding: 0 0.35rem;
  border-radius: 999px;
  background: var(--brand);
  color: var(--brand-text);
  font-size: 0.7rem;
}

.group-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-bottom: 0.75rem;
}

.group-chip {
  padding: 0.15rem 0.6rem;
  border-radius: 999px;
  font-size: 0.85rem;
  border: 1px solid var(--border);
}

.group-chip.tone-good,
.preview-group.tone-good > summary {
  background: var(--high-bg);
  color: var(--high-text);
}

.group-chip.tone-warn,
.preview-group.tone-warn > summary {
  background: var(--above-uc-bg);
  color: var(--above-uc-text);
}

.group-chip.tone-bad,
.preview-group.tone-bad > summary {
  background: var(--low-bg);
  color: var(--low-text);
}

.group-chip.tone-muted,
.preview-group.tone-muted > summary {
  background: var(--readonly-bg);
  color: var(--muted);
}

.preview-group {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin: 0.6rem 0;
}

.preview-group > summary {
  cursor: pointer;
  padding: 0.45rem 0.75rem;
  font-weight: 600;
  border-radius: var(--radius);
}

.preview-group > :not(summary) {
  margin: 0.5rem 0.75rem;
}

.preview-wrap {
  max-height: 22rem;
}

.preview-table {
  font-size: 0.85rem;
}

.import-actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 1rem;
}

/* ---------- Reports ---------- */

.reports-layout {
  display: grid;
  grid-template-columns: 15rem 1fr;
  gap: 1.25rem;
  align-items: start;
}

.report-picker {
  display: grid;
  gap: 0.25rem;
  position: sticky;
  top: 0.5rem;
}

.report-link {
  text-align: left;
  background: #fff;
  color: var(--text);
  border: 1px solid var(--border);
}

.report-link:hover {
  border-color: var(--brand);
}

.report-link.picked {
  background: var(--brand);
  color: var(--brand-text);
  border-color: var(--brand);
}

.report-panel {
  min-width: 0;
}

.report-panel > h2 {
  margin: 0 0 0.25rem;
}

.report-params {
  display: flex;
  flex-wrap: wrap;
  align-items: end;
  gap: 0.75rem 1rem;
  margin: 0.75rem 0;
}

.report-params select {
  max-width: 24rem;
}

.report-actions {
  display: flex;
  gap: 0.5rem;
  justify-content: flex-end;
  margin: 0.5rem 0;
}

.report-head h2 {
  margin: 0.25rem 0;
}

.report-head p {
  margin: 0.25rem 0;
}

.report-table-wrap {
  max-height: calc(100vh - 260px);
}

.report-table {
  font-size: 0.875rem;
}

.print-only {
  display: none;
}

/* ---------- Help ---------- */

.lead {
  font-size: 1.05rem;
  max-width: 60rem;
}

.help-section {
  max-width: 60rem;
}

.help-section li {
  margin-bottom: 0.35rem;
}

.help-colours {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 0.5rem 1rem;
  align-items: center;
  margin: 0;
}

.help-colours dd {
  margin: 0;
}

.help-gold {
  background: var(--input-bg);
  border: 1px solid var(--input-border);
}

.help-edited {
  background: #e8f1fb;
}

/* ---------- Toasts ---------- */

.toasts {
  position: fixed;
  right: 1rem;
  bottom: 1rem;
  display: grid;
  gap: 0.5rem;
  z-index: 10;
}

.toast {
  background: var(--brand);
  color: var(--brand-text);
  padding: 0.6rem 1rem;
  border-radius: var(--radius);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

/* ---------- Small screens ---------- */

@media (max-width: 700px) {
  .app-header {
    gap: 0.5rem;
  }

  /* One row of menu links that scrolls sideways, instead of three stacked rows. */
  .app-nav {
    order: 3;
    flex-basis: 100%;
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .app-nav a {
    white-space: nowrap;
  }

  .app-user {
    font-size: 0.75rem;
  }

  .page-head {
    flex-wrap: wrap;
  }

  /* Phones: a table may not be taller than most of the screen. */
  .table-wrap,
  .matrix-wrap,
  .report-table-wrap {
    max-height: 70vh;
  }

  .help-colours {
    grid-template-columns: 1fr;
  }

  .entry-head .summary {
    grid-template-columns: 1fr 1fr;
  }

  /* Narrower frozen columns so a phone still shows a few payer columns. */
  table.matrix .col-code {
    min-width: 4rem;
    max-width: 4rem;
  }

  table.matrix .col-desc {
    left: 4rem;
    min-width: 6.5rem;
    max-width: 6.5rem;
  }

  main {
    padding: 0.75rem;
  }

  .form-row {
    grid-template-columns: 1fr;
  }

  .toolbar input[type="search"] {
    min-width: 0;
    flex: 1 1 100%;
  }

  .delete-area {
    max-width: none;
  }

  .entry-head,
  .lookup-grid,
  .reports-layout {
    grid-template-columns: 1fr;
  }

  .report-picker {
    position: static;
  }
}

/* ---------- Printing (reports; "Save as PDF" in the print dialog makes a PDF) ---------- */

@media print {
  @page {
    margin: 0.5in;
  }

  html,
  body {
    background: #fff;
    font-size: 10pt;
  }

  .app-header,
  .app-footer,
  .skip-link,
  .banner,
  .toasts,
  .no-print,
  .side-panel,
  button {
    display: none !important;
  }

  .print-only {
    display: inline;
  }

  .print-org {
    display: block;
    font-weight: 600;
    color: var(--muted);
  }

  main {
    padding: 0;
    max-width: none;
  }

  .reports-layout {
    display: block;
  }

  .card {
    border: 0;
    padding: 0;
  }

  /* Let long tables flow across pages instead of scrolling inside a box. */
  .table-wrap {
    max-height: none !important;
    overflow: visible !important;
    border: 0;
  }

  table.data {
    font-size: 8.5pt;
  }

  /* The header row repeats at the top of every printed page. */
  table.data thead {
    display: table-header-group;
  }

  table.data thead th {
    position: static;
  }

  table.data tr {
    break-inside: avoid;
  }

  /* Browsers drop background colours when printing unless told to keep them. */
  table.data th,
  .rate-high,
  .rate-low,
  .rate-above-uc {
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
}
