/**
 * Mobile-Perfect Styles for Mall of Midnight
 * ==========================================
 * Target: ≤768px width (phones & small tablets)
 * Desktop: Completely unchanged (all rules scoped to mobile queries)
 * 
 * Features:
 * - Safe viewport units (dvh/svh)
 * - Safe-area insets (notch support)
 * - Touch targets ≥44px
 * - Zero horizontal scroll
 * - Keyboard handling
 * - One-column layout
 */

/* ========================================
   ROOT VARIABLES & BASE GUARANTEES
   ======================================== */

:root {
  --max-ch: 72ch;
  --choice-pad: 12px;
  --safe-inset-top: env(safe-area-inset-top, 0px);
  --safe-inset-right: env(safe-area-inset-right, 0px);
  --safe-inset-bottom: env(safe-area-inset-bottom, 0px);
  --safe-inset-left: env(safe-area-inset-left, 0px);
  --vvh: 100vh; /* Will be updated by JS for keyboard */
  --kb-inset: 0px; /* Keyboard inset */
}

/* ========================================
   BASE GUARANTEES (ALL SCREENS)
   ======================================== */

/* Prevent horizontal overflow globally */
html, body {
  overflow-x: hidden;
  max-width: 100%;
}

/* Prevent body scroll when sidebar or modal is open */
body.sidebar-open,
body.modal-open {
  overflow: hidden;
  position: fixed;
  width: 100%;
  height: 100%;
}

/* Flex children must allow shrinking */
#narrative-col {
  flex: 1;
  min-width: 0;
}

/* Narrative text must wrap */
#narrative-text,
#sceneText {
  display: block !important;
  max-width: var(--max-ch);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
}

/* Choice buttons must wrap */
#choices .choice,
#choices button {
  white-space: normal;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}

/* ========================================
   MOBILE-ONLY STYLES (≤768px)
   ======================================== */

