/* ============================================================
   BAR KEEP
   Modern CSS optimizations: color-scheme, accent-color,
   scrollbar-gutter, scroll-margin-top, text-underline-offset,
   text-wrap: balance/pretty, aspect-ratio, logical properties,
   native nesting, color-mix(), inline-flex buttons,
   outline-offset focus-visible, WCAG 2.5.5 touch targets
   ============================================================ */

:root {
  --border-radius: 5px;
  --border-radius-lg: 8px;
  --transition-speed: 0.15s ease-in-out;
  color-scheme: dark light;
}

html {
  scrollbar-gutter: stable;
}

/* ── Dark Theme ─────────────────────────────────────────── */
[data-bs-theme="dark"] {
  --bs-body-bg: #1a1a1a;
  --bs-body-color: #e0e0e0;
  --bs-emphasis-color: #f0f0f0;
  --bs-secondary-bg: #2c2c2c;
  --bs-tertiary-bg: #3c3c3c;
  --bs-border-color: #444;
  --bs-link-color: #6ea8fe;
  --bs-link-hover-color: #9ec5fe;
  --navbar-bg: #2c2c2c;
  --card-bg: #2c2c2c;
  --metadata-bg: rgba(255, 255, 255, 0.05);
  --stripe-bg: rgba(255, 255, 255, 0.02);
  --card-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  --text-muted: #9a9a9a;
  --dropdown-bg: var(--bs-secondary-bg);
  --dropdown-hover-bg: var(--metadata-bg);
  --toggler-icon: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'%3e%3cpath stroke='rgba%28255, 255, 255, 0.85%29' stroke-linecap='round' stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/%3e%3c/svg%3e");
  --bs-navbar-color: rgba(255, 255, 255, 0.85);
  --bs-navbar-hover-color: rgba(255, 255, 255, 1);
  --bs-navbar-disabled-color: rgba(255, 255, 255, 0.5);
  --bs-navbar-active-color: #fff;
  --bs-navbar-brand-color: #fff;
  --bs-navbar-brand-hover-color: #fff;
  --bs-navbar-toggler-border-color: rgba(255, 255, 255, 0.1);
  color-scheme: dark;
  accent-color: var(--bs-link-color);
}

/* ── Light Theme ────────────────────────────────────────── */
[data-bs-theme="light"] {
  --bs-body-bg: #e8e8e8;
  --bs-body-color: #1e2832;
  --bs-emphasis-color: #0d1117;
  --bs-secondary-bg: #dcdcdc;
  --bs-tertiary-bg: #d0d0d0;
  --bs-border-color: #b8b8b8;
  --bs-link-color: #0056d2;
  --bs-link-hover-color: #0045a8;
  --navbar-bg: #dedede;
  --card-bg: #efefef;
  --metadata-bg: rgba(0, 0, 0, 0.08);
  --stripe-bg: rgba(0, 0, 0, 0.04);
  --card-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.08);
  --text-muted: #555f70;
  --dropdown-bg: #f5f5f5;
  --dropdown-hover-bg: #e0e0e0;
  --toggler-icon: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'%3e%3cpath stroke='rgba%280, 0, 0, 0.65%29' stroke-linecap='round' stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/%3e%3c/svg%3e");
  --bs-navbar-color: rgba(0, 0, 0, 0.65);
  --bs-navbar-hover-color: rgba(0, 0, 0, 0.8);
  --bs-navbar-disabled-color: rgba(0, 0, 0, 0.3);
  --bs-navbar-active-color: rgba(0, 0, 0, 0.9);
  --bs-navbar-brand-color: rgba(0, 0, 0, 0.9);
  --bs-navbar-brand-hover-color: rgba(0, 0, 0, 0.9);
  --bs-navbar-toggler-border-color: rgba(0, 0, 0, 0.1);
  color-scheme: light;
  accent-color: var(--bs-link-color);
}

/* ── Safe area ──────────────────────────────────────────── */
@supports (padding: max(0px)) {
  body {
    padding-top: env(safe-area-inset-top);
    padding-inline: env(safe-area-inset-left) env(safe-area-inset-right);
  }
}

/* ── Base ────────────────────────────────────────────────── */
body { overscroll-behavior-y: auto; }

