/* --- Chart scaling: aspect-ratio driven sizing --- */
body,
.bslib-page-navbar,
.tab-content,
.modal,
.popover,
.dropdown-menu {
  font-family: "Segoe UI", Arial, sans-serif;
  letter-spacing: 0;
}

.tab-content > .tab-pane {
  background: #f2f2f2;
}

.tab-content > .tab-pane .card {
  border: 1px solid #e6e6e1;
  border-radius: 4px;
  box-shadow: none;
  background: #fff;
}

.tab-content > .tab-pane .card-header {
  background: #fff;
  border-bottom: 1px solid #e6e6e1;
  color: #00264d;
  font-family: "Segoe UI", Arial, sans-serif;
  font-weight: 600;
}

:root {
  --tab-pane-header-height: 54px;
}

:where(
  .nc-section-header,
  .target-section-header,
  .func-section-header,
  .subfunc-section-header,
  .ratio-section-header,
  .summary-section-header,
  .org-section-header,
  .welcome-page .access-panel
) {
  box-sizing: border-box !important;
  display: flex !important;
  flex-direction: column !important;
  justify-content: center !important;
  height: var(--tab-pane-header-height) !important;
  min-height: var(--tab-pane-header-height) !important;
  margin: 20px 0 8px 0 !important;
  padding: 7px 12px !important;
  border-left: 0 !important;
  /* Brand accent - declared as --ssm-accent on :root in app.R. This rule is
     !important and covers every section header on every page, so it decides
     the accent colour regardless of what the per-module CSS asks for. */
  border-top: 2px solid var(--ssm-accent) !important;
  border-radius: 0 !important;
  background: #e6e6e1 !important;
  color: #00264d !important;
  font-family: "Segoe UI", Arial, sans-serif !important;
  font-size: 0.86em !important;
  font-weight: 700 !important;
  line-height: 1.25 !important;
  letter-spacing: 0 !important;
  overflow: hidden !important;
}

:where(
  .nc-section-header,
  .target-section-header,
  .func-section-header,
  .subfunc-section-header,
  .ratio-section-header,
  .summary-section-header,
  .org-section-header,
  .welcome-page .access-panel
):first-child {
  margin-top: 0 !important;
}

:where(
  .nc-section-header,
  .target-section-header,
  .func-section-header,
  .subfunc-section-header,
  .ratio-section-header,
  .summary-section-header,
  .org-section-header,
  .welcome-page .access-panel
) > div {
  color: #2b72b5 !important;
  font-family: "Segoe UI", Arial, sans-serif !important;
  font-size: 0.85em !important;
  font-weight: 600 !important;
  letter-spacing: 0 !important;
}

.responsive-chart .shiny-plot-output {
  height: auto !important;
  min-height: 180px;
  aspect-ratio: 16 / 7;
}
.responsive-chart-tall .shiny-plot-output {
  height: auto !important;
  min-height: 200px;
  aspect-ratio: 4 / 3;
}
.responsive-chart-short .shiny-plot-output {
  height: auto !important;
  min-height: 140px;
  aspect-ratio: 21 / 9;
}

/* --- DT overflow guard: allow horizontal scroll in cards --- */
.card .dataTables_wrapper {
  overflow-x: auto;
}

