/* jarchi stage (stage.js): what the teacher shows gets the space. The page (a board page, a document page) keeps its
   shape and takes all the width it can, up to --stg-max-h (what the layout leaves of the screen); stage.js sizes it.
   The page is light paper in both schemes (ink colours mean the same on every screen); around it a quiet ground. */

.stg {
  --stg-ground: var(--bg-sunken);
  --paper: #fffefc;
  --stg-max-h: max(180px, calc(100dvh - var(--rbar-h) - var(--bar-h) - 150px));
  display: flex; flex-direction: column; gap: 10px;
  min-width: 0;
}
.room[data-role="teacher"] .stg { --stg-max-h: max(170px, calc(100dvh - var(--rbar-h) - var(--bar-h) - 200px)); }
@media (prefers-color-scheme: dark) {
  .stg { --stg-ground: #0d0c0a; --paper: #f2efe8; }
}

/* ---------- the teacher's switcher ---------- */
.stg-head { display: flex; align-items: center; flex-wrap: wrap; gap: 8px 10px; min-height: var(--tap); }
.stg-hint { color: var(--muted); font-size: 14px; font-weight: 700; }
.stg-switch { display: flex; flex-wrap: wrap; gap: 8px; }
.stg-sw {
  appearance: none;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: var(--tap); min-width: var(--tap);
  padding: 6px 16px;
  border: 1px solid var(--line-strong); border-radius: var(--r);
  background: var(--card); color: var(--ink);
  font-size: 14.5px; font-weight: 700; line-height: 1.4;
  cursor: pointer; touch-action: manipulation;
  transition: background-color .15s, border-color .15s, color .15s;
}
.stg-sw .ic { width: 20px; height: 20px; color: var(--accent-strong); }
.stg-sw[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent-line); color: var(--accent-strong); }
.stg-close { margin-inline-start: auto; border-color: var(--line); color: var(--muted); }
/* «اشتراک صفحه» on reads like a voice on the air. */
.stg-share[aria-pressed="true"] { background: var(--speak-soft); border-color: var(--speak-line); color: var(--speak-ink); }
.stg-share[aria-pressed="true"] .ic { color: var(--speak-ink); }
.stg-share[data-state="lost"], .stg-share[data-state="fail"] { border-color: var(--warn-line); }
.stg-close .ic { color: var(--muted); }
@media (hover: hover) {
  .stg-sw:not(:disabled):hover { border-color: var(--accent-line); }
  .stg-close:hover { color: var(--ink); }
}
.stg-err:empty { display: none; }
.stg-err {
  margin: 0; padding: 10px 14px; border-radius: var(--r);
  background: var(--danger-soft); color: var(--danger-ink); font-weight: 700; font-size: 14px;
}

