:root {
  --app-viewport-height: 100dvh;
  --site-header-height: 76px;
  --mobile-gutter: clamp(12px, 3.6vw, 24px);
  --tap-size: 44px;
}

html {
  width: 100%;
  overflow-x: hidden;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  width: 100%;
  min-width: 0;
  overflow-x: clip;
}

main,
section,
article,
aside,
header,
footer,
div {
  min-width: 0;
}

img,
video,
canvas,
svg,
iframe {
  max-width: 100%;
}

video,
audio {
  width: 100%;
}

button,
a,
select,
input,
textarea,
summary,
[role="button"] {
  touch-action: manipulation;
  -webkit-tap-highlight-color: rgba(73, 103, 72, .16);
}

.nav-toggle {
  display: none;
}

@media (pointer: coarse) {
  button,
  a,
  select,
  input:not([type="checkbox"]):not([type="radio"]),
  summary,
  [role="button"] {
    min-height: var(--tap-size);
  }

  button:hover:not(:disabled),
  a:hover {
    filter: none;
  }
}

@media (min-width: 701px) and (max-width: 1024px) {
  .workspace {
    grid-template-columns: minmax(0, 1fr);
    width: min(820px, 100%);
    padding-inline: clamp(20px, 4vw, 42px);
  }

  .scene {
    height: clamp(520px, 72vw, 680px);
  }

  .chat {
    height: min(760px, calc(var(--app-viewport-height) - 40px));
    min-height: 620px;
  }

  .interactions,
  .terms-preview,
  .arcade-shelf,
  .leisure,
  .omikuji-shell,
  .memory-shell,
  footer {
    width: auto;
    margin-inline: clamp(20px, 4vw, 42px);
  }
}

