/* ============================================================
 * MZ Cards Layout
 * App container, header, sidebar, main, responsive, foldable.
 * Phase 3 of UI Modernization.
 *
 * Extracted from app_container.css + im_styles.css layout sections.
 * ============================================================ */

/* === App Container === */
#app-container {
  border: none;
  background: var(--surface);
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 1200px;
  margin: auto;
  position: relative;
  padding-left: max(0, env(safe-area-inset-left));
  padding-right: max(0, env(safe-area-inset-right));
}

#app-container.wide {
  max-width: 1200px;
}

@media screen and (min-width: 1000px) {
  #app-container {
    max-width: 100%;
  }
}

@media screen and (max-width: 600px) {
  #app-container {
    max-width: 100%;
    margin: 0;
    border-left: none;
    border-right: none;
  }
}

#app-container > header {
  flex: 0 0 auto;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
  padding: 0.7em 1em;
  font-size: 0.95em;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.1em 0.15em;
}

#app-container > header a {
  color: var(--text-muted);
  padding: 0.3em 0.55em;
  border-radius: var(--radius-md);
  transition: background-color var(--transition-base), color var(--transition-base);
}

#app-container > header a.header-app-icon-link {
  align-items: center;
  line-height: 1;
}

#app-container > header img.header-app-icon {
  display: block;
  width: 1.5em;
  height: 1.5em;
}

#app-container > header a:hover {
  background: var(--surface-3);
  color: var(--text);
  text-decoration: none;
}

#app-container > header strong {
  color: var(--text);
  font-weight: var(--font-semibold);
}

#app-container > footer {
  flex: 0 0 auto;
}

.footer-links-right {
  text-align: right;
  padding: 4px 0;
}

#app-container > footer:has(.ios-float-nav) {
  flex: 0 0 0;
  height: 0;
  overflow: visible;
}

/* === Desktop Container (sidebar + main) === */
#desktop-container {
  flex: 1 1 auto;
  display: flex;
  flex-direction: row;
  align-items: stretch;
  flex: 1;
  overflow: hidden;
}

#desktop-container aside {
  background: var(--surface-2);
  border-right: 1px solid var(--border);
  flex: 0 0 280px !important;
  overflow-y: auto;
  overflow-x: hidden;
}

#desktop-container main {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 0 1em;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  min-width: 0;
  word-wrap: break-word;
  word-break: break-word;
}

#app-container:has(.ios-float-nav) #desktop-container main {
  padding-bottom: calc(74px + env(safe-area-inset-bottom, 0px));
}

/* === Mobile floating nav (iOS-style) === */
.ios-float-nav {
  position: fixed;
  left: 50%;
  bottom: calc(8px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  z-index: 1200;
  width: min(420px, calc(100vw - 35vw));
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  align-items: center;
  gap: 4px;
  padding: 5px;
  border-radius: var(--radius-full);
  overflow: hidden;
  background: rgba(249, 250, 251, 0.78);
  border: 1px solid rgba(15, 23, 42, 0.06);
  box-shadow: var(--shadow-lg), inset 0 1px 0 rgba(255, 255, 255, 0.7);
  -webkit-backdrop-filter: saturate(165%) blur(20px);
  backdrop-filter: saturate(165%) blur(20px);
}

.ios-float-nav-item {
  height: 44px;
  border-radius: var(--radius-full);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  text-decoration: none;
  transition: background-color var(--transition-slow), transform var(--transition-slow), color var(--transition-slow);
  -webkit-tap-highlight-color: transparent;
}

.ios-float-nav-item:hover {
  color: var(--accent-hover);
  background: var(--accent-soft);
  border-radius: var(--radius-full);
}

.ios-float-nav-item:active {
  transform: scale(0.97);
}

.ios-float-nav-item.is-active {
  color: var(--accent-contrast);
  background: var(--accent);
  box-shadow: 0 6px 14px rgba(79, 70, 229, 0.35);
}

.ios-float-nav-icon {
  font-size: 1.28rem;
  line-height: 1;
  font-weight: var(--font-medium);
  transform: translateY(-1px);
}

/* === Scrollbar === */
#desktop-container aside::-webkit-scrollbar,
#desktop-container main::-webkit-scrollbar {
  width: 6px;
}

#desktop-container aside::-webkit-scrollbar-track,
#desktop-container main::-webkit-scrollbar-track {
  background: transparent;
}

#desktop-container aside::-webkit-scrollbar-thumb,
#desktop-container main::-webkit-scrollbar-thumb {
  background: var(--border-strong);
  border-radius: 3px;
}

#desktop-container aside::-webkit-scrollbar-thumb:hover,
#desktop-container main::-webkit-scrollbar-thumb:hover {
  background: var(--text-subtle);
}

