/* ============================================================
   KingBot Responsive Design System
   Mobile-first approach with breakpoints:
   - xs: 0-320px (small phones)
   - sm: 321-375px (phones)
   - md: 376-414px (large phones)
   - lg: 415-768px (tablets)
   - xl: 769-1024px (small laptops)
   - 2xl: 1025px+ (desktops)
   ============================================================ */

/* ── CSS Variables for Responsive ─────────────────────────── */
:root {
  --mobile-nav-h: 52px;
  --mobile-sidebar-w: 260px;
  --safe-area-top: env(safe-area-inset-top, 0px);
  --safe-area-bottom: env(safe-area-inset-bottom, 0px);
  --safe-area-left: env(safe-area-inset-left, 0px);
  --safe-area-right: env(safe-area-inset-right, 0px);
}

/* ── Mobile Navigation Base Styles ────────────────────────── */
.mobile-nav {
  display: none;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: calc(56px + var(--safe-area-bottom));
  background: rgba(8, 8, 16, 0.98);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-top: 1px solid var(--border);
  z-index: 1000;
  padding-bottom: var(--safe-area-bottom);
}

.mobile-nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  color: var(--text-m);
  font-size: 10px;
  text-decoration: none;
  transition: all 0.2s ease;
  padding: 8px 4px;
  min-height: 44px;
}

.mobile-nav-item i {
  font-size: 20px;
  transition: transform 0.2s ease;
}

.mobile-nav-item.active {
  color: var(--accent);
}

.mobile-nav-item.active i {
  transform: translateY(-2px);
}

.mobile-nav-item:active {
  opacity: 0.7;
}

/* Add padding for bottom nav */
.has-mobile-nav .kb-main {
  padding-bottom: calc(70px + var(--safe-area-bottom));
}

/* ── Base Mobile Styles (0-320px) ─────────────────────────── */
@media (max-width: 320px) {
  html {
    font-size: 13px;
  }
  
  .kb-nav {
    padding: 0 8px;
    height: var(--mobile-nav-h);
  }
  
  .kb-nav-brand span {
    font-size: 14px;
  }
  
  .kb-layout {
    padding-top: var(--mobile-nav-h);
  }
  
  .kb-sidebar {
    width: var(--mobile-sidebar-w);
    min-width: var(--mobile-sidebar-w);
    top: var(--mobile-nav-h);
    transform: translateX(-100%);
    transition: transform 0.3s ease;
  }
  
  .kb-sidebar.active,
  .kb-sidebar.open {
    transform: translateX(0);
  }
  
  .kb-main {
    margin-left: 0;
    padding: 12px;
    padding-bottom: calc(12px + var(--safe-area-bottom));
  }
  
  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
  }
  
  .stat-card {
    padding: 12px;
  }
  
  .stat-value {
    font-size: 20px;
  }
  
  .card {
    padding: 14px;
  }
  
  .grid.grid-2,
  .grid.grid-3,
  .grid.grid-4 {
    grid-template-columns: 1fr;
    gap: 12px;
  }
  
  .form-row {
    flex-direction: column;
    gap: 0;
  }
  
  .bots-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }
  
  .servers-grid {
    grid-template-columns: 1fr;
    gap: 10px;
  }
  
  .kb-page-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
    padding: 14px;
  }
  
  .kb-page-header-icon {
    width: 44px;
    height: 44px;
    font-size: 18px;
  }
  
  .kb-page-header-name {
    font-size: 16px;
  }
  
  .btn {
    padding: 10px 14px;
    font-size: 12px;
    min-height: 44px;
  }
  
  .btn-sm {
    padding: 8px 12px;
    font-size: 11px;
    min-height: 36px;
  }
  
  /* Mobile sidebar overlay */
  .sidebar-overlay {
    display: block;
    position: fixed;
    top: 0;
    left: 280px; /* Start from right edge of sidebar */
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    z-index: 99;
    opacity: 0;
    visibility: hidden;
    transition: all 0.3s ease;
  }
  
  .sidebar-overlay.active {
    opacity: 1;
    visibility: visible;
  }
  
  /* Tables - horizontal scroll */
  .table-container {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin: 0 -12px;
    padding: 0 12px;
  }
  
  .kb-table {
    min-width: 600px;
  }
  
  /* Modal adjustments */
  .modal-content {
    width: calc(100% - 24px);
    max-width: none;
    margin: 12px auto;
    max-height: calc(100vh - 24px);
    overflow-y: auto;
  }
  
  /* Settings page specific */
  .settings-section .card {
    margin-bottom: 12px;
  }
  
  .switch {
    transform: scale(0.9);
  }
  
  /* Show mobile nav */
  .mobile-nav {
    display: flex;
  }
}

