/* Mobile-first enhancements — loaded after layout/components/pages */

:root {
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --bottom-nav-height: 0px;
  --touch-min: 44px;
}

.app-shell {
  min-height: 100dvh;
  width: 100%;
}

.main-content {
  min-height: 100dvh;
  padding-bottom: var(--bottom-nav-height);
  width: 100%;
  min-width: 0;
}

.top-header {
  padding-top: var(--safe-top);
  height: calc(var(--header-height) + var(--safe-top));
}

/* Dual list layout: desktop table / mobile cards */
.dt-mobile {
  display: none;
}

.dt-card {
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  background: var(--bg-card);
  margin-bottom: 0.75rem;
  padding: 0.15rem 0;
  overflow: hidden;
}

.dt-card.is-clickable {
  cursor: pointer;
}

.dt-card-row {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 0.75rem;
  padding: 0.55rem 0.85rem;
  border-bottom: 1px solid var(--border-color);
}

.dt-card-row:last-child {
  border-bottom: none;
}

.dt-card-row-bare {
  justify-content: flex-start;
}

.dt-card-label {
  font-size: 0.78rem;
  color: var(--text-muted);
  flex-shrink: 0;
  max-width: 36%;
  padding-top: 0.15rem;
}

.dt-card-value {
  flex: 1;
  min-width: 0;
  text-align: left;
  word-break: break-word;
}

.dt-card-value .actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  justify-content: flex-end;
}

.dt-card-value input[type="checkbox"] {
  width: 22px;
  height: 22px;
  min-height: 22px;
}

/* Sidebar backdrop */
.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  z-index: 90;
  border: none;
  padding: 0;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.sidebar-backdrop.is-visible {
  display: block;
}

body.sidebar-open {
  overflow: hidden;
  touch-action: none;
}

/* Bottom navigation */
.bottom-nav {
  display: none;
  position: fixed;
  right: 0;
  left: 0;
  bottom: 0;
  z-index: 80;
  background: var(--bg-card);
  border-top: 1px solid var(--border-color);
  padding: 0.25rem 0.35rem calc(0.25rem + var(--safe-bottom));
  grid-template-columns: repeat(5, 1fr);
  gap: 0.15rem;
}

.bottom-nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.15rem;
  min-height: var(--touch-min);
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-size: 0.68rem;
  padding: 0.35rem 0.15rem;
  border-radius: var(--radius-md);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.bottom-nav-item .bn-icon {
  font-size: 1.15rem;
  line-height: 1;
}

.bottom-nav-item.active {
  color: var(--color-primary);
  background: var(--color-primary-light);
}

.bottom-nav-item.bn-primary {
  color: white;
  background: var(--color-primary);
  border-radius: 999px;
  margin: 0 0.15rem;
  min-height: 48px;
}

.bottom-nav-item.bn-primary.active {
  color: white;
  background: var(--color-primary-dark);
}