/* --- Function/Sub-function tables: name column overflow --- */
.func-page table.dataTable td:first-child,
.subfunc-page table.dataTable td:first-child {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* --- Target-tab action bars (Function & Sub-Function) ---
   Shared styling for the Reset / Rebalance button row that sits below the
   editable target table on both tabs. Sized between Bootstrap's default
   and btn-sm so the controls have presence without dominating the page.
   Use .target-action-bar on the wrapping div, plus btn-target-reset or
   btn-target-rebalance on the buttons themselves. Primary actions carry navy
   C5 (#00264d); the rebalance-off toggle stays neutral slate so the on/off
   states read apart. */
.target-action-bar {
  display: flex;
  align-items: center;
  gap: 5px;
  margin-top: 8px;
  margin-bottom: 18px;
  padding: 7px 0 0 0;
  border-top: 1px solid #e9eef5;
  flex-wrap: wrap;
}
.target-action-bar .btn-target {
  font-size: 0.78em;
  padding: 3px 10px;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  min-height: 27px;
  line-height: 1.1;
  border-radius: 5px;
  box-shadow: 0 1px 1px rgba(31, 45, 61, 0.08);
  transition: background-color 0.15s ease, border-color 0.15s ease,
    color 0.15s ease, box-shadow 0.15s ease, transform 0.12s ease;
}
.target-action-bar .btn-target:focus {
  box-shadow: 0 0 0 0.16rem rgba(0, 38, 77, 0.25);
}
.target-action-bar .btn-target:hover {
  box-shadow: 0 2px 5px rgba(31, 45, 61, 0.16);
  transform: translateY(-1px);
}
.target-action-bar .btn-target .btn-icon {
  font-size: 1.1em;
  line-height: 1;
}
.target-action-bar .btn-target-rebalance {
  background: #00264d !important;
  border-color: #00264d !important;
  color: #fff !important;
}
.target-action-bar .btn-target-rebalance:hover {
  background: #001b38 !important;
  border-color: #001b38 !important;
  color: #fff !important;
}
.target-action-bar .btn-target-rebalance-off {
  background: #475569 !important;
  border-color: #334155 !important;
  color: #fff !important;
}
.target-action-bar .btn-target-rebalance-off:hover {
  background: #334155 !important;
  border-color: #1f2937 !important;
  color: #fff !important;
}
.target-action-bar .btn-target-rollup {
  background: #00264d !important;
  border-color: #00264d !important;
  color: #fff !important;
}
.target-action-bar .btn-target-rollup:hover {
  background: #001b38 !important;
  border-color: #001b38 !important;
  color: #fff !important;
}
.target-action-bar .btn-target-reset {
  background: #f8fafc !important;
  border-color: #94a3b8 !important;
  color: #334155 !important;
}
.target-action-bar .btn-target-reset:hover {
  background: #e2e8f0 !important;
  border-color: #64748b !important;
  color: #0f172a !important;
}
.target-action-bar .target-help {
  margin-left: -1px;
  margin-right: 7px;
  align-self: center;
}
.target-action-bar .target-help .info-icon {
  width: 13px;
  height: 13px;
  margin-left: 0;
  background: #d8e0e8;
  color: #53677a;
  border: 1px solid #c2ccd6;
  font-size: 9px;
}
.target-action-bar .target-help:hover .info-icon {
  background: #00264d;
  border-color: #00264d;
  color: #fff;
}
.target-action-bar .target-table-metric {
  margin-left: auto;
  display: inline-flex;
  align-items: center;
  justify-content: flex-end;
  gap: 3px;
  align-self: center;
  padding: 5px 12px;
  min-height: 27px;
  border: 1px solid #d9e0e8;
  border-radius: 6px;
  background: #f3f5f7;
  color: #334155;
  font-size: 0.78em;
  line-height: 1.2;
  white-space: nowrap;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.75);
}
.target-action-bar .target-table-metric .metric-label {
  color: #475569;
  font-weight: 600;
}
.target-action-bar .target-table-metric .metric-value {
  color: #0f172a;
  font-weight: 800;
}

/* --- Export buttons (shared by every analysis view) ---
   One rule set so the control looks identical wherever it appears. Built by
   ssm_export_actions()/ssm_export_button() in src/utils/ui_helpers.R. */
.ssm-export-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin: 0 0 6px 0;
}
.ssm-export-actions .ssm-export-btn {
  font-size: 0.78em;
  padding: 4px 10px;
}
@media (max-width: 575.98px) {
  .ssm-export-actions {
    flex-direction: column;
    align-items: stretch;
  }
}
/* --- Info tooltips (global) --- */
.info-tip {
  position: relative;
  display: inline-flex;
  align-items: center;
  cursor: help;
}
.info-tip .info-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 13px;
  height: 13px;
  border-radius: 50%;
  background: #b8c6d4;
  color: #fff;
  font-size: 9px;
  font-weight: 700;
  margin-left: 4px;
  flex-shrink: 0;
  border: 0;
  padding: 0;
  cursor: help;
  font-family: inherit;
}
.info-tip .info-icon:focus-visible {
  outline: 2px solid #0b5ed7;
  outline-offset: 2px;
}
.info-tip .info-text {
  display: none;
  position: fixed;
  width: 280px;
  background: #2C3E50;
  color: #fff;
  font-size: 11px;
  font-weight: 400;
  padding: 6px 10px;
  border-radius: 5px;
  line-height: 1.4;
  z-index: 10000;
  text-align: left;
  box-shadow: 0 2px 8px rgba(0,0,0,0.25);
  pointer-events: none;
  /* Reset white-space so tooltips inside nowrap containers
     (e.g. .target-action-bar .target-table-metric) still wrap
     within the fixed 280px box instead of overflowing. */
  white-space: normal;
}
.info-tip.is-open .info-text {
  display: block;
}
.info-tip .info-text ul,
.info-tip .info-text .tooltip-list {
  display: block;
  margin: 0;
  padding-left: 14px;
}
.info-tip .info-text li,
.info-tip .info-text .tooltip-point {
  display: list-item;
  margin: 0 0 3px 0;
}
.info-tip .info-text li:last-child,
.info-tip .info-text .tooltip-point:last-child {
  margin-bottom: 0;
}