/* ---------- the view: the page, the screen, the camera ---------- */
.stg-frame { display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.stg-view {
  position: relative;
  width: 100%;
  max-height: var(--stg-max-h);
  display: flex; align-items: center; justify-content: center;
  border-radius: var(--r-lg);
  background: var(--stg-ground);
  overflow: hidden;
}
.stg-page {
  position: relative; flex: none;
  background: var(--paper);
  box-shadow: 0 0 0 1px rgba(40, 33, 24, .10);
  overflow: hidden;
}
.stg-img {
  position: absolute; inset: 0; width: 100%; height: 100%;
  display: block; object-fit: fill;
  user-select: none; -webkit-user-select: none; pointer-events: none;
}
.stg-screen { position: absolute; inset: 0; display: grid; place-items: center; }
.stg-screen > video, .stg-screen .screen-view, .stg-screen .screen-view video { width: 100%; height: 100%; object-fit: contain; }
.stg-screen .screen-view { position: absolute; inset: 0; margin: 0; border-radius: 0; aspect-ratio: auto; }
.stg-screen .screen-self { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; background: #121110; }
.stg-wait {
  position: absolute; inset: auto 0 45% 0; margin: 0; text-align: center;
  color: var(--muted); font-size: 14px; font-weight: 700; pointer-events: none;
}

/* A student's line under the page: what page this is, the turn-your-phone hint, «بزرگ‌تر». */
.stg-bar { display: flex; align-items: center; gap: 8px; min-height: var(--tap); }
.stg-bar[hidden] { display: none; }
.stg-pagetag { color: var(--muted); font-size: 13px; font-weight: 700; white-space: nowrap; }
.stg-turn { display: none; flex: 1 1 auto; min-width: 0; color: var(--faint); font-size: 12.5px; line-height: 1.5; }
@media (max-width: 599px) and (orientation: portrait) {
  .stg[data-wide="1"] .stg-turn { display: block; }
}
.stg-big {
  appearance: none; margin-inline-start: auto;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: var(--tap); min-width: var(--tap); padding: 4px 12px;
  border: 1px solid var(--line); border-radius: 12px;
  background: var(--card); color: var(--ink);
  font-size: 13.5px; font-weight: 700; cursor: pointer; touch-action: manipulation;
}
.stg-big .ic { width: 20px; height: 20px; }
.tl.stg-big { margin-inline-start: 4px; padding: 0 8px; border-color: var(--line); }
.stg-max {
  appearance: none; position: absolute; z-index: 4; top: 8px; inset-inline-end: 8px;
  display: grid; place-items: center;
  width: var(--tap); height: var(--tap); padding: 0;
  border: 0; border-radius: 12px;
  background: rgba(24, 22, 19, .55); color: #fff;
  cursor: pointer; touch-action: manipulation;
}
.stg-max .ic { width: 22px; height: 22px; }
@media (hover: hover) { .stg-max:hover { background: rgba(24, 22, 19, .75); } }

/* The teacher's camera (the camera lane fills .stg-cam): the whole stage when nothing else is shown, else a
   small tile in a corner the teacher can drag it to. */
.stg-cam:empty, .stg:not([data-cam="1"]) .stg-cam { display: none; }
.stg-cam {
  position: absolute; z-index: 3;
  width: 26%; min-width: 112px; max-width: 280px; aspect-ratio: 16 / 9;
  border-radius: 12px; overflow: hidden;
  background: #000; box-shadow: var(--shadow-2);
  cursor: grab; touch-action: none;
}
.stg-cam > video, .stg-cam > img { display: block; width: 100%; height: 100%; object-fit: cover; }
.stg-cam[data-corner="end-bottom"] { inset-inline-end: 10px; bottom: 10px; }
.stg-cam[data-corner="end-top"] { inset-inline-end: 10px; top: 60px; }
.stg-cam[data-corner="start-bottom"] { inset-inline-start: 10px; bottom: 40px; }
.stg-cam[data-corner="start-top"] { inset-inline-start: 10px; top: 10px; }
.stg[data-kind=""] .stg-cam { inset: 0; width: 100%; max-width: none; border-radius: 0; box-shadow: none; cursor: default; }

/* ---------- the teacher's tools ---------- */
.stg-tools { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 6px 12px; }
.stg-tools[hidden] { display: none; }
.tl-group { display: flex; align-items: center; gap: 4px; }
.tl {
  appearance: none;
  display: inline-grid; place-items: center;
  min-width: var(--tap); height: var(--tap); padding: 0 8px;
  border: 1px solid transparent; border-radius: 12px;
  background: transparent; color: var(--ink);
  font-size: 14px; font-weight: 700; line-height: 1;
  cursor: pointer; touch-action: manipulation;
  transition: background-color .12s, border-color .12s;
}
.tl .ic { width: 22px; height: 22px; }
.tl[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent-line); color: var(--accent-strong); }
.tl:disabled { opacity: .38; cursor: default; }
@media (hover: hover) { .tl:not(:disabled):not([aria-pressed="true"]):hover { background: var(--bg-sunken); } }
.tl-swatches { display: none; gap: 2px; }
.sw-dot { display: block; width: 22px; height: 22px; border-radius: 50%; background: #1f2328; box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .25); }
[data-c="1"] > .sw-dot, .sw-dot[data-c="1"] { background: #1d5fc4; }
[data-c="2"] > .sw-dot, .sw-dot[data-c="2"] { background: #c62f2f; }
[data-c="3"] > .sw-dot, .sw-dot[data-c="3"] { background: #18804a; }
.tl.sw[aria-pressed="true"] { background: transparent; border-color: transparent; }
.tl.sw[aria-pressed="true"] .sw-dot { box-shadow: 0 0 0 3px var(--card), 0 0 0 5px var(--accent); }
.tl-pages { min-width: 84px; border-color: var(--line); font-variant-numeric: tabular-nums; }
.tl-new { display: inline-flex; gap: 6px; border-color: var(--line); }
.tl-new .ic { width: 20px; height: 20px; }
.tl-word { display: none; }
.tl-wide-only { display: none; }
@media (min-width: 700px) {
  .tl-swatches { display: flex; }
  .tl-color, .tl-narrow-only { display: none; }
  .tl-wide-only { display: inline-grid; }
  .tl-word { display: inline; }
}

/* ---------- sheets: colours, pages, «بیشتر» ---------- */
.stg-sheet .sheet-card > .btn.ghost:last-child { margin-top: 12px; }
.stg-colors { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.stg-color {
  appearance: none; display: flex; align-items: center; gap: 10px;
  min-height: 52px; padding: 8px 14px;
  border: 1px solid var(--line-strong); border-radius: var(--r);
  background: var(--card); color: var(--ink); font-weight: 700; cursor: pointer;
}
.stg-color[aria-pressed="true"] { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.stg-list { display: flex; flex-direction: column; gap: 8px; }
.stg-row { justify-content: flex-start; }
.stg-pagegrid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(52px, 1fr)); gap: 6px;
  max-height: 46dvh; overflow-y: auto; margin-bottom: 12px; padding: 2px;
}
.stg-pagegrid .btn { width: 100%; padding: 4px; font-variant-numeric: tabular-nums; }
.stg-pagegrid .btn[aria-current="page"] { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }

/* ---------- big: fullscreen, or over the page where the browser has no element fullscreen (iPhone) ---------- */
.stg-frame:fullscreen { background: #121110; padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); }
.stg-frame:-webkit-full-screen { background: #121110; }
.stg.is-max .stg-frame {
  position: fixed; inset: 0; z-index: 40;
  background: #121110;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
.stg-frame:fullscreen .stg-view, .stg.is-max .stg-view {
  --stg-fill: 1;
  flex: 1 1 auto; min-height: 0; height: auto; max-height: none;
  border-radius: 0; background: #121110;
}
.stg-frame:fullscreen .stg-tools, .stg.is-max .stg-tools { padding: 4px 8px; background: var(--card); border-radius: 12px; margin: 0 8px 8px; }

/* ====================================================================================================
   The room around the stage (data-stage on .room while something is shown)
   ==================================================================================================== */

/* A student's speaking state steps back to one row under the page; the floor's hint goes (the button says it). */
.room[data-role="student"][data-stage] .stage {
  display: grid; grid-template-columns: auto minmax(0, 1fr); align-items: center; column-gap: 12px;
  justify-content: center; text-align: start; padding: 2px 4px 0;
}
.room[data-role="student"][data-stage] .stage-ring { grid-row: 1 / span 2; padding: 3px; }
.room[data-role="student"][data-stage] .stage-name, .room[data-role="student"][data-stage] .stage-line { grid-column: 2; }
.room[data-role="student"][data-stage] .avatar.huge { width: 44px; height: 44px; font-size: 18px; }
.room[data-role="student"][data-stage] .avatar.huge .ic { width: 22px; height: 22px; }
.room[data-role="student"][data-stage] .stage-name { margin: 0; font-size: 16px; line-height: 1.5; }
.room[data-role="student"][data-stage] .stage-line { font-size: 13.5px; margin: 0; }
.room[data-role="student"][data-stage] .stage[data-mode="absent"] .stage-line { margin: 0; }
.room[data-role="student"][data-stage] .floor .lead { font-size: 16.5px; }
.room[data-role="student"][data-stage] .floor .hint { display: none; }
.room[data-role="student"][data-stage] .room-main { justify-content: flex-start; gap: 12px; }
.room[data-role="student"][data-stage] .stage .cam-row { grid-column: 1 / -1; margin-top: 4px; }

/* Narrow: a student's page goes edge to edge and stands in the middle of what the header and the thumb zone leave;
   the speaking state and the floor sit right above the hand button. */
@media (max-width: 959px) {
  .room[data-role="student"] .stg { --stg-max-h: max(180px, calc(100dvh - var(--rbar-h) - var(--bar-h) - 190px)); margin-inline: calc(-1 * var(--gut)); }
  .room[data-role="student"] .stg-view { border-radius: 0; }
  .room[data-role="student"] .stg-bar { padding-inline: var(--gut); }
  .room[data-role="student"][data-stage] .room-body { padding-top: 0; }
  .room[data-role="student"][data-stage] .stg { flex: 1 1 auto; justify-content: center; }
}

/* Wide: the stage leads the main column. The teacher's mic and hands dock beside it (stage.js moves them into
   .stg-dock, with the people), so the class tools never sit under the fold while teaching. */
@media (min-width: 960px) {
  .room[data-role="teacher"] .stg { --stg-max-h: max(240px, calc(100dvh - var(--rbar-h) - 180px)); }
  .room[data-role="student"] .stg { --stg-max-h: max(240px, calc(100dvh - var(--rbar-h) - 330px)); }
  .stg-dock {
    display: flex; flex-direction: column; gap: 16px; min-width: 0;
    position: sticky; top: calc(var(--rbar-h) + 20px); align-self: start;
    max-height: calc(100dvh - var(--rbar-h) - 40px);
    overflow-y: auto; overscroll-behavior: contain;
  }
  .stg-dock .room-side { position: static; max-height: none; overflow: visible; }
  /* The mic and the camera in one row: the mic first and wide, named «میکروفون» over «روشنه / خاموشه»; the
     camera a compact tile beside it. */
  .room[data-role="teacher"] .stg-dock .micbox { display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; }
  .room[data-role="teacher"] .stg-dock .micbox.has-cam { grid-template-columns: minmax(0, 1fr) 108px; }
  .room[data-role="teacher"] .stg-dock .speak { grid-template-columns: auto minmax(0, 1fr); min-height: 84px; padding: 10px 14px; gap: 12px; }
  .room[data-role="teacher"] .stg-dock .speak-ic { width: 52px; height: 52px; }
  .room[data-role="teacher"] .stg-dock .speak-ic .ic { width: 24px; height: 24px; }
  .room[data-role="teacher"] .stg-dock .speak b { display: flex; flex-direction: column; font-size: 18px; line-height: 1.3; }
  .room[data-role="teacher"] .stg-dock .speak-w { display: block; font-size: 13px; font-weight: 700; color: var(--muted); }
  .room[data-role="teacher"] .stg-dock .speak[data-state="on"] .speak-w { color: var(--speak-ink); }
  .room[data-role="teacher"] .stg-dock .speak small, .room[data-role="teacher"] .stg-dock .speak .lvl { display: none; }
  .room[data-role="teacher"] .stg-dock .tbar-jumps { display: none; }
  .room[data-role="teacher"] .stg-dock .cam-slot { display: flex; min-width: 0; }
  .room[data-role="teacher"] .stg-dock .cam-tile {
    flex-direction: column; justify-content: center; gap: 6px;
    min-height: 84px; padding: 8px 6px; text-align: center;
  }
  .room[data-role="teacher"] .stg-dock .cam-txt { align-items: center; }
  .room[data-role="teacher"] .stg-dock .cam-txt b { font-size: 14px; line-height: 1.35; }
  .room[data-role="teacher"] .stg-dock .cam-tile .cam-txt small { display: none; }
  .room[data-role="teacher"] .stg-dock .cam-tile .cam-ic { width: 40px; height: 40px; }
  .room[data-role="teacher"] .stg-dock .cam-tile .cam-prev { width: 88px; height: 50px; }
}

/* A phone on its side, a student, something shown: the page fills the screen; the class's words, the speaking
   state and the hand button stand in a narrow column beside it. */
@media (max-width: 959px) and (max-height: 540px) and (orientation: landscape) {
  .room[data-role="student"][data-stage] {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 236px;
    grid-auto-rows: min-content;
    align-content: start;
    padding-bottom: 0;
  }
  .room[data-role="student"][data-stage] .room-body,
  .room[data-role="student"][data-stage] .room-main { display: contents; }
  .room[data-role="student"][data-stage] > *,
  .room[data-role="student"][data-stage] .room-main > *,
  .room[data-role="student"][data-stage] .room-side { grid-column: 2; grid-row: auto; position: static; min-width: 0; margin-inline: 10px; }
  .room[data-role="student"][data-stage] .stg {
    grid-column: 1; grid-row: 1 / span 40;
    position: sticky; top: 0; align-self: start;
    height: 100dvh; margin: 0;
    --stg-max-h: 100dvh;
  }
  .room[data-role="student"][data-stage] .stg-frame, .room[data-role="student"][data-stage] .stg-view { height: 100%; }
  .room[data-role="student"][data-stage] .stg-view { --stg-fill: 1; max-height: none; border-radius: 0; }
  .room[data-role="student"][data-stage] .rbar {
    position: static; margin: 0; padding: max(6px, env(safe-area-inset-top)) 10px 6px; border-bottom: 0; background: none;
    gap: 6px;
  }
  .room[data-role="student"][data-stage] .rbar-title { flex: 1 1 100%; }
  .room[data-role="student"][data-stage] .rbar h1 { font-size: 15px; }
  .room[data-role="student"][data-stage] .rbar-sub { gap: 8px; font-size: 12px; }
  .room[data-role="student"][data-stage] .stage { margin-top: 4px; padding: 0; }
  .room[data-role="student"][data-stage] .stg-bar { padding-inline: 10px; }
  .room[data-role="student"][data-stage] .thumb { margin-top: 6px; }
  .room[data-role="student"][data-stage] .bigbtn { min-height: 52px; font-size: 17px; }
  .room[data-role="student"][data-stage] .room-side { margin-top: 12px; margin-bottom: 12px; }
}

/* A phone on its side, the teacher: the header scrolls away; the page and the tools sit above the mic bar. */
@media (max-width: 959px) and (max-height: 540px) and (orientation: landscape) {
  .room[data-role="teacher"] .stg { --stg-max-h: max(150px, calc(100dvh - var(--bar-h) - 120px)); }
}

/* The black ink's swatch keeps an edge on a dark ground. */
@media (prefers-color-scheme: dark) {
  .sw-dot, .sw-dot[data-c="0"] { box-shadow: inset 0 0 0 1.5px #8d9298; }
  [data-c="1"] > .sw-dot, .sw-dot[data-c="1"], [data-c="2"] > .sw-dot, .sw-dot[data-c="2"], [data-c="3"] > .sw-dot, .sw-dot[data-c="3"] { box-shadow: none; }
}
