/* layout.css - Sidebar Grid & Structure */
@layer layout {

  /* Body as Main Grid Container */
  body {
    display: grid;

    /* Defined Topology: Full Header, Sidebar|Main, Full Footer */
    grid-template-areas:
      "header header"
      "sidebar main"
      "footer footer";

    /* Columns: Sidebar (Fit Content) | Main (Flex) */
    grid-template-columns: min-content 1fr;

    /* Rows: Header (min) | Content (Expands) | Footer (min) */
    grid-template-rows: min-content 1fr min-content;

    /* App Grid Layout with Compact Padding */
    height: 100dvh;
    width: 100%;
    margin: 0;
    padding: var(--size-1);
    /* Minimal outer padding */
    gap: var(--size-2);
    /* Tighter separation */
    border: none;
    overflow: hidden;
    background: var(--bg-body);
  }

  /* Responsive rules live near the end of this layer so they override base layout. */

  /* Sidebar */
  .app-sidebar {
    grid-area: sidebar;
    background: var(--bg-sidebar);
    border: var(--border-width) solid var(--border);
    border-radius: var(--radius-3);
    box-shadow: var(--shadow-pop);

    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;
    height: 100%;

    /* Ensure sidebar and tooltips sit above main content */
    z-index: 20;
  }

  .sidebar-group {
    display: flex;
    flex-direction: column;
    gap: var(--size-5);
    align-items: center;
  }

  /* Remove spacer since we use justify-content: space-between */
  .sidebar-group.spacer {
    display: none;
  }

  /* Icons & Buttons */
  /* Sidebar Icon Buttons - 4.5rem (72px) */
  .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;
  }

  .sidebar-label {
    display: none;
    font-size: var(--font-size-00);
    font-weight: 700;
    line-height: 1;
    letter-spacing: -0.01em;
  }

  .sidebar-btn iconify-icon {
    font-size: 2.5rem;
    /* 40px */
  }

  /* Specific styling for the theme toggle which has two icons */
  .sidebar-btn .dark-icon,
  .sidebar-btn .light-icon {
    font-size: 2.5rem;
    /* 40px */
  }

  /* Show moon icon in light mode (to switch to dark) */
  :root:not([data-theme="dark"]) .sidebar-btn .dark-icon {
    display: block;
  }
  :root:not([data-theme="dark"]) .sidebar-btn .light-icon {
    display: none;
  }

  /* Show sun icon in dark mode (to switch to light) */
  :root[data-theme="dark"] .sidebar-btn .dark-icon {
    display: none;
  }
  :root[data-theme="dark"] .sidebar-btn .light-icon {
    display: block;
  }

  .sidebar-btn:hover {
    background: var(--bg-surface-alt);
    color: var(--primary);

    /* No scale, No shadow */
  }

  .sidebar-btn.is-active {
    background: var(--primary);
    color: var(--on-primary);

    /* No shadow */
  }

  /* Main Area */
  .app-main {
    grid-area: main;
    position: relative;
    display: grid;

    /* Content (expands) | HUD (fixed) */
    grid-template-rows: 1fr auto;
    height: 100%;
    overflow: hidden;
    background: var(--bg-main);
    border: var(--border-width) solid var(--border);
    /* Full Border */
    border-radius: var(--radius-3);
    /* Card Radius */
    box-shadow: var(--shadow-pop);
    padding: var(--size-4);
    padding-bottom: 0;
    gap: var(--size-3);
  }

  /* Footer (Bottom Strip) */
  .app-footer {
    grid-area: footer;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.5rem var(--size-4);
    /* Minimal vertical padding */
    background: var(--bg-footer);
    border: var(--border-width) solid var(--border);
    /* Full Border */
    border-radius: var(--radius-3);
    /* Card Radius */
    box-shadow: var(--shadow-pop);
    font-size: 0.85rem;
    /* Small but readable */
    font-weight: 600;
    /* Semi-bold */
    color: var(--text-2);
    /* Darker text than muted */
    user-select: none;
    min-height: 2.75rem;
    /* 44px */
  }

  .footer-status-bar {
    width: 100%;
    min-height: 2rem;
    display: flex;
    align-items: center;
    border-top: 1px solid var(--border);
    padding: 0 var(--size-3);
    color: var(--text-muted);
    font-size: 0.9rem;
    opacity: 0.6;
    /* transition: opacity 0.2s ease; */
  }

  .footer-status-bar.is-visible {
    opacity: 1;
  }

  .footer-status-bar.info {
    color: var(--text-muted);
  }

  .footer-status-bar.error {
    color: var(--danger);
  }

  .footer-status-inner {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
  }

  .status-spinner {
    width: 0.9rem;
    height: 0.9rem;
    border: 2px solid var(--border);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    display: inline-block;
  }

  /* App Header (Top Banner) */
  .app-header {
    grid-area: header;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    /* Left Align things per user request */
    gap: var(--size-5);
    /* consistent spacing */
    padding: var(--size-2) var(--size-4);
    min-height: 4.5rem;
    background: var(--bg-header);
    border: var(--border-width) solid var(--border);
    /* Full Border */
    border-radius: var(--radius-3);
    /* Card Radius */
    box-shadow: var(--shadow-pop);

    flex-wrap: nowrap;
    /* Prevent stacking */
    overflow: hidden;
  }

  .brand {
    display: flex;
    align-items: center;
    gap: var(--size-3);
    min-height: 3.25rem;
    font-weight: 800;
    /* Bold 800 */
    font-size: 2rem;
    /* Much Larger */
    color: var(--text-1);
    letter-spacing: -0.03em;
  }

  .brand-icon {
    font-size: 3.25rem;
    /* 52px */
    width: 3.25rem;
    height: 3.25rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: var(--primary);
  }

  .chat-pane {
    flex: 1;
    min-height: 0;
    /* Allow flex shrinking */
    display: flex;
    flex-direction: column;
  }

  /* Status HUD */

  /* Chat Status Bar (Bottom of Chat Log) */
  .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);
    /* Matches .chat-header padding */
    background: var(--bg-surface-alt);
    border-top: 1px solid var(--border);
    margin: 0;
    border-radius: 0;
    z-index: 10;
  }

  .status-group {
    display: flex;
    align-items: center;
    gap: var(--size-3);
  }

  .status-group iconify-icon {
    font-size: 1.75rem;
    /* 28px */
    width: 1.75rem;
    height: 1.75rem;
    /* transition: color 0.2s ease, filter 0.2s ease; */
  }

  /* Status States */
  .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 utilities replaced by .status-* classes in .chat-status-bar */
  /* Modals */
  .modal-overlay {
    position: fixed;
    inset: 0;
    background: rgb(0 0 0 / 60%);
    backdrop-filter: blur(4px);
    z-index: var(--layer-modal);
    display: grid;
    place-items: center;
    padding: var(--size-3);
  }

  .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;
    /* 480px */
    box-shadow: var(--shadow-6);
    position: relative;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }

  /* Modal Children - Moved from app.css */
  .modal-panel.border-contrast {
    border-color: var(--color-accent);
  }

  .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);
  }

  .modal-header h3 {
    margin: 0;
    font-size: 1.25rem;
    font-weight: 700;
  }

  .modal-content {
    padding: 1.5rem;
    overflow-y: auto;
    max-height: 80vh;
  }

  .modal-content p {
    margin-bottom: 0.75rem;
    line-height: 1.5;
  }

  .modal-content .subtle {
    color: var(--color-muted);
    font-size: 0.9em;
    font-style: italic;
    margin-top: 1rem;
  }

  .modal-panel .close-btn {
    width: 2rem;
    /* 32px */
    height: 2rem;
    /* 32px */
    border-radius: 999px;
    background: var(--veil-10);
    color: var(--color-text);
  }

  .modal-panel .close-btn:hover {
    background: var(--color-accent);
    color: white;
  }

  /* Tooltips */
  .tooltipped {
    position: relative;
  }

  .tooltipped::after {
    content: attr(data-tooltip);
    position: absolute;
    left: 110%;
    /* To the right of sidebar */
    top: 50%;
    transform: translateY(-50%) translateX(-0.625rem);
    /* -10px */
    background: var(--gray-9);
    color: var(--gray-0);
    padding: 4px 8px;
    /* keeping small padding as px is fine here? or convert? converting for consistency */
    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;
    /* transition: all 0.2s ease; */
    z-index: var(--layer-panel);
  }

  .tooltipped:hover::after {
    opacity: 1;
    transform: translateY(-50%) translateX(0);
  }

  /* Minimize sidebar on home page - show only essential actions */
  body[data-page="home"] .app-sidebar {
    /* Reduce visual weight on landing */
    opacity: 0.7;
    transition: opacity 0.2s ease;
  }

  body[data-page="home"] .app-sidebar:hover {
    opacity: 1;
  }

  /* Hide less essential sidebar items on home */
  body[data-page="home"] .sidebar-btn[aria-label="Overview"] {
    display: none; /* Already on home, redundant */
  }

  /* Responsive: Mobile / Tablet Portrait < 768px */
  @media (max-width: 768px) {
    /* Mobile layout: full-width content + bottom nav */
    body {
      grid-template-areas:
        "header"
        "main"
        "footer";
      grid-template-columns: 1fr;
      grid-template-rows: min-content 1fr min-content;
    }

    /* Slightly thinner borders on small screens (still cartoon-ish, less noisy) */
    :root { --border-width: 2px; }

    .app-main {
      padding: var(--size-3);
      padding-bottom: calc(var(--size-3) + 5.4rem + env(safe-area-inset-bottom, 0px)); /* bottom nav + safe-area */
    }

    .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)); /* above footer */

      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));
    }

    .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;
    }

    .sidebar-btn.is-active .sidebar-label {
      opacity: 1;
    }

    .sidebar-btn iconify-icon,
    .sidebar-btn .dark-icon,
    .sidebar-btn .light-icon {
      font-size: 1.5rem;
    }
  }

  /* Responsive: Mobile Narrow < 480px */
  @media (max-width: 480px) {
    .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;
    }

    .sidebar-btn {
      width: 2.6rem;
      height: 2.6rem;
    }

    .sidebar-btn iconify-icon,
    .sidebar-btn .dark-icon,
    .sidebar-btn .light-icon {
      font-size: 1.35rem;
    }
  }

  /* Animations */
  [x-cloak] {
    display: none !important;
  }
}

/* End Layer Layout */
