/* V1.0.2：iPhone / Samsung Galaxy 手機版面完整整理 */
:root {
  --mobile-safe-top: max(8px, env(safe-area-inset-top));
  --mobile-safe-right: max(7px, env(safe-area-inset-right));
  --mobile-safe-bottom: max(12px, env(safe-area-inset-bottom));
  --mobile-safe-left: max(7px, env(safe-area-inset-left));
}

html {
  min-width: 0;
  min-height: 100%;
  overflow-x: clip;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body,
.app,
.board,
.screen,
.screen > *,
.btn-row,
.system-shell,
.world-hub-shell,
.portable-home-shell,
.building-interior-shell,
.course-portal-grid,
.course-portal-detail,
.chapter-list,
.demo-card,
.story-box,
.explain-box,
.options {
  min-width: 0;
}

button,
input,
select,
textarea {
  max-width: 100%;
}

.btn,
.pick-card,
.chapter-card,
.stage-card,
.opt-btn,
.setting-row,
.manual-save-slot,
.resident-interaction-content,
.resident-collection-detail,
.world-card,
.course-portal-card {
  overflow-wrap: anywhere;
  word-break: normal;
}

.title-screen-dialog,
.asset-preload-card,
.game-modal-card,
.resident-collection-modal,
.resident-interaction-modal {
  max-width: calc(100vw - var(--mobile-safe-left) - var(--mobile-safe-right) - 12px);
}

@media (max-width: 600px) {
  body:not(.title-screen-open) {
    display: block;
    min-width: 0;
    min-height: 100dvh;
    padding: var(--mobile-safe-top) var(--mobile-safe-right)
      calc(var(--mobile-safe-bottom) + 18px) var(--mobile-safe-left);
    overflow-x: clip;
  }

  body:not(.title-screen-open) .app {
    width: 100%;
    max-width: 100%;
    margin: 0 auto;
  }

  .crumb {
    margin: 0 2px 7px;
    font-size: 11px;
    line-height: 1.35;
  }

  .topbar {
    display: grid;
    grid-template-columns: minmax(58px, 1fr) minmax(70px, 1fr) minmax(70px, 1fr);
    gap: 5px;
    margin: 0 0 8px;
    padding: 7px 8px;
    border-width: 2px;
    border-radius: 13px;
    font-size: 12px;
  }

  .topbar .group {
    min-width: 0;
    justify-content: center;
    gap: 4px;
    white-space: nowrap;
  }

  .heart { width: 16px; height: 16px; }
  .hearts { gap: 2px; }
  .progress-track { grid-column: 1 / -1; width: 100%; gap: 3px; }

  .board {
    width: 100%;
    min-height: calc(100dvh - var(--mobile-safe-top) - var(--mobile-safe-bottom) - 40px);
    padding: 13px 10px 16px;
    border-width: 2px;
    border-radius: 17px;
  }

  .board::before { inset: 5px; border-radius: 13px; }
  .screen { width: 100%; gap: 11px; }
  .screen.active { width: 100%; }

  h1.title,
  .screen h1.title[style] {
    max-width: 100%;
    margin: 1px 0 0 !important;
    font-size: clamp(22px, 7vw, 28px) !important;
    line-height: 1.18;
  }

  .subtitle {
    width: 100%;
    margin: 0;
    font-size: 12px;
    line-height: 1.55;
  }

  .chalk-badge {
    max-width: 100%;
    padding: 4px 10px;
    font-size: 11px;
    line-height: 1.3;
    white-space: normal;
  }

  .btn {
    min-width: 0;
    min-height: 44px;
    padding: 9px 12px;
    border-width: 2px;
    border-radius: 12px;
    font-size: 15px;
    line-height: 1.25;
    transform: none;
    touch-action: manipulation;
  }

  .btn.small { min-height: 42px; padding: 8px 11px; font-size: 13px; }
  .btn:hover { transform: none; }
  .btn-row { width: 100%; gap: 8px; }
  .btn-row > .btn { flex: 1 1 140px; }

  .profile-grid { gap: 9px; }
  .profile-card { width: 100%; min-height: 0; padding: 12px 10px; border-radius: 15px; }
  .profile-avatar { font-size: 36px; }
  .profile-card b { font-size: 16px; }
  .profile-card small { font-size: 11px; line-height: 1.45; }
  .profile-emergency-actions { width: 100%; margin-top: 8px; }
  .profile-emergency-actions .btn { width: 100%; }

  .setup-form { padding: 14px 12px; border-radius: 15px; }
  .setup-form label { margin-top: 9px; }
  .setup-form input,
  .setup-form select { min-height: 46px; font-size: 16px; }
  .hero-style-picker { gap: 9px; }
  .hero-style-card { min-height: 82px; grid-template-columns: 62px minmax(0, 1fr); padding: 8px 9px; }
  .hero-style-card img { width: 60px; height: 66px; }
  .hero-style-card b { font-size: 14px; }
  .hero-style-card small { font-size: 10px; line-height: 1.4; }
  #screen-profile-setup > .btn-row { display: grid; grid-template-columns: 1fr 1fr; }
  #screen-profile-setup > .btn-row .danger-btn { grid-column: 1 / -1; }

  .player-chip { width: 100%; padding: 9px; gap: 8px; }
  .player-chip > div { width: 100%; min-width: 0; }
  .player-chip > div:last-child { display: grid; grid-template-columns: 1fr 1fr; gap: 7px; }
  .player-chip > div:last-child .btn { width: 100%; }
  .player-stamina-row { align-items: stretch; }
  .stamina-recover-btn { flex: 1 1 150px; white-space: normal; }

  .home-world-selector { padding: 9px; gap: 8px; }
  .home-world-selector-title { display: block; }
  .home-world-selector-title small { display: block; margin-top: 3px; text-align: left; }
  .home-world-selector-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 5px; }
  .home-world-selector-grid button { min-width: 0; min-height: 68px; padding: 6px 2px; border-radius: 12px; }
  .home-world-selector-grid button span { font-size: 21px; }
  .home-world-selector-grid button b { font-size: 11px; }
  .home-world-selector-grid button small { font-size: 7px; overflow-wrap: anywhere; }
  .home-world-summary { padding: 9px; }
  .home-world-summary-stats { grid-template-columns: 1fr 1fr; }

  .hero-panel { width: 100%; grid-template-columns: 58px minmax(0, 1fr); gap: 8px; padding: 10px; }
  .hero-avatar { width: 56px; height: 56px; }
  .wallet { grid-column: 1 / -1; display: flex; justify-content: center; gap: 20px; text-align: center; }
  .main-menu { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; gap: 8px; }
  .main-menu .btn { width: 100%; min-height: 48px; padding: 8px 6px; font-size: 13px; }
  .main-menu #btnSystemSettings { grid-column: 1 / -1; }
  .rest-note { width: 100%; padding: 9px 10px; font-size: 11px; line-height: 1.5; }

  .setting-row { align-items: flex-start; padding: 9px; }
  .setting-row > span { min-width: 0; }
  .setting-volume-row { flex-direction: column; align-items: stretch; }
  .setting-volume { width: 100%; min-height: 34px; }
  .system-section { width: 100%; padding: 11px; border-radius: 15px; }
  .system-section h2 { font-size: 16px; }
  .system-section > p { font-size: 11px; }
  .backup-actions { grid-template-columns: 1fr; }
  .backup-mode { grid-template-columns: 1fr; }

  .course-portal-grid,
  .world-card-grid,
  .shop-grid { grid-template-columns: 1fr; gap: 9px; }
  .course-portal-card,
  .world-card,
  .shop-item { width: 100%; min-width: 0; }

  .world-map { width: 100%; padding-inline: 0; }
  .stage-node { grid-template-columns: minmax(0, 1fr) 54px minmax(0, 1fr); gap: 5px; }
  .stage-orb { width: 46px; height: 46px; border-width: 3px; font-size: 12px; }
  .stage-card { min-width: 0; padding: 9px 8px; }
  .stage-card b { font-size: 12px; }
  .stage-card small { font-size: 9px; line-height: 1.4; }

  .village-scene,
  .world-hub-scene,
  .portable-home-room,
  .building-room { max-width: 100%; }
  .village-scene { padding: 10px; border-width: 2px; border-radius: 16px; }
  .village-world-wrap { margin-top: 10px; }
  .village-status-strip { gap: 5px; }
  .village-status-strip div { padding: 6px 4px; }
  .village-building-detail { padding: 10px; }
  .village-section-title { display: block; }
  .village-section-title small { display: block; margin-top: 3px; }
  .resident-list,
  .world-resident-list { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 7px; }
  .resident-card { min-width: 0; }
  .growth-row,
  .world-travel-summary,
  .world-hub-stats { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px; }
  .growth-card { min-width: 0; padding: 8px 5px; }
  .village-actions,
  .world-hub-actions { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 7px; }
  .village-actions .btn,
  .world-hub-actions .btn { width: 100%; }
  .world-hub-title-row { width: 100%; align-items: stretch; }
  .world-test-controls { width: 100%; }
  .world-hub-shell { width: 100%; }
  .junior-course-panel,
  .upper-secondary-pathway,
  .university-pathway { min-width: 0; padding: 10px; }
  .pathway-route-grid,
  .university-route-grid,
  .university-track-grid,
  .university-field-grid,
  .university-field-overview { grid-template-columns: 1fr; }
  .world-bond-actions { grid-template-columns: 1fr; }

  .portable-home-room { min-height: 260px; border-radius: 17px; }
  .portable-collection { grid-template-columns: 1fr; }
  .building-room { height: 220px; border-radius: 16px; }
  .building-info-card { padding: 11px; }

  .grid-grade { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
  .pick-card { min-width: 0; min-height: 68px; padding: 13px 5px; font-size: 15px; }
  .semester-row { display: grid; grid-template-columns: 1fr 1fr; width: 100%; gap: 8px; }
  .semester-row .pick-card { width: 100%; padding: 15px 7px; font-size: 17px; }
  .chapter-card { gap: 8px; padding: 11px 10px; }
  .chapter-num { min-width: 26px; font-size: 17px; }
  .chapter-body .ct { font-size: 13px; }
  .chapter-body .cf { font-size: 10px; line-height: 1.45; }
  .chapter-tag { max-width: 34%; white-space: normal; text-align: center; font-size: 9px; }

  .demo-card { padding: 15px 11px; border-radius: 15px; }
  .demo-caption { min-height: 0; font-size: 12px; line-height: 1.55; }
  #quizContent { width: 100% !important; min-width: 0; gap: 10px !important; }
  .enemy-panel { width: 100%; padding: 9px; }
  .enemy-row { min-width: 0; gap: 7px; }
  .enemy-row > div:first-child { min-width: 0; text-align: left; }
  .enemy-art { flex: 0 0 auto; font-size: 42px; }
  .story-box,
  .explain-box { width: 100%; max-width: 100%; padding: 11px 12px; font-size: 12px; line-height: 1.6; }
  .expr-row { max-width: 100%; padding: 7px 3px; overflow-wrap: anywhere; }
  .options { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 9px; }
  .opt-btn { min-width: 0; min-height: 52px; padding: 11px 7px; font-size: clamp(16px, 5vw, 19px); line-height: 1.3; }
  .feedback { max-width: 100%; min-height: 23px; font-size: 14px; line-height: 1.45; }
  .explain-box.detailed { padding: 11px; font-size: 13px; }
  .explain-heading { font-size: 16px; }
  .explain-section { padding: 9px; }

  .stat-row { display: grid; grid-template-columns: 1fr 1fr; width: 100%; gap: 8px; }
  .stat-row > div { min-width: 0; padding: 8px 4px; border-radius: 11px; background: rgba(255,255,255,.06); }
  .stat-row > div:last-child { grid-column: 1 / -1; }
  .stars { max-width: 100%; font-size: 30px; letter-spacing: 3px; }
  .reward-box { width: 100%; padding: 12px; }

  .asset-preload-screen,
  .game-modal,
  .collection-modal-overlay,
  .resident-interaction-overlay {
    padding: var(--mobile-safe-top) var(--mobile-safe-right) var(--mobile-safe-bottom) var(--mobile-safe-left);
  }

  .asset-preload-card { max-height: calc(100dvh - var(--mobile-safe-top) - var(--mobile-safe-bottom)); overflow: auto; padding: 20px 15px; }
  .asset-preload-icon { font-size: 38px; }
  .asset-preload-card h1 { font-size: 24px; }
  .asset-preload-card p { font-size: 13px; }

  .game-modal { align-items: center; overflow: hidden; }
  .game-modal-card {
    width: 100%;
    max-height: calc(100dvh - var(--mobile-safe-top) - var(--mobile-safe-bottom));
    overflow: auto;
    padding: 16px 12px;
    border-radius: 16px;
    overscroll-behavior: contain;
  }
  .game-modal-card h2 { font-size: 21px; }
  .game-modal-card .btn-row { display: grid; grid-template-columns: 1fr; }
  .game-modal-card .btn { width: 100%; }
  .manual-save-slot { grid-template-columns: 38px minmax(0, 1fr); gap: 8px; padding: 9px; }
  .manual-save-slot .save-action { grid-column: 2; white-space: normal; }

  .resident-collection-modal {
    width: 100%;
    max-height: calc(100dvh - var(--mobile-safe-top) - var(--mobile-safe-bottom));
    padding: 48px 10px 12px;
    border-radius: 18px;
    overscroll-behavior: contain;
  }
  .collection-modal-close { top: 8px; right: 8px; width: 38px; height: 38px; }
  .collection-world-tabs { padding-right: 0; }
  .collection-world-tabs button { min-height: 34px; padding: 5px 9px; }
  .resident-collection-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .resident-collection-detail { grid-template-columns: 58px minmax(0, 1fr); }
  .resident-collection-detail img { width: 58px; height: 58px; }

  .resident-interaction-modal {
    width: 100%;
    max-height: calc(100dvh - var(--mobile-safe-top) - var(--mobile-safe-bottom));
    overflow: auto;
    overscroll-behavior: contain;
  }

  footer { margin-top: 12px; padding-bottom: var(--mobile-safe-bottom); font-size: 10px; }
  .app-toast { bottom: calc(var(--mobile-safe-bottom) + 8px); width: calc(100vw - 20px); }
}

@media (max-width: 390px) {
  .board { padding-inline: 8px; }
  .topbar { grid-template-columns: minmax(52px, .9fr) minmax(66px, 1fr) minmax(66px, 1fr); padding-inline: 6px; font-size: 11px; }
  .heart { width: 14px; height: 14px; }
  .home-world-selector-grid button { min-height: 63px; }
  .home-world-summary-head { grid-template-columns: 34px minmax(0, 1fr); }
  .home-world-summary-head em { grid-column: 1 / -1; justify-self: start; }
  .grid-grade { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .options { grid-template-columns: 1fr; }
  .opt-btn { min-height: 48px; }
  .resident-interaction-modal { grid-template-columns: 1fr; justify-items: center; gap: 8px; padding: 48px 12px 14px; text-align: center; }
  .resident-interaction-portrait { width: 76px; height: 76px; }
  .resident-interaction-content h2 { margin-right: 0; }
  .resident-interaction-content .btn-row { display: grid; grid-template-columns: 1fr; }
  .resident-interaction-content .btn { width: 100%; }
}

@media (orientation: landscape) and (max-height: 520px) {
  body:not(.title-screen-open) { padding-top: 6px; padding-bottom: 8px; }
  .board { min-height: calc(100dvh - 12px); padding: 9px; }
  .screen { gap: 8px; }
  .topbar { margin-bottom: 6px; padding-block: 5px; }
  .asset-preload-card,
  .game-modal-card,
  .resident-collection-modal,
  .resident-interaction-modal { max-height: calc(100dvh - 12px); }
}

@supports not (height: 100dvh) {
  body:not(.title-screen-open) { min-height: 100vh; }
  .board { min-height: calc(100vh - 40px); }
  .game-modal-card,
  .resident-collection-modal,
  .resident-interaction-modal { max-height: calc(100vh - 20px); }
}
