/* Screen layouts for Kōroa Matihiko activities and the hub.
   Desktop and laptop keep the two-column task. Tablet portrait stacks it.
   Phones use a separate, thumb-friendly layout. */

html { -webkit-text-size-adjust: 100%; }

/* Text-size zoom stays inside the screen. --ts is set from the activity. */
[data-motion] main {
  box-sizing: border-box;
  width: calc(100% / var(--ts, 1)) !important;
  max-width: calc(1320px / var(--ts, 1)) !important;
}

.tm-header__text { min-width: 0; }
.tm-header__title,
.tm-header__subtitle { overflow-wrap: break-word; }
#sb-prompt { min-width: 0; box-sizing: border-box; }
#sb-prompt p { overflow-wrap: break-word; }
.tm-choice__label,
.tm-choice__gloss { overflow-wrap: break-word; }
[role="status"] p { overflow-wrap: break-word; }
[role="status"] > div { align-items: center !important; }

/* Up to three sentence spaces on a line. The full stop sits at the end of the block. */
[aria-label="Tō rerenga · Your sentence"] {
  position: relative;
  flex-wrap: wrap !important;
  align-items: stretch !important;
  padding-right: 22px !important;
  box-sizing: border-box;
}
[aria-label="Tō rerenga · Your sentence"] > div {
  flex: 1 1 calc(33.333% - 10px) !important;
  max-width: calc(33.333% - 6px);
  min-width: 0 !important;
}
[aria-label="Tō rerenga · Your sentence"] > span {
  position: absolute !important;
  right: 0;
  bottom: 6px;
  line-height: 1 !important;
}
[data-slot] { height: auto !important; min-height: 96px; }
[data-slot] > div:not([aria-hidden="true"]) { min-height: 72px; flex: 1 1 auto; }
[data-slot] .tm-tile { min-width: 0 !important; height: auto !important; min-height: 64px; }

/* ---- Laptop and tablet landscape: keep two columns, protect the header ---- */
@media (max-width: 1200px) {
  .tm-header__title { font-size: clamp(26px, 3.2vw, 34px) !important; line-height: 1.15 !important; }
  .tm-header__subtitle { font-size: 17px !important; line-height: 1.35 !important; }
  .tm-header > a.tm-home img { height: clamp(72px, 10vw, 108px) !important; }
}

/* ---- Tablet portrait: picture, then the task, full width ---- */
@media (max-width: 900px), (max-height: 520px) and (max-width: 1100px) {
  [data-motion] main { padding: 12px 16px 20px !important; gap: 12px !important; }

  .tm-header { align-items: flex-start; padding: 16px 16px 12px !important; }
  .tm-header__text { flex: 1 1 auto; padding-right: 64px; }
  .tm-header__title { font-size: clamp(22px, 5vw, 32px) !important; line-height: 1.15 !important; }
  .tm-header__subtitle { font-size: 16px !important; line-height: 1.35 !important; }
  .tm-header > a.tm-home {
    position: absolute !important; top: 12px; right: 12px;
    height: 48px !important; max-width: 64px !important; margin: 0 !important;
  }
  .tm-header > a.tm-home img { height: 100% !important; width: auto; max-width: 100%; }
  .tm-header__pattern { width: 56px !important; right: 8px !important; opacity: 0.22 !important; }

  nav[aria-label="Ngā pātai · Questions"] { flex-wrap: wrap !important; row-gap: 4px; }
  nav[aria-label="Ngā pātai · Questions"] > :nth-child(1) { order: 1; }
  nav[aria-label="Ngā pātai · Questions"] > span { order: 2; flex: 1 1 auto; text-align: center; font-weight: 700; color: var(--text); }
  nav[aria-label="Ngā pātai · Questions"] > :nth-child(3) { order: 3; }
  nav[aria-label="Ngā pātai · Questions"] > ol {
    order: 4;
    flex: 1 1 100% !important;
    min-width: 0;
    justify-content: center !important;
  }

  section[data-screen-label="Sentence builder"] { gap: 16px !important; padding: 16px !important; }
  [data-col="picture"],
  [data-col="work"] { flex: 1 1 100% !important; width: 100% !important; }

  [data-col="picture"] > figure { align-self: center; width: min(280px, 70vw); }
  [data-col="picture"] [role="img"],
  [data-col="picture"] > figure > div[style*="aspect-ratio"] { width: min(260px, 56vw) !important; }
  [data-col="picture"] > div { width: 100%; box-sizing: border-box; padding: 4px 0 0 !important; }
  #sb-prompt { width: 100%; }
  #sb-prompt p { white-space: normal !important; }

  [style*="height:104px"], [style*="height: 104px"] { height: auto !important; min-height: 72px; }
  ol[aria-label="Ngā hipanga · Steps"] > li { flex: 1 1 calc(50% - 8px); flex-wrap: wrap; }

  [role="status"] > div { flex-wrap: wrap !important; align-items: center !important; }
  .tm-fb-char { width: 180px; max-width: 42%; }
}

