/* Responsive Mobile & Landscape: 2-Row Shell, Compact Buttons & 100% Full-Expanded Views (No Split Drawer) */

:root {
  --touch-target-min: 28px;
  --keyboard-inset-height: 0px;
}

html,
body {
  width: 100%;
  max-width: 100vw;
  overflow-x: hidden;
  height: 100dvh;
  grid-template-columns: minmax(0, 1fr);
}

#guild-canvas {
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.camera-focus-group,
.camera-zoom-group {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

/* Mobile Portrait & Narrow Tablet Breakpoint (<= 960px) */
@media (max-width: 960px) {
  html,
  body {
    position: fixed;
    inset: 0;
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto minmax(0, 1fr);
    width: 100%;
    max-width: 100vw;
    height: 100%;
    max-height: 100%;
    margin: 0;
    padding: 0;
    overflow: hidden;
  }

  .btn,
  .drawer-tabs .btn,
  .roster-row-btn,
  .camera-overlay .btn {
    min-height: var(--touch-target-min);
    padding: 3px 7px;
    font-size: 10px;
  }

  .skill-chip-btn,
  .scroll-delivery-modes .btn {
    min-height: 26px;
    padding: 3px 7px;
    font-size: 9.5px;
    flex-shrink: 0;
  }

  .gate-opt-chip {
    min-height: 26px;
    padding: 3px 7px;
    font-size: 9.5px;
    flex-shrink: 1;
    max-width: 100%;
    min-width: 0;
    white-space: normal;
    overflow-wrap: break-word;
    word-break: break-word;
  }

  .command-scroll-card {
    padding: 4px 6px;
    margin-bottom: 0;
  }

  .command-scroll-input {
    height: 38px;
    min-height: 38px;
    max-height: 68px;
    padding: 6px 8px;
  }

  /* Row 1: Single-Row Safe-Area Header on Mobile (Crest + Project Worlds + Summon/Profile) */
  .top-bar {
    padding-top: calc(env(safe-area-inset-top, 0px) + 4px);
    padding-bottom: 4px;
    padding-left: calc(env(safe-area-inset-left, 0px) + 6px);
    padding-right: calc(env(safe-area-inset-right, 0px) + 6px);
    min-height: 36px;
    background: #16110e;
    width: 100%;
    min-width: 0;
    max-width: 100%;
    display: flex;
    flex-wrap: nowrap;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
    box-sizing: border-box;
    z-index: 60;
  }

  .brand { gap: 4px; flex-shrink: 0; order: 1; }
  .brand-crest { width: 22px; height: 22px; font-size: 11px; }
  .brand-text,
  .scenario-label,
  .world-toolbar-label,
  #daemon-indicator,
  #summary-indicator,
  #header-machine-strip,
  #btn-open-pairing-modal,
  #btn-refresh-live,
  #btn-toggle-lineage,
  #btn-toggle-style,
  #btn-hide-world,
  #btn-toggle-gamemaster,
  #btn-restore-gamemaster,
  #layout-resizer,
  .camera-zoom-group,
  #btn-focus-split,
  #btn-demolish-active-room,
  .sheet-hero-sub,
  .inv-pill-workflow,
  .subtab-desktop-suffix,
  .command-scroll-trigger-hint {
    display: none !important;
  }

  .scenario-buttons {
    order: 2;
    flex: 1 1 0;
    width: auto;
    min-width: 0;
    gap: 4px;
    padding-top: 0;
    border-top: none;
  }

  .adapter-controls {
    display: flex;
    flex-wrap: nowrap;
    align-items: center;
    justify-content: flex-end;
    gap: 4px;
    flex: 0 0 auto;
    min-width: 0;
    order: 3;
  }

  .adapter-controls::-webkit-scrollbar { display: none; }

  .adapter-controls .btn {
    padding: 3px 7px;
    font-size: 9.5px;
  }

  /* Row 2: Full-Expanded Content (Either 100% World OR 100% Details — Never Split!) */
  .main-stage {
    display: grid !important;
    grid-template-columns: minmax(0, 1fr) !important;
    grid-template-rows: minmax(0, 1fr) auto !important;
    width: 100%;
    height: 100%;
    min-height: 0;
    overflow: hidden;
  }

  .canvas-container {
    grid-template-rows: auto minmax(0, 1fr);
    padding: 6px 6px 0 6px;
  }

  .camera-overlay {
    position: static;
    width: fit-content;
    max-width: calc(100vw - 16px);
    margin: 0 auto 6px auto;
    justify-content: center;
    flex-wrap: nowrap;
    overflow-x: auto;
    background: rgba(20, 16, 13, 0.88);
    border: 1px solid #4b3a2f;
    border-radius: 999px;
    padding: 3px 6px;
    gap: 4px;
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.65);
    scrollbar-width: none;
  }

  .camera-overlay .btn {
    border-radius: 999px;
    padding: 3px 9px;
  }

  .camera-overlay::-webkit-scrollbar { display: none; }
  #zoom-readout { font-size: 9.5px; padding: 0 2px; }

  .alcove-tooltip {
    top: 6px !important;
    left: 6px !important;
    right: 6px !important;
    max-width: none !important;
    min-width: 0 !important;
    width: auto !important;
    z-index: 45;
  }

  #drawer-tab-world {
    display: inline-flex;
  }

  .drawer-chrome {
    order: 3;
    margin-top: auto;
    margin-bottom: 0;
    padding: 2px 4px 0 4px;
    padding-bottom: 0;
    bottom: 0;
    background: #16110e;
    border-top: 1px solid var(--border-gold);
    border-bottom: none;
    flex-shrink: 0;
    width: 100%;
    z-index: 50;
  }

  .drawer-tabs {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 4px;
    width: 100%;
    margin: 0;
  }

  .drawer-tabs .btn {
    padding: 4px 4px;
    margin: 0;
    font-size: 10px;
    min-height: 28px;
    border-bottom-left-radius: 0;
    border-bottom-right-radius: 0;
    border-bottom: none;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  /* State A: Mobile World View (data-drawer-state="collapsed") -> World 100% Height + Bottom Footer Tab Bar */
  .spectator-sidebar[data-drawer-state="collapsed"] {
    height: auto !important;
    border-left: none;
    border-top: none;
    display: flex !important;
    flex-direction: column;
  }

  .spectator-sidebar[data-drawer-state="collapsed"] .character-sheet,
  .spectator-sidebar[data-drawer-state="collapsed"] .ticker-panel {
    display: none !important;
  }

  /* State B: Mobile Details View (peek / expanded) -> Same Header + Top Pill Bar + Content + Bottom Footer */
  .main-stage[data-mobile-view="details"] {
    grid-template-rows: minmax(0, 1fr) !important;
  }

  .main-stage[data-mobile-view="details"] .world-section {
    display: none !important;
  }

  .spectator-sidebar[data-drawer-state="peek"],
  .spectator-sidebar[data-drawer-state="expanded"] {
    width: 100%; max-width: 100%; min-width: 0; box-sizing: border-box;
    height: 100% !important;
    max-height: calc(100% - var(--keyboard-inset-height, 0px)) !important;
    margin-bottom: var(--keyboard-inset-height, 0px);
    background: #14100d; display: flex !important; flex-direction: column;
    border-left: none; border-top: none; overflow: hidden;
  }

  .spectator-sidebar[data-drawer-tab="ticker"] .character-sheet,
  .spectator-sidebar[data-drawer-tab="ledger"] .ticker-panel,
  .spectator-sidebar[data-drawer-tab="character"] .ticker-panel {
    display: none !important;
  }

  .spectator-sidebar[data-drawer-tab="ticker"] .ticker-panel,
  .character-sheet {
    order: 1; display: flex !important; flex-direction: column; flex: 1 1 0;
    min-height: 0; min-width: 0; max-width: 100%; box-sizing: border-box;
    height: auto; width: 100%; padding: 6px 6px 0 6px; padding-bottom: 0;
    background: #14100d; border-bottom: none; overflow: hidden;
  }

  #sheet-header-slot { order: 1; flex-shrink: 0; display: flex; flex-direction: column; }
  #sheet-header-slot .subtab-deck-bar,
  .spectator-sidebar[data-drawer-tab="ticker"] .ticker-header {
    order: 1;
    width: fit-content;
    max-width: calc(100vw - 16px);
    margin: 0 auto 6px auto;
    justify-content: center;
    flex-wrap: nowrap;
    overflow-x: auto;
    background: rgba(20, 16, 13, 0.88);
    border: 1px solid #4b3a2f;
    border-radius: 999px;
    padding: 3px 6px;
    gap: 4px;
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.65);
    scrollbar-width: none;
    text-transform: none;
  }
  #sheet-header-slot .subtab-deck-bar::-webkit-scrollbar,
  .spectator-sidebar[data-drawer-tab="ticker"] .ticker-header::-webkit-scrollbar { display: none; }
  #sheet-header-slot .subtab-deck-bar .subtab-btn { flex: 0 0 auto; border-radius: 999px; padding: 3px 9px; }
  .spectator-sidebar[data-drawer-tab="ticker"] .ticker-header span:first-child { background: #78350f; border: 1px solid var(--accent-amber); color: #fef3c7; border-radius: 999px; padding: 3px 9px; min-height: var(--touch-target-min); display: inline-flex; align-items: center; font-weight: 600; }
  .spectator-sidebar[data-drawer-tab="ticker"] #fps-counter { background: var(--bg-card); border: 1px solid var(--border-slate); color: var(--text-main); border-radius: 999px; padding: 3px 9px; min-height: var(--touch-target-min); display: inline-flex; align-items: center; }
  .spectator-sidebar[data-drawer-tab="ticker"] .ticker-entries { order: 2; background: #191410; border: 1px solid #3b2e27; border-radius: 6px; padding: 8px 10px; }
  #sheet-header-slot .sheet-header-bar { order: 2; margin-bottom: 6px; }
  #sheet-static-slot { order: 2; flex: 1 1 0; min-height: 0; width: 100%; min-width: 0; max-width: 100%; box-sizing: border-box; overflow-y: auto; overflow-x: hidden; -webkit-overflow-scrolling: touch; display: flex; flex-direction: column; }
  .spectator-sidebar[data-drawer-tab="ledger"] #sheet-static-slot > * { flex-shrink: 0; min-width: 0; max-width: 100%; box-sizing: border-box; }
  .spectator-sidebar[data-drawer-tab="ledger"] .sheet-card-flex-fill { flex: 1 0 auto; min-height: auto; }
  .spectator-sidebar[data-drawer-tab="ledger"] .sheet-scroll-list { flex: 0 0 auto; min-height: auto; max-height: none; overflow: visible; }

  #sheet-interactive-slot {
    order: 3;
    position: sticky;
    bottom: 0;
    z-index: 15;
    background: linear-gradient(180deg, rgba(24, 19, 15, 0.98), #14100d);
    padding-top: 2px;
    padding-bottom: 0;
    margin-top: auto;
    margin-bottom: 0;
    border-top: 1px solid #4b3a2f;
    flex-shrink: 0;
  }

  .kill-section-wrap,
  .sheet-card-flex-fill {
    margin-bottom: 0;
    padding-bottom: 0;
  }

  /* 16px inputs on mobile so iOS Safari never auto-zooms */
  #command-scroll-input,
  .command-scroll-input,
  .gate-custom-input,
  .modal-input,
  #summon-directory-select,
  #summon-agent-class,
  #summon-title-input,
  #summon-prompt-input {
    font-size: 16px !important;
  }
}

/* Mobile Landscape Breakpoint (max-height: 540px and orientation: landscape) */
@media (max-height: 540px) and (orientation: landscape) {
  .top-bar { padding: 2px 6px; min-height: 30px; }
  .camera-overlay { padding: 2px 6px; }
}