h1, h2, h3, h4, h5, h6 { text-wrap: balance; }

p { text-wrap: pretty; }

[id] { scroll-margin-top: 1rem; }

main {
  overscroll-behavior-y: auto;
  -webkit-overflow-scrolling: touch;
}

.container-fluid { overscroll-behavior-y: auto; }

/* ── Flash messages ─────────────────────────────────────── */
.flash-messages:not(:has(.alert)) { display: none; }

/* ── Links ───────────────────────────────────────────────── */
a {
  color: var(--bs-link-color);

  &:hover { color: var(--bs-link-hover-color); }
}

a:not([class]) { text-underline-offset: 0.25em; }

.text-decoration-none { color: var(--bs-body-color) !important; }
.text-muted { color: var(--text-muted) !important; }

/* ── Collapse chevron ──────────────────────────────────── */
[data-bs-toggle="collapse"] {
  .bi-chevron-down { transition: transform 0.2s ease-in-out; }

  &[aria-expanded="true"] .bi-chevron-down { transform: rotate(180deg); }
  &:hover { opacity: 0.8; }
}

/* ── Buttons ─────────────────────────────────────────────── */
/* inline-flex centers content; min-height satisfies WCAG 2.5.5 touch targets */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 1.1;
  min-height: 44px;
  touch-action: manipulation;
  user-select: none;
  -webkit-tap-highlight-color: transparent;

  &:focus-visible {
    outline: 2px solid var(--bs-link-color);
    outline-offset: 2px;
    box-shadow: none;
  }
}

.btn-sm { min-height: unset; }

/* Touch/coarse pointer adjustments */
@media (hover: none) and (pointer: coarse) {
  .btn {
    &:focus,
    &:active:focus,
    &.active:focus,
    &.focus,
    &:active.focus,
    &.active.focus {
      outline: none;
      box-shadow: none;
    }

    &:focus-visible {
      outline: 2px solid var(--bs-link-color);
      outline-offset: 2px;
    }
  }
}

@media (max-width: 767px) {
  .btn-danger {
    background-color: #dc3545 !important;
    border-color: #dc3545 !important;
    color: white !important;
  }

  .btn-outline-danger {
    background-color: transparent !important;
    border-color: #dc3545 !important;
    color: #dc3545 !important;
  }
}

/* ── Navbar ─────────────────────────────────────────────── */
.navbar { background-color: var(--navbar-bg) !important; }

[data-bs-theme="light"] .navbar {
  border-block-end: 1px solid var(--bs-border-color);
}

.navbar-toggler-icon { background-image: var(--toggler-icon); }

/* ── Dropdown ──────────────────────────────────────────── */
.dropdown-menu {
  --bs-dropdown-bg: var(--dropdown-bg);
  --bs-dropdown-link-color: var(--bs-body-color);
  --bs-dropdown-link-hover-color: var(--bs-emphasis-color);
  --bs-dropdown-link-hover-bg: var(--dropdown-hover-bg);
  --bs-dropdown-divider-bg: var(--bs-border-color);

  form { margin: 0; }

  form button.dropdown-item {
    width: 100%;
    text-align: left;
    border: none;
    background: none;
    padding: 0.25rem 1rem;
    color: var(--bs-dropdown-link-color);
    transition: color 0.15s ease-in-out, background-color 0.15s ease-in-out;

    &:hover {
      background-color: var(--bs-dropdown-link-hover-bg);
      color: var(--bs-dropdown-link-hover-color);
    }
  }
}

[data-bs-theme="light"] .dropdown-menu {
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.12), 0 1px 3px rgba(0, 0, 0, 0.08);
}

/* ── Cards ───────────────────────────────────────────────── */
.card, .mobile-item-card {
  background-color: var(--card-bg);
  border-color: var(--bs-border-color);
}

[data-bs-theme="light"] .card,
[data-bs-theme="light"] .mobile-item-card {
  box-shadow: var(--card-shadow);
  border: 1px solid rgba(0, 0, 0, 0.12);
}

[data-bs-theme="dark"] .card { box-shadow: var(--card-shadow); }

/* ── Theme toggle ──────────────────────────────────────── */
.theme-toggle {
  background: none;
  border: none;
  color: var(--bs-body-color);
  cursor: pointer;
  padding: 0.375rem 0.75rem;
  border-radius: 0.25rem;
  transition: background-color var(--transition-speed);

  &:hover { background-color: var(--metadata-bg); }
}

