/* =====================================================
   Theme Safety Rule
   - Colours must use CSS variables from appdb.tokens.css.
   - Do not introduce new hex/rgb/rgba colours here.
   - If a new colour is needed, add a semantic token in appdb.tokens.css
     (light + dark).
   ===================================================== */

/* Base style overrides */

html,
body {
  font-family: 'Poppins', sans-serif;
  font-weight: normal;
}

body {
  overflow-y: scroll;
  overflow-x: hidden;
}

label {
  font-size: 0.75rem;
  color: var(--app-text-muted);
}

/* Default Styling 
---------------------------------------------- */

h1 {
  color: var(--app-text);
  font-weight: normal;
  font-size: 2rem;
}

h2 {
  color: var(--app-text);
  font-weight: normal;
  font-size: 1.4rem;
}

h3 {
  color: var(--app-text);
  font-weight: normal;
  font-size: 1.1rem;
}

h4 {
  color: var(--app-text);
  font-weight: normal;
  font-size: 1.1rem;
}

h5 {
  color: var(--app-text);
  font-weight: normal;
  font-size: 1.1rem;
}

h6 {
  color: var(--app-text);
  font-weight: normal;
  font-size: 1rem;
  font-style: italic;
}

/* Prevent image drag by default
---------------------------------------------- */

img {
  -webkit-user-drag: none;
  user-drag: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

/* Bootstrap overrides
---------------------------------------------- */

.table thead th {
  border-bottom: solid 1px var(--app-table-border);
}

.table td,
.table th {
  border-top: none;
  border-bottom: solid 1px var(--app-table-border);
}

.btn-warning,
.badge-warning {
  background-color: var(--app-warning);
  border-color: var(--app-warning);
  color: var(--app-accent-contrast);
}

.btn-danger,
.badge-danger {
  background-color: var(--app-danger);
  border-color: var(--app-danger);
  color: var(--app-accent-contrast);
}

.btn-danger:hover {
  background-color: var(--app-danger-hover);
  border-color: var(--app-danger-hover);
  color: var(--app-accent-contrast);
}

.btn-success,
.badge-success {
  background-color: var(--app-success);
  border-color: var(--app-success);
  color: var(--app-accent-contrast);
}

.btn-success:hover {
  background-color: var(--app-success-hover);
  border-color: var(--app-success-hover);
}

.btn-primary {
  background-color: var(--app-accent);
  border-color: var(--app-accent);
}

.btn-primary:hover {
  background-color: var(--app-accent-hover);
  border-color: var(--app-accent-hover);
}

.btn-secondary {
  font-size: medium;
  color: var(--app-action-secondary-text);
  background-color: var(--app-action-secondary-bg);
  border-color: var(--app-border-subtle);
}

.btn-secondary:hover {
  background-color: var(--app-action-secondary-bg-hover);
  border-color: var(--app-border-subtle);
}

.list-group-item.active {
    color: var(--app-action-secondary-text);
    background-color: var(--app-action-secondary-bg-hover);
    border-color: var(--app-border-subtle)
}

/* Specific application styles
---------------------------------------------- */

/* Grid chrome + sticky actions/header */
.grid-container {
  border: 1px solid var(--app-border-subtle);
  border-radius: var(--app-radius-lg);
  background: var(--app-surface);
  overflow: visible;
}

/* Hide grid tables until first data-based autosize completes (prevents width jump). */
.grid-container.grid-initializing .grid-header table,
.grid-container.grid-initializing .grid-body table,
.grid-container.grid-initializing .grid-hscroll {
  visibility: hidden;
}

/* Sticky bar (actions + header). This sticks to the top of the viewport while
   the grid is in view (page scroll), giving more room for rows on screen. */
.grid-sticky {
  position: sticky;
  top: 0;
  z-index: 1002;
  background: var(--app-surface);
}

/* Canonical grid header sticks during page scroll (list + sublist share this shell). */
.grid-header {
  position: sticky;
  top: 0;
  z-index: 1002;
  min-height: 3.5rem;
  box-sizing: border-box;
  background: var(--app-surface);
}

/* Avoid duplicate heading lines: remove toolbar divider, header top edge,
   and first body-row top edge while keeping a single header-bottom divider. */
.grid-container .appdb-grid-toolbar {
  border-bottom: 0 !important;
}

.grid-header .appdb-table th {
  border-top: 0 !important;
}

.grid-body .appdb-table tbody tr:first-child > td {
  border-top: 0;
}

.grid-actions {
  padding: 0 .75rem 0.25rem 0;
}

/* Global action host spacing (list header, modal footer, full edit top bar) */
[data-role="grid-list-header-actions"] > [id$="_header_actions"],
.appdb-modal-footer-actions,
.appdb-form-top-actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--app-space-8);
}