@media (max-width: 768px) {
  /* ----------------
     Typography
     ---------------- */
  
  html {
    font-size: 16px; /* Prevent iOS zoom on input focus */
  }
  
  body {
    line-height: 1.6;
    -webkit-text-size-adjust: 100%;
  }

  /* ----------------
     Layout: One Column
     ---------------- */
  
  main {
    display: grid;
    grid-template-columns: 1fr;
    gap: 12px;
    width: min(100%, 1000px);
    margin-inline: auto;
    padding:
      clamp(8px, 2vw, 16px)
      calc(var(--safe-inset-right) + clamp(8px, 2vw, 16px))
      calc(var(--safe-inset-bottom) + clamp(8px, 2vw, 16px))
      calc(var(--safe-inset-left) + clamp(8px, 2vw, 16px));
  }

  /* Sidebar becomes collapsible sections */
  aside {
    order: 2;
    padding: 0;
  }

  aside .panel {
    margin-bottom: 8px;
    border-radius: 8px;
    overflow: hidden;
  }

  /* Narrative takes top priority */
  #content {
    order: 1;
    padding: clamp(12px, 2.5vw, 18px);
    min-height: auto;
  }

  #narrative-col,
  #content {
    max-width: 100%;
  }

  #sceneText,
  #narrative-text {
    max-width: 100%;
    font-size: 18px;
    line-height: 1.6;
    margin-bottom: 20px;
    padding: 0 4px;
  }

  /* ----------------
     Choice Buttons: Full Width + Big Targets
     ---------------- */

  #choices {
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin-top: 16px;
    max-height: min(44dvh, 460px);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding-right: 2px;
  }

  #choices .choice,
  #choices button {
    display: block;
    width: 100%;
    text-align: left;
    padding: 16px 20px;
    min-height: 56px; /* Increased from 44px for better touch targets */
    font-size: 17px; /* Slightly larger for readability */
    line-height: 1.4;
    border-radius: 12px;
    border: 2px solid var(--border);
    background: var(--panel);
    color: var(--fg);
    font-weight: 500;
    transition: all 0.15s ease;

    /* Performance: no tap delay, no highlight */
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    -webkit-appearance: none;
    appearance: none;
  }

  #choices .choice:hover,
  #choices button:hover {
    background: var(--accent);
    border-color: var(--accent);
    color: white;
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(179, 140, 255, 0.3);
  }

  #choices .choice:active,
  #choices button:active {
    transform: scale(0.98);
    box-shadow: 0 2px 8px rgba(179, 140, 255, 0.4);
  }

  /* ----------------
     Panels: Full-Screen Sheets
     ---------------- */
  
  #modal {
    width: 100%;
    max-width: none;
    height: 100dvh; /* Dynamic viewport height (accounts for URL bar) */
    margin: 0;
    border-radius: 0;
    padding-bottom: max(16px, var(--safe-inset-bottom), var(--kb-inset));
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
  }

  .modal-content {
    max-width: 100%;
    width: 100%;
    max-height: min(80dvh, calc(var(--vvh, 100dvh) - 48px));
    border-radius: 16px 16px 0 0; /* Round top only */
    background: var(--bg, #0f1014);
    margin-top: auto;
    position: relative;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.3);
  }

  /* Swipe indicator on modal header */
  .modal-content::before {
    content: '';
    position: absolute;
    top: 8px;
    left: 50%;
    transform: translateX(-50%);
    width: 40px;
    height: 4px;
    background: var(--muted, #666);
    border-radius: 2px;
    opacity: 0.5;
  }

  /* Hidden panels must not block layout */
  .modal[aria-hidden="true"],
  .modal.hidden,
  .overlay[aria-hidden="true"],
  .overlay.hidden {
    display: none !important;
    pointer-events: none !important;
  }

  /* ----------------
     Sidebar Toggle Button
     ---------------- */

  #sidebar-mobile-toggle {
    position: fixed;
    top: calc(16px + var(--safe-inset-top));
    right: calc(16px + var(--safe-inset-right));
    z-index: 101;
    background: var(--accent);
    color: white;
    border: none;
    border-radius: 999px;
    min-height: 48px;
    padding: 0 20px 0 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    font-size: 16px;
    font-weight: 600;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
    cursor: pointer;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    transition: all 0.2s ease;
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
  }

  #sidebar-mobile-toggle:hover {
    background: var(--accent);
    transform: scale(1.05);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.5);
  }

  #sidebar-mobile-toggle:active {
    transform: scale(0.95);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
  }

  #sidebar-mobile-toggle::before {
    content: '☰';
    font-size: 20px;
    line-height: 1;
  }


  /* ----------------
     Sidebar: Inline + Scroll Jump
     ---------------- */

  #sidebar {
    position: relative;
    width: 100%;
    transform: none;
    margin: 0;
    padding: 0;
    scroll-margin-top: 96px;
  }

  #sidebar.mobile-highlight {
    animation: sidebarPulse 1.2s ease;
  }

  @keyframes sidebarPulse {
    0% {
      box-shadow: 0 0 0 0 rgba(179, 140, 255, 0.45);
    }
    50% {
      box-shadow: 0 0 0 10px rgba(179, 140, 255, 0.2);
    }
    100% {
      box-shadow: 0 0 0 0 rgba(179, 140, 255, 0);
    }
  }

  /* ----------------
     Collapsible Sections (Accordions)
     ---------------- */
  
  .sidebar section,
  aside .panel {
    background: var(--panel-bg, #16161a);
    border: 1px solid var(--border);
  }

  .sidebar details {
    background: var(--panel-bg, #16161a);
  }

  .sidebar summary {
    padding: 14px 16px;
    font-weight: 600;
    cursor: pointer;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    min-height: 44px;
    display: flex;
    align-items: center;
  }

  .sidebar .section-body,
  .sidebar details[open] > div {
    padding: 12px 16px;
  }

  /* ----------------
     Game Controls Panel (Always Expanded)
     ---------------- */

  #gameControlsPanel {
    background: linear-gradient(135deg, var(--accent, #b38cff), #9c27b0);
    border: 2px solid var(--accent, #b38cff);
    border-radius: 12px;
    margin-bottom: 16px;
    padding: 16px;
  }

  #gameControlsPanel h3 {
    margin: 0 0 16px 0;
    color: white;
    font-size: 18px;
    font-weight: 600;
    text-align: center;
  }

  #gameControlsPanel .btnrow {
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin-bottom: 16px;
  }

  #gameControlsPanel .btnrow:last-child {
    margin-bottom: 0;
  }

  #gameControlsPanel button {
    min-height: 48px;
    padding: 12px 16px;
    font-size: 16px;
    font-weight: 600;
    border-radius: 8px;
    border: none;
    background: white;
    color: var(--accent, #b38cff);
    cursor: pointer;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    transition: all 0.2s ease;
  }

  #gameControlsPanel button:hover {
    background: rgba(255, 255, 255, 0.9);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
  }

  #gameControlsPanel button:active {
    transform: scale(0.98);
  }

  #gameControlsPanel #newGame {
    background: #28a745;
    color: white;
  }

  #gameControlsPanel #newGame:hover {
    background: #218838;
  }

  #gameControlsPanel #continueGame {
    background: #007bff;
    color: white;
  }

  #gameControlsPanel #continueGame:hover {
    background: #0056b3;
  }

  #gameControlsPanel #saveGame {
    background: #ffc107;
    color: #212529;
  }

  #gameControlsPanel #saveGame:hover {
    background: #e0a800;
  }

  /* Special styling for the slot selection row */
  .choice-tools {
    align-items: stretch;
    row-gap: 10px;
  }

  #choiceFilter {
    min-width: 0;
    flex: 1 1 100%;
  }

  #clearChoiceFilter {
    width: 100%;
  }

  #gameControlsPanel .btnrow:has(#slotSelect) {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
  }

  #gameControlsPanel #slotSelect {
    flex: 1;
    min-height: 48px;
    padding: 12px;
    border-radius: 8px;
    border: 2px solid rgba(255, 255, 255, 0.3);
    background: rgba(255, 255, 255, 0.9);
    color: var(--accent, #b38cff);
    font-size: 16px;
  }

  #gameControlsPanel label[for="slotSelect"] {
    color: white;
    font-weight: 600;
    margin-bottom: 0;
    white-space: nowrap;
  }

  /* ----------------
     Touch Targets: ≥44px
     ---------------- */
  
  button,
  a,
  [role="button"],
  input[type="checkbox"],
  input[type="radio"],
  .modal-close {
    min-height: 44px;
    min-width: 44px;
    padding: 12px;
  }

  /* Specific UI elements */
  #sidebar button {
    min-height: 44px;
    font-size: 15px;
  }

  .modal-close,
  .panel-close {
    min-width: 44px;
    min-height: 44px;
    padding: 12px;
  }

  #touch-controls {
    pointer-events: none;
  }

  #touch-controls > div {
    pointer-events: auto;
    touch-action: none;
  }

  #touch-controls .touch-hint {
    position: absolute;
    bottom: 12%;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(23, 24, 33, 0.8);
    color: #fff;
    font-size: 13px;
    padding: 6px 12px;
    border-radius: 999px;
    border: 1px solid rgba(255, 255, 255, 0.15);
    backdrop-filter: blur(8px);
    opacity: 0.85;
    transition: opacity 0.4s ease;
    pointer-events: none;
  }

  #touch-controls .touch-hint.hidden {
    opacity: 0;
  }

  /* ----------------
     Safe Viewport & Keyboard Handling
     ---------------- */
  
  .app-shell,
  body {
    min-height: 100svh; /* Safe viewport (accounts for browser chrome) */
    min-height: var(--vvh, 100svh); /* JS-updated for keyboard */
  }

  /* Adjust for keyboard */
  body {
    padding-bottom: max(
      var(--safe-inset-bottom),
      var(--kb-inset, 0px)
    );
  }

  /* ----------------
     Cutscene Modal
     ---------------- */
  
  .cutscene-modal {
    height: 100dvh;
    width: 100vw;
    padding: var(--safe-inset-top) var(--safe-inset-right) var(--safe-inset-bottom) var(--safe-inset-left);
  }

  /* ----------------
     Notifications
     ---------------- */
  
  .notification,
  .achievement-notification {
    right: var(--safe-inset-right);
    left: var(--safe-inset-left);
    top: calc(20px + var(--safe-inset-top));
    max-width: calc(100vw - var(--safe-inset-left) - var(--safe-inset-right) - 40px);
  }

  /* ----------------
     Forms & Inputs
     ---------------- */
  
  input[type="text"],
  input[type="number"],
  input[type="email"],
  textarea,
  select {
    font-size: 16px; /* Prevent iOS zoom */
    min-height: 44px;
  }

  /* ----------------
     Overflow Prevention
     ---------------- */
  
  * {
    max-width: 100%;
  }

  img,
  video,
  iframe {
    max-width: 100%;
    height: auto;
  }

  pre {
    white-space: pre-wrap;
    word-wrap: break-word;
    overflow-x: auto;
  }

  code {
    word-wrap: break-word;
  }
}

