/* Layout corrections shared by the existing views. Load after home-b.css.
   Keep artwork/reel transforms owned by their existing presentation code. */

/* The existing fullscreen bridge owns body padding. Outside fullscreen, use
   content margins so entering fullscreen does not add the safe area twice. */
body.soft-velvet:not(.ols-fullscreen) {
  --sv-safe-top: calc(max(env(safe-area-inset-top, 0px), var(--tg-safe-area-inset-top, 0px)) + var(--tg-content-safe-area-inset-top, 0px));
  --sv-safe-bottom: calc(max(env(safe-area-inset-bottom, 0px), var(--tg-safe-area-inset-bottom, 0px)) + var(--tg-content-safe-area-inset-bottom, 0px));
  --sv-safe-left: calc(max(env(safe-area-inset-left, 0px), var(--tg-safe-area-inset-left, 0px)) + var(--tg-content-safe-area-inset-left, 0px));
  --sv-safe-right: calc(max(env(safe-area-inset-right, 0px), var(--tg-safe-area-inset-right, 0px)) + var(--tg-content-safe-area-inset-right, 0px));
}
body.soft-velvet:not(.ols-fullscreen) > :is(.app-header, main, .server-status, .game-notice) {
  margin-left: var(--sv-safe-left);
  margin-right: var(--sv-safe-right);
}
body.soft-velvet:not(.ols-fullscreen) > .app-header {
  margin-top: var(--sv-safe-top);
}
body.soft-velvet:not(.ols-fullscreen) .main-nav {
  left: calc(50% + (var(--sv-safe-left) - var(--sv-safe-right)) / 2);
  width: min(780px, calc(100% - var(--sv-safe-left) - var(--sv-safe-right) - 24px));
}
body.soft-velvet:not(.ols-fullscreen) dialog:modal {
  position: fixed;
  top: calc(var(--sv-safe-top) + 12px);
  bottom: calc(var(--sv-safe-bottom) + 12px);
  left: calc(var(--sv-safe-left) + 12px);
  right: calc(var(--sv-safe-right) + 12px);
  margin: auto;
  width: calc(100vw - var(--sv-safe-left) - var(--sv-safe-right) - 24px);
}

/* social.css is loaded after velvet.css and reset this notice onto the dock. */
body.soft-velvet .social-unsaved {
  bottom: var(--nav-reserve, calc(90px + var(--sv-safe-bottom, 0px)));
  max-height: calc(100vh - var(--sv-safe-top, 0px) - var(--nav-reserve, calc(90px + var(--sv-safe-bottom, 0px))) - 24px);
  overflow-y: auto;
  overscroll-behavior: contain;
}
@supports (height: 1dvh) {
  body.soft-velvet .social-unsaved {
    max-height: calc(100dvh - var(--sv-safe-top, 0px) - var(--nav-reserve, calc(90px + var(--sv-safe-bottom, 0px))) - 24px);
  }
}

/* Text may grow with the user's settings; flex/grid children must still shrink. */
body.soft-velvet :is(.heading, .set-heading, .panel-header, .season-preview-head) {
  min-width: 0;
  gap: 8px;
}
body.soft-velvet :is(.heading, .set-heading) {
  flex-wrap: wrap;
}
body.soft-velvet :is(.heading, .set-heading, .panel-header, .season-preview-head) :is(h2, h3) {
  min-width: 0;
  overflow-wrap: anywhere;
}
body.soft-velvet :is(.panel-header, .season-preview-head) > button {
  flex-shrink: 0;
}
body.soft-velvet :is(.cv-set-intro, .cv-summary-top, .setting-row) > :not(img):not(input):not(.switch) {
  min-width: 0;
}
body.soft-velvet :is(.cv-set-intro, .cv-summary-top, .setting-row, .exchange-choice, .exchange-card) {
  overflow-wrap: anywhere;
}

/* Collection's 8px override and the dock's fixed height clip enlarged labels.
   The viewport fitter must reserve the dock's actual measured height. */
body.soft-velvet .main-nav button[data-tab],
body.soft-velvet .main-nav #sv-market-slot {
  height: auto;
  min-height: 54px;
  font-size: .6875rem;
  line-height: 1.3;
}