/* === Responsive: hide sidebar on small screens === */
@media screen and (max-width: 999px) {
  #desktop-container aside {
    display: none;
  }

  #desktop-container {
    flex-direction: column;
  }

  .comment-bubble {
    max-width: 90%;
  }

  .cards-grid {
    grid-template-columns: 1fr;
    padding: 4px;
    gap: 4px;
  }

  #app-container {
    height: 100dvh;
  }

  #app-container > header {
    padding: 0.4em 0.4em;
    font-size: 0.8em;
  }

  #app-container > header a {
    padding: 0.15em 0.3em;
  }

  #desktop-container main {
    padding: 0;
  }

  .card > .card-bubble {
    margin: 4px;
    padding: 8px 10px;
    border-radius: 3px;
  }

  .comment-show {
    margin: 4px;
  }

  .button_to_group,
  .grouped-links {
    margin: 4px;
  }

  .button_to_group button,
  .button_to_group input[type="submit"] {
    padding: 4px 8px;
    font-size: 0.8em;
  }

  .send-message-footer {
    padding: 6px;
  }

  .send-message-footer textarea {
    padding: 8px;
  }

  .pagy {
    margin: 4px;
  }

  table {
    margin: 4px;
    width: calc(100% - 8px);
  }

  .simple-calendar,
  .calendar-nav,
  .switch-view-group,
  .deck-header {
    margin: 4px !important;
    padding: 8px !important;
  }

  ul.deck-list li > a {
    padding: 10px 8px;
  }

  .deck-profile-pic {
    width: 36px;
    height: 36px;
    min-width: 36px;
    font-size: 1em;
  }

  .ios-float-nav {
    width: min(420px, calc(100vw - 35vw));
    bottom: calc(8px + env(safe-area-inset-bottom, 0px));
    gap: 3px;
    padding: 4px;
  }

  .ios-float-nav-item {
    height: 40px;
  }

  .ios-float-nav-icon {
    font-size: 1.16rem;
  }

  #app-container:has(.ios-float-nav) #desktop-container main {
    padding-bottom: calc(66px + env(safe-area-inset-bottom, 0px));
  }

  .page-header {
    margin: 4px;
  }

  .page-header h1 {
    font-size: 1.1em;
  }

  .page-toolbar {
    margin: 4px;
    gap: 4px;
  }

  .page-toolbar .btn-primary,
  .page-toolbar .btn-secondary {
    padding: 4px 8px;
    font-size: 0.85em;
  }

  .deck-header-container {
    margin: 4px;
    padding: 10px;
  }

  .deck-title-row {
    flex-wrap: wrap;
    gap: 8px;
  }

  .deck-title-row h1 {
    font-size: 1.1em;
    flex: 1 1 auto;
    min-width: 0;
  }

  .deck-header-actions {
    gap: 8px;
    flex-wrap: nowrap;
  }

  .btn-icon {
    padding: 4px 6px;
    font-size: 0.9em;
  }

  .switch-view-group.compact {
    padding: 2px 6px;
    gap: 6px;
  }

  .switch-view-group.compact label {
    font-size: 0.8em;
  }

  .switch-view-group.compact input[type="radio"] {
    width: 14px;
    height: 14px;
  }

  .deck-description {
    font-size: 0.85em;
  }

  .column-filter {
    margin: 4px;
  }

  .column-filter > a {
    padding: 4px 8px;
    font-size: 0.8em;
  }

  .calendar-link {
    margin: 4px;
  }

  .pagination-top,
  .pagination-bottom {
    margin: 4px;
  }

  .card-meta-label {
    font-size: 0.7em;
    gap: 4px;
  }

  .card-title {
    font-size: 0.95em;
    margin-bottom: 6px;
  }

  .ai-response-section {
    margin-top: 10px;
    padding-top: 10px;
  }

  .ai-content {
    padding: 8px;
  }

  .column-switch-group,
  .mark-actions-group {
    padding: 6px 8px;
  }

  .column-switch-group button,
  .mark-actions-group button {
    padding: 3px 6px;
    font-size: 0.75em;
    min-width: auto;
  }

  .reference-thread-title {
    margin: 10px 4px;
  }
}

/* === Foldable device: single screen (folded mode only) ===
   Note: horizontal-viewport-segments: 1 matches ALL non-foldable
   devices too, so we gate on device-posture: folded to avoid hiding
   the sidebar on regular desktops. === */
@media (horizontal-viewport-segments: 1) and (device-posture: folded) {
  #desktop-container aside {
    display: none;
  }

  #app-container {
    max-width: 100%;
    margin: 0;
    border-left: none;
    border-right: none;
  }
}

/* === Foldable device: dual screen === */
@media (horizontal-viewport-segments: 2) {
  #app-container {
    max-width: 100%;
  }

  #desktop-container {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: max(env(fold-width, 0), 1rem);
  }

  aside {
    grid-column: 1;
    display: block !important;
    border-right: none;
  }

  main {
    grid-column: 2;
  }
}

/* === Foldable posture === */
@media (device-posture: folded) {
  #app-container {
    max-width: 100%;
    margin: 0;
    border-left: none;
    border-right: none;
  }

  #desktop-container aside {
    display: none;
  }
}

