/* Sound menu */
.sound-menu { position: relative; display: inline-block; }
.sound-menu-btn.active { background: linear-gradient(180deg, #f3ffe9, #e7f8db); border-color: rgba(103,178,44,.46); }
.sound-panel {
  position: absolute;
  right: 0;
  top: calc(100% + 8px);
  z-index: 12000;
  min-width: 210px;
  padding: 11px;
  border-radius: 16px;
  border: 1px solid rgba(84,68,48,.16);
  background: rgba(255,252,246,.98);
  box-shadow: 0 16px 42px rgba(44,34,25,.20);
  display: grid;
  gap: 7px;
  text-align: left;
}
.sound-panel.hidden { display: none; }
.sound-panel label { display: flex; align-items: center; gap: 7px; font-size: 12px; cursor: pointer; white-space: nowrap; }
.sound-panel input { accent-color: #82bf66; }
.sound-panel input:disabled + span { opacity: .55; }
.sound-master { font-weight: 700; }
.sound-panel-divider { height: 1px; background: rgba(84,68,48,.12); margin: 2px 0; }

/* Shared layout and gesture guards */
#app { overflow-x: clip; }
.layout { position: relative; z-index: 2; }
.panel { position: relative; z-index: 3; }
.lineage-main-card {
  position: relative;
  z-index: 1;
  overflow: hidden;
  contain: layout paint;
}
#gameCanvas {
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}
.archive-content,
.lineage-main-content,
.lineage-family-tree {
  max-width: 100%;
  box-sizing: border-box;
}
.log {
  overscroll-behavior-y: auto !important;
}
body.touch-mobile-ui .tool-tip-popover {
  max-width: min(520px, calc(100vw - 24px));
}
body.touch-mobile-ui .tool-category:not([data-tool-category="operate"]).collapsed .tool-category-body {
  display: none;
}

/* Mobile input controls */
.mobile-mode-controls,
.mobile-rotate-controls {
  display: none;
  position: absolute;
  z-index: 24;
  gap: 6px;
  padding: 6px;
  border-radius: 14px;
  background: rgba(255, 251, 245, 0.92);
  box-shadow: 0 8px 22px rgba(80,60,35,.18);
  border: 1px solid rgba(120, 95, 60, .16);
  backdrop-filter: blur(8px);
}
.mobile-mode-controls {
  left: 10px;
  bottom: 10px;
  gap: 5px;
  background: rgba(255, 251, 245, 0.90);
}
.mobile-rotate-controls {
  right: 10px;
  bottom: 58px;
}
.mobile-mode-controls button,
.mobile-rotate-controls button {
  border: 0;
  border-radius: 10px;
  padding: 7px 10px;
  font-size: 12px;
  font-weight: 800;
  color: #65533f;
  background: rgba(255,255,255,.82);
  box-shadow: inset 0 -1px 0 rgba(0,0,0,.05);
}
.mobile-mode-controls button { padding: 7px 8px; background: rgba(255,255,255,.78); }
.mobile-mode-controls button.active {
  color: #3d321f;
  background: linear-gradient(180deg, #ffe9a8, #ffd376);
  box-shadow: 0 3px 10px rgba(150, 105, 30, .20);
}
.mobile-rotate-controls button:active { transform: translateY(1px); }
body.touch-mobile-ui .mobile-mode-controls:not([hidden]),
body.touch-mobile-ui .mobile-rotate-controls:not([hidden]) { display: flex; }
body:not(.touch-mobile-ui) .mobile-mode-controls,
body:not(.touch-mobile-ui) .mobile-rotate-controls { display: none !important; }
body.touch-mobile-ui.mobile-mode-camera #gameCanvas { cursor: grab; }
body.touch-mobile-ui.mobile-mode-tool #gameCanvas { cursor: crosshair; }


/* Back to canvas affordance */
.back-to-game-btn {
  position: fixed;
  right: calc(env(safe-area-inset-right, 0px) + 12px);
  bottom: calc(env(safe-area-inset-bottom, 0px) + 68px);
  z-index: 12000;
  border: 1px solid rgba(103,178,44,.38);
  border-radius: 999px;
  padding: 9px 12px;
  background: rgba(246, 255, 236, 0.94);
  color: #3f5f2b;
  font-weight: 900;
  font-size: 12px;
  box-shadow: 0 10px 25px rgba(54, 76, 40, .20);
  backdrop-filter: blur(8px);
}
.back-to-game-btn[hidden] { display: none !important; }

@media (min-width: 981px) {
  html,
  body {
    overflow-x: hidden;
    overflow-y: auto;
  }
  #app {
    width: min(1480px, 100vw);
    padding: 18px;
    overflow: visible;
  }
  .topbar { gap: 12px; }
  .top-left { max-width: 100%; }
  .brand,
  .brand h1,
  h1 {
    flex: 0 0 auto;
    min-width: max-content;
    max-width: none !important;
    overflow: visible !important;
    text-overflow: clip !important;
  }
  .top-actions { margin-left: auto; }
  .top-actions .btn,
  .top-actions .sound-menu-btn,
  .room-link {
    min-height: 38px;
    padding: 8px 10px;
    font-size: 12px;
    line-height: 1.12;
  }
  .room-link {
    max-width: 180px;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .layout {
    display: grid;
    grid-template-columns: minmax(680px, 1fr) minmax(320px, 390px);
    align-items: start;
  }
  .panel {
    position: sticky;
    top: 12px;
    align-self: start;
    max-height: calc(100vh - 24px);
    overflow-y: auto;
    overflow-x: hidden;
  }
  .panel-quick-tabs {
    position: sticky;
    top: 0;
    grid-template-columns: repeat(5, minmax(0, 1fr));
  }
  .panel-quick-tabs [data-panel-tab="game"] { display: none !important; }
  .lineage-main-card {
    display: block;
    visibility: visible;
    width: auto;
    margin: 16px 0 0;
  }
  .lineage-main-content {
    height: min(78vh, 980px);
    min-height: 520px;
    max-height: calc(100vh - 120px);
    overflow: auto;
    overscroll-behavior: auto;
  }
  .back-to-game-btn { display: none !important; }
}

@media (max-width: 980px) {
  html,
  body {
    width: 100%;
    max-width: 100%;
    min-height: 100%;
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior-y: auto;
  }
  #app {
    width: 100%;
    max-width: 100%;
    padding: 10px max(10px, env(safe-area-inset-right, 0px)) calc(env(safe-area-inset-bottom, 0px) + 104px) max(10px, env(safe-area-inset-left, 0px));
    overflow-x: clip;
    overflow-y: visible;
  }
  .topbar,
  .top-left,
  .brand,
  .layout,
  .stage-wrap,
  .panel,
  .card,
  .lineage-main-card {
    min-width: 0;
    max-width: 100%;
    box-sizing: border-box;
  }
  .topbar {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
  }
  .top-left {
    align-items: flex-start;
    flex-direction: column;
    gap: 8px;
  }
  .brand {
    align-items: center;
    flex-direction: row;
    gap: 10px;
  }
  .brand h1,
  h1 {
    max-width: 100%;
    overflow: visible;
    text-overflow: clip;
    white-space: nowrap;
    font-size: clamp(20px, 7vw, 30px);
  }
  .top-actions {
    justify-content: flex-start;
    gap: 5px;
    width: 100%;
    overflow-x: auto;
    overflow-y: hidden;
    padding: 1px 0 5px;
    -webkit-overflow-scrolling: touch;
  }
  .top-actions > .btn,
  .top-actions > .sound-menu,
  .top-actions .sound-menu-btn {
    flex: 0 0 auto;
    white-space: nowrap;
  }
  .top-actions .btn,
  .top-actions .sound-menu-btn {
    min-height: 34px;
    padding: 7px 9px;
    font-size: 11px;
  }
  .sound-panel { right: auto; left: 0; }
  .layout {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
    overflow: visible;
  }
  .field-choice-grid { grid-template-columns: 1fr; }
  .reset-preset-choice-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  #gameCanvas {
    height: var(--field-mobile-height, 66vh);
    min-height: var(--field-mobile-min-height, 460px);
  }
  .stage-wrap {
    position: relative;
    width: 100%;
    min-height: var(--field-mobile-min-height, 460px);
    overflow: hidden;
  }
  .panel {
    position: relative !important;
    top: auto !important;
    display: flex;
    flex-direction: column;
    gap: 10px;
    width: 100%;
    max-height: none !important;
    height: auto !important;
    overflow: visible !important;
    isolation: auto;
  }
  body.touch-mobile-ui .panel-quick-tabs,
  .panel-quick-tabs {
    position: fixed;
    left: max(8px, env(safe-area-inset-left, 0px));
    right: max(8px, env(safe-area-inset-right, 0px));
    bottom: calc(env(safe-area-inset-bottom, 0px) + 8px);
    top: auto;
    z-index: 12002;
    display: grid;
    grid-template-columns: repeat(6, minmax(0, 1fr));
    gap: 4px;
    width: auto;
    max-width: none;
    margin: 0;
    padding: 6px;
    border: 1px solid rgba(102, 169, 215, 0.34);
    border-radius: 16px;
    background: rgba(244, 250, 247, 0.95);
    box-shadow: 0 12px 28px rgba(42, 54, 64, 0.18);
  }
  .panel-quick-tabs button {
    min-height: 34px;
    padding: 5px 2px;
    font-size: 10px;
  }
  body.touch-mobile-ui .mobile-mode-controls:not([hidden]) {
    display: flex !important;
    position: absolute;
    left: 50%;
    right: auto;
    bottom: 10px;
    transform: translateX(-50%);
    z-index: 24;
    max-width: calc(100% - 18px);
  }
  body.touch-mobile-ui .mobile-rotate-controls:not([hidden]) {
    position: fixed;
    left: 50%;
    right: auto;
    bottom: calc(env(safe-area-inset-bottom, 0px) + 56px);
    transform: translateX(-50%);
    z-index: 12002;
    max-width: calc(100vw - 22px);
  }
  body.touch-mobile-ui.mobile-mode-auto #gameCanvas {
    touch-action: pan-y pinch-zoom !important;
  }
  body.touch-mobile-ui.mobile-mode-camera #gameCanvas,
  body.touch-mobile-ui.mobile-mode-tool #gameCanvas {
    touch-action: none !important;
  }
  .log {
    max-height: none !important;
    height: auto !important;
    overflow: visible !important;
  }
  .lineage-main-card {
    display: block !important;
    visibility: visible !important;
    width: 100%;
    margin: 10px 0 calc(env(safe-area-inset-bottom, 0px) + 132px);
    overflow: visible !important;
    contain: none !important;
    clear: both;
  }
  .lineage-main-content {
    display: block !important;
    height: auto !important;
    min-height: 360px;
    max-height: none !important;
    overflow-x: hidden !important;
    overflow-y: visible !important;
    overscroll-behavior-y: auto !important;
    -webkit-overflow-scrolling: auto;
    touch-action: pan-y pinch-zoom;
  }
  .lineage-family-tree {
    overflow-x: auto !important;
    overflow-y: visible !important;
    overscroll-behavior-x: contain;
    overscroll-behavior-y: auto;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-x pan-y pinch-zoom;
  }
  .lineage-family-tree .lineage-tree-panel { max-width: none; }
  .selected-card,
  .tool-card,
  .colony-card,
  .log-card,
  .lineage-main-card,
  .stage-wrap {
    scroll-margin-top: 0 !important;
    scroll-margin-bottom: calc(env(safe-area-inset-bottom, 0px) + 104px);
  }
  .back-to-game-btn {
    bottom: calc(env(safe-area-inset-bottom, 0px) + 62px);
    right: max(10px, env(safe-area-inset-right, 0px));
  }
}