/* ---- Phone: a stacked lesson. Big tap targets, one job at a time. ---- */
@media (max-width: 700px), (max-height: 480px) and (max-width: 950px) {
  [data-motion] main {
    padding: 8px 12px calc(16px + env(safe-area-inset-bottom)) !important;
    gap: 10px !important;
  }

  .tm-header { border-radius: 16px 28px 16px 16px !important; padding: 16px 14px !important; min-height: 96px; }
  .tm-header__text { padding-right: 96px; }
  .tm-header__eyebrow { letter-spacing: 0.12em !important; }
  .tm-header__title { font-size: clamp(20px, 6.2vw, 28px) !important; }
  .tm-header > a.tm-home {
    top: 50% !important;
    right: 16px !important;
    width: 76px !important;
    height: 68px !important;
    max-width: 76px !important;
    margin: 0 !important;
    transform: translateY(-50%);
    display: flex !important;
    align-items: center;
    justify-content: center;
  }
  .tm-header > a.tm-home img { height: 64px !important; width: auto !important; max-width: 100% !important; }

  /* Arrows sit against the counter, and that group is centred. The sentence strip scrolls underneath. */
  nav[aria-label="Ngā pātai · Questions"] { justify-content: center !important; column-gap: 4px; }
  nav[aria-label="Ngā pātai · Questions"] > :nth-child(1) button { order: 1; flex: 0 0 auto; }
  nav[aria-label="Ngā pātai · Questions"] > :nth-child(3) button { order: 3; flex: 0 0 auto; }
  nav[aria-label="Ngā pātai · Questions"] > span {
    order: 2;
    flex: 0 0 auto !important;
    min-width: 4.75em;
    font-size: 16px !important;
    color: var(--text) !important;
  }
  nav[aria-label="Ngā pātai · Questions"] > ol {
    order: 4;
    flex: 1 1 100% !important;
    flex-wrap: nowrap !important;
    justify-content: flex-start !important;
    overflow-x: auto;
    overscroll-behavior-x: contain;
    scrollbar-width: none;
    gap: 0 !important;
    padding: 2px 0 4px;
    -webkit-overflow-scrolling: touch;
  }
  nav[aria-label="Ngā pātai · Questions"] > ol::-webkit-scrollbar { display: none; }
  nav[aria-label="Ngā pātai · Questions"] > ol > li { flex: 0 0 40px !important; }
  nav[aria-label="Ngā pātai · Questions"] > ol button { width: 36px !important; height: 36px !important; }

  section[data-screen-label="Sentence builder"] {
    display: flex !important;
    flex-direction: column !important;
    align-items: stretch !important;
    padding: 12px !important;
    gap: 14px !important;
    border-radius: 16px 28px 16px 16px !important;
  }
  [data-col="picture"],
  [data-col="work"],
  [data-work] { display: contents !important; }

  /* Picture, prompt, task, Tirohia, feedback, then the Te Mātāwai logo and option icons. */
  ol[aria-label="Ngā hipanga · Steps"] { order: 1; }
  [data-col="picture"] > figure { order: 2; width: min(168px, 46vw) !important; align-self: center !important; }
  #sb-prompt { order: 3; padding: 12px 12px !important; gap: 8px !important; border-radius: 16px !important; }
  [data-col="work"] > div:not([data-work]):not([data-actions]):not([role="status"]) { order: 4; width: 100%; }
  [data-work] > * { order: 5; width: 100%; }
  [data-actions] { order: 6 !important; width: 100%; }
  [data-work] > [role="status"] { order: 7 !important; width: 100%; }
  [data-col="picture"] > div[style*="space-between"] { order: 8; width: 100%; }

  [data-col="picture"] [role="img"],
  [data-col="picture"] > figure > div[style*="aspect-ratio"] { width: min(168px, 46vw) !important; }
  #sb-prompt p[lang] { font-size: 18px !important; line-height: 1.35 !important; }
  [data-col="picture"] > div[style*="space-between"] {
    justify-content: center !important;
    align-items: center !important;
    padding: 4px 0 0 !important;
    gap: 14px !important;
  }
  [data-col="picture"] > div[style*="space-between"] > div {
    margin-left: 0 !important;
    flex: 0 1 auto !important;
    flex-wrap: wrap;
    justify-content: center;
    gap: 8px !important;
  }
  [data-col="picture"] > div[style*="space-between"] img { height: 44px !important; }
  [data-col="work"] > div[style*="height:32px"],
  [data-col="work"] > div[style*="height: 32px"] { display: none !important; }

  ol[aria-label="Ngā hipanga · Steps"] > li { flex: 1 1 100%; }

  /* Answer spaces: two large places per line, in reading order. */
  [aria-label="Tō rerenga · Your sentence"] {
    gap: 8px !important;
    padding: 8px;
    border-radius: 16px;
    background: var(--surface);
  }
  [aria-label="Tō rerenga · Your sentence"] > div {
    flex: 1 1 calc(50% - 8px) !important;
    max-width: calc(50% - 4px);
  }
  [aria-label="Tō rerenga · Your sentence"] > span { font-size: 22px !important; }
  [data-slot] { min-height: 72px !important; }
  [data-slot] > div { min-height: 68px; }
  [data-slot] .tm-tile { min-height: 64px !important; padding: 8px 10px !important; align-items: center; justify-content: center; }
  [data-slot] .tm-tile__kupu {
    font-size: 16px !important;
    line-height: 1.25 !important;
    white-space: normal !important;
    overflow-wrap: anywhere;
    word-break: break-word;
    text-align: center;
  }

  /* Word bank: two columns of buttons, no nested scrolling. */
  [data-tray] {
    height: auto !important;
    min-height: 0 !important;
    max-height: none !important;
    overflow: visible !important;
    padding: 12px !important;
    border-radius: 16px !important;
  }
  [data-tray] > div {
    display: grid !important;
    grid-template-columns: 1fr 1fr;
    gap: 8px !important;
    min-height: 0 !important;
  }
  [data-tray] > div > p { grid-column: 1 / -1; }
  [data-tray] > div > div { min-width: 0; display: flex; }
  [data-tray] .tm-tile {
    width: 100%;
    min-width: 0 !important;
    min-height: 56px;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 8px 10px;
  }
  [data-tray] .tm-tile__kupu {
    font-size: 17px;
    line-height: 1.25;
    text-align: center;
    white-space: normal;
    overflow-wrap: anywhere;
  }

  [data-actions],
  section[data-screen-label="Sentence builder"] div[style*="justify-content:flex-end"],
  section[data-screen-label="Sentence builder"] div[style*="justify-content: flex-end"] {
    justify-content: stretch !important;
    gap: 8px !important;
  }
  [data-actions] .tm-btn,
  section[data-screen-label="Sentence builder"] div[style*="justify-content:flex-end"] .tm-btn,
  section[data-screen-label="Sentence builder"] div[style*="justify-content: flex-end"] .tm-btn {
    flex: 1 1 160px;
    min-height: 52px;
  }

  .tm-choice { align-items: flex-start !important; padding: 12px !important; }
  .tm-choice__label { font-size: 17px !important; line-height: 1.35 !important; }
  .tm-typed__input { font-size: 18px !important; }
  .tm-mic { width: 84px !important; height: 84px !important; }

  [role="status"] > div {
    display: grid !important;
    grid-template-columns: auto minmax(0, 1fr);
    align-items: center !important;
    padding: 8px 10px !important;
    gap: 8px 10px !important;
  }
  [role="status"] > div > div { width: auto !important; min-width: 0 !important; align-self: center !important; }
  [role="status"] > div > div > div:first-child { flex-wrap: wrap !important; }
  .tm-fb-char { width: 108px !important; max-width: 34vw; }
  /* Incorrect: short character beside the label, icons on one row underneath. */
  [role="status"] > .tm-fb-card {
    grid-template-areas: "char head" "icons icons";
    gap: 6px 10px !important;
  }
  [role="status"] > .tm-fb-card > .tm-fb-char { grid-area: char; }
  [role="status"] > .tm-fb-card > .tm-fb-body { display: contents !important; }
  [role="status"] > .tm-fb-card .tm-fb-head { grid-area: head; min-width: 0; align-self: center; }
  [role="status"] > .tm-fb-card .tm-fb-icons { grid-area: icons; }
  [role="status"] > .tm-fb-card > .tm-fb-body > :not(.tm-fb-head):not(.tm-fb-icons) { grid-column: 1 / -1; }
  [role="status"] p[lang="mi"] { font-size: 17px !important; line-height: 1.3 !important; }

  section[data-screen-label="Completion"] { padding: 22px 16px !important; }
  section[data-screen-label="Completion"] [role="group"] { flex-direction: column; align-items: stretch; }
  section[data-screen-label="Completion"] [role="group"] button {
    width: 100%;
    min-height: 48px;
    text-align: left;
    box-sizing: border-box;
  }
  section[data-screen-label="Completion"] > div[aria-hidden="true"] { width: 48px !important; right: 0 !important; }

  /* Instructions: character on top, words full width, dialog scrolls. */
  div[style*="z-index:45"], div[style*="z-index: 45"] {
    align-items: flex-start !important;
    padding: 12px !important;
    overflow-y: auto;
  }
  #sb-intro {
    --cw: 0px;
    margin-top: 0 !important;
    max-height: none;
    border-radius: 20px !important;
  }
  #sb-intro > div[aria-hidden="true"]:first-child {
    width: 100% !important;
    height: 8px !important;
    bottom: auto !important;
    border-radius: 20px 20px 0 0 !important;
  }
  #sb-intro > div[aria-hidden="true"]:nth-child(2) {
    position: relative !important;
    left: auto !important;
    right: auto !important;
    top: auto !important;
    bottom: auto !important;
    width: 112px !important;
    height: 140px !important;
    margin: 8px auto 0 !important;
    aspect-ratio: auto !important;
  }
  #sb-intro > div[style*="padding:28px"], div[style*="padding: 28px"] {
    padding: 8px 16px 16px !important;
    min-height: 0 !important;
    gap: 14px !important;
  }
  #sb-intro h2 { font-size: 24px !important; line-height: 1.2 !important; }
  #sb-intro > div[style*="padding:28px"], div[style*="padding: 28px"] > div:last-child {
    flex-direction: column;
    align-items: stretch !important;
  }
  #sb-intro > div[style*="padding:28px"], div[style*="padding: 28px"] > div:last-child .tm-btn { justify-content: center; }

  #a11y-drawer,
  #sb-review {
    width: 100% !important;
    max-width: 100% !important;
    border-radius: 0 !important;
    padding: 16px 16px calc(20px + env(safe-area-inset-bottom)) !important;
  }
  #a11y-drawer h2,
  #sb-review h2 { font-size: 24px !important; line-height: 1.2 !important; }
  #a11y-drawer section > div { flex-wrap: wrap !important; }
}