@media (max-width: 1024px) {
  .site-header {
    position: sticky;
    top: 0;
    z-index: 150;
    height: auto;
    min-height: 76px;
    margin-inline: 0;
    padding: max(10px, env(safe-area-inset-top)) var(--mobile-gutter) 10px;
    gap: 12px;
    background: rgba(245, 247, 241, .96);
    backdrop-filter: blur(14px);
    isolation: isolate;
  }

  .site-header > p {
    display: none;
  }

  .site-header .brand {
    min-width: 0;
    font-size: clamp(20px, 4.8vw, 24px);
    letter-spacing: .12em;
  }

  .site-header .sun {
    width: 42px;
    height: 42px;
    flex: 0 0 42px;
  }

  .nav-toggle {
    width: 46px;
    height: 46px;
    margin-left: auto;
    padding: 11px;
    border: 1px solid #cbd8c4;
    border-radius: 50%;
    background: #fffefa;
    display: grid;
    align-content: center;
    gap: 5px;
  }

  .nav-toggle span {
    display: block;
    width: 100%;
    height: 2px;
    border-radius: 999px;
    background: #496748;
    transition: transform .2s ease, opacity .2s ease;
  }

  .site-header[data-nav-open="true"] .nav-toggle span:nth-child(1) {
    transform: translateY(7px) rotate(45deg);
  }

  .site-header[data-nav-open="true"] .nav-toggle span:nth-child(2) {
    opacity: 0;
  }

  .site-header[data-nav-open="true"] .nav-toggle span:nth-child(3) {
    transform: translateY(-7px) rotate(-45deg);
  }

  .site-header nav,
  .site-header nav#site-nav {
    position: fixed;
    top: var(--site-header-height);
    left: var(--mobile-gutter);
    right: var(--mobile-gutter);
    z-index: 1000;
    display: none;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
    max-height: calc(var(--app-viewport-height) - 92px);
    padding: 12px;
    overflow-y: auto;
    overscroll-behavior: contain;
    border: 1px solid #d4decf;
    border-radius: 16px;
    background: #fffefa;
    box-shadow: 0 18px 50px rgba(35, 55, 42, .2);
  }

  .site-header[data-nav-open="true"] nav#site-nav {
    display: grid;
  }

  .site-header nav#site-nav button,
  .site-header nav#site-nav button:first-child {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    min-height: 48px;
    padding: 10px 12px;
    border: 1px solid #e0e7da;
    border-radius: 11px;
    background: #f8faf5;
    font-size: 14px;
    line-height: 1.35;
    text-align: left;
  }

  .site-header nav#site-nav button[aria-current="page"] {
    border-color: #496748;
    background: #496748;
    color: #fff;
  }

  .workspace {
    grid-template-columns: minmax(0, 1fr);
  }

  .interactions {
    flex-wrap: wrap;
  }

  .actions {
    flex: 1 1 100%;
    flex-wrap: wrap;
  }

  .actions button {
    flex: 1 1 150px;
    min-height: 48px;
  }

  .term-preview-grid,
  .arcade-game-grid,
  .game-overview,
  .arcade-stats {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 700px) {
  body {
    font-size: 16px;
    line-height: 1.6;
  }

  .workspace {
    width: 100%;
    margin-top: 14px;
    padding-inline: var(--mobile-gutter);
    gap: 18px;
  }

  .scene-column,
  .chat {
    width: 100%;
    max-width: 100%;
  }

  .interactions,
  .terms-preview,
  .arcade-shelf,
  .leisure,
  .omikuji-shell,
  .memory-shell,
  footer {
    width: calc(100% - (2 * var(--mobile-gutter)));
    max-width: none;
  }

  .eyebrow {
    gap: 12px;
    font-size: 13px;
    letter-spacing: .12em;
  }

  .scene-picker {
    display: grid;
    grid-auto-flow: column;
    grid-auto-columns: minmax(112px, 38vw);
    grid-template-columns: none;
    max-height: none;
    padding: 0 0 7px;
    overflow-x: auto;
    overflow-y: hidden;
    scroll-snap-type: x proximity;
    overscroll-behavior-inline: contain;
    scrollbar-width: thin;
  }

  .scene-picker button {
    min-height: 46px;
    padding: 9px 11px;
    scroll-snap-align: start;
    white-space: nowrap;
  }

  .scene {
    height: clamp(410px, 122vw, 540px);
    border-radius: 16px;
  }

  .scene-top {
    top: 14px;
    left: 14px;
    right: 14px;
  }

  .scene-top > span {
    max-width: calc(100% - 58px);
    padding: 7px 10px;
    overflow: hidden;
    font-size: 13px;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .scene-top button {
    width: 44px;
    height: 44px;
  }

  #character-hit {
    inset: 62px 12% 118px;
  }

  .speech {
    top: 82px;
    right: 13px;
    max-width: min(53vw, 190px);
    max-height: 156px;
    padding: 12px 14px;
    font-size: 15px;
    line-height: 1.65;
  }

  .caption {
    left: 18px;
    right: 18px;
    bottom: 22px;
  }

  .caption h1,
  h1 {
    font-size: clamp(27px, 9vw, 34px);
    letter-spacing: .1em;
  }

  .caption p {
    font-size: 13px;
    letter-spacing: .08em;
  }

  .scene-controls {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
  }

  .scene-controls label {
    display: grid;
    gap: 5px;
  }

  .scene-controls select {
    width: 100%;
    min-height: 44px;
  }

  .scene-controls .depth-toggle,
  .conversation-tools button {
    height: 44px;
    min-height: 44px;
  }

  .scene-controls > span,
  .pose-note {
    grid-column: 1 / -1;
  }

  .scene-story {
    padding-inline: 2px;
  }

  .scene-story p {
    font-size: 14px;
    line-height: 1.75;
  }

  .chat {
    height: clamp(820px, 120dvh, 960px);
    min-height: 820px;
    border-radius: 16px;
    overflow: hidden;
    scroll-margin-top: 82px;
  }

  .chat-head {
    min-height: 72px;
    padding: 13px 14px;
  }

  .chat-head > div:nth-child(2) {
    min-width: 0;
  }

  .chat-head h2 {
    overflow: hidden;
    font-size: 17px;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .chat-head #settings {
    min-width: 70px;
    min-height: 44px;
  }

  .mode-switch {
    gap: 8px;
    padding: 10px 12px 0;
  }

  .mode-switch button {
    min-height: 56px;
    padding: 9px;
  }

  .profile-summary,
  .quick-guide {
    margin-inline: 12px;
  }

  .profile-summary {
    min-height: 52px;
  }

  .quick-guide {
    grid-template-columns: minmax(0, 1fr) auto;
  }

  .chat-scroll {
    min-height: 0;
    padding: 16px 14px;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }

  .message {
    max-width: 92%;
    margin-block: 14px;
    font-size: 15px;
    line-height: 1.72;
  }

  .message p {
    padding: 12px 14px;
  }

  .composer {
    position: sticky;
    bottom: 0;
    z-index: 12;
    max-height: 49dvh;
    padding: 10px 12px max(10px, env(safe-area-inset-bottom));
    overflow-y: auto;
    border-top: 1px solid #e2e8dc;
    background: rgba(255, 254, 250, .98);
    box-shadow: 0 -10px 28px rgba(41, 62, 46, .07);
    overscroll-behavior: contain;
  }

  .composer .ai-password-bar {
    grid-template-columns: auto minmax(0, 1fr) auto;
  }

  .composer .ai-password-bar label {
    grid-column: auto;
  }

  .chat-attachment:empty {
    margin: 0;
  }

  #chat-form .voice-status {
    min-height: 20px;
    max-height: 24px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .suggestions {
    flex-wrap: nowrap;
    margin-bottom: 9px;
    padding-bottom: 3px;
    overflow-x: auto;
    scroll-snap-type: x proximity;
    scrollbar-width: none;
  }

  .suggestions::-webkit-scrollbar {
    display: none;
  }

  .suggestions button {
    flex: 0 0 auto;
    min-height: 44px;
    scroll-snap-align: start;
  }

  .ai-password-bar {
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 7px;
  }

  .ai-password-bar label {
    grid-column: 1 / -1;
  }

  .ai-password-bar input {
    min-height: 44px;
    font-size: 16px;
  }

  #chat-form {
    padding: 10px;
  }

  #chat-form textarea {
    min-height: 62px;
    height: 62px;
    font-size: 16px;
    line-height: 1.55;
  }

  .input-bottom {
    gap: 8px;
  }

  .input-bottom span {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .input-bottom button,
  .input-actions button,
  .input-bottom .input-tool {
    min-width: 44px;
    min-height: 44px;
    height: 44px;
  }

  .input-actions {
    flex-wrap: wrap;
    justify-content: flex-end;
  }

  .footnote {
    line-height: 1.5;
  }

  body.keyboard-open .chat {
    height: max(820px, calc(var(--app-viewport-height) + 340px));
    min-height: 820px;
  }

  body.keyboard-open .composer {
    max-height: 58dvh;
  }

  .interactions,
  .terms-preview,
  .arcade-shelf,
  .leisure,
  .omikuji-shell,
  .memory-shell,
  footer {
    margin-inline: var(--mobile-gutter);
  }

  button,
  select,
  summary,
  [role="button"] {
    min-height: 44px;
  }

  .interactions {
    display: grid;
    gap: 14px;
    padding-block: 20px;
  }

  .interactions > div:first-child {
    width: 100%;
  }

  .actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    width: 100%;
    margin-top: 0;
  }

  .actions button {
    width: 100%;
    min-height: 52px;
    padding: 10px;
  }

  .terms-preview {
    grid-template-columns: minmax(0, 1fr);
    padding-inline: 0;
  }

  .term-preview-grid,
  .arcade-game-grid,
  .game-overview,
  .arcade-stats,
  .poetry-saved-list {
    grid-template-columns: minmax(0, 1fr);
  }

  .term-card,
  .term-row,
  .arcade-tab-card,
  .game-overview-card,
  .arcade-stat-card {
    min-height: 52px;
  }

  .arcade-toolbar,
  .arcade-toolbar-actions,
  .game-mode-row,
  .arcade-actions,
  .music-entry-actions,
  .poetry-actions,
  .guide-actions,
  .profile-actions {
    align-items: stretch;
    flex-direction: column;
  }

  .arcade-toolbar label,
  .arcade-toolbar select,
  .game-mode-row label,
  .game-mode-row select,
  .arcade-actions button,
  .music-entry-actions button,
  .poetry-actions button,
  .guide-actions button,
  .profile-actions button {
    width: 100%;
    min-height: 46px;
  }

  .duel-form {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 8px;
  }

  .duel-form input {
    min-width: 0;
    min-height: 46px;
    font-size: 16px;
  }

  .duel-form button {
    min-width: 72px;
    min-height: 46px;
  }

  dialog:not(.memory-modal):not(.arcade-game-modal):not(.music-theater-modal):not(.poetry-modal):not(.omikuji-modal) {
    width: 100vw;
    max-width: none;
    height: var(--app-viewport-height);
    max-height: none;
    margin: 0;
    padding: max(56px, calc(env(safe-area-inset-top) + 48px)) var(--mobile-gutter) max(18px, env(safe-area-inset-bottom));
    border: 0;
    border-radius: 0;
  }

  #close-dialog {
    position: fixed;
    top: max(8px, env(safe-area-inset-top));
    right: max(8px, env(safe-area-inset-right));
    z-index: 20;
    width: 48px;
    height: 48px;
    border: 1px solid #d5dfce;
    border-radius: 50%;
    background: #fffefa;
  }

  .poetry-modal,
  .omikuji-modal,
  .memory-modal,
  .arcade-game-modal,
  .music-theater-modal {
    width: 100vw;
    height: var(--app-viewport-height);
    max-width: none;
    max-height: none;
    margin: 0;
    border-radius: 0;
  }

  .poetry-header,
  .omikuji-modal-header,
  .memory-header,
  .arcade-game-modal-bar {
    padding-top: max(10px, env(safe-area-inset-top));
  }

  .poetry-close,
  .omikuji-close,
  .memory-close,
  .arcade-game-modal-close {
    min-width: 46px;
    min-height: 46px;
  }

  .gallery-thumbs,
  .poetry-library-list,
  .term-list,
  .lateral-history {
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
  }

  footer {
    padding-bottom: max(10px, env(safe-area-inset-bottom));
  }
}

