/* One phone layout, independent of focus and the visual viewport. The browser
   owns document scrolling when its keyboard or address bar changes size. */
@media (max-width: 780px) {
  .header-inner {
    min-height: 0;
    grid-template-columns: minmax(44px, 1fr) 44px max-content;
    gap: 4px;
    padding: 6px 16px 4px;
  }
  .brand {
    width: auto;
    min-width: 0;
    min-height: 44px;
    gap: 7px;
  }
  .brand > span {
    min-width: 0;
    overflow: hidden;
  }
  .brand img {
    flex: 0 0 auto;
    width: 30px;
    height: 30px;
  }
  .brand strong {
    overflow: hidden;
    font-size: 19px;
    text-overflow: ellipsis;
  }
  .brand small {
    display: none;
  }
  .header-inner > .header-actions {
    grid-column: 3;
    grid-row: 1;
    gap: 0;
  }
  .header-language {
    flex-basis: 74px;
    gap: 3px;
    padding: 0 5px;
    font-size: 11px;
  }
  .header-language > svg {
    width: 14px;
    height: 14px;
  }
  /* Theme remains available in Settings; language needs a visible phone entry. */
  .header-actions > #theme-toggle {
    display: none;
  }
  .icon-button {
    width: 44px;
    height: 44px;
  }
  .header-inner > .help-button {
    grid-column: 2;
    grid-row: 1;
    width: 44px;
    min-width: 44px;
    height: 44px;
    padding: 0;
  }
  /* Keep the translated help label accessible without a second header row. */
  .header-inner > .help-button > span {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
  }
  .main-content {
    padding: 0 16px;
  }
  .page-heading {
    padding: 24px 0 20px;
    gap: 8px;
  }
  h1 {
    font-size: 34px;
    letter-spacing: -1px;
    text-wrap: balance;
  }
  .eyebrow {
    gap: 6px;
    font-size: 11px;
    letter-spacing: 0.6px;
  }
  .heading-note {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 2px 6px;
    font-size: 13px;
  }
  .heading-note br {
    display: none;
  }
  .text-button {
    min-height: 44px;
  }
  input[type="text"],
  input[type="search"],
  #animal-search,
  #guide-search {
    font-size: 16px;
  }
  .search-results {
    max-height: 18rem;
    padding: 5px;
  }
  .search-results [role="option"] {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    min-height: 52px;
    padding: 10px;
  }
  .search-results [role="option"] small {
    grid-column: 1;
  }
  .search-results [role="option"] strong {
    font-size: 14px;
  }
  .search-results .suggestion-action {
    grid-column: 2;
    grid-row: 1 / 3;
    margin-left: 8px;
    color: var(--accent);
    font-size: 12px;
    font-weight: 600;
  }
  .search-results [data-guessed="true"] {
    color: var(--muted);
    background: var(--surface);
  }
  .search-results .search-tip {
    min-height: 0;
    padding: 8px 10px;
    border-bottom: 1px solid var(--line);
    border-radius: 0;
    color: var(--muted);
    font-size: 12px;
    cursor: default;
  }
  .guess-entry-row,
  .photo-entry-row {
    gap: 7px;
  }
  .guess-entry-row .primary-button,
  .photo-entry-row .primary-button {
    padding: 10px;
    font-size: 13px;
  }
  .guess-entry-row .primary-button svg,
  .photo-entry-row .primary-button svg {
    display: none;
  }

  #play-arena {
    gap: 6px;
    margin-top: 12px;
  }
  .play-card-top .section-label {
    font-size: 14px;
  }
  .attempt-summary strong {
    font-size: 16px;
  }
  .attempt-summary > span:first-child {
    gap: 5px;
  }
  .guess-form label {
    margin-bottom: 6px;
  }
  #search-help {
    margin: 0;
  }
  #game-view .feedback-card {
    gap: 0 8px;
    font-size: 13px;
  }
  .feedback-inspect {
    font-size: 12px;
  }
  .tree-content .tree-svg--compact {
    min-height: 0;
    margin: 0 auto;
  }
  .table-view {
    min-height: 0;
    padding: 8px 0;
    overflow-x: auto;
  }
  .history-table {
    font-size: 12px;
  }
  .history-table th,
  .history-table td {
    padding-left: 5px;
    padding-right: 5px;
  }
  .history-row--latest {
    background: var(--soft);
  }
  .history-latest-badge {
    display: inline-block;
    margin-left: 4px;
    color: var(--accent);
    font-size: 11px;
  }
  .extra-actions {
    gap: 4px 16px;
  }
  .extra-actions .hint-button {
    font-size: 12px;
  }
  #learning-details {
    margin: 16px;
  }
  .learning-card .animal-detail-image {
    width: 100%;
    float: none;
    margin: 0 0 14px;
  }
  .how-section {
    grid-template-columns: minmax(0, 1fr);
    gap: 18px;
    margin-top: 28px;
    padding: 24px 0;
  }
  .how-steps {
    grid-template-columns: minmax(0, 1fr);
    gap: 18px;
    padding: 0;
  }
  .how-steps p {
    max-width: none;
    font-size: 13px;
  }
  .result-panel {
    grid-template-columns: minmax(0, 1fr);
    border-radius: 18px;
  }
  .result-photo {
    height: 180px;
    min-height: 0;
  }
  .result-copy {
    padding: 20px;
  }
  .result-copy h2 {
    font-size: 30px;
  }
  .result-actions {
    flex-wrap: wrap;
  }
  .result-actions button {
    flex: 1 1 150px;
  }

  .photo-view,
  .kinship-view {
    padding: 14px 0 24px;
  }
  .photo-layout {
    grid-template-columns: minmax(0, 1fr);
    gap: 12px;
  }
  .photo-frame,
  .photo-play-card {
    border-radius: 16px;
  }
  .photo-image-wrap {
    height: 200px;
    min-height: 0;
    aspect-ratio: auto;
  }
  .photo-frame-top {
    min-height: 40px;
    padding: 8px 12px;
  }
  .photo-credits {
    padding: 8px 12px;
    font-size: 11px;
  }
  .photo-play-card {
    padding: 16px;
  }
  .photo-feedback {
    min-height: 0;
    margin: 12px 0 0;
    padding: 10px 12px;
    font-size: 13px;
  }
  .photo-history {
    margin-top: 16px;
  }
  .photo-form .input-help {
    margin: 6px 0;
  }
  .photo-actions {
    margin-top: 8px;
  }
  .photo-actions button {
    min-height: 44px;
    font-size: 13px;
  }
  .photo-result h2 {
    font-size: 26px;
  }
  .photo-storage-note {
    margin-top: 14px;
    font-size: 12px;
  }
  .kinship-card {
    padding: 18px;
  }
  .kinship-dots {
    margin: 12px 0 16px;
  }
  #kinship-question {
    font-size: 22px;
    line-height: 1.45;
  }
  .kinship-options {
    gap: 8px;
  }
  .kinship-options > button {
    min-height: 96px;
    padding: 12px 10px;
  }
  .kinship-options strong {
    font-size: 16px;
  }
  .kinship-options small {
    font-size: 12px;
  }
  .kinship-feedback {
    padding: 14px;
  }
  .kinship-feedback p,
  .kinship-review p {
    font-size: 14px;
  }
  #kinship-next {
    width: 100%;
  }

  .home-hero {
    padding: 18px 0;
  }
  #home-title {
    font-size: 34px;
    letter-spacing: -0.8px;
    line-height: 1.25;
    text-wrap: balance;
  }
  .home-games,
  .home-explore-links {
    grid-template-columns: minmax(0, 1fr);
    gap: 14px;
  }
  .home-game-card {
    display: grid;
    grid-template-columns: 72px minmax(0, 1fr);
  }
  .home-game-art {
    min-height: 0;
    border-bottom: 0;
    border-right: 1px solid var(--line);
  }
  .home-game-art::before {
    width: 48px;
    height: 60px;
    border-radius: 12px;
  }
  .home-game-art svg {
    width: 34px;
    height: 34px;
  }
  .home-game-copy {
    padding: 16px;
  }
  .home-game-copy h2 {
    font-size: 21px;
  }
  .home-game-copy p {
    margin: 10px 0 14px;
    font-size: 14px;
  }
  .home-explore-links strong {
    font-size: 15px;
  }
  .home-explore-links small,
  .home-trust {
    font-size: 13px;
  }
  .guide-toolbar {
    flex-direction: column;
    align-items: stretch;
    gap: 16px;
  }
  .guide-search {
    width: 100%;
  }
  .guide-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
  }
  .guide-card img {
    height: 140px;
  }
  .guide-card .guide-card-content {
    padding: 12px;
  }
  .guide-card-content strong {
    font-size: 14px;
  }
  .story-grid,
  .story-related .story-grid,
  .story-animal-links {
    grid-template-columns: minmax(0, 1fr);
  }
  .story-actions {
    flex-wrap: wrap;
  }
  .story-actions > a {
    flex: 1 1 180px;
  }
  .story-link {
    padding: 22px;
  }
  .story-view {
    padding: 24px 20px;
    margin: 28px auto 40px;
    border-radius: 18px;
  }
  .story-dek {
    font-size: 21px;
  }
  .story-view section p {
    font-size: 16px;
  }
  .info-sections {
    padding: 22px 18px;
    border-radius: 18px;
  }
  .info-sections p {
    font-size: 15px;
  }
  .game-about {
    margin-top: 30px;
    padding-top: 22px;
  }
  .game-about h2 {
    font-size: 24px;
  }
  .game-about p,
  .game-about-faq summary {
    font-size: 14px;
  }
  .game-about-faq summary {
    min-height: 44px;
  }
  .game-about-steps {
    grid-template-columns: minmax(0, 1fr);
    gap: 16px;
  }
  .site-footer {
    flex-direction: column;
    align-items: flex-start;
    gap: 14px;
    margin: 0 16px;
    padding: 24px 0 calc(24px + env(safe-area-inset-bottom, 0px));
    font-size: 13px;
  }
  .site-footer > div {
    display: grid;
    gap: 6px;
  }
  .site-footer p {
    display: flex;
    flex-wrap: wrap;
    gap: 0 16px;
    margin: 0;
  }
  .site-footer a {
    display: inline-flex;
    align-items: center;
    min-height: 44px;
  }
  .app-dialog {
    width: calc(100% - 24px);
    padding: 22px 18px;
    border-radius: 18px;
  }
  .dialog-heading {
    align-items: flex-start;
  }
  .dialog-heading h2 {
    min-width: 0;
    font-size: 22px;
    line-height: 1.35;
    overflow-wrap: anywhere;
  }
  .dialog-heading > form {
    flex: 0 0 auto;
  }
  .dialog-content .stats-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .toast {
    bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  }
}

