/* ═══════════════════════════════════════
   RESPONSIVE — Adaptive Layouts
   ═══════════════════════════════════════ */

/* ── Burger & Info buttons: hidden on desktop ── */
.burger-btn,
.info-btn {
  display: none;
}

/* ── RTL Support ── */
[dir="rtl"] {
  direction: rtl;
}

[dir="rtl"] .panel-left {
  order: 1;
  border-right: none;
  border-left: 1px solid var(--border-color);
}

[dir="rtl"] .viewport-container {
  order: 0;
}

[dir="rtl"] .panel-right {
  order: -1;
  border-left: none;
  border-right: 1px solid var(--border-color);
}

[dir="rtl"] .excitation-controls {
  flex-direction: row-reverse;
}

[dir="rtl"] .btn-group {
  flex-direction: row-reverse;
}

[dir="rtl"] .top-bar-left {
  flex-direction: row-reverse;
}

[dir="rtl"] .top-bar-right {
  flex-direction: row-reverse;
}

[dir="rtl"] .element-display {
  flex-direction: row-reverse;
}

[dir="rtl"] .language-switcher {
  flex-direction: row-reverse;
}

[dir="rtl"] .overlay-badge.top-left {
  left: auto;
  right: var(--sp-3);
}

/* ── Large Screens (>1600px) ── */
@media (min-width: 1600px) {
  :root {
    --panel-width: 340px;
    --periodic-height: 300px;
  }

  .element-cell {
    min-height: 34px;
    font-size: 10px;
  }

  .element-cell .el-symbol {
    font-size: 12px;
  }
}

/* ── Medium Screens (1024px - 1366px) ── */
@media (max-width: 1366px) {
  :root {
    --panel-width: 260px;
  }

  .btn-toggle {
    font-size: 10px;
    padding: 3px 6px;
  }
}

/* ── Small Screens / Tablets (<1024px) ── */
@media (max-width: 1024px) {
  :root {
    --panel-width: 280px;
    --periodic-height: 220px;
  }

  /* Show burger & info on tablets */
  .burger-btn,
  .info-btn {
    display: flex;
  }

  /* Hide desktop panel-toggle buttons */
  .panel-toggle {
    display: none;
  }
  .panel-reopen {
    display: none !important;
  }

  .panel-left,
  .panel-right {
    position: absolute;
    top: 0;
    bottom: 0;
    z-index: var(--z-panels);
    width: var(--panel-width);
    min-width: var(--panel-width);
    transition: transform 0.35s cubic-bezier(0.4, 0, 0.2, 1);
    will-change: transform;
  }

  .panel-left {
    left: 0;
    transform: translateX(-100%);
  }

  .panel-left.open {
    transform: translateX(0);
  }

  .panel-right {
    right: 0;
    transform: translateX(100%);
  }

  .panel-right.open {
    transform: translateX(0);
  }

  .panel.collapsed {
    width: var(--panel-width);
    min-width: var(--panel-width);
  }
}