/* Landscape phone: picture beside the prompt. Logo and options stay under Tirohia. */
@media (max-height: 500px) and (orientation: landscape) and (max-width: 950px) {
  section[data-screen-label="Sentence builder"] {
    display: grid !important;
    grid-template-columns: 104px minmax(0, 1fr);
    column-gap: 12px;
    row-gap: 10px;
    align-items: center;
  }
  [data-col="picture"] > figure { grid-column: 1; grid-row: 1; width: 104px !important; }
  [data-col="picture"] [role="img"],
  [data-col="picture"] > figure > div[style*="aspect-ratio"] { width: 96px !important; }
  #sb-prompt { grid-column: 2; grid-row: 1; }
  ol[aria-label="Ngā hipanga · Steps"],
  [data-work] > *,
  [data-actions],
  [data-col="picture"] > div[style*="space-between"],
  [data-work] > [role="status"] { grid-column: 1 / -1; }
  .tm-header { padding: 8px 12px !important; min-height: 0; }
  .tm-header__title { font-size: 20px !important; }
  .tm-header__subtitle { display: none; }
  .tm-header > a.tm-home { height: 52px !important; width: 60px !important; }
  .tm-header > a.tm-home img { height: 48px !important; }
}

/* ---- Hub ---- */
@media (max-width: 1100px) {
  /* The cast and the TKW logo move as one piece, logo still on the
     cast's right. They sit as far right as the logo can without
     leaving the banner, so the title stays clear of the characters. */
  .hub-banner-art { justify-content: flex-end; padding-right: 16px; box-sizing: border-box; }
  .hub-banner-art__frame { display: flex; align-items: center; margin-left: 0; gap: clamp(16px, 2vw, 32px); }
  .hub-banner-art__frame > a.tm-home { position: static; left: auto; top: auto; transform: none; }
  .hub-banner-art__frame > a.tm-home img { height: clamp(72px, 11vw, 120px); }
  header:not(.tm-header) div[style*="padding-right:clamp"],
  header:not(.tm-header) div[style*="padding-right: clamp"] {
    padding-right: clamp(96px, 18vw, 180px) !important;
  }
}