/* Width alone controls density. Focus and keyboard events never switch layout. */
@media (max-width: 400px) {
  .header-inner {
    padding-left: 12px;
    padding-right: 12px;
  }
  .main-content {
    padding: 0 12px;
  }
  .brand strong {
    font-size: 18px;
  }
  .brand img {
    width: 28px;
    height: 28px;
  }
  .brand {
    gap: 5px;
  }
  #home-title {
    font-size: 30px;
  }
  .home-game-card {
    grid-template-columns: 60px minmax(0, 1fr);
  }
  .home-game-art::before {
    width: 40px;
    height: 52px;
  }
  .home-game-copy {
    padding: 14px;
  }
  .home-game-copy h2 {
    font-size: 19px;
  }
  .photo-image-wrap {
    height: 160px;
  }
  .photo-play-card,
  .kinship-card {
    padding: 12px;
  }
  .photo-entry-row .primary-button,
  .guess-entry-row .primary-button {
    padding-left: 8px;
    padding-right: 8px;
    font-size: 12px;
  }
}

@media (max-width: 350px) {
  .header-inner {
    column-gap: 2px;
  }
  /* The logo still links home; reserve space for the 44px game controls. */
  .brand > span {
    display: none;
  }
  .brand {
    gap: 4px;
  }
  .brand img {
    width: 24px;
    height: 24px;
  }
  .brand strong {
    font-size: 16px;
  }
}

