/* Responsive Design - Mobile, Tablet, Desktop Optimization */

/* ============================================
   MOBILE NAVIGATION - Slide-in Sidebar
   ============================================ */

/* The current shell uses a 72px icon rail at desktop and tablet widths.
   Keep the legacy sidebar variable from creating an artificial 210px gutter. */
:root { --sidebar: 72px; }

.skip-link {
  position: fixed;
  top: 8px;
  left: 8px;
  z-index: 100;
  transform: translateY(-160%);
  padding: 10px 14px;
  border-radius: 6px;
  color: #fff;
  background: var(--navy);
  font-weight: 700;
  text-decoration: none;
  transition: transform .2s ease;
}

.skip-link:focus {
  transform: translateY(0);
}

.sidebar-icon.mobile-open {
  transform: translateX(0);
  box-shadow: 2px 0 20px rgba(0, 0, 0, 0.3);
}

.sidebar-icon.open {
  transform: translateX(0);
  box-shadow: 2px 0 20px rgba(0, 0, 0, 0.3);
}

.sub-nav.mobile-open {
  transform: translateX(0);
  box-shadow: 2px 0 15px rgba(0, 0, 0, 0.2);
}

.mobile-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 14;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.mobile-overlay.show {
  opacity: 1;
  pointer-events: auto;
}

.mobile-overlay {
  pointer-events: none;
}

body.mobile-nav-open {
  overflow: hidden;
}

/* ============================================
   TABLET (768px - 1024px)
   ============================================ */

@media (max-width: 1024px) {
  /* Dashboard adjustments */
  .dashboard-grid {
    grid-template-columns: 1fr;
  }

  /* Stats grid - 2 columns */
  .stats {
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
  }

  /* Content padding */
  .content {
    padding: 24px 20px 40px;
  }

  /* Page header - stack on smaller tablets */
  .page-head {
    flex-wrap: wrap;
  }

  /* Form grids */
  .form-grid {
    grid-template-columns: 1fr;
  }

  /* Master data grid - 2 columns */
  .master-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  /* Modal sizing */
  .modal.large {
    width: min(95vw, 100%);
  }

  .modal.wide {
    width: min(90vw, 100%);
  }

  /* Table responsiveness */
  .filterbar {
    flex-wrap: wrap;
    gap: 8px;
  }

  .filterbar input {
    min-width: 140px;
  }

  /* Topbar adjustments */
  .topbar {
    padding: 0 20px;
  }

  .account-identity {
    display: none;
  }
}

/* ============================================
   MOBILE (max-width: 768px)
   ============================================ */