/* ========================================
   COARSE POINTER (TOUCH DEVICES)
   ======================================== */

@media (hover: none) and (pointer: coarse) {
  /* All interactive elements must have big touch targets */
  button,
  a,
  [role="button"],
  input,
  select,
  textarea {
    min-height: 44px;
    min-width: 44px;
  }

  /* Remove hover effects on touch devices */
  button:hover,
  a:hover,
  [role="button"]:hover {
    /* Hover state same as default */
    transition: none;
  }

  /* Add active state for touch feedback */
  button:active,
  a:active,
  [role="button"]:active {
    opacity: 0.7;
    transform: scale(0.98);
    transition: transform 0.1s, opacity 0.1s;
  }

  /* Enhanced touch feedback for choice buttons */
  #choices button:active {
    background: var(--accent, #6366f1);
    opacity: 0.9;
    transform: scale(0.97);
  }
}

/* ========================================
   LANDSCAPE ORIENTATION
   ======================================== */

@media (max-width: 768px) and (orientation: landscape) {
  /* Adjust for landscape (keyboard takes ~40% of screen) */
  body {
    padding-bottom: max(
      var(--safe-inset-bottom),
      calc(var(--kb-inset, 0px) * 1.5)
    );
  }

  .modal-content {
    max-height: calc(100dvh - 20px);
  }
}