@media (max-width: 760px) {
  .hub-banner-art { justify-content: center; padding-right: 0; }
  .hub-banner-art__frame {
    position: absolute;
    top: 0;
    left: auto;
    right: -24px;
    display: block;
    gap: 0;
    margin: 0;
    height: 100%;
    max-width: 78%;
  }
  .hub-banner-art__cast { opacity: 0.4; max-width: 100%; object-fit: contain; object-position: right center; }
  .hub-banner-art__frame > a.tm-home { position: absolute !important; left: auto !important; right: 36px !important; top: 50% !important; transform: translateY(-50%) !important; }
  .hub-banner-art__frame > a.tm-home img { height: 52px !important; }
  header:not(.tm-header) div[style*="padding-right:clamp"],
  header:not(.tm-header) div[style*="padding-right: clamp"] {
    padding-right: 64px !important;
    min-height: 0 !important;
  }

  section[aria-labelledby="h-pr"] li > a {
    display: flex !important;
    flex-wrap: wrap;
    align-items: center;
    gap: 10px !important;
  }
  section[aria-labelledby="h-pr"] li > a > span[role="progressbar"] { flex: 1 1 100%; }
  section[aria-labelledby="h-ab"] img { height: 48px !important; }
  footer nav a { white-space: normal !important; }
}