@media (max-width: 768px) {
  /* CRITICAL: Hide sidebar completely on mobile by default */
  .sidebar-icon {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    width: 72px;
    transform: translateX(-100%);
    transition: transform 0.3s ease;
    z-index: 25;
  }

  /* Remove all margins from main content */
  .main-icon,
  .main-content {
    margin-left: 0 !important;
    width: 100% !important;
  }

  .main-icon.with-subnav {
    margin-left: 0 !important;
  }

  /* Hide sub-nav by default on mobile */
  .sub-nav {
    position: fixed;
    transform: translateX(-100%);
    transition: transform 0.3s ease;
    left: var(--sidebar);
    width: min(240px, calc(100vw - var(--sidebar)));
    padding-top: 80px;
    z-index: 20;
  }

  /* Show mobile menu button */
  .mobile-menu {
    display: block;
  }

  /* Show overlay when menu open */
  .mobile-overlay {
    display: block;
  }

  /* Stats - 2 columns on mobile for better use of space */
  .stats {
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
    margin-bottom: 16px;
  }

  .stat {
    min-height: 100px;
    padding: 12px;
  }

  .stat .label {
    font-size: 10px;
    font-weight: 600;
  }

  .stat .value {
    font-size: 20px;
    margin-top: 6px;
  }

  .stat .trend {
    font-size: 9px;
    margin-top: 6px;
    line-height: 1.3;
  }

  .stat-glyph {
    font-size: 42px;
    right: 8px;
    bottom: -12px;
  }

  /* Master data grid - single column */
  .master-grid {
    grid-template-columns: 1fr;
  }

  /* Content padding */
  .content {
    padding: 12px 10px 32px;
  }

  /* Dashboard grid - full width on mobile */
  .dashboard-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  /* Panel adjustments for mobile */
  .panel-head {
    padding: 12px 14px;
    min-height: auto;
  }

  .panel-head h2 {
    font-size: 14px;
  }

  .panel-head p {
    font-size: 10px;
  }

  .panel-body {
    padding: 14px;
  }

  /* Topbar */
  .topbar {
    height: 56px;
    padding: 0 12px;
    z-index: 30;
  }

  .topbar h1 {
    font-size: 16px;
  }

  /* Breadcrumb - compact on mobile */
  .crumb {
    font-size: 11px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: calc(100vw - 160px);
  }

  /* Hide role switch on mobile */
  .role-switch {
    display: none;
  }

  /* Profile trigger - avatar only */
  .profile-trigger .account-identity {
    display: none;
  }

  /* Hide global search on mobile */
  .top-actions .search-mini {
    display: none;
  }

  /* Page header - compact mobile version */
  .page-head {
    margin-bottom: 14px;
  }

  .page-head h1 {
    font-size: 18px;
    line-height: 1.3;
  }

  .page-head p {
    font-size: 11px;
    margin-top: 3px;
  }

  .page-head .actions {
    margin-top: 10px;
  }

  .page-head .button {
    font-size: 13px;
  }

  /* Form grid - single column */
  .form-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  /* Buttons - full width in mobile contexts */
  .modal-foot .button {
    flex: 1;
    min-width: 0;
  }

  /* Tables - horizontal scroll */
  .table-wrap {
    border-radius: 0;
    margin: 0 -12px;
  }

  table {
    min-width: 700px;
    font-size: 12px;
  }

  th, td {
    padding: 10px 8px;
  }

  th {
    font-size: 8px;
  }

  .cell-sub {
    font-size: 10px;
  }

  /* Filterbar */
  .filterbar {
    padding: 10px 12px;
    flex-direction: column;
    align-items: stretch;
  }

  .filterbar input,
  .filterbar select,
  .filterbar .button {
    width: 100%;
    min-width: 0;
  }

  .search-mini {
    width: 100%;
  }

  /* Modals - full width with padding */
  .modal-backdrop {
    padding: 0;
  }

  .modal,
  .modal.small,
  .modal.wide,
  .modal.large {
    width: 100%;
    max-height: 100vh;
    border-radius: 0;
  }

  .modal-head {
    padding: 14px 16px;
  }

  .modal-head h2 {
    font-size: 16px;
  }

  .modal-body {
    padding: 16px;
  }

  .modal-foot {
    padding: 12px 16px;
    flex-wrap: wrap;
  }

  .crm-customer-search-row { align-items: stretch; }
  .crm-customer-search-row .btn { flex: 0 0 auto; }

  /* Toast notifications */
  .toast {
    right: 12px;
    bottom: 12px;
    left: 12px;
    max-width: none;
    font-size: 13px;
  }

  /* Panel adjustments */
  .panel-head {
    padding: 12px 14px;
    flex-wrap: wrap;
  }

  .panel-body {
    padding: 14px;
  }

  /* Activity timeline */
  .activity-row {
    grid-template-columns: 20px 1fr;
    gap: 8px;
    padding: 10px 0;
  }

  /* Login page - single column */
  .login-container {
    grid-template-columns: 1fr;
    grid-template-areas: "login";
  }

  .login-banner {
    display: none;
  }

  .login-card {
    padding: 24px 16px;
  }

  .login-brand h1 {
    font-size: 24px;
  }

  .login-hero h2 {
    font-size: 24px;
  }

  /* Form controls */
  .field-control {
    font-size: 16px; /* Prevent zoom on iOS */
  }

  .login-form .form-input {
    font-size: 16px; /* Prevent zoom on iOS */
  }
}

/* Keyboard and touch affordances shared by every viewport. */
button,
a,
input,
select,
textarea {
  -webkit-tap-highlight-color: transparent;
}

button:focus-visible,
a:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
  outline: 3px solid rgba(8, 127, 149, .42);
  outline-offset: 2px;
}

