/*
 * App chrome and safe-area contract
 * ---------------------------------
 * Every app opened by #appModal shares one outer navigation control and one
 * content viewport.  App-specific styles remain responsible for the content;
 * this layer only prevents the shell, title and nested panels from colliding.
 */

.app-modal .modal-panel > [hidden],
.app-modal .modal-panel [hidden] {
  display: none !important;
}

.app-modal .modal-panel {
  position: relative;
  isolation: isolate;
}

.app-modal .modal-close {
  box-sizing: border-box;
  flex: 0 0 auto;
  display: grid;
  width: 40px;
  height: 40px;
  margin: 0 0 8px;
  padding: 0;
  place-items: center;
  border: 1px solid rgba(51, 51, 47, .06);
  border-radius: 50%;
  background: rgba(247, 247, 244, .96);
  color: #3c3c38;
  box-shadow: none;
  line-height: 1;
  z-index: 200;
}

.app-modal .modal-close svg {
  display: block;
  width: 18px;
  height: 18px;
  margin: auto;
}

.app-modal .modal-close:focus-visible {
  outline: 2px solid #575752;
  outline-offset: 3px;
}

/* Direct app children must never sit underneath the shared back button. */
.app-modal .modal-panel > :is(
  #modalGeneric,
  #focusApp,
  #memoryApp,
  #backupApp,
  #beautyApp,
  #plannerApp,
  #writingApp,
  #notesApp,
  #scheduleApp,
  #diaryApp,
  #musicApp,
  #characterStudio,
  #apiSettings
):not([hidden]) {
  box-sizing: border-box;
  width: 100%;
  max-width: 100%;
  min-width: 0;
}

