/* Core Light Theme Variables */
:root {
  --tb-bg: #ffffff;
  --tb-text: #333333;
  --tb-text-muted: #64748b;
  --tb-text-label: #475569;
  --tb-border: #e2e8f0;
  --tb-border-input: #cbd5e1;
  --tb-hover-bg: #f1f5f9;

  --tb-primary: #2563eb;
  --tb-primary-hover: #1d4ed8;
  --tb-primary-focus: rgba(37, 99, 235, 0.2);
  --tb-primary-text: #ffffff;

  --tb-panel-bg: #f8fafc;
  --tb-active-bg: #e2e8f0;
  --tb-active-text: #0f172a;

  --tb-danger: #dc2626;
  --tb-danger-hover-bg: #fef2f2;
  --tb-danger-hover-text: #991b1b;

  --tb-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
  --tb-dropdown-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

/* Container & Table Layout Defaults */
.tb-container {
  font-family:
    -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial,
    sans-serif;
  color: var(--tb-text);
  margin: 1.5rem 0;
  width: 100%;
  overflow-x: visible; /* CRITICAL: Prevent horizontal scroll wrapper from clipping dropdowns */
  overflow-y: visible;
}

/* Button & Controls */
.tb-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.45rem 0.85rem;
  font-size: 0.875rem;
  font-weight: 500;
  border-radius: 6px;
  border: 1px solid var(--tb-border-input);
  background-color: var(--tb-bg);
  color: var(--tb-text);
  cursor: pointer;
  text-decoration: none;
  transition: all 0.15s ease-in-out;
}

.tb-toolbar {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.tb-toolbar-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.tb-toolbar-button {
  min-height: 2.5rem;
}

.tb-toolbar-icon-button,
.tb-toolbar-button.tb-btn {
  justify-content: center;
}

.tb-toolbar-icon-button {
  width: 2.5rem;
  padding: 0;
}

.tb-toolbar-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.tb-sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.tb-btn:hover {
  background-color: var(--tb-hover-bg);
  border-color: var(--tb-text-muted);
}

.tb-toolbar-active {
  background-color: var(--tb-active-bg);
  border-color: var(--tb-text-muted);
  color: var(--tb-active-text);
}

.tb-btn-primary {
  background-color: var(--tb-primary);
  border-color: var(--tb-primary);
  color: var(--tb-primary-text);
}

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

.tb-btn-link {
  border: none;
  background: transparent;
  color: var(--tb-text-muted);
  text-decoration: underline;
}

.tb-btn-link:hover {
  color: var(--tb-active-text);
}

/* Filter Panel */
.tb-filter-container {
  margin-bottom: 1rem;
}

.tb-filter-panel {
  margin-top: 0.75rem;
  padding: 1rem;
  background-color: var(--tb-panel-bg);
  border: 1px solid var(--tb-border);
  border-radius: 8px;
}

.tb-filter-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 1rem;
  margin-bottom: 1rem;
}

