/* ==========================================================================
   《未央・雙北同居手札》Dedicated Mobile & Touch Edition Stylesheet
   Optimized for iOS Safari / Android Chrome portrait & landscape screens
   ========================================================================== */

:root {
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

html, body {
  width: 100%;
  height: 100dvh;
  min-height: 100dvh;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* Prevent iOS Safari automatic zoom on input/select focus */
input, select, textarea {
  font-size: 16px !important;
}

/* Smooth touch scrollbars */
.story-scroll,
.modal-body,
.line-feed,
.status-list,
.style-controls,
.quick-actions,
.hud-group {
  -webkit-overflow-scrolling: touch;
}

/* ==========================================================================
   TITLE SCREEN — MOBILE SINGLE-COLUMN FLOW
   ========================================================================== */
#screen-title {
  padding: calc(10px + var(--safe-top)) 10px calc(72px + var(--safe-bottom));
  height: 100dvh;
  align-items: flex-start;
}

.title-shell {
  width: 100%;
  max-width: 560px;
  height: auto;
  min-height: 0;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  border-radius: 20px;
  overflow: visible;
}

.title-preview {
  height: auto;
  border-right: none;
  border-bottom: 1px solid var(--border);
  padding: 16px 16px 0;
  border-radius: 20px 20px 0 0;
}

.brand-tag {
  font-size: 10.5px;
  letter-spacing: 0.16em;
}

.game-logo {
  font-size: 26px;
}

.game-subtitle {
  font-size: 12.5px;
  line-height: 1.5;
}

.preview-portrait-wrap {
  height: 210px;
  flex: none;
  margin-top: 6px;
}

.preview-portrait-wrap img {
  max-height: 210px;
}

.preview-badge {
  bottom: 8px;
  font-size: 11.5px;
  padding: 4px 12px;
}

.title-form {
  padding: 16px 14px 20px;
  overflow: visible;
  gap: 12px;
}

.gender-cards {
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.gender-card {
  padding: 10px;
}

.gc-title {
  font-size: 13px;
}

.gc-desc {
  font-size: 11px;
  line-height: 1.4;
}

.diff-grid {
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

.form-grid {
  grid-template-columns: 1fr;
  gap: 10px;
}

.field input[type="text"],
.field input[type="password"],
.field input[type="number"],
.field select {
  padding: 10px 12px;
  min-height: 44px;
}

.title-actions {
  flex-direction: column;
  gap: 9px;
}

.title-actions .btn,
.title-actions label.btn {
  width: 100%;
  min-height: 46px;
  font-size: 15px;
}

/* ==========================================================================
   MAIN GAME STAGE — COMPACT MOBILE HUD + FRAMED PORTRAIT + THUMB DOCK
   ========================================================================== */
#screen-game {
  position: relative;
  height: 100dvh;
  width: 100vw;
  max-width: 100vw;
  overflow: hidden;
}

.stage-bg,
.stage-vignette {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100dvh;
  animation: none !important;
  transform: none !important;
}

.top-hud {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  transform: none !important;
  width: 100%;
  max-width: 100vw;
  z-index: 25;
  padding: calc(5px + var(--safe-top)) 6px 5px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  background: linear-gradient(180deg, rgba(14, 12, 20, 0.94) 0%, rgba(14, 12, 20, 0.78) 88%, transparent 100%);
  pointer-events: auto;
}

.top-hud .hud-group {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 5px;
}

/* Row 1: Date & Location compact pill */
.top-hud .hud-group:nth-child(1) {
  overflow-x: auto;
  scrollbar-width: none;
}
.top-hud .hud-group:nth-child(1)::-webkit-scrollbar {
  display: none;
}

.hud-pill {
  width: 100%;
  padding: 5px 9px;
  gap: 3px 8px;
  font-size: 11px;
  border-radius: 12px;
  justify-content: space-between;
  flex-wrap: wrap;
  white-space: normal;
  overflow: visible;
}

.hud-loc {
  max-width: 68vw;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Row 2: 4 Vital Bars in one compact horizontal row */
.top-hud .hud-group:nth-child(2) {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 4px;
}

.vital-chip {
  min-width: 0 !important;
  width: 100% !important;
  padding: 4px 5px !important;
  border-radius: 9px;
}

.vital-top {
  font-size: 9.5px;
  gap: 1px;
  white-space: nowrap;
}

@media (max-width: 440px) {
  .vital-top {
    flex-direction: column;
    align-items: flex-start;
    line-height: 1.25;
  }
}

.vital-top strong {
  font-size: 10px;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
}

.vital-bar {
  height: 4px;
  margin-top: 3px;
}

/* Row 3: Clean 4-column HUD Tool Grid — zero cut-off buttons */
.top-hud .hud-group:nth-child(3) {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 4px;
  overflow: visible;
  padding-bottom: 0;
}

.hud-btn {
  width: 100%;
  min-width: 0;
  padding: 5px 2px;
  font-size: 10.5px;
  letter-spacing: -0.02em;
  min-height: 30px;
  border-radius: 10px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 2px;
}

/* Character Portrait anchored strictly BELOW the top HUD & centered with zero translateX drift */
.stage-character {
  position: fixed !important;
  top: calc(138px + var(--safe-top)) !important;
  bottom: 300px !important;
  left: 0 !important;
  right: 0 !important;
  transform: none !important;
  height: auto !important;
  width: 100% !important;
  padding: 0 !important;
  display: flex;
  justify-content: center;
  align-items: flex-end;
  pointer-events: none;
  z-index: 4;
  transition: top 0.25s ease, bottom 0.25s ease;
}

.stage-character.mobile-expanded {
  top: calc(134px + var(--safe-top)) !important;
  bottom: 185px !important;
}

#char-sprite,
.stage-character img {
  height: 100% !important;
  max-height: 100% !important;
  max-width: 94vw !important;
  width: auto !important;
  object-fit: contain;
  object-position: bottom center;
}

.away-badge {
  top: calc(146px + var(--safe-top));
  width: min(92vw, 380px);
  padding: 10px 14px;
  font-size: 12px;
}

/* Bottom Dialogue Dock & Full-Visibility Quick Actions — MUST reset transform: none! */
.dialogue-dock {
  position: fixed !important;
  left: 0 !important;
  right: 0 !important;
  bottom: 0 !important;
  transform: none !important;
  z-index: 20;
  padding: 0 6px calc(54px + var(--safe-bottom));
  gap: 5px;
  width: 100% !important;
  max-width: 100vw !important;
  align-items: center;
}

.dialogue-box {
  width: 100%;
  margin: 0 auto;
  padding: 9px 11px;
  border-radius: 16px;
  gap: 6px;
  transition: width 0.15s ease, padding 0.2s ease;
}

/* Collapsed Dialogue Mode (tap 🔽 收合 to view full portrait) */
.dialogue-box.mobile-collapsed {
  padding: 7px 11px;
  gap: 0;
}
.dialogue-box.mobile-collapsed .story-scroll,
.dialogue-box.mobile-collapsed .choices-row {
  display: none !important;
}

.dialogue-header {
  gap: 5px;
  flex-wrap: wrap;
}

.speaker-badge {
  font-size: 11.5px;
  padding: 3px 8px;
}

.scene-title {
  font-size: 11px;
}

.mood-pill,
.style-trigger-btn {
  font-size: 11px;
  padding: 3px 7px;
}

/* Do NOT cap height to 21dvh so the Appearance Customizer height slider works in real time! */
.story-scroll {
  min-height: 60px;
  max-height: 55dvh;
  padding-right: 4px;
}

.choices-row {
  grid-template-columns: 1fr;
  gap: 6px;
  padding-top: 6px;
}

.choice-btn {
  min-height: 40px;
  padding: 8px 10px;
  font-size: 12.5px;
  border-radius: 11px;
}

.control-bar {
  width: 100%;
  gap: 6px;
}

/* Clean 4-column Quick Action Grid — every button 100% visible, never clipped left or right! */
.quick-actions {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  justify-content: stretch;
  gap: 5px;
  padding: 0;
  overflow: visible;
}

.quick-actions .qa-btn {
  width: 100%;
  min-width: 0;
  min-height: 34px;
  padding: 5px 2px;
  font-size: 10.5px;
  letter-spacing: -0.02em;
  border-radius: 10px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.chat-form {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 6px;
}

.chat-form .qa-btn {
  width: 42px;
  min-width: 42px;
  flex: 0 0 42px;
  min-height: 42px;
  padding: 0 !important;
  font-size: 18px;
  border-radius: 12px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.chat-input {
  flex: 1;
  min-width: 0;
  min-height: 42px;
  padding: 8px 12px;
  border-radius: 12px;
}

.chat-send {
  min-height: 42px;
  padding: 0 14px;
  font-size: 14px;
  border-radius: 12px;
  flex-shrink: 0;
}

/* ==========================================================================
   MODALS, LINE APP, ACCOUNTING APP & DRAWERS — NATIVE MOBILE SHEET FEEL
   ========================================================================== */
.modal-backdrop {
  padding: calc(8px + var(--safe-top)) 6px calc(60px + var(--safe-bottom));
  align-items: flex-start;
}

.modal-card {
  width: 96vw;
  max-width: 560px;
  max-height: calc(100dvh - 76px - var(--safe-top) - var(--safe-bottom));
  margin: auto 0;
  border-radius: 18px;
}

.modal-header {
  padding: 12px 14px;
}

.modal-header h3 {
  font-size: 15px;
}

.modal-body {
  padding: 12px;
  gap: 10px;
  overflow-x: hidden;
}

.modal-card.line-modal {
  width: 96vw;
  max-width: 540px;
  height: calc(100dvh - 76px - var(--safe-top) - var(--safe-bottom));
  max-height: calc(100dvh - 76px - var(--safe-top) - var(--safe-bottom));
}

.modal-card.line-modal .modal-body {
  padding: 0;
  overflow-x: hidden;
}

.line-feed {
  overflow-x: hidden;
}

.line-input-bar {
  gap: 4px !important;
  padding: 7px 8px !important;
}

.line-input-bar input {
  min-width: 0 !important;
  flex: 1 !important;
  padding: 7px 10px !important;
}

.line-icon-btn {
  width: 34px !important;
  min-width: 34px !important;
  height: 36px !important;
  padding: 0 !important;
}

.drawer {
  width: min(92vw, 400px);
  max-width: 92vw;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
}

/* Compact Calendar Grid on Mobile */
.cal-grid {
  gap: 3px;
}

.cal-cell {
  min-height: 48px;
  padding: 3px 2px;
  border-radius: 8px;
}

.cal-day-num {
  font-size: 11px;
}

.cal-tag,
.cal-roster {
  font-size: 8.5px;
  padding: 1px 2px;
}

.cal-action-grid {
  grid-template-columns: 1fr;
}

/* Lazy Mode 3-tier grid stacks cleanly on narrow phones */
@media (max-width: 440px) {
  #lazy-tier-grid {
    grid-template-columns: 1fr !important;
  }
}

/* Ending Overlay */
.ending-card {
  width: 94vw;
  max-height: 90dvh;
  overflow-y: auto;
  padding: 22px 16px;
  border-radius: 18px;
}

.ending-title {
  font-size: 22px;
}

/* Toast sits neatly below compact mobile HUD */
#toast-container {
  top: calc(114px + var(--safe-top));
  left: 10px;
  right: 10px;
  transform: none;
  width: auto;
  align-items: center;
}

.toast {
  max-width: 100%;
  font-size: 12.5px;
  padding: 9px 13px;
}

/* Desktop preview fallback when someone opens mobile edition on a wider screen */
@media (min-width: 760px) {
  #screen-game {
    max-width: 520px;
    margin: 0 auto;
    border-left: 1px solid rgba(232, 197, 137, 0.2);
    border-right: 1px solid rgba(232, 197, 137, 0.2);
    box-shadow: 0 0 60px rgba(0, 0, 0, 0.7);
  }
  .stage-bg,
  .stage-vignette {
    max-width: 520px;
    left: 50%;
    right: auto;
    transform: translateX(-50%) !important;
  }
  .top-hud,
  .dialogue-dock,
  .stage-character {
    max-width: 520px !important;
    left: 50% !important;
    right: auto !important;
    transform: translateX(-50%) !important;
  }
}