/* Invoice mobile sticky bar */
.invoice-mobile-bar {
  display: none;
  position: fixed;
  right: 0;
  left: 0;
  bottom: var(--bottom-nav-height);
  z-index: 70;
  background: var(--bg-card);
  border-top: 1px solid var(--border-color);
  padding: 0.65rem 1rem calc(0.65rem + var(--safe-bottom));
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.invoice-mobile-bar-total {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
  min-width: 0;
}

.invoice-mobile-bar-total span {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.invoice-mobile-bar-total strong {
  font-size: 1.05rem;
  color: var(--color-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.invoice-mobile-bar .btn {
  flex-shrink: 0;
  min-height: var(--touch-min);
  padding-inline: 1.25rem;
}

body.invoice-editing .invoice-mobile-bar {
  display: flex;
}

body.invoice-editing {
  --invoice-bar-height: 0px;
}

body.invoice-editing:has(.invoice-mobile-bar) {
  --invoice-bar-height: 72px;
}

body.invoice-editing .main-content {
  padding-bottom: calc(var(--bottom-nav-height) + var(--invoice-bar-height, 0px));
}

/* Invoice items */
.invoice-items-mobile {
  display: none;
}

.invoice-item-card {
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 0.85rem;
  margin-bottom: 0.75rem;
  background: var(--bg-card);
}

.invoice-item-card-head {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
  margin-bottom: 0.65rem;
}

.invoice-item-card-head .invoice-zone-badge {
  flex-shrink: 0;
  margin-top: 0.35rem;
}

.invoice-item-card-head input[data-field="title"] {
  flex: 1;
  min-width: 0;
}

.invoice-item-card-row {
  display: grid;
  grid-template-columns: 1fr 1.4fr;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}

.invoice-item-card-row label {
  display: block;
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-bottom: 0.2rem;
}

.invoice-item-card-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding-top: 0.35rem;
  border-top: 1px solid var(--border-color);
  font-size: 0.9rem;
}

.invoice-item-card-foot .btn {
  min-width: var(--touch-min);
  min-height: var(--touch-min);
}

/* Schematic collapse */
.vs-collapse {
  border: none;
}

.vs-collapse > summary {
  cursor: pointer;
  list-style: none;
  font-weight: 600;
  padding: 0.35rem 0;
  color: var(--text-primary);
  user-select: none;
}

.vs-collapse > summary::-webkit-details-marker {
  display: none;
}

.vs-collapse > summary::after {
  content: ' ▼';
  font-size: 0.7rem;
  color: var(--text-muted);
}

.vs-collapse[open] > summary::after {
  content: ' ▲';
}

.vs-collapse-body {
  margin-top: 0.5rem;
}

/* Phone + tablet portrait */
@media (max-width: 900px) {
  :root {
    --bottom-nav-height: calc(56px + env(safe-area-inset-bottom, 0px));
    --header-height: 56px;
  }

  html, body {
    overflow-x: hidden;
    max-width: 100%;
  }

  #app,
  .app-shell,
  .main-content,
  .page-container,
  .card,
  .table-wrapper {
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
    box-sizing: border-box;
  }

  .main-content {
    margin-right: 0 !important;
  }

  .sidebar {
    transform: translateX(100%);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .menu-toggle {
    display: flex !important;
  }

  .bottom-nav {
    display: grid;
  }

  .dt-desktop {
    display: none !important;
  }

  .dt-mobile {
    display: block !important;
  }

  .invoice-items-desktop {
    display: none !important;
  }

  .invoice-items-mobile {
    display: block !important;
  }

  .page-container {
    padding: 0.75rem;
  }

  .card-body {
    padding: 0.85rem;
  }

  .card-header {
    padding: 0.75rem 0.85rem;
  }

  .page-header {
    margin-bottom: 0.85rem;
    gap: 0.65rem;
    flex-direction: column;
    align-items: stretch;
  }

  .page-header h2 {
    font-size: 1.15rem;
  }

  .page-actions {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  .page-actions .btn {
    flex: 1 1 calc(50% - 0.5rem);
    justify-content: center;
    min-height: var(--touch-min);
  }

  .page-actions .btn-ghost {
    display: none;
  }

  body.invoice-editing .page-actions .btn-secondary,
  body.invoice-editing .page-actions .btn-ghost[onclick*="preview"],
  body.invoice-editing .page-actions .btn-ghost[onclick*="print"],
  body.invoice-editing .page-actions .btn-ghost.is-mobile-keep {
    display: inline-flex;
  }

  .search-bar {
    display: flex;
    flex-direction: column !important;
    align-items: stretch;
    gap: 0.5rem;
  }

  .search-bar input,
  .search-bar select,
  .search-bar .search-select-wrapper,
  .search-bar .search-select-input {
    max-width: none !important;
    width: 100% !important;
  }

  .search-trigger {
    min-width: 0;
    width: 44px;
    height: 44px;
    padding: 0;
    justify-content: center;
  }

  .search-trigger span,
  .search-trigger kbd {
    display: none;
  }

  .search-trigger::before {
    content: '⌕';
    font-size: 1.2rem;
    line-height: 1;
  }

  .menu-toggle,
  .theme-toggle {
    width: var(--touch-min);
    height: var(--touch-min);
    min-width: var(--touch-min);
    min-height: var(--touch-min);
  }

  .btn {
    min-height: var(--touch-min);
  }

  .btn-sm {
    min-height: 40px;
    min-width: 40px;
    padding: 0.45rem 0.85rem;
    font-size: 0.88rem;
  }

  .btn-icon,
  .modal-close {
    width: var(--touch-min);
    height: var(--touch-min);
  }

  .nav-item {
    min-height: var(--touch-min);
    padding: 0.85rem 1rem;
  }

  input, select, textarea,
  .search-select-input {
    min-height: var(--touch-min);
    font-size: 16px;
    padding: 0.65rem 0.75rem;
  }

  textarea {
    min-height: 88px;
  }

  .toast-container {
    top: calc(0.75rem + var(--safe-top));
    left: 0.5rem;
    right: 0.5rem;
    align-items: stretch;
  }

  .toast {
    min-width: 0;
    width: 100%;
    max-width: 100%;
  }

  .customer-profile-header {
    flex-direction: column;
    align-items: stretch;
    gap: 1rem;
  }

  .plate-input {
    flex-wrap: wrap;
    justify-content: center;
    gap: 0.5rem;
  }

  .plate-input input,
  .plate-input select {
    min-height: var(--touch-min);
    font-size: 16px;
  }

  .plate-input .plate-part1 { width: 64px; }
  .plate-input .plate-letter { width: 72px; min-width: 72px; }
  .plate-input .plate-part2 { width: 80px; }
  .plate-input .plate-iran { width: 64px; }

  .invoice-summary {
    position: static;
  }

  .invoice-desktop-save {
    display: none;
  }

  .add-item-row .btn {
    flex: 1 1 calc(50% - 0.5rem);
    justify-content: center;
  }

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

  .date-field-wrap {
    display: flex;
    gap: 0.5rem;
    align-items: stretch;
  }

  .date-field-wrap input {
    flex: 1;
  }

  .date-field-wrap .btn {
    flex-shrink: 0;
    white-space: nowrap;
  }

  .sidebar {
    padding-top: var(--safe-top);
    padding-bottom: var(--safe-bottom);
  }

  .vs-biw-frame {
    max-height: 160px;
  }

  .grid-2, .grid-3, .grid-4 {
    grid-template-columns: 1fr !important;
  }

  .selection-bar .page-actions {
    width: 100%;
  }

  .selection-bar .page-actions .btn {
    flex: 1 1 100%;
  }

  /* Invoice preview modal */
  .modal-overlay {
    padding: 0.5rem;
    align-items: flex-end;
  }

  .modal,
  .modal.modal-lg,
  .modal.modal-xl {
    max-width: 100% !important;
    width: 100% !important;
    max-height: 92dvh;
    border-radius: 14px 14px 0 0;
    margin: 0;
  }

  .modal-body {
    padding: 0.75rem;
    overflow-x: hidden;
  }
}

@media (min-width: 901px) {
  .bottom-nav,
  .sidebar-backdrop,
  .invoice-mobile-bar {
    display: none !important;
  }

  body.invoice-editing .main-content {
    padding-bottom: 0;
  }

  .vs-collapse > summary {
    pointer-events: none;
  }

  .vs-collapse > summary::after {
    display: none;
  }

  .dt-mobile {
    display: none !important;
  }

  .invoice-items-mobile {
    display: none !important;
  }
}

@media (max-width: 480px) {
  .breadcrumb span:first-child,
  .breadcrumb span:nth-child(2) {
    display: none;
  }

  .report-filters {
    flex-direction: column;
    align-items: stretch;
  }

  .page-actions .btn {
    flex: 1 1 100%;
  }
}

[data-theme="dark"] .bottom-nav-item.active {
  background: rgba(196, 30, 58, 0.22);
}

[data-theme="dark"] .bottom-nav-item.bn-primary {
  background: var(--color-primary);
  color: #fff;
}