.tb-filter-group {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.tb-filter-label {
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.025em;
  color: var(--tb-text-label);
}

.tb-filter-input {
  padding: 0.45rem 0.65rem;
  font-size: 0.875rem;
  border: 1px solid var(--tb-border-input);
  background-color: var(--tb-bg);
  color: var(--tb-text);
  border-radius: 4px;
  outline: none;
}

.tb-filter-input:focus {
  border-color: var(--tb-primary);
  box-shadow: 0 0 0 2px var(--tb-primary-focus);
}

.tb-filter-actions {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.tb-query-groups {
  display: grid;
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.tb-query-group {
  display: grid;
  gap: 0.5rem;
}

.tb-query-row {
  display: grid;
  grid-template-columns: minmax(140px, 1.2fr) minmax(150px, 1fr) minmax(170px, 1fr) auto;
  align-items: center;
  gap: 0.5rem;
}

.tb-query-values,
.tb-query-row-actions {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  min-width: 0;
}

.tb-query-values .tb-filter-input {
  width: 100%;
  min-width: 0;
}

.tb-query-row-actions {
  flex-wrap: wrap;
}

.tb-query-logic,
.tb-query-current-user {
  white-space: nowrap;
}

.tb-query-remove {
  color: var(--tb-danger);
  min-width: 2.25rem;
  justify-content: center;
}

.tb-query-or-label {
  color: var(--tb-primary);
  font-weight: 600;
  padding-left: 0.25rem;
}

.tb-query-range-separator {
  color: var(--tb-text-muted);
  font-size: 0.875rem;
}

@media (max-width: 760px) {
  .tb-query-row {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  }

  .tb-query-values,
  .tb-query-row-actions {
    grid-column: 1 / -1;
  }
}

/* Add Record Panel & Typeahead */
.tb-add-panel {
  display: none;
  margin-bottom: 1rem;
  padding: 1rem;
  border: 1px solid var(--tb-border);
  border-radius: 0.375rem;
  background-color: var(--tb-panel-bg);
}

.tb-add-panel.is-active {
  display: block;
}

.tb-typeahead-container {
  position: relative;
}

.tb-typeahead-results {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  z-index: 1050;
  max-height: 200px;
  overflow-y: auto;
  background-color: var(--tb-bg);
  border: 1px solid var(--tb-border);
  border-radius: 0.375rem;
  margin-top: 2px;
  box-shadow: var(--tb-dropdown-shadow);
}

.tb-typeahead-results.is-open {
  display: block;
}

.tb-typeahead-item {
  width: 100%;
  padding: 0.5rem 0.75rem;
  text-align: left;
  background: none;
  border: none;
  color: var(--tb-text);
  cursor: pointer;
}

.tb-typeahead-item:hover,
.tb-typeahead-item.is-highlighted {
  background-color: var(--tb-primary);
  color: var(--tb-primary-text);
}

/* Table Design */
.tb-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  background-color: var(--tb-bg);
  border: 1px solid var(--tb-border);
  border-radius: 8px;
  box-shadow: var(--tb-shadow);
}

.tb-table th,
.tb-table td {
  padding: 0.75rem 1rem;
  text-align: left;
  border-bottom: 1px solid var(--tb-border);
  font-size: 0.875rem;
  position: static !important; /* CRITICAL: Prevents table cells from acting as positioning contexts */
}

.tb-table tr:first-child th:first-child {
  border-top-left-radius: 8px;
}
.tb-table tr:first-child th:last-child {
  border-top-right-radius: 8px;
}
.tb-table tr:last-child td:first-child {
  border-bottom-left-radius: 8px;
}
.tb-table tr:last-child td:last-child {
  border-bottom-right-radius: 8px;
}

.tb-table th {
  background-color: var(--tb-panel-bg);
  font-weight: 600;
  color: var(--tb-text);
  user-select: none;
}

.tb-table tbody tr:last-child td {
  border-bottom: none;
}

.tb-table tbody tr:hover {
  background-color: var(--tb-hover-bg);
}

/* Header Sort Styling */
.tb-sort-link {
  color: inherit;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
}

.tb-sort-icon {
  font-size: 0.75rem;
  color: var(--tb-text-muted);
}

.tb-sort-link:hover .tb-sort-icon {
  color: var(--tb-primary);
}

.tb-empty {
  text-align: center;
  color: var(--tb-text-muted);
  padding: 2rem !important;
}

/* Pagination */
.tb-pagination-wrapper {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 1rem 0;
  font-size: 0.875rem;
  color: var(--tb-text-label);
}

.tb-pagination-nav {
  display: flex;
  list-style: none;
  margin: 0;
  padding: 0;
  gap: 0.25rem;
}

.tb-page-item .tb-page-link {
  display: inline-block;
  padding: 0.35rem 0.7rem;
  border: 1px solid var(--tb-border-input);
  border-radius: 4px;
  color: var(--tb-text);
  text-decoration: none;
  font-weight: 500;
}

.tb-page-item.tb-active .tb-page-link {
  background-color: var(--tb-primary);
  border-color: var(--tb-primary);
  color: var(--tb-primary-text);
}

.tb-page-item:not(.tb-active):not(.tb-disabled) .tb-page-link:hover {
  background-color: var(--tb-hover-bg);
}

.tb-page-item.tb-disabled .tb-page-link {
  color: var(--tb-border-input);
  border-color: var(--tb-border);
  cursor: not-allowed;
}

/* --- Action Menu Dropdown Styles --- */
.tb-dropdown {
  position: relative;
  display: inline-block;
}

.tb-dropdown-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.35rem 0.5rem;
  background: var(--tb-bg);
  border: 1px solid var(--tb-border-input);
  border-radius: 4px;
  color: var(--tb-text-label);
  cursor: pointer;
  transition: all 0.15s ease;
}

.tb-dropdown-toggle:hover {
  background-color: var(--tb-hover-bg);
  color: var(--tb-active-text);
  border-color: var(--tb-text-muted);
}

.tb-dropdown-menu {
  display: none;
  flex-direction: column;
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  z-index: 9999; /* High z-index to stay above everything else */
  min-width: 140px;
  margin: 0;
  padding: 0.35rem 0;
  list-style: none;
  padding-left: 0;
  background-color: var(--tb-bg);
  border: 1px solid var(--tb-border);
  border-radius: 6px;
  box-shadow: var(--tb-dropdown-shadow);
}

.tb-dropdown.tb-show .tb-dropdown-menu {
  display: flex !important;
}

/* Dropup adjustment when near bottom viewport */
.tb-dropdown.tb-dropup .tb-dropdown-menu {
  top: auto !important;
  bottom: calc(100% + 4px) !important;
}

.tb-dropdown-item-wrapper,
.tb-action-form {
  display: block;
  width: 100%;
  margin: 0;
  padding: 0;
  list-style: none;
}

.tb-dropdown-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
  padding: 0.4rem 0.75rem;
  font-size: 0.875rem;
  color: var(--tb-text);
  text-decoration: none;
  background: transparent;
  border: none;
  cursor: pointer;
  text-align: left;
  box-sizing: border-box;
}

.tb-dropdown-item:hover {
  background-color: var(--tb-hover-bg);
  color: var(--tb-active-text);
}

.tb-dropdown-item.text-danger {
  color: var(--tb-danger);
}

.tb-dropdown-item.text-danger:hover {
  background-color: var(--tb-danger-hover-bg);
  color: var(--tb-danger-hover-text);
}

.tb-action-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.tb-action-icon svg {
  width: 16px;
  height: 16px;
}