/* Focus owns its own back control; music does the same after music.js loads. */
.app-modal .modal-panel:has(#focusApp:not([hidden])) > .modal-close,
.app-modal .modal-panel:has(#musicApp:not([hidden])) > .modal-close {
  display: none !important;
}

/* A nested editor/sheet owns its own close control.  Never leave the outer
   app back button visible behind it: that created the doubled/overlapping
   arrows seen on compact phones and also closed the whole app unexpectedly. */
.app-modal .modal-panel:has(#plannerEditor:not([hidden])) > .modal-close,
.app-modal .modal-panel:has(#scheduleWeekSheet:not([hidden])) > .modal-close,
.app-modal .modal-panel:has(#scheduleEditor:not([hidden])) > .modal-close,
.app-modal .modal-panel:has(#scheduleThemePanel:not([hidden])) > .modal-close,
.app-modal .modal-panel:has(#diaryComposer:not([hidden])) > .modal-close {
  visibility: hidden !important;
  pointer-events: none !important;
}

/* Older app roots used absolute positioning and therefore covered the shared
   back button.  They now take the remaining flex height like every other app. */
.app-modal .modal-panel:has(#focusApp:not([hidden])),
.app-modal .modal-panel:has(#memoryApp:not([hidden])),
.app-modal .modal-panel:has(#scheduleApp:not([hidden])),
.app-modal .modal-panel:has(#diaryApp:not([hidden])),
.app-modal .modal-panel:has(#musicApp:not([hidden])),
.app-modal .modal-panel:has(#characterStudio:not([hidden])),
.app-modal .modal-panel:has(#apiSettings:not([hidden])),
.app-modal .modal-panel:has(.utility-app:not([hidden])),
.app-modal .modal-panel:has(.workspace-app:not([hidden])) {
  display: flex;
  min-height: 0;
  flex-direction: column;
  overflow: hidden;
  text-align: left;
}

.app-modal #focusApp:not([hidden]),
.app-modal #memoryApp:not([hidden]) {
  position: relative !important;
  inset: auto !important;
  flex: 1 1 auto;
  min-height: 0;
  height: auto !important;
}

.app-modal #scheduleApp:not([hidden]),
.app-modal #diaryApp:not([hidden]),
.app-modal #musicApp:not([hidden]),
.app-modal #characterStudio:not([hidden]),
.app-modal #apiSettings:not([hidden]),
.app-modal .utility-app:not([hidden]),
.app-modal .workspace-app:not([hidden]) {
  flex: 1 1 auto;
  min-height: 0;
  height: auto !important;
  max-height: none !important;
}

.app-modal #scheduleApp:not([hidden]),
.app-modal #characterStudio:not([hidden]),
.app-modal #apiSettings:not([hidden]),
.app-modal .utility-app:not([hidden]),
.app-modal .workspace-app:not([hidden]) {
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

/* The diary and music roots already own their internal scroll regions. */
.app-modal #diaryApp:not([hidden]),
.app-modal #musicApp:not([hidden]) {
  overflow: hidden;
}

/* Internal app headers remain in normal flow and cannot slide behind the
   shared control even when an app stylesheet uses sticky positioning. */
.app-modal :is(
  .utility-app-head,
  .workspace-head,
  .diary-header,
  .music-header,
  .ai-room-header,
  .memory-app > header,
  .university-schedule > header
) {
  box-sizing: border-box;
  min-width: 0;
}

/* Phone and touch-tablet treatment.  A white surface is extended through the
   top safe area, removing the unrelated grey strip above full-screen apps. */
@media (max-width: 430px),
       (pointer: coarse) and (max-width: 1366px),
       (hover: none) and (max-width: 1366px) {
  html:has(.app-modal.open),
  body:has(.app-modal.open),
  body:has(.app-modal.open) > .phone,
  .app-modal.open,
  .app-modal.open .modal-panel {
    background-color: #fff !important;
  }

  .app-modal.open {
    background-image: none !important;
  }

  .app-modal .modal-panel {
    display: flex;
    flex-direction: column;
    width: 100vw !important;
    height: 100dvh !important;
    max-height: 100dvh !important;
    padding:
      calc(env(safe-area-inset-top) + 10px)
      max(18px, calc((100vw - 720px) / 2))
      calc(env(safe-area-inset-bottom) + 12px) !important;
    overflow: hidden !important;
    border-radius: 0 !important;
  }

  .app-modal .modal-close {
    width: 40px;
    height: 40px;
    margin-bottom: 8px;
    background: #f7f7f4;
  }

  /* Keep the first content header clear even on 320 px devices and in large
     Dynamic Type.  Text may wrap; it is never allowed underneath controls. */
  .utility-app-head,
  .workspace-head,
  .diary-header,
  .ai-room-header,
  .memory-app > header {
    gap: 10px;
  }

  .utility-app-head > :first-child,
  .workspace-head > :first-child,
  .diary-header > :first-child,
  .ai-room-header > :first-child,
  .memory-app > header > :first-child {
    min-width: 0;
  }

  .utility-app-head strong,
  .workspace-head strong,
  .diary-header strong,
  .ai-room-header h2,
  .memory-app > header strong {
    overflow-wrap: anywhere;
  }

  /* Focus and music use their own header back controls, so their content may
     start at the safe-area edge without leaving an empty duplicate row. */
  .app-modal .modal-panel:has(#focusApp:not([hidden])),
  .app-modal .modal-panel:has(#musicApp:not([hidden])) {
    padding-top: calc(env(safe-area-inset-top) + 10px) !important;
  }

  .app-modal #focusApp:not([hidden]),
  .app-modal #memoryApp:not([hidden]) {
    padding-top: 0 !important;
  }

  .app-modal #scheduleApp:not([hidden]) > header {
    top: 0;
    background: rgba(255, 255, 255, .96);
    backdrop-filter: blur(18px);
  }

  .app-modal #scheduleApp:not([hidden]) .term-date-strip {
    top: 48px;
    background: rgba(255, 255, 255, .96);
  }

  .app-modal #apiSettings:not([hidden]) {
    padding: 4px 1px calc(30px + env(safe-area-inset-bottom)) !important;
  }

  .app-modal #characterStudio:not([hidden]) {
    padding-top: 4px !important;
  }

  /* Sheets that replace an app view receive their own safe-area inset. */
  .workspace-sheet,
  .focus-ai-picker {
    padding-top: calc(18px + env(safe-area-inset-top));
  }

  .schedule-week-sheet {
    max-height: calc(100% - env(safe-area-inset-top) - env(safe-area-inset-bottom) - 24px);
  }
}
