/* layout.css - Global Layout Grid & Structure (CSS Nesting)
 * ==========================================================
 * MODERNIZED: Uses native CSS nesting (baseline 2023, 96%+ support)
 *
 * This file contains truly global layout styles that cannot be component-scoped:
 *
 * MUST STAY GLOBAL:
 * - Body grid layout (page structure)
 * - .app-sidebar (inline HTML in room.webc uses these)
 * - .app-main (grid area definitions)
 * - .modal-overlay/.modal-panel (HTMX partials use these)
 * - .status-* classes (inline HTML in room.webc uses these)
 * - .tooltipped (global utility)
 * - Responsive media queries
 * - WebRTC error panel (HTMX partial uses these)
 */
@layer layout {

  /* ========================================
     MOBILE-FIRST GLOBAL OVERFLOW PREVENTION
     ======================================== */
  /* Allow all flex/grid items to shrink below content size */
  *,
  *::before,
  *::after {
    min-width: 0;
  }

  /* Body as Main Grid Container */
  body {
    display: grid;
    grid-template-areas:
      "header header"
      "sidebar main"
      "footer footer";
    grid-template-columns: min-content 1fr;
    /* CLS FIX: More explicit row heights to prevent shifts */
    grid-template-rows: auto 1fr auto;
    height: 100dvh;
    min-height: 100dvh;
    width: 100%;
    margin: 0;
    padding: var(--size-1);
    gap: var(--size-2);
    border: none;
    overflow: hidden;
    background: var(--bg-body);

    /* Page-specific: Minimize sidebar on home page */
    &[data-page="home"] .app-sidebar {
      opacity: 0.7;
      transition: opacity 0.2s ease;

      &:hover { opacity: 1; }
    }

    &[data-page="home"] .sidebar-btn[aria-label="Overview"] {
      display: none;
    }

    /* Landing pages (base layout): Center content, override grid */
    /* Uses data-page attribute from app-shell.webc, not class */
    &[data-page="landing"],
    &[data-page="home"] {
      display: block;
      padding: 0;
      overflow: auto;

      & > main {
        display: flex;
        flex-direction: column;
        align-items: center;
        min-height: 100dvh;
        padding: var(--size-4);
        /* Remove card-like appearance for landing */
        border: none;
        box-shadow: none;
        border-radius: 0;
        background: transparent;
      }

      /* Hide sidebar on landing page - it's not needed */
      & .app-sidebar {
        display: none;
      }
    }
  }

  /* ========================================
     SIDEBAR - All related styles nested
     ======================================== */
  .app-sidebar {
    grid-area: sidebar;
    background: var(--surface-1);
    /* Subtle border - not debug/wireframe look */
    border: 1px solid var(--surface-3);
    border-radius: var(--radius-3);
    /* Gentle shadow for depth */
    box-shadow: var(--shadow-2);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    gap: var(--size-4);
    padding: var(--size-3) var(--size-2);
    width: fit-content;
    min-width: min-content;
    /* CLS FIX: Explicit height prevents layout shifts */
    height: 100%;
    min-height: 600px;
    /* No z-index needed - CSS Grid handles layout stacking */
  }

  /* Dark mode adjustments for sidebar */
  :root[data-theme="dark"] .app-sidebar {
    background: var(--surface-2);
    border-color: var(--surface-4);
  }

  .sidebar-group {
    display: flex;
    flex-direction: column;
    gap: var(--size-5);
    align-items: center;

    &.spacer { display: none; }
  }

  .sidebar-divider {
    width: 3rem;
    height: 1px;
    background: var(--border);
    margin: 0.5rem auto;
    opacity: 0.5;
  }

  .sidebar-label {
    display: none;
    font-size: var(--font-size-00);
    font-weight: 700;
    line-height: 1;
    letter-spacing: -0.01em;
  }

  /* Sidebar Button - All variants/states nested together */
  .sidebar-btn {
    width: 4.5rem;
    height: 4.5rem;
    border-radius: var(--radius-4);
    background: transparent;
    border: none;
    color: var(--text-muted);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background 0.2s ease, color 0.2s ease;
    position: relative;

    /* Icon sizing */
    & svg.icon,
    & .dark-icon,
    & .light-icon {
      font-size: 2.5rem;
    }

    /* States */
    &:hover {
      background: var(--bg-surface-alt);
      color: var(--primary);
    }

    &.is-active {
      background: var(--primary);
      color: var(--on-primary);
      position: relative;

      & .sidebar-label { opacity: 1; }

      /* Accent bar indicator for active view */
      &::before {
        content: '';
        position: absolute;
        left: -0.5rem;
        top: 50%;
        transform: translateY(-50%);
        width: 4px;
        height: 60%;
        background: var(--primary);
        border-radius: 0 var(--radius-2) var(--radius-2) 0;
        box-shadow: 0 0 8px var(--primary);
      }
    }

    /* Primary variant (e.g., New Room) */
    &--primary {
      background: var(--primary);
      color: var(--on-primary);

      &:hover {
        background: var(--blue-8);
        color: var(--on-primary);
      }
    }
  }

  /* Theme toggle icon visibility (light/dark mode) */
  :root:not([data-theme="dark"]) .sidebar-btn {
    & .dark-icon { display: block; }
    & .light-icon { display: none; }
  }

  :root[data-theme="dark"] .sidebar-btn {
    & .dark-icon { display: none; }
    & .light-icon { display: block; }
  }

  /* ========================================
     MAIN AREA
     ======================================== */
  .app-main {
    grid-area: main;
    position: relative;
    display: grid;
    grid-template-rows: 1fr auto;
    height: 100%;
    overflow: hidden;
    background: var(--surface-1);
    /* Subtle border - not debug/wireframe look */
    border: 1px solid var(--surface-3);
    border-radius: var(--radius-3);
    /* Gentle shadow for depth */
    box-shadow: var(--shadow-2);
    padding: var(--size-2);
    gap: var(--size-2);
    /* CLS FIX: Contain layout to prevent shifts from propagating */
    contain: layout;
  }

  /* Dark mode adjustments for main area */
  :root[data-theme="dark"] .app-main {
    background: var(--surface-2);
    border-color: var(--surface-4);
  }

  .chat-pane {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    max-width: 100%;
    width: 100%;
  }

  /* ========================================
     FOOTER
     ======================================== */
  .app-footer {
    grid-area: footer;
    display: flex;
    align-items: center;
    justify-content: center;
    /* CLS FIX: Prevent footer from zero height causing layout shifts */
    min-height: 32px;
    padding: 0 var(--size-3);
    font-size: 0.8rem;
    color: var(--text-muted);
  }

  /* ========================================
     STATUS HUD - Nested by context
     ======================================== */
  .room-shell .chat-status-bar {
    width: 100%;
    min-height: auto;
    display: flex;
    align-items: center;
    gap: var(--size-3);
    padding: var(--size-2) var(--size-3);
    background: var(--bg-surface-alt);
    border-top: 1px solid var(--border);
    margin: 0;
    border-radius: 0;
    /* No z-index needed - part of flex flow */
  }

  .status-group {
    display: flex;
    align-items: center;
    gap: var(--size-3);

    & svg.icon {
      font-size: 1.75rem;
      width: 1.75rem;
      height: 1.75rem;
    }
  }

  /* Status state modifiers - grouped together */
  .status-enabled {
    color: var(--success);
    filter: drop-shadow(0 0 2px var(--success));
  }

  .status-disabled {
    color: var(--text-muted);
    opacity: 0.6;
  }

  .status-warning { color: var(--warning); }

  .status-success {
    color: var(--green-6);
    filter: drop-shadow(0 0 3px var(--green-6));
  }

  .status-error {
    color: var(--red-6);
    filter: drop-shadow(0 0 2px var(--red-6));
  }

  .blink {
    animation: blink-pulse 0.8s ease-in-out infinite;
  }

  @keyframes blink-pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.4; }
  }

  .poller-indicator {
    font-size: 0.75rem;
    margin-left: -0.25rem;
    color: var(--blue-6);
    opacity: 0.8;
  }

  .status-clickable {
    cursor: pointer;
    position: relative;

    &:hover { transform: scale(1.1); }
  }

  .error-count-badge {
    position: absolute;
    top: -4px;
    right: -4px;
    min-width: 16px;
    height: 16px;
    padding: 0 4px;
    font-size: 10px;
    font-weight: 600;
    color: white;
    background: var(--red-6);
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  /* ========================================
     INERT ATTRIBUTE - HTML5 Background Disable
     ======================================== */
  [inert] {
    pointer-events: none;
    user-select: none;
  }

  /* Optional visual dimming for inert content */
  [inert] > * {
    opacity: 0.5;
    /* No transition - instant change prevents CLS */
  }

  /* ========================================
     HTML5 DIALOG MODALS - Native dialog support
     ======================================== */

  /* Generic dialog styling */
  dialog {
    position: fixed;
    inset: 0;
    margin: auto;
    border: none;
    padding: 0;
    background: transparent;
    max-width: min(90vw, var(--modal-width, 30rem));
    width: 100%;
    max-height: calc(100dvh - var(--size-6));
    overflow: visible;
  }

  dialog::backdrop {
    position: fixed;
    inset: 0;
    background: oklch(0% 0 0 / 0.6);
    backdrop-filter: blur(4px);
    animation: dialog-backdrop-fade-in 0.2s var(--ease-2);
  }

  dialog[open] {
    animation: dialog-slide-in 0.2s var(--ease-out-3);
  }

  @keyframes dialog-slide-in {
    from { opacity: 0; transform: scale(0.95) translateY(10px); }
    to { opacity: 1; transform: scale(1) translateY(0); }
  }

  @keyframes dialog-backdrop-fade-in {
    from { opacity: 0; }
    to { opacity: 1; }
  }

  /* Dialog-specific widths */
  .share-modal-dialog {
    max-width: min(90vw, 420px);
  }

  .waiting-room-dialog {
    max-width: min(90vw, 400px);
  }

  .webrtc-errors-dialog {
    max-width: min(90vw, 500px);
  }

  /* ========================================
     MODAL CONTENT STYLES - Used inside <dialog> elements
     NOTE: <dialog> provides the overlay via ::backdrop pseudo-element.
     These classes style the content panel inside dialogs.
     ======================================== */
  .modal-panel {
    background: var(--bg-surface);
    border: var(--border-width) solid var(--border);
    border-radius: var(--radius-5);
    padding: var(--size-5);
    width: 100%;
    max-width: 30rem;
    box-shadow: var(--shadow-6);
    position: relative;
    display: flex;
    flex-direction: column;
    overflow: hidden;

    &.border-contrast { border-color: var(--color-accent); }

    /* Close button inside modal */
    & .close-btn {
      width: 2rem;
      height: 2rem;
      border-radius: 999px;
      background: var(--veil-10);
      color: var(--color-text);

      &:hover {
        background: var(--color-accent);
        color: white;
      }
    }
  }

  .modal-header {
    padding: 1rem 1.5rem;
    border-bottom: 2px solid var(--veil-10);
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: var(--bg-surface-alt);

    & h3 {
      margin: 0;
      font-size: 1.25rem;
      font-weight: 700;
    }
  }

  .modal-content {
    padding: 1.5rem;
    overflow-y: auto;
    max-height: 80vh;

    & p {
      margin-bottom: 0.75rem;
      line-height: 1.5;
    }

    & .subtle {
      color: var(--color-muted);
      font-size: 0.9em;
      font-style: italic;
      margin-top: 1rem;
    }
  }

  /* ========================================
     TOOLTIPS
     ======================================== */
  .tooltipped {
    position: relative;

    &::after {
      content: attr(data-tooltip);
      position: absolute;
      left: 110%;
      top: 50%;
      transform: translateY(-50%) translateX(-0.625rem);
      background: var(--gray-9);
      color: var(--gray-0);
      padding: 0.25rem 0.5rem;
      border-radius: var(--radius-2);
      font-size: var(--font-size-00);
      font-weight: 600;
      opacity: 0;
      pointer-events: none;
      white-space: nowrap;
      z-index: var(--layer-popup);
      /* Delay tooltip appearance for better UX and to prevent flash in tests */
      transition: opacity 0.15s ease-out 0.3s, transform 0.15s ease-out 0.3s;
    }

    &:hover::after {
      opacity: 1;
      transform: translateY(-50%) translateX(0);
      /* Remove delay on show, but keep delay on hide */
      transition-delay: 0.3s;
    }
  }

  /* ========================================
     WEBRTC ERROR PANEL - Nested structure
     ======================================== */
  .webrtc-error-panel {
    max-width: 500px;
    width: 100%;
  }

  .error-summary .badge {
    display: inline-flex;
    align-items: center;
    gap: var(--size-1);
    padding: var(--size-1) var(--size-2);
    border-radius: var(--radius-2);
    font-size: var(--font-size-0);
    font-weight: 500;
  }

  /* Badge variants using nesting */
  .error-summary {
    & .error-badge { background: var(--red-3); color: var(--red-9); }
    & .warning-badge { background: var(--orange-3); color: var(--orange-9); }
    & .info-badge { background: var(--blue-3); color: var(--blue-9); }
    & .success-badge { background: var(--green-3); color: var(--green-9); }
  }

  .error-list {
    display: flex;
    flex-direction: column;
    gap: var(--size-2);
  }

  /* Error entry - all variants and children nested */
  .error-entry {
    display: flex;
    gap: var(--size-2);
    padding: var(--size-2);
    border-radius: var(--radius-2);
    background: var(--surface-2);
    align-items: flex-start;

    /* Type variants */
    &.error-item {
      border-left: 3px solid var(--red-6);
      & .error-icon { color: var(--red-6); }
    }

    &.warning-item {
      border-left: 3px solid var(--orange-6);
      & .error-icon { color: var(--orange-6); }
    }

    &.info-item {
      border-left: 3px solid var(--blue-6);
      & .error-icon { color: var(--blue-6); }
    }

    /* Children */
    & .error-icon {
      flex-shrink: 0;
      font-size: 1.25rem;
    }

    & .error-content {
      flex: 1;
      min-width: 0;
    }

    & .error-message {
      font-weight: 500;
      color: var(--text-1);
    }

    & .error-meta {
      display: flex;
      gap: var(--size-2);
      font-size: var(--font-size-00);
      color: var(--text-2);
      margin-top: var(--size-1);
      flex-wrap: wrap;
    }

    & .error-code {
      font-family: var(--font-mono);
      background: var(--surface-3);
      padding: 0 var(--size-1);
      border-radius: var(--radius-1);
    }

    /* Report button with states nested */
    & .report-btn {
      flex-shrink: 0;
      background: transparent;
      border: 1px solid var(--border);
      border-radius: var(--radius-2);
      padding: var(--size-1) var(--size-2);
      cursor: pointer;
      color: var(--text-2);
      transition: background-color 0.2s var(--ease-2), color 0.2s var(--ease-2), border-color 0.2s var(--ease-2);

      &:hover:not(:disabled) {
        background: var(--surface-3);
        color: var(--text-1);
        border-color: var(--blue-6);
      }

      &.reported {
        color: var(--green-6);
        border-color: var(--green-6);
        cursor: default;
      }

      &.reporting {
        color: var(--blue-6);

        & svg.icon { animation: spin 1s linear infinite; }
      }
    }
  }

  /* ========================================
     VIEW SWITCHING LAYOUT
     ======================================== */
  .main-area {
    position: relative;
    flex: 1;
    overflow: hidden;
    display: flex;
    flex-direction: column;
  }

  #main-pane,
  .secondary-view {
    position: absolute;
    inset: 0;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    /* CLS FIX: Prevent height doubling (287px -> 574px observed in tests) */
    min-height: 574px;
  }

  /* No z-index needed - Alpine x-show uses display:none for hidden views */
  .secondary-view {
    background: var(--bg-body);
    /* CLS FIX: Optimize rendering of off-screen views */
    content-visibility: auto;
  }

  .scroll-spacer {
    flex-shrink: 0;
    pointer-events: none;
  }

  .chat-log-virtual { position: relative; }

  /* Active navigation states */
  .icon-btn.active,
  .nav-btn.active {
    background: var(--primary);
    color: var(--text-on-primary);

    &:hover { background: var(--primary-hover, var(--primary)); }
  }

  .view-loading {
    padding: var(--size-4);
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 200px;
  }

  .view-loading-text {
    color: var(--text-muted);
    text-align: center;
  }

  .view-error-text {
    color: var(--red-7);
    text-align: center;
  }

  /* ========================================
     UTILITIES
     ======================================== */
  /* x-cloak handled in base.css */

  /* ========================================
     RESPONSIVE: Tablet/Mobile < 768px
     ======================================== */
  @media (max-width: 768px) {
    body {
      grid-template-areas:
        "header"
        "main"
        "footer";
      grid-template-columns: minmax(0, 1fr);  /* minmax(0, 1fr) prevents content from expanding grid */
      grid-template-rows: min-content 1fr min-content;
      /* Prevent horizontal overflow on mobile */
      width: 100%;
      max-width: 100vw;
      min-width: 0;
      overflow-x: hidden;
    }

    :root { --border-width: 2px; }

    .app-main {
      padding: var(--size-3);
      padding-bottom: calc(var(--size-3) + 5.4rem + env(safe-area-inset-bottom, 0px));
      /* Constrain width on mobile - prevent grid item from expanding beyond viewport */
      width: 100%;
      max-width: 100%;
      min-width: 0;  /* Allow shrinking below content size */
      overflow-x: hidden;
    }

    .app-header {
      padding: var(--size-2) var(--size-3);
      gap: var(--size-3);
    }

    .brand {
      font-size: 1.35rem;
      min-height: 3.5rem;
      white-space: nowrap;
    }

    .brand-icon {
      font-size: 2.25rem;
      width: 2.25rem;
      height: 2.25rem;
    }

    .brand-tagline,
    .header-extras { display: none; }

    /* Bottom navigation (with iOS safe-area support) */
    .app-sidebar {
      position: fixed;
      left: var(--size-2);
      right: var(--size-2);
      bottom: calc(env(safe-area-inset-bottom, 0px) + var(--size-1) + 2.75rem + var(--size-2));
      width: auto;
      height: calc(5.4rem + env(safe-area-inset-bottom, 0px));
      min-width: 0;
      flex-direction: row;
      justify-content: space-between;
      align-items: center;
      gap: var(--size-2);
      padding: var(--size-1) var(--size-2);
      padding-bottom: calc(var(--size-1) + env(safe-area-inset-bottom, 0px));
      z-index: 100; /* Ensure sidebar is above message composer */
    }

    .sidebar-label {
      display: block;
      color: currentColor;
      opacity: 0.75;
    }

    body[data-page="room"] .app-sidebar { justify-content: center; }

    .sidebar-group {
      flex-direction: row;
      gap: var(--size-2);
    }

    .sidebar-btn {
      width: 3.25rem;
      height: 3.25rem;
      border-radius: var(--radius-3);
      flex-direction: column;
      gap: 0.2rem;
      padding-top: 0.25rem;

      & svg.icon,
      & .dark-icon,
      & .light-icon { font-size: 1.5rem; }
    }

    .sidebar-hide-mobile {
      display: none !important;
    }

    /* Mobile content containers - ensure proper scrolling */
    .static-page-content,
    .landing-container,
    .view-container {
      /* Let flexbox handle height naturally - parent (.app-main) is already 1fr in body grid */
      /* Don't use 100dvh calc here - it double-counts header/footer */
      overflow-y: auto;
      -webkit-overflow-scrolling: touch; /* Smooth scrolling on iOS */
      padding-bottom: var(--size-8); /* Breathing room at bottom */
    }

    /* Ensure content doesn't get cut off */
    .content-card {
      margin-bottom: var(--size-6);
    }
  }

  /* ========================================
     RESPONSIVE: Narrow Mobile < 480px
     ======================================== */
  @media (max-width: 480px) {
    /* Hide footer on mobile room pages to maximize vertical space */
    body[data-page="room"] .app-footer,
    body[data-page="app"] .app-footer {
      display: none;
    }

    body {
      grid-template-rows: min-content 1fr;
      min-height: 100dvh;
    }

    .app-sidebar {
      left: var(--size-1);
      right: var(--size-1);
      height: calc(4rem + env(safe-area-inset-bottom, 0px));
      padding: var(--size-1);
      padding-bottom: calc(var(--size-1) + env(safe-area-inset-bottom, 0px));
    }

    .brand { font-size: 1.15rem; }

    /* Touch targets: Minimum 44px (2.75rem) per WCAG guidelines */
    .sidebar-btn {
      width: 2.75rem;
      height: 2.75rem;

      & svg.icon,
      & .dark-icon,
      & .light-icon { font-size: 1.35rem; }
    }
  }
}
/* End Layer Layout */