/* ── Responsive button text ────────────────────────────── */
.responsive-btn {
  .btn-text-mobile { display: none; }
  .btn-text-desktop { display: inline; }

  @media (max-width: 767px) {
    font-size: 14px;
    padding: 6px 12px;
    .btn-text-mobile { display: inline; }
    .btn-text-desktop { display: none; }
  }
}

/* ── Images ────────────────────────────────────────────── */
img { max-width: 100%; height: auto; }

.item-thumbnail {
  width: 120px;
  aspect-ratio: 1;
  object-fit: contain;
  border-radius: var(--border-radius);
  background-color: transparent;

  span { font-size: 36px; }
}

.item-image {
  max-width: 200px;
  max-height: 200px;
  object-fit: contain;
  border-radius: 10px;
  background-color: transparent;
}

/* ── Metadata / filters ─────────────────────────────────── */
.metadata-item, .category-filters {
  background-color: var(--metadata-bg);
  padding: 10px;
  border-radius: var(--border-radius);
}

.metadata-item { margin-block-end: 10px; }

.category-filters {
  border: 1px solid var(--bs-border-color);
  max-height: 200px;
  overflow-y: auto;
}

/* ── Quantity controls ─────────────────────────────────── */
.quantity-controls, .mobile-quantity-controls {
  display: flex;
  align-items: center;
  gap: 10px;

  form { margin: 0; display: flex; }
}

.quantity-input {
  width: 4ch;
  padding: 2px 4px;
  border: 1px solid transparent;
  border-radius: var(--border-radius);
  background-color: transparent;
  color: inherit;
  font: inherit;
  font-variant-numeric: tabular-nums;
  text-align: center;
  appearance: textfield;

  &::-webkit-outer-spin-button,
  &::-webkit-inner-spin-button { appearance: none; margin: 0; }

  &:hover { border-color: var(--bs-border-color); }

  &:focus {
    border-color: var(--bs-link-color);
    background-color: var(--bs-body-bg);
    outline: 2px solid var(--bs-link-color);
    outline-offset: 1px;
  }

  &:disabled { cursor: not-allowed; }
}

.quantity-value { font-variant-numeric: tabular-nums; }

/* ── Category picker ───────────────────────────────────── */
.category-suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  max-height: 8rem;
  overflow-y: auto;
}

.mobile-quantity-controls {
  gap: 10px;

  form { display: inline-block; margin: 0; }

  .btn {
    padding: 6px 12px;
    font-size: 16px;
    min-width: 44px;
    min-height: 44px;
  }
}