/* Scroll containers must retain both vertical access and their focus outline. */
body.soft-velvet :is(.panel-dialog, .owner-review, .activity-dialog, .puzzle-dialog, .season-preview-dialog) {
  max-height: calc(100vh - var(--sv-safe-top, 0px) - var(--sv-safe-bottom, 0px) - 24px);
  overscroll-behavior: contain;
}
@supports (height: 1dvh) {
  body.soft-velvet :is(.panel-dialog, .owner-review, .activity-dialog, .puzzle-dialog, .season-preview-dialog) {
    max-height: calc(100dvh - var(--sv-safe-top, 0px) - var(--sv-safe-bottom, 0px) - 24px);
  }
}

/* A wrapped rarity/badge header needs an intrinsic row rather than 36 fixed px. */
body.soft-velvet #reward.art-dialog > .art-shell {
  grid-template-rows: auto minmax(0, 1fr) auto auto;
}
body.soft-velvet #reward .art-top {
  flex-wrap: wrap;
}
body.soft-velvet #reward .ru-kind {
  flex-wrap: wrap;
}
body.soft-velvet #reward #reward-close {
  margin-left: auto;
}
body.soft-velvet #reward .ru-name-row {
  flex-wrap: wrap;
}

@media (max-width: 600px) {
  body.soft-velvet .app-header .rules-button,
  body.soft-velvet .app-header #settings-open {
    width: 44px;
    min-width: 44px;
    height: 44px !important;
    min-height: 44px !important;
    flex: 0 0 44px;
  }
  /* iOS zooms into focused controls with a smaller computed font. Let the page,
     not that automatic focus zoom, choose the layout. User zoom remains enabled. */
  body.soft-velvet input:not([type=checkbox]):not([type=radio]):not([type=range]),
  body.soft-velvet textarea,
  body.soft-velvet select {
    font-size: max(1rem, 16px);
  }
}

@media (max-width: 360px) {
  body.soft-velvet .vitrine-search > div {
    flex-wrap: wrap;
  }
  body.soft-velvet .vitrine-search input {
    flex-basis: 100%;
  }
  body.soft-velvet .vitrine-search button {
    width: 100%;
  }
  body.soft-velvet .cv-set-intro {
    gap: 10px;
  }
  body.soft-velvet .cv-hero-art {
    width: min(100px, 32%);
    flex-shrink: 0;
  }
}

/* Portrait phones with little available height (or large text) still need a
   usable artwork area and reachable actions; scroll rather than collapse it. */
@media (max-width: 559px) and (max-height: 650px) {
  body.soft-velvet #reward.art-dialog > .art-shell {
    grid-template-rows: auto minmax(120px, 1fr) auto auto;
    overflow-y: auto;
    overscroll-behavior: contain;
  }
  body.soft-velvet #detail.art-dialog > .art-shell {
    grid-template-rows: auto minmax(120px, 1fr) auto;
    overflow-y: auto;
    overscroll-behavior: contain;
  }
}

/* Preserve the established two-column landscape composition. */
@media (orientation: landscape) and (min-width: 560px) and (max-height: 650px) {
  body.soft-velvet #reward.art-dialog > .art-shell,
  body.soft-velvet #detail.art-dialog > .art-shell {
    grid-template-rows: auto minmax(0, 1fr);
  }
}

/* Reserve the measured dock and unsaved notice on every content view. */
body.soft-velvet:not(.home-b-active) {
  padding-bottom: var(--layout-bottom-reserve, 110px);
}

/* Enlarged labels should remain whole words, not six columns of letters. */
@media (max-width: 600px) {
  body.soft-velvet .main-nav.layout-large-text {
    display: flex;
    overflow-x: auto;
    overscroll-behavior-x: contain;
    scrollbar-width: thin;
  }
  body.soft-velvet .main-nav.layout-large-text button {
    flex: 0 0 auto;
    width: auto;
    min-width: max-content;
    padding-left: 12px;
    padding-right: 12px;
    white-space: nowrap;
    overflow-wrap: normal;
  }
}

/* Long rarity labels can exceed their counter cell with enlarged text. */
body.soft-velvet .cv-rarity,
body.soft-velvet .cv-rarity > span {
  min-width: 0;
  overflow-wrap: anywhere;
}