/* ========================================
   PERFORMANCE OPTIMIZATIONS
   ======================================== */

@media (max-width: 768px) {
  /* Use GPU acceleration for transforms */
  .modal,
  .sidebar {
    will-change: transform;
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
  }

  /* Smooth scrolling */
  html {
    scroll-behavior: smooth;
  }

  /* Optimize touch scrolling */
  .modal-body,
  .panel-body,
  #sidebar {
    -webkit-overflow-scrolling: touch;
  }
}

/* ========================================
   ACCESSIBILITY
   ======================================== */

@media (max-width: 768px) {
  /* Focus visible on mobile */
  *:focus-visible {
    outline: 3px solid var(--accent, #3b82f6);
    outline-offset: 2px;
  }

  /* Skip to content link */
  .skip-to-content {
    position: fixed;
    top: -100px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 9999;
    padding: 12px 20px;
    background: var(--accent, #3b82f6);
    color: #fff;
    text-decoration: none;
    border-radius: 0 0 8px 8px;
    transition: top 0.3s;
  }

  .skip-to-content:focus {
    top: 0;
  }
}

/* ========================================
   DEBUG MODE (Remove in production)
   ======================================== */

@media (max-width: 768px) {
  .quest-card {
    padding: 8px 10px;
  }

  .stats-summary {
    flex-direction: column;
    align-items: flex-start;
  }

  .vitals-bar-labels {
    font-size: 10px;
  }

  /* Show overflow warning */
  body.mobile-overflow-detected::after {
    content: "⚠️ Horizontal Overflow Detected";
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: #ff0000;
    color: #fff;
    text-align: center;
    padding: 8px;
    font-size: 12px;
    z-index: 99999;
  }

  /* Keep tab controls usable on narrow screens without crushing labels */
  .inventory-tabs,
  .statistics-tabs,
  .skill-tree-tabs,
  .character-tabs {
    display: flex;
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
    gap: 0.5rem;
    padding: 0.25rem 0.125rem 0.35rem;
    scroll-snap-type: x proximity;
    overscroll-behavior-inline: contain;
    border-bottom: 1px solid var(--border, rgba(255, 255, 255, 0.14));
  }

  .tab-btn,
  .stat-tab,
  .skill-tab,
  .character-tab {
    flex: 0 0 auto;
    white-space: nowrap;
    min-height: 44px;
    scroll-snap-align: start;
    border-radius: 999px;
    padding-inline: 0.875rem;
  }

  .tab-btn.active,
  .stat-tab.active,
  .skill-tab.active,
  .character-tab.active {
    box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent, #9d7dff) 38%, transparent);
  }

  .tab-btn:focus-visible,
  .stat-tab:focus-visible,
  .skill-tab:focus-visible,
  .character-tab:focus-visible {
    outline: 2px solid var(--accent, #9d7dff);
    outline-offset: 2px;
  }
}