.grid-container:focus {
  border-color: var(--app-focus);
  box-shadow: 0 0 0 0.15rem var(--app-focus-ring);
}

/* Header cells (not sticky themselves; the sticky behaviour is provided by .grid-sticky) */
.grid-header thead th {
  font-size: 0.75rem;
  font-weight: normal;
  color: var(--app-text-muted);
  user-select: none;
  height: 3.5rem;
  vertical-align: bottom;
}

/* Resizer handle on header cells */
.th-resize-wrap {
  position: relative;
  padding-right: 14px;
  /* space for handle */
}

.th-resizer {
  position: absolute;
  top: 0;
  right: 0;
  width: 10px;
  height: 100%;
  cursor: col-resize;
  z-index: 3;
}

.th-resizer:after {
  content: "";
  position: absolute;
  top: 20%;
  bottom: 20%;
  left: 4px;
  width: 1px;
  background: var(--app-border-subtle);
}

/* Active (keyboard) cell highlighting */
.grid-table td.active-cell,
.grid-table th.active-cell {
  outline: 2px solid var(--app-focus-strong);
  outline-offset: -2px;
  background: var(--app-selected-bg);
}

.grid-table tr.active-row {
  background: var(--app-selected-bg);
}

.sort-indicator {
  font-size: 0.8em;
  margin-left: .25rem;
  opacity: .7;
}

.cell-nowrap {
  white-space: nowrap;
}