/* ── Small Phones (321-375px) ─────────────────────────────── */
@media (min-width: 321px) and (max-width: 375px) {
  html {
    font-size: 13.5px;
  }
  
  .kb-nav {
    padding: 0 12px;
  }
  
  .kb-main {
    padding: 14px;
    margin-left: 0;
  }
  
  .kb-sidebar {
    transform: translateX(-100%);
    transition: transform 0.3s ease;
  }
  
  .kb-sidebar.active {
    transform: translateX(0);
  }
  
  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 10px;
  }
  
  .grid.grid-2,
  .grid.grid-3 {
    grid-template-columns: 1fr;
  }
  
  .grid.grid-4 {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .bots-grid {
    grid-template-columns: 1fr;
  }
  
  .servers-grid {
    grid-template-columns: 1fr;
  }
  
  .form-row {
    flex-direction: column;
  }
  
  .mobile-nav {
    display: flex;
  }
  
  .has-mobile-nav .kb-main {
    padding-bottom: calc(70px + var(--safe-area-bottom));
  }
}

/* ── Large Phones (376-414px) ─────────────────────────────── */
@media (min-width: 376px) and (max-width: 414px) {
  .kb-main {
    margin-left: 0;
    padding: 16px;
  }
  
  .kb-sidebar {
    transform: translateX(-100%);
  }
  
  .kb-sidebar.active {
    transform: translateX(0);
  }
  
  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .grid.grid-2 {
    grid-template-columns: 1fr;
  }
  
  .grid.grid-3,
  .grid.grid-4 {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .bots-grid {
    grid-template-columns: 1fr;
  }
  
  .servers-grid {
    grid-template-columns: 1fr;
  }
  
  .mobile-nav {
    display: flex;
  }
  
  .has-mobile-nav .kb-main {
    padding-bottom: calc(70px + var(--safe-area-bottom));
  }
}

/* ── Tablets (415-768px) ──────────────────────────────────── */
@media (min-width: 415px) and (max-width: 768px) {
  .kb-main {
    margin-left: 0;
    padding: 20px;
  }
  
  .kb-sidebar {
    transform: translateX(-100%);
    width: 280px;
  }
  
  .kb-sidebar.active {
    transform: translateX(0);
  }
  
  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .grid.grid-2 {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .grid.grid-3 {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .grid.grid-4 {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .bots-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .servers-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .mobile-nav {
    display: flex;
  }
  
  .has-mobile-nav .kb-main {
    padding-bottom: calc(70px + var(--safe-area-bottom));
  }
}

/* ── Small Laptops (769-1024px) ───────────────────────────── */
@media (min-width: 769px) and (max-width: 1024px) {
  :root {
    --sidebar-w: 180px;
  }
  
  .kb-main {
    margin-left: var(--sidebar-w);
    padding: 20px;
  }
  
  .kb-sidebar {
    width: var(--sidebar-w);
    min-width: var(--sidebar-w);
  }
  
  .stats-grid {
    grid-template-columns: repeat(4, 1fr);
  }
  
  .grid.grid-2 {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .grid.grid-3 {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .grid.grid-4 {
    grid-template-columns: repeat(3, 1fr);
  }
  
  .bots-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .servers-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .mobile-nav {
    display: none;
  }
}

/* ── Desktops (1025px+) ───────────────────────────────────── */
@media (min-width: 1025px) {
  .mobile-nav {
    display: none;
  }
  
  .sidebar-overlay {
    display: none !important;
  }
}

/* ── Large Desktops (1440px+) ─────────────────────────────── */
@media (min-width: 1440px) {
  .kb-content {
    max-width: 1800px;
  }
  
  .stats-grid {
    grid-template-columns: repeat(4, 1fr);
  }
  
  .bots-grid {
    grid-template-columns: repeat(auto-fill, minmax(360px, 1fr));
  }
  
  .servers-grid {
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  }
}

/* ── Ultra-wide (1920px+) ─────────────────────────────────── */
@media (min-width: 1920px) {
  .kb-content {
    max-width: 2000px;
  }
  
  .grid.grid-4 {
    grid-template-columns: repeat(4, 1fr);
  }
}

/* ── Landscape Mode Fixes ─────────────────────────────────── */
@media (max-height: 500px) and (orientation: landscape) {
  .kb-nav {
    height: 48px;
  }
  
  .kb-layout {
    padding-top: 48px;
  }
  
  .kb-sidebar {
    top: 48px;
  }
  
  .mobile-nav {
    height: 50px;
  }
  
  .modal-content {
    max-height: 90vh;
  }
}

/* ── Touch Device Optimizations ───────────────────────────── */
@media (pointer: coarse) {
  .btn,
  .kb-nav-list a,
  .server-card,
  .bot-card {
    min-height: 44px;
  }
  
  .form-control,
  .form-select,
  .form-input {
    font-size: 16px; /* Prevent zoom on iOS */
  }
  
  .toggle-slider {
    min-height: 24px;
  }
  
  /* Larger touch targets */
  .kb-table td,
  .kb-table th {
    padding: 14px;
  }
}

/* ── Print Styles ─────────────────────────────────────────── */
@media print {
  .kb-nav,
  .kb-sidebar,
  .mobile-nav,
  .btn {
    display: none !important;
  }
  
  .kb-main {
    margin-left: 0;
    padding: 0;
  }
  
  body {
    background: white;
    color: black;
  }
  
  .card {
    border: 1px solid #ccc;
    break-inside: avoid;
  }
}

/* ── Utility Classes ──────────────────────────────────────── */
.hide-mobile { display: inherit; }
.hide-tablet { display: inherit; }
.hide-desktop { display: none; }
.show-mobile { display: none; }
.show-tablet { display: none; }

@media (max-width: 768px) {
  .hide-mobile { display: none !important; }
  .show-mobile { display: inherit !important; }
}

@media (min-width: 769px) and (max-width: 1024px) {
  .hide-tablet { display: none !important; }
  .show-tablet { display: inherit !important; }
}

@media (min-width: 1025px) {
  .hide-desktop { display: none !important; }
}

/* Flex utilities */
.flex-col-mobile {
  display: flex;
  flex-direction: row;
}

@media (max-width: 768px) {
  .flex-col-mobile {
    flex-direction: column;
  }
}

/* Text utilities */
.text-center-mobile {
  text-align: inherit;
}

@media (max-width: 768px) {
  .text-center-mobile {
    text-align: center;
  }
}

/* Safe area utilities for notched phones */
.safe-area-top {
  padding-top: var(--safe-area-top);
}

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

/* Prevent text overflow */
.truncate {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.truncate-2 {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* Scrollbar hide for mobile carousels */
.scrollbar-hide {
  -ms-overflow-style: none;
  scrollbar-width: none;
}

.scrollbar-hide::-webkit-scrollbar {
  display: none;
}

/* Aspect ratio utilities */
.aspect-video {
  aspect-ratio: 16 / 9;
}

.aspect-square {
  aspect-ratio: 1 / 1;
}

/* Container queries support (modern browsers) */
@supports (container-type: inline-size) {
  .container-query {
    container-type: inline-size;
  }
  
  @container (max-width: 400px) {
    .cq-grid {
      grid-template-columns: 1fr;
    }
  }
}
