/* Alpha 0.2: phone safe areas, touch sizing and full-screen app layout.
   The native shell and localhost device preview may override these variables. */
:root {
  --app-safe-top: env(safe-area-inset-top, 0px);
  --app-safe-bottom: env(safe-area-inset-bottom, 0px);
  --app-safe-left: env(safe-area-inset-left, 0px);
  --app-safe-right: env(safe-area-inset-right, 0px);
  --app-nav-reserve: calc(96px + var(--app-safe-bottom));
}

html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
html[data-native=true], html[data-native=true] body { overscroll-behavior: none; }
body { min-height: 100svh; }
html:has(dialog[open]), body:has(dialog[open]) { overflow: hidden; }
html[data-keyboard=open] .bottom-nav { visibility: hidden; pointer-events: none; }

@media (max-width: 800px), (max-height: 550px) {
  html { scroll-padding-top: calc(var(--app-safe-top) + 80px); scroll-padding-bottom: var(--app-nav-reserve); }
  .topbar {
    max-width: none;
    width: 100%;
    height: calc(72px + var(--app-safe-top));
    padding: var(--app-safe-top) calc(18px + var(--app-safe-right)) 0 calc(18px + var(--app-safe-left));
  }
  .top-actions { gap: 8px; flex: none; }
  .icon-button, .avatar { width: 44px; height: 44px; flex: none; }
  .button, .text-link, .segmented button, .search-results a { min-height: 44px; }
  .text-link { align-items: center; }
  .page {
    width: calc(100% - 36px - var(--app-safe-left) - var(--app-safe-right));
    margin-top: 20px;
    padding-bottom: calc(var(--app-nav-reserve) + 28px);
  }
  #toast { bottom: calc(var(--app-nav-reserve) + 4px); max-width: calc(100vw - 32px - var(--app-safe-left) - var(--app-safe-right)); }
  html[data-keyboard=open] #toast { bottom: calc(12px + var(--app-safe-bottom)); }

  /* The original blue scene keeps its composition while the toolbar clears the notch. */
  body[data-page=overview] .topbar {
    height: calc(64px + var(--app-safe-top));
    padding: var(--app-safe-top) calc(12px + var(--app-safe-right)) 0 calc(12px + var(--app-safe-left));
    gap: 6px;
  }
  body[data-page=overview] .panel-menu,
  body[data-page=overview] #notifications,
  body[data-page=overview] .avatar { width: 44px; height: 44px; flex: none; }
  body[data-page=overview] .top-actions { gap: 2px; }
  body[data-page=overview] .panel-search { height: 44px; min-width: 0; max-width: none; padding: 0 10px; margin-right: 0; gap: 8px; font-size: 11px; }
  body[data-page=overview] .panel-search span { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
  body[data-page=overview] #main.content {
    min-height: 100svh;
    padding: calc(64px + var(--app-safe-top)) calc(20px + var(--app-safe-right)) calc(var(--app-nav-reserve) + 30px) calc(20px + var(--app-safe-left));
  }
  body[data-page=overview] .source-overview { min-height: 0; }
  .source-overview .owned-device { min-height: 44px; margin-block: -8px; }
  .source-overview .device-option { min-height: 56px; }
  .source-overview .minimap-expand,
  .source-overview .minimap-info { width: 44px; height: 44px; display: grid; place-items: center; }
  .source-overview .minimap-expand { top: 0; right: 0; }
  .source-overview .minimap-info { bottom: 0; right: 0; }
  .source-overview .quick-tool { min-height: 72px; }

  /* Touch controls and inputs stay usable when the keyboard is present. */
  input:not([type=checkbox]):not([type=radio]):not([type=range]):not([type=file]),
  select, textarea,
  .management-dialog .mg-field input,
  .management-dialog .mg-field select { font-size: 16px !important; min-height: 44px; scroll-margin-block: 100px; }
  input::placeholder, textarea::placeholder { font-size: 14px; }
  #panel-search-input { font-size: 16px; }
  .setting-row > div { flex: 1; min-width: 0; }
  .setting-row .button, .setting-row .icon-button, .setting-row .text-link { flex: none; }
  .setting-row p { overflow-wrap: anywhere; }
  .switch { position: relative; width: 48px; height: 44px; padding: 0; background: transparent; }
  .switch::before { content: ''; position: absolute; top: 8px; left: 0; right: 0; height: 28px; border-radius: 16px; background: #cad9e4; }
  .switch span { position: absolute; top: 13px; left: 5px; width: 18px; height: 18px; }
  .switch[aria-checked=true] { background: transparent; }
  .switch[aria-checked=true]::before { background: #427c9f; }
  .switch[aria-checked=true] span { transform: translateX(20px); }
  .theme-option { min-height: 108px; }
  .source-note { min-height: 44px; flex-wrap: wrap; font-size: 10px; line-height: 1.6; }
  .statistics-page .toolbar { display: grid; grid-template-columns: 1fr; gap: 10px; }
  .statistics-page .segmented { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); width: 100%; }
  .statistics-page .segmented button { font-size: 12px; padding: 10px 8px; }
  .statistics-page .toolbar select { width: 100%; min-width: 0; }
  .statistics-page .metric-card { padding: 16px 12px; }
  .statistics-page .metric-label { font-size: 11px; line-height: 1.4; min-height: 31px; align-items: flex-start; gap: 4px; }
  .statistics-page .metric-value { font-size: 24px; letter-spacing: -1px; }
  .statistics-page .metric-card p { font-size: 10px; line-height: 1.5; min-height: 30px; }
  .statistics-page .chart-note, .statistics-page .bar-column small { font-size: 11px; line-height: 1.5; }
  .statistics-page .table-scroll table { font-size: 12px; }
  .bar-chart { gap: 8px; padding-inline: 8px; }
  .bar-well { width: clamp(36px, 12vw, 65px); }
  .table-scroll { -webkit-overflow-scrolling: touch; overscroll-behavior-x: contain; }

  /* The six career folders remain two columns on phones. */
  .management-page {
    min-height: calc(100svh - 72px - var(--app-safe-top));
    padding: 16px calc(18px + var(--app-safe-right)) calc(var(--app-nav-reserve) + 24px) calc(18px + var(--app-safe-left));
  }
  .mg-close, .mg-icon-button, .mg-history-delete { min-width: 44px; width: 44px; min-height: 44px; height: 44px; }
  .mg-button, .mg-button-small, .management-dialog .mg-button { min-height: 44px; }
  .mg-quiz-actions, .mg-dialog-actions { flex-wrap: wrap; }
  .mg-quiz-facts { flex-wrap: wrap; row-gap: 14px; }
  .mg-payment-fields { min-width: 0; }
  .mg-field { min-width: 0; }
  .mg-payment-result dd { font-variant-numeric: tabular-nums; }

  #app-dialog,
  .management-dialog {
    top: calc(var(--app-safe-top) + 8px);
    bottom: calc(var(--app-safe-bottom) + 8px);
    left: calc(var(--app-safe-left) + 8px);
    right: calc(var(--app-safe-right) + 8px);
    margin: auto;
    width: auto;
    max-width: none;
    height: max-content;
    max-height: calc(var(--app-viewport-height, 100dvh) - var(--app-safe-top) - var(--app-safe-bottom) - 16px);
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }
  #app-dialog { padding: 22px 20px; }
  .dialog-head { gap: 12px; }
  .dialog-head h2 { font-size: 21px; }
  .mg-dialog-header { padding: 18px 18px 16px; gap: 12px; }
  .mg-dialog-content { padding: 22px 18px 26px; }
  .management-dialog.mg-dialog-documents,
  .management-dialog.mg-dialog-images {
    width: auto;
    height: calc(var(--app-viewport-height, 100dvh) - var(--app-safe-top) - var(--app-safe-bottom) - 16px);
    max-height: calc(var(--app-viewport-height, 100dvh) - var(--app-safe-top) - var(--app-safe-bottom) - 16px);
    overflow: hidden;
    border-radius: 18px;
  }
  .management-dialog .mg-library-content { padding: 0; }
  .search-results a { font-size: 14px; }
}

@media (max-width: 600px) {
  body[data-page=overview] .source-overview .device-hero {
    min-height: clamp(576px, calc(100svh - var(--app-safe-top) - var(--app-safe-bottom) - 160px), 676px);
  }
  .source-overview .device-welcome .device-identity { margin-top: 14px; gap: 8px; }
  .source-overview .device-welcome .device-options { max-width: calc(100vw - 40px - var(--app-safe-left) - var(--app-safe-right)); }
  .source-overview .device-stat { min-width: 0; }
  .source-overview .device-stat .stat-text { font-size: clamp(18px, 5vw, 22px); }
  .source-overview .device-welcome h1 { line-height: 1.2; }
}

@media (max-width: 600px) and (max-height: 850px) {
  .source-overview .product-stage { height: 340px; }
  .source-overview .product-figure.heroshot-product:not(.custom-device-photo) {
    width: min(205px, 51vw);
    left: 50%;
    transform: translateX(-50%);
  }
}

@media (max-width: 370px) {
  body[data-page=overview] #main.content { padding-inline: calc(16px + var(--app-safe-left)) calc(16px + var(--app-safe-right)); }
  body[data-page=overview] .source-overview .device-welcome h1 { font-size: 29px; }
  body[data-page=overview] .panel-search { gap: 5px; padding-inline: 8px; }
  .source-overview .device-stat { padding-inline: 15px; }
  .source-overview .stat-label { font-size: 9px; gap: 5px; }
  .statistics-page .metrics-row { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .statistics-page .metric-card:last-child { grid-column: 1 / -1; }
  .mg-payment-fields { grid-template-columns: 1fr; }
  .mg-folder-title strong { font-size: 16px; }
  .mg-folder-front { padding-inline: 12px; }
  .management-page { padding-inline: calc(14px + var(--app-safe-left)) calc(14px + var(--app-safe-right)); }
}

/* Landscape keeps the scene usable and leaves room beside the camera housing. */
@media (orientation: landscape) and (max-height: 550px) {
  .topbar { height: calc(60px + var(--app-safe-top)); padding-top: var(--app-safe-top); padding-inline: calc(16px + var(--app-safe-left)) calc(16px + var(--app-safe-right)); }
  body[data-page=overview] .topbar { height: calc(60px + var(--app-safe-top)); }
  body[data-page=overview] #main.content { padding-top: calc(60px + var(--app-safe-top)); padding-inline: calc(24px + var(--app-safe-left)) calc(24px + var(--app-safe-right)); }
  .page { width: calc(100% - 40px - var(--app-safe-left) - var(--app-safe-right)); }
  body[data-page=overview] .source-overview .device-hero { display: grid; grid-template-columns: 1fr 1fr; min-height: 380px; }
  .source-overview .device-welcome { padding-top: 20px; padding-bottom: 20px; }
  .source-overview .device-welcome h1 { font-size: 32px; }
  .source-overview .device-location-block { position: relative; inset: auto; margin-top: 18px; width: 100%; }
  .source-overview .minimal-map .device-map-viewport { height: 110px; }
  body[data-page=overview] .source-overview .product-stage { position: relative; right: auto; bottom: auto; width: 100%; height: 360px; }
  .source-overview .product-figure.heroshot-product:not(.custom-device-photo) { width: 210px; left: 50%; top: 12px; transform: translateX(-50%); }
  .management-page { padding-top: 12px; }
  .mg-folder-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); grid-template-rows: repeat(2, minmax(185px, 1fr)); }
  .mg-folder-card { min-height: 185px; }
  .mg-folder-art { inset: 0 -4px 65px; }
  .mg-folder-front { height: 73px; padding: 23px 14px 12px; }
  .mg-folder-title strong { font-size: 18px; }
  .mg-folder-meta { font-size: 10px; }
}