/* v39.16.40: selected tool descriptions appear at the top on touch devices. */
body.touch-mobile-ui .tool-tip-popover.mobile-selection-tip {
  top: calc(env(safe-area-inset-top, 0px) + 8px) !important;
  left: 50% !important;
  transform: translateX(-50%);
  width: min(680px, calc(100vw - 18px));
  max-width: calc(100vw - 18px);
  padding: 10px 12px;
  border-radius: 12px;
  background: rgba(46,38,30,.90);
  font-size: 12px;
  line-height: 1.45;
  text-align: left;
}
@media (max-width: 980px) {
  .top-actions { overflow: visible; }
  .top-menu { flex: 0 0 auto; }
  .top-menu-panel { position: fixed; top: calc(env(safe-area-inset-top, 0px) + 58px); right: max(10px, env(safe-area-inset-right, 0px)); }
}

@media (max-width: 980px) {
  .top-panel-search {
    flex: 0 0 220px;
    width: 220px;
    min-width: 220px;
  }
  .top-panel-search .panel-search-input { height: 34px; }
}
@media (max-width: 980px) {
  .top-actions { overflow-x: auto; overflow-y: hidden; }
}

/* Mobile toolbar: keep every primary action reachable without hidden horizontal overflow. */
@media (max-width: 520px) {
  .top-actions {
    display: flex;
    flex-wrap: wrap;
    overflow: visible;
    align-items: center;
  }
  .top-panel-search {
    flex: 1 1 100%;
    width: 100%;
    min-width: 0;
  }
  .top-actions > .btn,
  .top-actions > .top-menu,
  .top-actions > .speed-menu {
    flex: 0 1 auto;
  }
}