/* ── Mobile (<768px) ── */
@media (max-width: 768px) {
  :root {
    --top-bar-height: 44px;
    --panel-width: 85vw;
    --periodic-height: 200px;
  }

  /* Adjust app layout to accommodate PT and credits */
  .app-container {
    display: flex;
    flex-direction: column;
    /* Use dynamic viewport height which accounts for browser UI */
    height: 100dvh;
    height: 100vh; /* Fallback for older browsers */
  }

  /* Main content should not take all space */
  .main-content {
    flex: 1 1 auto;
    min-height: 0;
    overflow: hidden;
    display: flex;
    flex-direction: column;
  }

  /* Viewport container should fit within available space */
  .viewport-container {
    flex: 1;
    min-height: 0;
  }

  /* Show mobile info overlay */
  .mobile-info-overlay {
    display: flex;
  }

  /* Hide topbar center and language switcher on mobile */
  .top-bar-center {
    display: none;
  }

  .language-switcher {
    display: none;
  }

  .top-bar {
    padding: 0 var(--sp-2);
    justify-content: space-between;
    flex-shrink: 0;
  }

  .top-bar-right {
    gap: var(--sp-2);
  }

  .app-logo {
    font-size: var(--fs-base);
    letter-spacing: 2px;
  }

  .app-version {
    display: none;
  }

  .panel {
    position: fixed;
    top: var(--top-bar-height);
    bottom: 0;
    width: 85vw;
    min-width: 85vw;
    z-index: var(--z-modal);
    box-shadow: var(--shadow-lg);
  }

  .panel-left {
    left: 0;
    transform: translateX(-100%);
  }

  .panel-right {
    right: 0;
    transform: translateX(100%);
  }

  .panel-left.open,
  .panel-right.open {
    transform: translateX(0);
  }

  /* Ensure periodic table is visible on mobile */
  .periodic-table-container {
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    max-height: var(--periodic-height);
    min-height: 48px;
    z-index: 100;
    position: relative;
    /* Minimal bottom spacing - safe area handled by parent */
    margin-bottom: 0;
    padding-bottom: 0;
  }

  /* Make collapsed state sticky and more accessible on mobile */
  .periodic-table-container.collapsed {
    max-height: 48px;
    position: sticky;
    bottom: 0;
    z-index: 200;
    box-shadow: 0 -3px 15px rgba(0, 229, 255, 0.25);
    border-top: 3px solid rgba(0, 229, 255, 0.5);
    margin-bottom: 0;
    padding-bottom: env(safe-area-inset-bottom, 0px);
    background: rgba(10, 15, 35, 0.98);
    backdrop-filter: blur(16px);
  }

  /* Make entire header clickable on mobile when collapsed */
  .periodic-table-container.collapsed .periodic-table-header {
    cursor: pointer;
    user-select: none;
    -webkit-tap-highlight-color: rgba(0, 229, 255, 0.3);
    padding: 14px var(--sp-4);
    position: relative;
    background: linear-gradient(180deg, rgba(0, 229, 255, 0.08), transparent);
    min-height: 48px;
    display: flex;
    align-items: center;
  }

  .periodic-table-container.collapsed .periodic-table-header:active {
    background: rgba(0, 229, 255, 0.12);
  }

  /* Add visual indicator (chevron) on mobile when collapsed */
  .periodic-table-container.collapsed .periodic-table-header h2::before {
    content: '▲';
    display: inline-block;
    margin-right: 10px;
    color: var(--accent-cyan);
    font-size: 14px;
    animation: bounce-up 1.5s ease-in-out infinite;
    filter: drop-shadow(0 0 4px var(--accent-cyan));
  }

  @keyframes bounce-up {
    0%, 100% { transform: translateY(0); opacity: 1; }
    50% { transform: translateY(-4px); opacity: 0.7; }
  }

  /* Hide animation when expanded */
  .periodic-table-container:not(.collapsed) .periodic-table-header h2::before {
    content: none;
  }

  /* Add tap here hint on mobile */
  .periodic-table-container.collapsed .periodic-table-header h2::after {
    content: ' • TAP TO OPEN';
    font-size: 9px;
    margin-left: 8px;
    color: var(--accent-cyan);
    opacity: 0.8;
    font-weight: 500;
    letter-spacing: 0.5px;
  }

  /* Larger touch target for toggle button on mobile */
  .periodic-table-header .btn-sm {
    min-width: 70px;
    min-height: 36px;
    font-size: var(--fs-sm);
    font-weight: 600;
    padding: 8px 12px;
  }

  /* Ensure credits bar is visible on mobile */
  .credits-bar {
    display: flex;
    flex-shrink: 0;
    z-index: 50;
    position: relative;
    margin-top: auto;
    /* Minimal bottom padding with safe area */
    padding-bottom: max(4px, env(safe-area-inset-bottom));
  }

  /* Periodic table responsive grid */
  .pt-grid {
    grid-template-columns: repeat(18, minmax(16px, 1fr));
    gap: 1px;
  }

  /* Remove bottom padding to eliminate black space on mobile */
  .periodic-table {
    padding: 0 var(--sp-2) var(--sp-2);
  }

  .pt-element {
    min-height: 22px;
    min-width: 16px;
    padding: 1px 0;
  }

  .pt-element .pt-z {
    display: none;
  }

  .pt-element .pt-sym {
    font-size: 8px;
  }

  .pt-legend {
    display: none;
  }
}

/* ── Very Small (<480px) ── */
@media (max-width: 480px) {
  .periodic-table-container {
    max-height: 160px;
    min-height: 50px;
    margin-bottom: 0;
  }

  .periodic-table-container.collapsed {
    max-height: 50px;
    min-height: 50px;
  }

  .periodic-table {
    max-height: calc(160px - 50px);
    padding: 0 var(--sp-2) var(--sp-1);
  }

  .pt-grid {
    grid-template-columns: repeat(18, minmax(14px, 1fr));
  }

  .pt-element .pt-sym {
    font-size: 7px;
  }
  
  /* Ensure header is more prominent on very small screens */
  .periodic-table-header {
    padding: var(--sp-3) var(--sp-3);
  }
  
  .periodic-table-header h2 {
    font-size: 11px;
  }
}

/* ── Print Styles ── */
@media print {
  .panel, .top-bar, .periodic-table-container,
  .overlay-badge, .measurement-overlay {
    display: none !important;
  }

  .viewport-container {
    width: 100% !important;
  }

  body {
    background: white;
  }
}