/* ── Stock status ───────────────────────────────────────── */
/* color-mix() replaces hardcoded rgba() */
.out-of-stock, .out-of-stock td {
  background-color: color-mix(in srgb, #dc3545 10%, transparent) !important;
}

.low-stock, .low-stock td {
  background-color: color-mix(in srgb, #ffc107 10%, transparent) !important;
}

/* ── Table ─────────────────────────────────────────────── */
.table {
  table-layout: auto;
  --bs-table-border-color: var(--bs-border-color);
}

[data-bs-theme="light"] .table { --bs-table-border-color: rgba(0, 0, 0, 0.12); }

.table-striped > tbody > tr:nth-of-type(odd) > * { background-color: var(--stripe-bg); }

[data-bs-theme="light"] .table-hover > tbody > tr:hover > * { background-color: rgba(0, 0, 0, 0.04); }

.table th:last-child, .table td:last-child {
  white-space: nowrap;
  min-width: 120px;
}

.table td:last-child .d-flex { flex-wrap: nowrap !important; }
.table td:last-child .btn { white-space: nowrap; }

/* ── Mobile item card ──────────────────────────────────── */
.mobile-item-card {
  border-radius: var(--border-radius-lg);
  padding: 12px;
  margin-block-end: 12px;
  height: 100%;
  display: flex;
  flex-direction: column;
}

/* ── Sticky sidebar ────────────────────────────────────── */
@media (min-width: 768px) {
  .col-md-3 .card {
    position: sticky;
    top: 20px;
    z-index: 100;
  }
}

/* ── Tablet ─────────────────────────────────────────────── */
@media (max-width: 991px) and (min-width: 768px) {
  .item-image { max-width: 150px !important; max-height: 150px !important; }

  .table td { padding: 0.5rem; vertical-align: middle; }
}

/* ── Mobile general ─────────────────────────────────────── */
@media (max-width: 767px) {
  .container-fluid { padding-inline: 10px !important; }

  h1 { font-size: 24px; }
  .card-body h5 { font-size: 18px; }

  .col-lg-9 {
    h1 { font-size: 28px; margin-block-end: 1rem; }
    .d-flex { flex-wrap: wrap; gap: 0.5rem; }
    .btn { font-size: 14px; padding: 8px 16px; }
  }

  .category-filters { max-height: 150px !important; }

  .item-image {
    max-width: 100%;
    max-height: 200px;
    margin: 0 auto;
    display: block;
  }

  .quantity-controls {
    justify-content: center;

    .btn { padding: 8px 16px; font-size: 16px; min-width: 44px; }
  }
}

@media (max-width: 576px) {
  .row > * { padding-inline: 10px; }
  [class*="col-"] { width: 100%; }
}

@media (max-width: 375px) {
  .mobile-item-image { width: 60px; height: 60px; }
  .mobile-item-name { font-size: 14px; }
}

/* ── Item list ──────────────────────────────────────────── */
turbo-frame#items {
  display: block;
  container: items / inline-size;
}

.items-header { display: none; }

.items-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
  --items-columns: 76px minmax(150px, 1.6fr) minmax(110px, 1fr) 90px 160px 200px;

  &.guest-view {
    --items-columns: 64px minmax(120px, 1.4fr) 90px minmax(96px, 1fr) 72px 160px 200px;
  }
}