@media (max-width: 768px) {
  .topbar {
    gap: 10px;
  }

  .crumb {
    display: block;
    flex: 1 1 auto;
    min-width: 0;
    max-width: none;
  }

  .top-actions {
    flex: 0 0 auto;
    min-width: 0;
  }

  .role-switch {
    display: flex;
    align-items: center;
    gap: 4px;
    border: 0;
    padding: 0;
  }

  .profile-trigger {
    padding: 2px;
  }

  .profile-trigger .account-identity {
    display: none;
  }

  .profile-trigger .avatar {
    display: grid;
  }

  .page-head {
    align-items: flex-start;
    flex-direction: column;
    gap: 10px;
  }

  .page-head .actions {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
  }

  .page-head .actions .button,
  .page-head .actions .btn {
    flex: 1 1 auto;
    min-width: min(150px, 100%);
  }

  .panel-head {
    align-items: flex-start;
  }

  .panel-head > * {
    min-width: 0;
  }

  .panel-head .actions {
    flex-wrap: wrap;
  }

  .table-wrap {
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
  }

  .modal-backdrop {
    align-items: start;
    overflow-y: auto;
  }

  .modal {
    margin: auto 0;
    width: min(100%, 680px);
  }
}

@media (max-width: 480px) {
  .topbar {
    padding-inline: 10px;
  }

  .mobile-menu {
    flex: 0 0 40px;
  }

  .crumb {
    font-size: 11px;
  }

  .top-actions {
    gap: 4px;
  }

  .sub-nav {
    width: calc(100vw - var(--sidebar));
  }

  .page-head .actions .button,
  .page-head .actions .btn {
    width: 100%;
  }

  .filterbar-multi .filter-actions,
  .bulk-actions {
    display: grid;
    grid-template-columns: 1fr;
  }

  .filterbar-multi .filter-actions .button,
  .bulk-actions .button {
    width: 100%;
  }
}

/* ============================================
   SMALL MOBILE (max-width: 480px)
   ============================================ */

@media (max-width: 480px) {
  /* Typography */
  h1 {
    font-size: 17px;
  }

  h2 {
    font-size: 13px;
  }

  /* Content */
  .content {
    padding: 10px 8px 24px;
  }

  /* Topbar */
  .topbar {
    height: 52px;
  }

  /* Breadcrumb even more compact */
  .crumb {
    font-size: 10px;
    max-width: calc(100vw - 140px);
  }

  /* Stats - keep 2 columns but more compact */
  .stats {
    gap: 6px;
    margin-bottom: 12px;
  }

  .stat {
    min-height: 90px;
    padding: 10px;
  }

  .stat .label {
    font-size: 9px;
  }

  .stat .value {
    font-size: 18px;
    margin-top: 5px;
  }

  .stat .trend {
    font-size: 8px;
    margin-top: 4px;
  }

  .stat-glyph {
    font-size: 36px;
    right: 6px;
    bottom: -10px;
  }

  /* Panel */
  .panel-head {
    padding: 10px 12px;
  }

  .panel-head h2 {
    font-size: 13px;
  }

  .panel-head p {
    font-size: 9px;
  }

  .panel-body {
    padding: 12px;
  }

  /* Buttons - smaller padding */
  .button {
    padding: 8px 10px;
    font-size: 13px;
    min-height: 36px;
  }

  .button.small {
    padding: 4px 7px;
    font-size: 11px;
    min-height: 28px;
  }

  /* Tables - more compact */
  table {
    min-width: 600px;
    font-size: 11px;
  }

  th, td {
    padding: 8px 6px;
  }

  .cell-title {
    font-size: 12px;
  }

  .cell-sub {
    font-size: 9px;
  }

  /* Status badges - smaller */
  .status {
    padding: 3px 6px;
    font-size: 8px;
  }

  /* Modals */
  .modal-head h2 {
    font-size: 15px;
  }

  .modal-body {
    padding: 12px;
  }

  /* Form grid */
  .form-grid {
    gap: 10px;
  }

  .field {
    gap: 4px;
  }

  /* Toast */
  .toast {
    padding: 10px 12px;
    font-size: 12px;
  }

  /* Sub-nav */
  .sub-nav {
    width: 200px;
  }

  .sub-nav-item {
    font-size: 12px;
    height: 36px;
  }
}

/* ============================================
   LANDSCAPE MOBILE (max-height: 500px)
   ============================================ */