/* These legacy ID-scoped rules outrank the generic mobile control rule. */
@media (max-width: 600px) {
  body.soft-velvet #opening-season,
  body.soft-velvet.home-b-active #opening-season,
  body.soft-velvet #market-page .market-filters > input,
  body.soft-velvet #market-page .market-filter-footer select,
  body.soft-velvet #market-page .market-select-repeat,
  body.soft-velvet #market-page #market-season {
    font-size: max(1rem, 16px);
  }
}

/* WebKit's native select needs room for 16px text and its disclosure arrow. */
@media (max-width: 600px) {
  body.soft-velvet #market-page .market-filter-footer #market-sort {
    width: min(100%, 12em);
    max-width: 100%;
    flex: 0 1 auto;
  }
}

/* A quiz hand must preserve every rank/suit when the user enlarges text.
   Normal text keeps the existing four-card row; large text can form two rows. */
body.soft-velvet .activity-dialog .activity-hand {
  flex-wrap: wrap;
}
body.soft-velvet .activity-dialog .activity-playing-card {
  flex: 0 1 clamp(1.8em, calc((100% - 24px) / 4), 60px);
  min-width: 1.8em;
  width: max(60px, 1.8em);
  height: max(86px, 2.8em);
}

/* Large answer labels need a whole row rather than narrow columns of letters. */
@media (max-width: 600px) {
  body.soft-velvet:has(.main-nav.layout-large-text) #activity-options {
    grid-template-columns: minmax(0, 1fr);
  }
}
body.soft-velvet .activity-dialog .icon-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

/* UI-004: keep dock labels intact at ordinary iPhone widths. */
body.soft-velvet .main-nav button[data-tab],
body.soft-velvet .main-nav #market-open {
  white-space: nowrap;
  overflow-wrap: normal;
  font-size: .625rem;
}
body.soft-velvet .main-nav button span { white-space: nowrap; }
/* Portrait uses the available Telegram content height, not artwork width. */
@media (orientation: portrait), (max-width: 599px) {
  body.soft-velvet .puzzle-dialog.puzzle-game-dialog[open]:modal {
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
    gap: 8px;
    box-sizing: border-box;
    height: min(800px, calc(var(--mini-viewport-height, 100dvh) - var(--sv-safe-top, 0px) - var(--sv-safe-bottom, 0px) - 24px));
    max-height: calc(var(--mini-viewport-height, 100dvh) - var(--sv-safe-top, 0px) - var(--sv-safe-bottom, 0px) - 24px);
    padding: 12px;
    overflow: auto;
  }
  body.soft-velvet .puzzle-game-dialog > .heading { margin: 0; }
  body.soft-velvet .puzzle-game-dialog .puzzle-layout {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto minmax(132px, 1fr) auto;
    gap: 8px;
    min-height: 0;
    min-width: 0;
  }
  body.soft-velvet .puzzle-game-dialog .puzzle-stages { margin: 0; }
  body.soft-velvet .puzzle-game-dialog .puzzle-game {
    display: grid; place-items: center; min-height: 132px; min-width: 0;
  }
  body.soft-velvet .puzzle-game-dialog .puzzle-board { margin: 0; max-width: 100%; max-height: 100%; }
  body.soft-velvet .puzzle-game-dialog .puzzle-controls {
    display: flex; flex-direction: column; gap: 6px; min-width: 0;
    max-height: 35dvh; overflow-y: auto;
  }
  body.soft-velvet .puzzle-game-dialog .puzzle-controls > * { margin: 0; flex-shrink: 0; }
  body.soft-velvet .puzzle-game-dialog .puzzle-controls > button { min-height: 44px; }
  body.soft-velvet .puzzle-game-dialog .puzzle-controls > .panel-note:empty { display: none; }
  body.soft-velvet .puzzle-game-dialog .puzzle-controls .panel-note { font-size: 12px; line-height: 1.4; }
  body.soft-velvet .puzzle-stage-complete .puzzle-help { display: none; }
}

@media (orientation: portrait), (max-width: 599px) {
  body.soft-velvet .puzzle-dialog.puzzle-game-dialog[open]:modal {
    top: calc(var(--mini-viewport-offset, 0px) + var(--sv-safe-top, 0px) + 12px);
    bottom: auto;
    margin: 0 auto;
  }
}

body.soft-velvet .main-nav:not(.layout-large-text) {
  grid-template-columns: .8fr 1.3fr 1.05fr 1fr .95fr .75fr;
}