/* === Dark mode layout overrides === */
html[data-theme="dark"] body {
  background: var(--bg);
  color: var(--text);
}

html[data-theme="dark"] #app-container {
  background: var(--surface);
  box-shadow: 0 0 24px rgba(0, 0, 0, 0.45);
}

html[data-theme="dark"] #app-container > header {
  background: var(--surface-2);
  border-bottom-color: var(--border);
}

html[data-theme="dark"] #desktop-container aside {
  background: var(--surface-2);
  border-right-color: var(--border);
}

html[data-theme="dark"] #app-container > header a,
html[data-theme="dark"] ul.deck-list li > a {
  color: var(--text);
}

html[data-theme="dark"] #app-container > header a:hover {
  background: var(--surface-3);
}

html[data-theme="dark"] #desktop-container aside::-webkit-scrollbar-thumb,
html[data-theme="dark"] #desktop-container main::-webkit-scrollbar-thumb {
  background: var(--border-strong);
}

html[data-theme="dark"] .ios-float-nav {
  background: rgba(15, 17, 21, 0.82);
  border-color: var(--accent-border);
  box-shadow: var(--shadow-lg), inset 0 1px 0 rgba(255, 255, 255, 0.06);
}

html[data-theme="dark"] .ios-float-nav-item {
  color: rgba(209, 223, 241, 0.92);
}

html[data-theme="dark"] .ios-float-nav-item:hover {
  color: var(--accent-hover);
  background: var(--accent-soft);
}

html[data-theme="dark"] a:link,
html[data-theme="dark"] a:visited {
  color: var(--accent);
}

html[data-theme="dark"] a:hover {
  background: var(--accent-soft);
}

html[data-theme="dark"] table,
html[data-theme="dark"] table th,
html[data-theme="dark"] table td {
  border-color: var(--border);
}

html[data-theme="dark"] table {
  background: var(--surface);
}

html[data-theme="dark"] table th {
  background: var(--surface-3);
  color: var(--text);
}

html[data-theme="dark"] table tr:nth-child(even) {
  background: var(--surface-2);
}

html[data-theme="dark"] table tr:hover {
  background: var(--surface-3);
}

html[data-theme="dark"] .deck-list-filter-input,
html[data-theme="dark"] input:not([type]),
html[data-theme="dark"] input[type="text"],
html[data-theme="dark"] input[type="email"],
html[data-theme="dark"] input[type="password"],
html[data-theme="dark"] input[type="search"],
html[data-theme="dark"] input[type="url"],
html[data-theme="dark"] input[type="number"],
html[data-theme="dark"] input[type="date"],
html[data-theme="dark"] input[type="datetime-local"],
html[data-theme="dark"] input[type="time"],
html[data-theme="dark"] input[type="month"],
html[data-theme="dark"] input[type="week"],
html[data-theme="dark"] textarea,
html[data-theme="dark"] select {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
}

html[data-theme="dark"] ul.deck-list li:hover {
  background: var(--surface-3);
}

html[data-theme="dark"] ul.deck-list li:active,
html[data-theme="dark"] ul.deck-list li.active {
  background: var(--surface-3);
}

html[data-theme="dark"] .notice {
  color: #9bf0be;
  background: rgba(16, 40, 29, 0.6);
  border-color: #2e5d44;
}

html[data-theme="dark"] trix-toolbar {
  background: var(--surface);
}

/* === App Header (Phase 8 restructure) === */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
  flex-wrap: wrap;
}

.app-header__left {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  min-width: 0;
  flex: 1;
}

.app-header__right {
  flex-shrink: 0;
}

.app-nav {
  display: flex;
  align-items: center;
  gap: 0;
}

.app-nav__link {
  padding: 0.3em 0.55em;
  border-radius: var(--radius-md);
  color: var(--text-muted);
  text-decoration: none;
  font-size: var(--text-sm);
  transition: background var(--transition-base), color var(--transition-base);
}

.app-nav__link:hover {
  background: var(--surface-3);
  color: var(--text);
  text-decoration: none;
}

.breadcrumb {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  font-size: var(--text-sm);
  color: var(--text-muted);
  min-width: 0;
}

.breadcrumb a {
  color: var(--text-muted);
  text-decoration: none;
}

.breadcrumb a:hover {
  color: var(--accent);
  text-decoration: none;
}

.breadcrumb__separator {
  color: var(--text-subtle);
}

.breadcrumb__add {
  font-size: 1.1em;
  line-height: 1;
  padding: 0 0.2em;
}

.app-header__title {
  font-weight: var(--font-semibold);
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Responsive: compact header on small screens */
@media screen and (max-width: 999px) {
  .app-header {
    padding: 0.4em 0.4em;
    font-size: 0.8em;
  }

  .app-nav__link {
    padding: 0.15em 0.3em;
    font-size: 0.85em;
  }

  .app-header__right {
    gap: 0;
  }

  .app-header__right .btn-icon {
    padding: 4px 6px;
    font-size: 0.9em;
  }
}