@media (max-height: 500px) and (orientation: landscape) {
  .topbar {
    height: 50px;
  }

  .content {
    padding: 12px 16px 24px;
  }

  .modal {
    max-height: 100vh;
  }

  .sidebar-icon {
    padding: 12px 0 10px;
  }

  .nav-icon-btn {
    width: 40px;
    height: 40px;
  }

  .nav-icon-img {
    width: 20px;
    height: 20px;
  }
}

/* ============================================
   LARGE DESKTOP (min-width: 1440px)
   ============================================ */

@media (min-width: 1440px) {
  .content {
    max-width: 1800px;
  }

  .stats {
    grid-template-columns: repeat(4, 1fr);
    gap: 18px;
  }

  .dashboard-grid {
    grid-template-columns: 1.75fr 1fr;
    gap: 24px;
  }

  .master-grid {
    grid-template-columns: repeat(4, 1fr);
  }

  /* Larger modals on big screens */
  .modal.large {
    width: min(1300px, 100%);
  }

  .modal.wide {
    width: min(1000px, 100%);
  }
}

/* ============================================
   EXTRA LARGE DESKTOP (min-width: 1920px)
   ============================================ */

@media (min-width: 1920px) {
  .content {
    max-width: 2000px;
    padding: 32px 48px 60px;
  }

  .stats {
    gap: 20px;
  }

  .stat {
    min-height: 140px;
    padding: 20px;
  }

  .stat .value {
    font-size: 32px;
  }
}

/* ============================================
   TOUCH DEVICE OPTIMIZATIONS
   ============================================ */

@media (hover: none) and (pointer: coarse) {
  /* Larger touch targets */
  .button {
    min-height: 44px;
    padding: 10px 16px;
  }

  .nav-icon-btn {
    width: 52px;
    height: 52px;
  }

  .sub-nav-item {
    height: 44px;
  }

  /* Remove hover effects on touch devices */
  .button:hover,
  .nav-icon-btn:hover,
  .sub-nav-item:hover,
  tbody tr:hover {
    background: initial;
    transform: none;
  }

  /* Disable tooltips on touch */
  .nav-icon-btn::after {
    display: none;
  }
}

/* ============================================
   PRINT STYLES
   ============================================ */

@media print {
  .sidebar-icon,
  .sub-nav,
  .topbar,
  .mobile-menu,
  .button,
  .actions,
  .filterbar {
    display: none !important;
  }

  .main-icon {
    margin-left: 0 !important;
  }

  .content {
    padding: 0;
  }

  .table-wrap {
    border: none;
    box-shadow: none;
  }

  table {
    min-width: 0;
  }

  .panel,
  .stat {
    box-shadow: none;
    break-inside: avoid;
  }
}

/* ============================================
   ACCESSIBILITY - REDUCED MOTION
   ============================================ */

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }

  .sidebar-icon,
  .sub-nav,
  .modal {
    transition: none;
  }
}

/* ============================================
   HIGH CONTRAST MODE
   ============================================ */

@media (prefers-contrast: high) {
  .button {
    border-width: 2px;
  }

  .status {
    border: 1px solid currentColor;
  }

  .field-control:focus {
    outline: 2px solid var(--brand);
    outline-offset: 2px;
  }
}

/* On mobile the sub-navigation belongs to the page flow. It stays below the
   topbar and can scroll horizontally instead of covering the content. */
@media (max-width: 768px) {
  .sub-nav {
    position: sticky;
    top: 56px;
    left: auto;
    bottom: auto;
    width: 100%;
    max-width: none;
    height: auto;
    min-height: 56px;
    transform: none !important;
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 6px;
    overflow-x: auto;
    overflow-y: hidden;
    padding: 8px 10px;
    border-right: 0;
    border-bottom: 1px solid var(--line);
    box-shadow: 0 2px 8px rgba(16, 42, 67, .06);
    z-index: 9;
    scrollbar-width: thin;
  }

  .sub-nav-item {
    flex: 0 0 auto;
    min-width: max-content;
    height: 40px;
    padding: 0 14px;
    border-radius: 8px;
  }
}

@media (max-width: 480px) {
  .sub-nav {
    top: 52px;
    min-height: 52px;
    padding-inline: 8px;
  }
}