.item-row {
  display: grid;
  grid-template-areas:
    "image name name"
    "image categories categories"
    "type type type"
    "quantity quantity actions";
  grid-template-columns: 80px minmax(0, 1fr) auto;
  gap: 8px;
  padding: 16px;
  background-color: var(--card-bg);
  border: 1px solid var(--bs-border-color);
  border-radius: var(--border-radius-lg);

  &.out-of-stock { background-color: color-mix(in srgb, #dc3545 12%, var(--card-bg)); }
  &.low-stock { background-color: color-mix(in srgb, #ffc107 12%, var(--card-bg)); }
}

.guest-view .item-row {
  grid-template-areas:
    "image name name"
    "image categories categories"
    "owner owner owner"
    "type type type"
    "quantity quantity actions";
}

.item-image-cell {
  grid-area: image;
  align-self: start;
  display: flex;

  .item-thumbnail { width: 80px; aspect-ratio: 1; }
}

.item-name-cell {
  grid-area: name;
  min-width: 0;
  font-size: 18px;
  font-weight: 600;
  align-self: center;
  line-height: 1.2;

  a {
    display: block;
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}

.item-categories-cell {
  grid-area: categories;
  min-width: 0;
  align-self: center;
  display: flex;
  flex-wrap: nowrap;
  overflow: hidden;
  gap: 4px;
  min-height: 1.375rem;
  mask-image: linear-gradient(to right, #000 calc(100% - 1.25rem), transparent);

  .badge { flex: 0 0 auto; font-size: 11px; padding: 3px 8px; }
}

.item-type-cell {
  grid-area: type;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 14px;
  color: var(--text-muted);
  padding-block-start: 8px;
  border-block-start: 1px solid var(--bs-border-color);
}

.item-owner-cell {
  grid-area: owner;
  font-size: 14px;
  color: var(--text-muted);
  padding-block-start: 8px;
  border-block-start: 1px solid var(--bs-border-color);
}

.item-quantity-cell {
  grid-area: quantity;
  min-width: 0;
  display: flex;
  align-items: center;
  padding-block-start: 8px;

  .quantity-controls { gap: 8px; }
  .btn { padding: 6px 10px; font-size: 16px; min-width: 40px; min-height: 40px; }
}

.item-actions-cell {
  grid-area: actions;
  min-width: 0;
  justify-self: end;
  display: flex;
  align-items: center;
  padding-block-start: 8px;

  .action-buttons { display: flex; gap: 6px; }
  .btn { padding: 8px 12px; font-size: 14px; min-height: 40px; }
}

/* ── Narrow phones, stack the card footer ───────────────── */
@container items (max-width: 400px) {
  .item-row {
    grid-template-areas:
      "image name name"
      "image categories categories"
      "type type type"
      "quantity quantity quantity"
      "actions actions actions";
  }

  .guest-view .item-row {
    grid-template-areas:
      "image name name"
      "image categories categories"
      "owner owner owner"
      "type type type"
      "quantity quantity quantity"
      "actions actions actions";
  }

  .item-actions-cell {
    justify-self: stretch;

    .action-buttons { width: 100%; }
    .btn { flex: 1 1 0; min-width: 0; padding-inline: 4px; }
  }
}

/* ── Card grid, wide containers ─────────────────────────── */
@container items (min-width: 720px) {
  :root[data-items-view="cards"] {
    .items-list {
      display: grid;
      grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
      align-items: start;
      gap: 1rem;
    }

    .item-row {
      grid-template-areas:
        "image"
        "name"
        "categories"
        "type"
        "quantity"
        "actions";
      grid-template-columns: minmax(0, 1fr);
    }

    .guest-view .item-row {
      grid-template-areas:
        "image"
        "name"
        "categories"
        "owner"
        "type"
        "quantity"
        "actions";
    }

    .item-image-cell {
      justify-content: center;

      .item-thumbnail { width: 100%; max-width: 11rem; aspect-ratio: 1; }
    }

    .item-quantity-cell { justify-content: center; }

    .item-actions-cell {
      justify-self: stretch;

      .action-buttons { width: 100%; }
      .btn { flex: 1 1 0; min-width: 0; padding-inline: 4px; }
    }
  }
}

/* ── Row list, wide containers ──────────────────────────── */
@container items (min-width: 820px) {
  :root[data-items-view="rows"] {
    .items-list { display: block; }

    .items-header {
      display: grid;
      grid-template-columns: var(--items-columns);
      border-block-end: 2px solid var(--bs-border-color);
      font-weight: 600;

      > div { padding: 0.75rem; }
    }

    .item-row {
      grid-template-columns: var(--items-columns);
      gap: 0;
      padding: 0;
      border: none;
      border-radius: 0;
      border-block-end: 1px solid var(--bs-border-color);

      &:last-child { border-block-end: none; }
      &:nth-child(odd) { background-color: var(--bs-body-bg); }
      &:nth-child(even) { background-color: var(--stripe-bg); }
      &.out-of-stock { background-color: color-mix(in srgb, #dc3545 10%, transparent); }
      &.low-stock { background-color: color-mix(in srgb, #ffc107 10%, transparent); }

      > div {
        grid-area: auto;
        align-self: auto;
        justify-self: auto;
        padding: 0.75rem;
        display: flex;
        align-items: center;
        background-color: transparent;
        border-block-start: none;
        font-size: inherit;
        font-weight: inherit;
        color: inherit;
      }
    }

    .item-image-cell {
      justify-content: center;

      .item-thumbnail { width: 76px; aspect-ratio: 1; }
    }


    .item-quantity-cell {
      justify-content: flex-start;

      .quantity-controls { gap: 6px; }
    }

    .item-actions-cell {
      justify-self: stretch;

      .action-buttons { gap: 5px; white-space: nowrap; }
      .btn { padding: 6px 10px; font-size: 13px; min-height: 40px; }
    }
  }
}

/* ── Accent buttons ─────────────────────────────────────── */
.btn-accent,
.view-toggle .btn.active {
  background-color: var(--bs-link-color);
  border-color: var(--bs-link-color);
  color: var(--bs-body-bg);
  font-weight: 500;
}

.btn-accent {
  &:hover,
  &:focus-visible {
    background-color: var(--bs-link-hover-color);
    border-color: var(--bs-link-hover-color);
    color: var(--bs-body-bg);
  }
}

.btn.disabled {
  background-color: transparent;
  border-color: var(--bs-border-color);
  border-style: dashed;
  color: var(--text-muted);
  opacity: 1;
}

/* ── View toggle ────────────────────────────────────────── */

@media (max-width: 991px) {
  .view-toggle { display: none; }
}