@media (max-width: 700px) {
  .home-games {
    grid-template-columns: 1fr;
  }
  .order-play {
    padding: 6px 0;
  }
  .order-top {
    margin-bottom: 14px;
  }
  .order-instructions {
    font-size: 12px;
    margin-bottom: 16px;
  }
  .order-cards {
    grid-template-columns: 1fr;
    gap: 9px;
    margin: 16px 0;
  }
  .order-card {
    display: flex;
    align-items: center;
    border-radius: 13px;
  }
  .order-card-main {
    flex: 1;
    min-width: 0;
    flex-direction: row;
    gap: 10px;
    padding: 10px;
    text-align: left;
  }
  .order-card-main img {
    width: 58px;
    height: 58px;
    flex: 0 0 58px;
    border-radius: 7px;
  }
  .order-card-main img.image--fallback {
    padding: 8px;
  }
  .order-position {
    width: 20px;
    height: 20px;
    flex: 0 0 20px;
    font-size: 11px;
  }
  .order-card-copy {
    width: auto;
  }
  .order-card-copy strong {
    font-size: 14px;
  }
  .order-weight {
    font-size: 13px;
  }
  .order-moves {
    flex-direction: column;
    flex: 0 0 44px;
    gap: 0;
    padding: 0;
    align-self: stretch;
    border-left: 1px solid var(--line);
  }
  .order-move {
    border: 0;
    border-radius: 0;
    min-height: 44px;
    flex: 1;
    background: transparent;
  }
  #order-submit {
    width: 100%;
  }
  .order-sources {
    grid-template-columns: 1fr;
    gap: 12px;
  }
  .order-about {
    margin-top: 12px;
  }
}

@media (max-width: 780px) {
  .logic-view {
    padding-top: 14px;
  }
  .logic-rules {
    font-size: 13px;
    margin-bottom: 10px;
  }
  .logic-status {
    margin-bottom: 8px;
  }
  .logic-conflict {
    margin: 8px 0;
  }
  .logic-tools button {
    font-size: 12px;
  }
}

@media (max-width: 400px) {
  /* Keep four cards and their check action reachable on a short phone. */
  .order-card-main {
    gap: 8px;
    padding: 8px;
  }
  .order-card-main img {
    width: 44px;
    height: 44px;
    flex-basis: 44px;
  }
  .order-moves {
    flex-direction: row;
    flex-basis: 88px;
  }
  /* Full translated tool names need room even on a 320px screen. */
  .logic-tools {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}