@media (max-width: 380px) {
  :root {
    --mobile-gutter: 10px;
  }

  .site-header .brand {
    gap: 8px;
    font-size: 19px;
  }

  .site-header .brand small {
    font-size: 10px;
  }

  .site-header nav,
  .site-header nav#site-nav {
    grid-template-columns: minmax(0, 1fr);
  }

  .scene {
    height: 400px;
  }

  .speech {
    max-width: 58vw;
    font-size: 14px;
  }

  .chat-head .avatar {
    width: 42px;
    height: 42px;
    flex: 0 0 42px;
  }

  .mode-switch small,
  .profile-summary small,
  .quick-guide small {
    line-height: 1.35;
  }

  .actions {
    grid-template-columns: minmax(0, 1fr);
  }
}

@media (orientation: landscape) and (max-height: 540px) and (max-width: 1024px) {
  .site-header {
    position: relative;
    min-height: 62px;
  }

  .chat {
    height: calc(var(--app-viewport-height) - 16px);
    min-height: 360px;
  }

  .scene {
    height: min(82dvh, 500px);
  }

  .scene-picker {
    grid-auto-columns: minmax(128px, 24vw);
  }
}

@media (prefers-reduced-motion: reduce) {
  .nav-toggle span {
    transition: none;
  }
}