/* --- Editable cell styling for DT tables ---
   One rule per edge: the inset bar owns the left edge and `border-bottom` owns
   the bottom. A second shadow alongside either one renders as a doubled line,
   because an inset shadow paints just inside the border it sits behind. */
.dt-editable-yellow {
  position: relative;
  background:
    linear-gradient(180deg, #fffdf3 0%, #fff8db 100%) !important;
  border-bottom: 1px solid #dfc24b !important;
  box-shadow: inset 3px 0 0 #d4a017 !important;
  color: #2f2f2f;
  cursor: pointer !important;
  font-weight: 700;
  transition:
    background-color 0.12s ease,
    box-shadow 0.12s ease,
    filter 0.12s ease;
}
.dt-editable-purple {
  position: relative;
  background:
    linear-gradient(180deg, #fbf6fd 0%, #f2e8f7 100%) !important;
  border-bottom: 1px solid #b985cb !important;
  box-shadow: inset 3px 0 0 #8e44ad !important;
  color: #2f2f2f;
  cursor: pointer !important;
  font-weight: 700;
  transition:
    background-color 0.12s ease,
    box-shadow 0.12s ease,
    filter 0.12s ease;
}
.dt-editable-yellow::after,
.dt-editable-purple::after {
  content: "";
  position: absolute;
  top: 4px;
  left: 7px;
  width: 7px;
  height: 7px;
  border-top: 2px solid currentColor;
  border-left: 2px solid currentColor;
  opacity: 0.42;
  pointer-events: none;
}
table.dataTable tbody td.dt-editable-yellow:hover,
table.dataTable tbody tr:hover td.dt-editable-yellow {
  background: #fff2b8 !important;
  box-shadow:
    inset 3px 0 0 #b78100,
    inset 0 0 0 1px rgba(183, 129, 0, 0.42) !important;
}
table.dataTable tbody td.dt-editable-purple:hover,
table.dataTable tbody tr:hover td.dt-editable-purple {
  background: #ead8f1 !important;
  box-shadow:
    inset 3px 0 0 #7d3c98,
    inset 0 0 0 1px rgba(125, 60, 152, 0.36) !important;
}
table.dataTable tbody td.dt-editable-yellow:focus,
table.dataTable tbody td.dt-editable-purple:focus,
table.dataTable tbody td.dt-editable-yellow:focus-within,
table.dataTable tbody td.dt-editable-purple:focus-within {
  outline: 2px solid #0d6efd !important;
  outline-offset: -2px;
  box-shadow:
    inset 3px 0 0 #0d6efd,
    inset 0 0 0 1px rgba(13, 110, 253, 0.42) !important;
}
table.dataTable tbody td.dt-editable-yellow input,
table.dataTable tbody td.dt-editable-purple input {
  width: 100%;
  min-width: 0;
  border: 1px solid #0d6efd;
  border-radius: 3px;
  box-shadow: 0 0 0 0.16rem rgba(13, 110, 253, 0.16);
  font-weight: 700;
  text-align: right;
}
.dt-rebalanced {
  background: #eef5ff !important;
  font-weight: 600;
}

/* De-emphasised NC columns */
.dt-nc-muted {
  color: #8a9bb0 !important;
  font-size: 0.92em;
}

/* Plan-table sub-total footer row (Functional / Sub-Functional Plan) */
table.dataTable tfoot th.ssm-total-label,
table.dataTable tfoot th.ssm-total-cell {
  background: #e8edf3;
  border-top: 2px solid #00264d;
  border-bottom: none;
  color: #00264d;
  font-weight: 700;
  font-size: 0.92em;
  padding: 6px 10px;
  white-space: nowrap;
}
table.dataTable tfoot th.ssm-total-label {
  text-align: left;
}
table.dataTable tfoot th.ssm-total-cell {
  text-align: right;
}
table.dataTable tfoot th.ssm-total-cell.dt-nc-muted {
  color: #5d7089 !important;
  font-weight: 600;
}
/* DataTables copies a column's className onto its footer cell. Strip the
   column tints back off so the total reads as one band, and so the manual
   column's editable affordance is not offered on a cell nobody can edit. */
table.dataTable tfoot th.ssm-total-cell.dt-editable-yellow,
table.dataTable tfoot th.ssm-total-cell.dt-editable-purple,
table.dataTable tfoot th.ssm-total-cell.dt-rebalanced {
  background: #e8edf3 !important;
  box-shadow: none !important;
  border-bottom: none !important;
  color: #00264d;
  cursor: default !important;
}
table.dataTable tfoot th.ssm-total-cell.dt-editable-yellow::after,
table.dataTable tfoot th.ssm-total-cell.dt-editable-purple::after {
  content: none;
}

/* Table legend strip */
.table-legend {
  display: flex;
  gap: 16px;
  font-size: 0.72em;
  color: #6c757d;
  padding: 4px 8px;
  margin-top: 2px;
}
.table-legend .legend-item {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
.table-legend .swatch {
  display: inline-block;
  width: 14px;
  height: 14px;
  border-radius: 3px;
}
.table-legend .swatch-yellow {
  position: relative;
  background: linear-gradient(180deg, #fffdf3 0%, #fff8db 100%);
  border: 1px solid #dfc24b;
  box-shadow: inset 3px 0 0 #d4a017;
}
.table-legend .swatch-purple {
  position: relative;
  background: linear-gradient(180deg, #fbf6fd 0%, #f2e8f7 100%);
  border: 1px solid #b985cb;
  box-shadow: inset 3px 0 0 #8e44ad;
}
.table-legend .swatch-yellow::after,
.table-legend .swatch-purple::after {
  content: "";
  position: absolute;
  top: 2px;
  right: 2px;
  width: 5px;
  height: 5px;
  border-top: 1px solid currentColor;
  border-right: 1px solid currentColor;
  opacity: 0.5;
}


/* --- NC delta header: shows the divergence between Target NC (set on
   Overall tab) and Effective NC (sum of effective function targets /
   baseline predicted spend). Only rendered when |delta| > 0.05.
   Negative delta = effective is below target = more saving than goal,
   shown amber. Positive delta = less saving than goal, shown muted. */
.nc-delta-header {
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 6px 12px;
  margin: 8px 0 0 0;
  border-radius: 6px;
  font-size: 0.80em;
  background: #fff8ec;
  border: 1px solid #fde68a;
  color: #5b3a08;
  flex-wrap: wrap;
}
.nc-delta-header.nc-delta-positive {
  background: #f3f4f6;
  border-color: #d1d5db;
  color: #374151;
}
.nc-delta-header .nc-delta-pair {
  display: inline-flex;
  align-items: baseline;
  gap: 4px;
}
.nc-delta-header .nc-delta-label {
  font-weight: 600;
  opacity: 0.8;
}
.nc-delta-header .nc-delta-value {
  font-weight: 800;
}
.nc-delta-header .nc-delta-detail {
  font-weight: 400;
  opacity: 0.85;
  font-size: 0.92em;
}
/* Over-delivery explainer: wraps onto its own line inside the
   flex container thanks to flex-basis: 100%. Shown only when
   rebalance is active and manual cuts have over-delivered. */
.nc-delta-header .nc-delta-explanation {
  flex-basis: 100%;
  font-weight: 400;
  font-size: 0.92em;
  line-height: 1.45;
  padding-top: 5px;
  margin-top: 4px;
  border-top: 1px solid rgba(180, 130, 30, 0.18);
  opacity: 0.92;
}

/* --- Selectize dropdown: opaque menu ---
   Without an explicit background the open option list renders
   see-through, letting the page content behind it bleed into the
   list (e.g. the University dropdown on the Setup page). Force a
   solid background, border, shadow and a z-index above adjacent
   inputs/cards so the menu sits cleanly on top. */
.selectize-dropdown,
.selectize-dropdown.form-control {
  background: #fff !important;
  border: 1px solid #ced4da;
  border-radius: 0 0 4px 4px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.18);
  z-index: 1100;
}
.selectize-dropdown .selectize-dropdown-content {
  background: #fff;
}
.selectize-dropdown .option {
  background: #fff;
  color: #212529;
}
.selectize-dropdown .option.active,
.selectize-dropdown .option:hover {
  background: #2563eb;
  color: #fff;
}


/* --- Checklist filter popovers ---
   One-option-per-row chip checklist behind a button trigger, shared by every
   multi-select filter (Org Impact org units/functions, T/NT categories). Global
   because bslib::popover moves its content to document.body on open, so a rule
   scoped to a page wrapper (.tnt-page, .org-page) stops matching once shown;
   modules keep only their own trigger metrics. Values live in the
   checkboxGroupInput - :has() just restyles the labels. */
.fn-filter-btn {
  background: white;
  border: 1px solid #ced4da;
  border-radius: 4px;
  padding: 4px 9px;
  font-size: 0.8em;
  cursor: pointer;
  line-height: 1.4;
  color: #495057;
  white-space: nowrap;
  min-width: 160px;
  text-align: left;
  display: inline-flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.04);
}
.fn-filter-btn:hover {
  border-color: #86b7fe;
  background: #f8fbff;
}
.fn-filter-btn:focus {
  outline: 0;
  border-color: #86b7fe;
  box-shadow: 0 0 0 0.16rem rgba(13, 110, 253, 0.16);
}
.fn-filter-btn .caret {
  color: #6c757d;
  font-size: 0.75em;
}
.chip-group {
  min-width: 270px;
  max-width: 380px;
  max-height: 320px;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 2px;
}
.chip-group .shiny-options-group {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 4px;
  margin-top: 0;
  width: 100%;
}
.chip-group .checkbox-inline {
  display: flex;
  align-items: center;
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
  position: relative;
  padding: 6px 10px 6px 30px;
  margin: 0 !important;
  border-radius: 6px;
  border: 1px solid #dee2e6;
  background: #fff;
  color: #334155;
  font-size: 0.76em;
  line-height: 1.25;
  cursor: pointer; user-select: none;
  transition: background 0.12s, border-color 0.12s, color 0.12s;
  min-height: 30px;
  white-space: normal;
}
.chip-group .checkbox-inline:hover {
  background: #f8f9fa;
  border-color: #b6c2cf;
}
.chip-group .checkbox-inline::before {
  content: '';
  position: absolute;
  left: 10px;
  top: 50%;
  width: 12px;
  height: 12px;
  transform: translateY(-50%);
  border-radius: 3px;
  border: 1px solid #adb5bd;
  background: #fff;
  box-sizing: border-box;
}
.chip-group .checkbox-inline input[type='checkbox'] {
  display: none;
}
.chip-group .checkbox-inline:has(input[type='checkbox']:checked) {
  background: #e8f5ef;
  color: #0f5132;
  border-color: #8fd3b0;
  font-weight: 600;
}
.chip-group .checkbox-inline:has(input[type='checkbox']:checked)::before {
  content: '✓';
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-color: #198754;
  background: #198754;
  color: #fff;
  font-size: 9px;
  line-height: 1;
}
.chip-actions {
  display: flex;
  gap: 8px;
  margin-top: 10px; padding-top: 8px;
  border-top: 1px solid #dee2e6; font-size: 0.8em;
}
.chip-actions a {
  color: #0d6efd; text-decoration: none;
  cursor: pointer;
  border-radius: 4px;
  padding: 2px 4px;
}
.chip-actions a:hover {
  background: #eef5ff;
  text-decoration: none;
}
/* Bootstrap popovers have a conservative default max-width and body padding,
   so the shell needs to size to the checklist rather than clipping it. */
.popover:has(.chip-group) {
  --bs-popover-max-width: min(420px, calc(100vw - 24px));
  max-width: var(--bs-popover-max-width);
}
.popover:has(.chip-group) .popover-body {
  box-sizing: border-box;
  overflow: hidden;
  padding: 10px 12px;
}
.popover:has(.chip-group) .chip-group,
.popover:has(.chip-group) .chip-actions {
  box-sizing: border-box;
  width: min(380px, calc(100vw - 56px));
  max-width: 100%;
  min-width: 0;
}
.popover:has(.chip-group) .checkbox-inline {
  min-width: 0;
  overflow-wrap: anywhere;
}
/* --- Side panels: stack gracefully on narrow screens --- */
@media (max-width: 1199px) {
  .nc-side-panel {
    height: auto;
    min-height: unset;
    border-right: none;
    border-bottom: 1px solid #dee2e6;
  }
  .func-list-panel {
    height: auto;
    max-height: 200px;
    border-right: none;
    border-bottom: 1px solid #dee2e6;
  }
}

/* --- Numeric inputs: type-only --- */
/* The native spinners let a click-repeat queue one Shiny input event per click,
   which raced the server into a limit cycle on the Setup Annual % box. Hiding
   the buttons is only half of it: type="number" still steps on the wheel and on
   Up/Down, so app.R blocks both. */
input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button {
  -webkit-appearance: none;
  appearance: none;
  margin: 0;
}
input[type="number"] {
  -moz-appearance: textfield;
  appearance: textfield;
}
