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

html,
body,
#game-container {
  width: 100%;
  height: 100dvh;
  min-height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
}

.orientation-guard {
  position: fixed;
  z-index: 9999;
  inset: 0;
  display: none;
  place-items: center;
  padding: max(24px, var(--safe-t)) max(24px, var(--safe-r)) max(24px, var(--safe-b)) max(24px, var(--safe-l));
  color: var(--text);
  background: #000;
}

.orientation-guard p {
  margin: 0;
  font: 600 18px/1.4 var(--font-serif);
}

@media (orientation: portrait) and (pointer: coarse) {
  .orientation-guard { display: grid; }
  #game-container { visibility: hidden; }
}

@media (max-width: 1000px), (max-height: 560px) {
  :where(button, [role="button"], a) { touch-action: manipulation; }

  .hub-view {
    display: grid;
    grid-template-columns: clamp(164px, 25vw, 220px) minmax(0, 1fr);
    align-items: center;
    gap: 16px;
    height: 100dvh;
    padding: max(18px, calc(var(--safe-t) + 10px)) max(18px, calc(var(--safe-r) + 12px)) max(18px, calc(var(--safe-b) + 10px)) max(18px, calc(var(--safe-l) + 12px));
    overflow: hidden;
  }

  .hub-copy { align-self: center; }
  .hub-copy h1 { margin-bottom: 8px; }
  .hub-lead { max-width: 19em; }
  .hub-progress { margin-top: 14px; }
  .chapter-list {
    align-self: stretch;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    grid-template-rows: repeat(2, minmax(0, 1fr));
    gap: 10px;
    min-width: 0;
    height: 100%;
  }
  .chapter-entry,
  .chapter-entry.is-current {
    min-width: 0;
    min-height: 0;
    height: 100%;
    padding: 12px;
    grid-template-columns: 22px minmax(0, 1fr);
    align-content: center;
  }
  .chapter-entry.is-current { padding: 11px; }
  .chapter-name,
  .chapter-number { font-size: 13px; }
  .chapter-name small {
    margin-top: 5px;
    font-size: 10px;
    line-height: 1.4;
  }
  .chapter-state { margin-top: 7px; }

  .status-hud {
    min-height: 48px;
    padding-top: var(--safe-t);
    padding-right: max(14px, calc(var(--safe-r) + 8px));
    padding-left: max(14px, calc(var(--safe-l) + 8px));
  }
  .global-back {
    top: var(--safe-t);
    left: max(4px, var(--safe-l));
    width: 44px;
    height: 44px;
  }
  .hud-left { margin-left: 48px; }
  .hud-values span:nth-child(4) { display: inline-flex; }

  .dialogue-wrap {
    left: max(36px, calc(var(--safe-l) + 18px), 5vw);
    bottom: max(24px, calc(var(--safe-b) + 16px));
    width: min(76vw, 640px);
    min-height: 130px;
    padding: 0;
  }
  .dialogue-name {
    min-height: 20px;
    margin: 0 0 8px;
    font-size: 16px;
    line-height: 1.2;
  }
  .dialogue-box {
    display: block;
    width: 100%;
    min-height: calc(3 * 18px * 1.75);
    padding: 0;
  }
  #dialogue-text {
    font-size: 18px;
    line-height: 1.75;
  }
  .dialogue-box small { margin-left: 10px; }
  .story-tools {
    top: var(--safe-t);
    right: max(14px, var(--safe-r));
  }
  .story-tools button { min-width: 44px; min-height: 44px; }

  #game-container[data-view="play"] .global-back { top: var(--safe-t); }
  .play-header {
    top: max(6px, calc(var(--safe-t) + 4px));
    left: max(60px, calc(var(--safe-l) + 54px));
    width: calc(100% - max(60px, calc(var(--safe-l) + 54px)) - max(170px, calc(var(--safe-r) + 160px)));
    min-width: 0;
  }
  .play-header .eyebrow { display: none; }
  .play-header h2 { font-size: 17px; line-height: 1.2; }
  .play-objective { margin-top: 6px; gap: 8px; min-width: 0; }
  .play-objective p {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 13px;
    line-height: 1.4;
  }
  .play-progress { margin-top: 5px; gap: 8px; }
  .play-tools {
    top: max(4px, var(--safe-t));
    right: max(8px, calc(var(--safe-r) + 4px));
  }
  .tool-icon,
  .play-tools button { width: 44px; height: 44px; min-width: 44px; min-height: 44px; }

  .game-screen,
  .game-screen.has-submit {
    top: max(96px, calc(var(--safe-t) + 92px));
    right: max(14px, calc(var(--safe-r) + 10px));
    bottom: max(8px, calc(var(--safe-b) + 6px));
    left: max(14px, calc(var(--safe-l) + 10px));
    padding: 0 0 58px;
    overflow: hidden;
  }
  .game-screen.is-active { display: block; overflow: hidden; }
  .game-screen.has-submit::after { height: 56px; }
  .game-submit {
    right: max(14px, calc(var(--safe-r) + 10px));
    bottom: max(8px, calc(var(--safe-b) + 6px));
    min-width: 136px;
    min-height: 44px;
  }
  .ritual-board,
  .trace-board,
  .dispatch-board {
    width: 100%;
    height: 100%;
    max-height: 100%;
    min-height: 0;
    overflow: auto;
    overscroll-behavior: contain;
    scrollbar-gutter: stable;
  }
  .section-label { margin-bottom: 8px; }

  .sequence-slots { gap: 8px; margin-bottom: 8px; }
  .sequence-slot { min-height: 48px; padding: 8px; }
  .law-actions { gap: 8px; }
  .choice-line { min-height: 44px; padding: 8px 10px; }

  .banquet-stage,
  .game-screen.has-submit .banquet-stage,
  .route-board {
    width: 100%;
    height: 100%;
    min-height: 0;
  }
  .person-pool { top: 0; }
  .seat-node { width: 92px; min-height: 44px; }
  .seat-node[data-seat="emperor"] { left: calc(50% - 46px) !important; top: 48px !important; }
  .seat-node[data-seat="r1"] { left: calc(76% - 46px) !important; top: 76px !important; }
  .seat-node[data-seat="r2"] { left: calc(86% - 46px) !important; top: 128px !important; }
  .seat-node[data-seat="r3"] { left: calc(76% - 46px) !important; top: 180px !important; }
  .seat-node[data-seat="south"] { left: calc(50% - 46px) !important; top: 196px !important; }
  .seat-node[data-seat="l2"] { left: calc(14% - 46px) !important; top: 128px !important; }
  .banquet-table { left: calc(50% - 72px); top: 92px; width: 144px; height: 108px; }

  .trace-chain { gap: 10px; margin: 10px 0; }
  .trace-step { min-height: 52px; padding: 8px; }
  .trace-pool { gap: 8px; margin-bottom: 8px; }
  .trace-token { min-height: 44px; }

  .dispatch-board {
    display: grid;
    grid-template-columns: clamp(158px, 25vw, 210px) minmax(0, 1fr);
    grid-template-rows: auto minmax(0, 1fr);
    gap: 6px 12px;
    overflow: hidden;
  }
  .dispatch-board > .section-label { grid-column: 1 / -1; margin: 0; }
  .attendant-list,
  .task-list {
    min-height: 0;
    grid-template-rows: repeat(3, minmax(0, 1fr));
    gap: 6px;
  }
  .attendant-choice,
  .task-row {
    min-height: 44px;
    height: 100%;
    padding: 7px 9px;
  }
  .attendant-choice { padding-right: 58px; }
  .attendant-choice::after { right: 6px; }
  .task-row { grid-template-columns: minmax(76px, .9fr) minmax(0, 1.4fr) 102px; gap: 8px; }
  .task-name { grid-column: 1; grid-row: 1; }
  .task-detail {
    grid-column: 2;
    grid-row: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .task-assignee { grid-column: 3; grid-row: 1; min-height: 44px; }

  .route-board { overflow: hidden; }
  .route-node { width: clamp(72px, 13%, 94px); min-height: 44px; }
  .route-node[data-route-node="start"] { left: 2% !important; }
  .route-node[data-route-node="gate"] { left: auto !important; right: 2% !important; }
  .route-log { right: auto; bottom: 0; left: 0; max-width: 55%; }

  .lantern-game {
    width: 100%;
    height: 100%;
    padding: 0 8px;
    grid-template-rows: 44px minmax(0, 1fr) 20px 44px;
  }
  .lantern-status { grid-template-columns: 112px minmax(0, 1fr) 46px; }
  .lantern { min-height: 80px; }
  .lantern i { width: 42px; height: 50px; }

  .mission-brief {
    inset: max(10px, calc(var(--safe-t) + 6px)) max(10px, calc(var(--safe-r) + 6px)) max(10px, calc(var(--safe-b) + 6px)) max(10px, calc(var(--safe-l) + 6px));
    width: auto;
    height: auto;
    max-height: none;
    padding: 18px 58px 14px 20px;
    overflow: hidden;
  }
  .mission-brief.is-mobile-open {
    display: grid;
    grid-template-rows: auto auto minmax(0, 1fr) auto;
  }
  .mission-brief-close {
    top: 4px;
    right: 6px;
    display: grid;
    width: 44px;
    height: 44px;
  }
  .brief-copy,
  .brief-rules { overflow: auto; overscroll-behavior: contain; }
  .brief-actions { position: static; padding-top: 10px; }
  .brief-enter { min-height: 44px; }

  .result-layer { padding: max(10px, var(--safe-t)) max(10px, var(--safe-r)) max(10px, var(--safe-b)) max(10px, var(--safe-l)); }
  .result-copy {
    width: 100%;
    height: 100%;
    max-height: none;
    min-height: 0;
    overflow: hidden;
  }
  .result-summary { padding: 14px; }
  .result-detail { min-height: 0; padding: 14px; overflow: auto; overscroll-behavior: contain; }
  .result-actions { position: sticky; bottom: 0; padding-top: 8px; background: #101015; }

  .archive-layer {
    padding: max(16px, calc(var(--safe-t) + 10px)) max(18px, calc(var(--safe-r) + 12px)) max(16px, calc(var(--safe-b) + 10px)) max(18px, calc(var(--safe-l) + 12px));
    overflow: hidden;
  }
  .relationship-list { max-height: calc(100dvh - 76px); overflow: auto; overscroll-behavior: contain; }
  .story-history { padding: max(10px, var(--safe-t)) max(16px, var(--safe-r)) max(10px, var(--safe-b)) max(16px, var(--safe-l)); }
  .story-history-list { min-height: 0; overflow: auto; overscroll-behavior: contain; }
  .reset-confirm { max-width: min(520px, calc(100vw - 32px)); max-height: calc(100dvh - 24px); overflow: auto; }
}

@media (max-width: 780px), (max-height: 390px) {
  .hub-view { grid-template-columns: 156px minmax(0, 1fr); gap: 12px; padding: max(12px, calc(var(--safe-t) + 6px)) max(14px, calc(var(--safe-r) + 8px)) max(12px, calc(var(--safe-b) + 6px)) max(14px, calc(var(--safe-l) + 8px)); }
  .chapter-entry,
  .chapter-entry.is-current { padding: 8px; grid-template-columns: 20px minmax(0, 1fr); gap: 5px; }
  .chapter-entry.is-current { padding: 7px; }
  .chapter-name,
  .chapter-number { font-size: 12px; }
  .chapter-name small { font-size: 9px; }
  .chapter-state { margin-top: 4px; font-size: 9px; }
  .play-header { width: calc(100% - max(60px, calc(var(--safe-l) + 54px)) - max(146px, calc(var(--safe-r) + 136px))); }
  .play-tools { gap: 6px; }
  .game-screen,
  .game-screen.has-submit { top: max(92px, calc(var(--safe-t) + 88px)); }
  .dispatch-board { grid-template-columns: 150px minmax(0, 1fr); gap: 5px 9px; }
  .attendant-choice,
  .task-row { padding: 5px 7px; }
  .attendant-choice strong,
  .task-name,
  .task-assignee strong { font-size: 12px; }
  .attendant-choice small,
  .task-detail,
  .task-assignee small { font-size: 9px; }
  .task-row { grid-template-columns: 78px minmax(0, 1fr) 88px; gap: 6px; }
}