.cell-ellipsis {
  max-width: 280px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.cell-checkbox {
  text-align: center;
}

.cell-checkbox input[type="checkbox"] {
  vertical-align: middle;
}

/* Read-only boolean value checkboxes in list/sublist cells */
.list-boolean-cell {
  position: relative;
  display: inline-block;
  top: 0.2rem;
  left: 0.5rem;
}

.list-boolean-cell input[type="checkbox"].list-boolean-checkbox {
  width: 0;
  height: 0;
  clip: rect(0 0 0 0);
  position: absolute;
}

.list-boolean-cell input[type="checkbox"].list-boolean-checkbox + label.list-boolean-checkbox-label {
  background-color: var(--app-surface);
  border: 1px solid var(--app-muted-border);
  padding: 15px;
  display: inline-block;
  position: relative;
  margin-bottom: 0;
}

.list-boolean-cell input[type="checkbox"].list-boolean-checkbox + label.list-boolean-checkbox-label:hover,
.list-boolean-cell input[type="checkbox"].list-boolean-checkbox:checked + label.list-boolean-checkbox-label:hover {
  background-color: var(--app-surface);
  border-color: var(--app-text);
}

.list-boolean-cell input[type="checkbox"].list-boolean-checkbox:checked + label.list-boolean-checkbox-label {
  border: 1px solid var(--app-muted-border);
  color: var(--app-text);
}

.list-boolean-cell input[type="checkbox"].list-boolean-checkbox:checked + label.list-boolean-checkbox-label:after {
  font-size: xx-large;
  content: '\2714';
  position: absolute;
  top: -8px;
  left: 0;
  width: 100%;
  height: 100%;
  display: inline-block;
  vertical-align: middle;
  text-align: center;
  color: var(--app-text-muted);
}

.list-boolean-cell input[type="checkbox"].list-boolean-checkbox:focus + label.list-boolean-checkbox-label {
  border: 1px solid var(--app-focus);
  box-shadow: 0 0 0 0.2rem var(--app-focus-ring);
}

.list-boolean-cell input[type="checkbox"].list-boolean-checkbox:disabled + label.list-boolean-checkbox-label,
.list-boolean-cell input[type="checkbox"].list-boolean-checkbox:disabled:focus + label.list-boolean-checkbox-label,
.list-boolean-cell input[type="checkbox"].list-boolean-checkbox:disabled:hover + label.list-boolean-checkbox-label {
  background-color: var(--app-surface);
  color: var(--app-text-muted) !important;
}

/* sortable affordance */
.grid-row-draggable {
  cursor: move;
}

.grid-body .appdb-table tbody tr.appdb-row-readonly > td {
  opacity: var(--app-row-readonly-opacity);
  border-bottom-color: var(--app-row-readonly-border);
}

.grid-body .appdb-table tbody tr.appdb-row-pinned > td:first-child {
  position: relative;
  box-shadow: inset 3px 0 0 var(--app-row-pinned-accent);
}

.grid-body .appdb-table tbody tr.appdb-row-pinned > td:first-child::before {
  content: "\1F4CC";
  position: absolute;
  top: 0.15rem;
  left: 0.25rem;
  font-size: 0.72rem;
  line-height: 1;
  color: var(--app-row-pinned-icon);
  pointer-events: none;
}

.grid-body .appdb-table tbody tr.appdb-row-colour-red > td {
  background-color: var(--app-row-colour-red-bg);
  color: var(--app-row-colour-red-text);
}

.grid-body .appdb-table tbody tr.appdb-row-colour-amber > td {
  background-color: var(--app-row-colour-amber-bg);
  color: var(--app-row-colour-amber-text);
}

.grid-body .appdb-table tbody tr.appdb-row-colour-green > td {
  background-color: var(--app-row-colour-green-bg);
  color: var(--app-row-colour-green-text);
}

.grid-body .appdb-table tbody tr.appdb-row-colour-purple > td {
  background-color: var(--app-row-colour-purple-bg);
  color: var(--app-row-colour-purple-text);
}

.grid-body .appdb-table tbody tr.appdb-row-colour-blue > td {
  background-color: var(--app-row-colour-blue-bg);
  color: var(--app-row-colour-blue-text);
}

.grid-body .appdb-table tbody tr.appdb-row-colour-lightblue > td {
  background-color: var(--app-row-colour-lightblue-bg);
  color: var(--app-row-colour-lightblue-text);
}

.grid-body .appdb-table tbody tr.appdb-row-colour-red > td a,
.grid-body .appdb-table tbody tr.appdb-row-colour-amber > td a,
.grid-body .appdb-table tbody tr.appdb-row-colour-green > td a,
.grid-body .appdb-table tbody tr.appdb-row-colour-purple > td a,
.grid-body .appdb-table tbody tr.appdb-row-colour-blue > td a,
.grid-body .appdb-table tbody tr.appdb-row-colour-lightblue > td a {
  color: inherit;
}

/* When using page scroll, sticky bottom banners feel odd; keep them in flow */
.grid-loading,
.grid-end {
  background: var(--app-surface);
  padding: 1rem;
  font-size: 0.68rem;
  color: var(--app-text-muted);
}

.grid-button-cell {
  padding-left: 0 !important;
}

.grid-button {
  padding: 2px 6px;
}

.recents-item {
  display: block;
  border-radius: 8px;
}

.recents-title {
  font-weight: 600;
}

.recents-subtitle {
  font-size: 0.85rem;
  opacity: 0.75;
}

/* example style classes you might send from API in *_css */
.right-aligned {
  text-align: right;
}

.highlighted {
  font-weight: 600;
}

.red {
  color: var(--app-danger);
}

.green {
  color: var(--app-success);
}

.orange {
  color: var(--app-warning);
}

.blue {
  color: var(--app-accent);
}

.field-conflict {
  outline: 2px solid var(--app-danger);
  outline-offset: 1px
}

.field-dynamic {}

.right-aligned {
  text-align: right
}

/* Horizontal scrolling: body can scroll horizontally when the sum of column widths
   exceeds the viewport. The header mirrors scrollLeft via JS.
   NOTE: we keep vertical scrolling as page scroll (not inside the grid). */
.grid-header {
  overflow-x: auto;
  overflow-y: hidden;
}

.grid-header::-webkit-scrollbar {
  height: 0;
}

.grid-header {
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.grid-body {
  overflow-x: auto;
  overflow-y: visible;
  font-size: 0.95rem;
}

.grid-body td {
  vertical-align: middle;
}

/* Sublists should use page vertical scroll only; keep horizontal scroll in-container. */
[data-role="sublist-scroll"].table-responsive,
[data-role="sublist-scroll"] {
  overflow-x: auto;
  overflow-y: hidden !important;
  max-height: none;
}

/* Sticky horizontal scrollbar at the bottom of the viewport (page scroll).
   This is a separate scroll surface that mirrors the grid's horizontal scroll,
   so users can scroll sideways even while infinite rows are still loading. */
.grid-hscroll {
  position: sticky;
  bottom: 0;
  z-index: 210;
  background: var(--app-surface);
  border-top: 1px solid var(--app-border-subtle);
  overflow-x: auto;
  overflow-y: hidden;
}

.grid-hscroll-inner {
  height: 1px;
  /* just enough content to force a scrollbar */
}

/* Ensure header and body tables use fixed layout, but do NOT stretch to 100% viewport width.
   JS sets table width/min-width to the sum of column widths. */
.grid-header table,
.grid-body table {
  width: auto;
  table-layout: fixed;
}

.grid-header th,
.grid-body td {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Sticky left columns (selection + configured columns) during horizontal scroll */
.sticky-col {
  position: sticky;
  background: var(--app-surface);
}

.grid-header th.sticky-col {
  z-index: 7;
  background: var(--app-surface);
}

.grid-body td.sticky-col {
  z-index: 5;
}

.sticky-col-0 {
  left: 0;
}

/* sticky-col-N left offsets are set dynamically in JS */

/* Yes/No (and Yes/No/All) toggle
(thanks to https://cssdeck.com/labs/ufct35ys5t)
---------------------------------------------- */

.toggle_radio {
  border-radius: 0.25rem;
}

.toggle_radio label,
.toggle_option_slider {
  border-radius: 10px;
}

.toggle_radio {
  position: relative;
  overflow: hidden;
  height: 38px;
  width: 100%;
}

.toggle_radio>* {
  float: left;
}

.toggle_radio input[type=radio] {
  display: none;
}

.toggle_radio label {
  color: var(--app-text-muted);
  font-size: medium;
  display: block;
  width: calc(33.3333% - 5px);
  height: 34px;
  margin: 2px 2px;
  line-height: 34px;
  cursor: pointer;
  text-align: center;
  position: relative;
  z-index: 1;
  transform: translateY(-0.4rem);
}

.toggle_radio label.YN {
  width: calc(50% - 5px);
}

.toggle_option_slider {
  display: block;
  width: calc(33.3333% - 5px) !important;
  height: 34px;
  position: absolute;
  top: 2px;
  transition: all .4s ease;
  opacity: 0;
  pointer-events: none;
}

/* Match bootstrap focus flash for Active toggles (filter + edit form) */
.toggle_radio:focus-within {
  border-color: var(--app-focus);
  box-shadow: 0 0 0 0.2rem var(--app-focus-ring);
}

.toggle_option_slider.YN {
  width: calc(50% - 5px) !important;
}

.toggle_option.option_1:checked~.toggle_option_slider,
.toggle_option.option_2:checked~.toggle_option_slider,
.toggle_option.option_3:checked~.toggle_option_slider,
.toggle_option.option_Y:checked~.toggle_option_slider.YN,
.toggle_option.option_N:checked~.toggle_option_slider.YN {
  opacity: 1;
}

.toggle_option.option_1:checked~.toggle_option_slider,
.toggle_option.option_Y:checked~.toggle_option_slider.YN {
  background-color: var(--app-surface-2);
  border-left: solid 10px var(--app-surface);
  border-right: solid 10px var(--app-surface);
  border-top: solid 3px var(--app-surface);
  border-bottom: solid 3px var(--app-surface);
  left: 10px;
}

.toggle_option.option_2:checked~.toggle_option_slider {
  background-color: var(--app-surface-2);
  border-left: solid 10px var(--app-surface);
  border-right: solid 10px var(--app-surface);
  border-top: solid 3px var(--app-surface);
  border-bottom: solid 3px var(--app-surface);
  left: calc(33.3333% + 1px);
}

.toggle_option.option_N:checked~.toggle_option_slider.YN {
  background-color: var(--app-surface-2);
  border-left: solid 10px var(--app-surface);
  border-right: solid 10px var(--app-surface);
  border-top: solid 3px var(--app-surface);
  border-bottom: solid 3px var(--app-surface);
  left: calc(50% + 2px);
}

.toggle_option.option_3:checked~.toggle_option_slider {
  background-color: var(--app-surface-2);
  border-left: solid 10px var(--app-surface);
  border-right: solid 10px var(--app-surface);
  border-top: solid 3px var(--app-surface);
  border-bottom: solid 3px var(--app-surface);
  left: calc(66.6666% - 3px);
}

/* =====================================================
   appDB dropdown (searchable lookup)
   ===================================================== */
.appdb-dropdown {
  position: relative;
}

.appdb-dropdown .dropdown-display {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.appdb-dropdown .dropdown-text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.appdb-dropdown .dropdown-caret {
  margin-left: auto;
  opacity: 0.7;
  padding-left: 0.5rem;
}

.appdb-dropdown .dropdown-panel {
  position: absolute;
  z-index: 1050;
  left: 0;
  right: 0;
  top: 100%;
  margin-top: 2px;
  max-height: 320px;
  overflow: auto;
}

.appdb-dropdown .dropdown-results .list-group-item {
  cursor: pointer;
}

/* Dropdown chip UI (v1-style single select; multi-select ready) */
.appdb-dropdown .dropdown-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
  flex: 1 1 auto;
  min-width: 0;
}

.appdb-dropdown .dropdown-placeholder {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1 1 auto;
  min-width: 0;
}

.appdb-dropdown .dropdown-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  background: var(--app-chip-bg);
  border: 1px solid var(--app-chip-border);
  color: var(--app-chip-text);
  border-radius: 4px;
  padding: 0.05rem 0.4rem;
  max-width: 100%;
  overflow: hidden;
}

.appdb-dropdown .dropdown-chip-text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  line-height: 1.2;
  flex: 1 1 auto;
  min-width: 0;
}

.appdb-dropdown .dropdown-chip-x {
  display: inline-block;
  cursor: pointer;
  opacity: 0.65;
  line-height: 1;
}

.appdb-dropdown .dropdown-chip-x:hover {
  opacity: 1;
  background-color: var(--app-danger);
  border-color: var(--app-danger);
  color: var(--app-accent-contrast);
}

/* Keep caret visible when chip present */
.appdb-dropdown .dropdown-display {
  gap: 0.5rem;
}

/* Filter-field wrapper: match existing filter row alignment */
.appdb-filter-field {
  margin-bottom: 0;
}

.appdb-filter-field .invalid-feedback {
  display: none;
}
