/* DS_UI_Mobile rebuild for the teacher workbench. Desktop rules remain in index.html. */
@media (max-width: 820px) {
  :root {
    --p: #5f874b;
    --primary: #5f874b;
    --primary-dark: #466b37;
    --accent: #b77935;
    --bg: #f7f7f2;
    --ink: #292d27;
    --muted: #858980;
    --line: #e7eae3;
    --card: #fff;
    --panel: #fff;
    --panel-soft: #f3f5f1;
    --panel-hover: #edf2e9;
    --field: #fff;
    --accent-soft: #e9f1e4;
    --shadow: 0 10px 30px rgba(53, 66, 47, .075);
  }

  html {
    background: var(--bg);
    color-scheme: light;
    scroll-padding-top: 84px;
  }

  body {
    min-width: 320px;
    background: var(--bg);
    color: var(--ink);
    font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", sans-serif;
    font-size: 14px;
    font-weight: 500;
    -webkit-text-size-adjust: 100%;
    overscroll-behavior-y: none;
  }

  button, a, input, select, textarea { touch-action: manipulation; }
  button, .btn, a.btn { min-height: 44px; }
  button:active, .btn:active, a.btn:active, .homework-entry-grid > button:active,
  .homework-assignment:active, .dictation-session:active, .mobile-hub-item:active {
    transform: scale(.97) !important;
    transition: transform .15s ease !important;
  }

  input, select, textarea {
    min-height: 48px;
    border: 1px solid var(--line);
    border-radius: 14px;
    padding: 12px 14px;
    background: var(--field);
    color: var(--ink);
    font-size: 16px;
  }
  textarea { min-height: 112px; line-height: 1.65; }
  input::placeholder, textarea::placeholder { color: #a3a89f; }

  .app {
    display: block;
    min-height: 100dvh;
    padding: 0 0 calc(82px + env(safe-area-inset-bottom));
    background: var(--bg);
  }
  main { display: block; min-width: 0; }

  .top {
    position: sticky;
    z-index: 25;
    top: 0;
    min-height: 72px;
    height: auto;
    padding: max(12px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) 12px max(16px, env(safe-area-inset-left));
    align-items: center;
    gap: 10px;
    border: 0;
    border-bottom: 1px solid rgba(231, 234, 227, .88);
    background: rgba(247, 247, 242, .9);
    box-shadow: none;
    backdrop-filter: blur(18px) saturate(150%);
    -webkit-backdrop-filter: blur(18px) saturate(150%);
  }
  .top > div:first-child { min-width: 92px; }
  .top > div:first-child small {
    display: block;
    max-height: none;
    color: var(--muted);
    font-size: 11px;
    font-weight: 600;
  }
  .top h1 {
    overflow: hidden;
    margin: 3px 0 0;
    color: var(--ink);
    font-size: 20px;
    line-height: 1.2;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .top-tools {
    display: flex;
    max-width: 64%;
    margin-left: auto;
    gap: 7px;
    overflow-x: auto;
    scrollbar-width: none;
  }
  .top-tools::-webkit-scrollbar { display: none; }
  .top-tools .btn { flex: none; white-space: nowrap; }
  .top-tools #passkeySettings, .top-tools #themeToggle { display: none; }

  .content {
    width: 100%;
    max-width: none;
    padding: 16px max(16px, env(safe-area-inset-right)) 22px max(16px, env(safe-area-inset-left));
    overflow-x: clip;
  }
  #pages, .page { min-width: 0; max-width: 100%; }
  .page {
    opacity: 0;
    transform: translateY(18px);
    animation: dsMobileEnter .55s ease forwards;
  }

  .side {
    position: fixed;
    z-index: 40;
    inset: auto max(10px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
    width: auto;
    height: 70px;
    padding: 6px;
    border: 1px solid rgba(255, 255, 255, .72);
    border-radius: 24px;
    background: rgba(255, 255, 255, .62);
    box-shadow: 0 12px 38px rgba(38, 52, 78, .18), inset 0 1px rgba(255, 255, 255, .72);
    backdrop-filter: blur(26px) saturate(180%);
    -webkit-backdrop-filter: blur(26px) saturate(180%);
    transform: translateZ(0);
    transition: background-color .26s ease, border-color .26s ease, box-shadow .26s ease !important;
    will-change: auto !important;
  }
  .side .brand, .side > div:nth-child(2), .side .class { display: none !important; }
  .nav {
    position: relative;
    z-index: 41;
    inset: auto;
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 3px;
    width: 100%;
    height: 60px;
    overflow: visible;
    border: 1px solid transparent;
    border-radius: 18px;
    transition: background-color .24s ease, border-color .24s ease, box-shadow .24s ease;
  }
  .nav button {
    display: flex !important;
    min-width: 0 !important;
    min-height: 56px;
    height: 56px;
    padding: 5px 2px !important;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    gap: 3px;
    border: 0;
    border-radius: 16px;
    color: #7c869c;
    background: transparent;
    box-shadow: none !important;
    transform: none !important;
    transition: opacity .18s ease, color .2s ease, background-color .2s ease !important;
  }
  .nav button::before { content: none !important; }
  .nav button.active {
    color: var(--primary-dark);
    background: linear-gradient(135deg, #eef0ff, #e8faff);
    box-shadow: none !important;
  }
  .nav button:nth-child(2) .nav-icon { color: #0891b2; }
  .nav button:nth-child(3) .nav-icon { color: #f97366; }
  .nav button:nth-child(4) .nav-icon { color: #8b5cf6; }
  .nav button.active .nav-icon { color: currentColor; }
  .nav-icon { width: auto; font-size: 19px; line-height: 1; }
  .nav-label { max-width: 100%; font-size: 10px; font-weight: 700; line-height: 1.1; white-space: nowrap; }

  .card {
    min-width: 0;
    border: 1px solid rgba(255, 255, 255, .85);
    border-radius: 26px;
    padding: 18px;
    background: var(--card);
    box-shadow: var(--shadow);
  }
  .grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
    margin: 14px 0;
  }
  .grid .stat { min-height: 118px; display: flex; flex-direction: column; justify-content: center; }
  .stat b { margin: 5px 0; color: var(--primary-dark); font-size: 26px; }
  .stat small, .stat .sub { line-height: 1.45; }
  .two, .split, .form, .info-grid, .grade-curve-grid { grid-template-columns: minmax(0, 1fr) !important; gap: 12px; }

  .hero, .homework-hero, .dictation-hero {
    position: relative;
    overflow: hidden;
    padding: 25px 22px;
    border: 0;
    border-radius: 24px;
    background: linear-gradient(135deg, #466b37 0%, #5f874b 58%, #7da663 100%);
    color: #fff;
    box-shadow: 0 14px 34px rgba(70, 107, 55, .22);
  }
  .hero::after, .homework-hero::after, .dictation-hero::after {
    content: "";
    position: absolute;
    width: 128px;
    height: 128px;
    right: -44px;
    top: -54px;
    border: 18px solid rgba(255, 255, 255, .12);
    border-radius: 50%;
    pointer-events: none;
  }
  .hero h2, .homework-hero h2, .dictation-hero h2 { position: relative; z-index: 1; margin: 6px 0; font-size: 23px; line-height: 1.3; letter-spacing: -.01em; }
  .hero p, .homework-hero p, .dictation-hero p { position: relative; z-index: 1; color: #edf5e9; line-height: 1.65; }

  .btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 999px;
    padding: 10px 13px;
    font-weight: 650;
  }
  .btn.primary, .primary {
    border: 0;
    background: var(--primary);
    color: #fff;
    box-shadow: 0 7px 18px rgba(95, 135, 75, .24);
  }
  .btn.primary:active, .primary:active { background: var(--primary-dark); }
  .btn.danger, .danger { color: #c56352; }

  .head, .toolbar { min-width: 0; gap: 9px; }
  .head { align-items: flex-start; }
  .head h2, .head h3 { margin-top: 4px; }
  .toolbar {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    margin-bottom: 14px;
  }
  .toolbar > input, .toolbar > select, .toolbar > textarea { grid-column: 1 / -1; width: 100%; min-width: 0 !important; }
  .toolbar > .btn, .toolbar > a.btn { width: 100%; }
  .line { min-width: 0; align-items: flex-start; flex-wrap: wrap; padding: 13px 0; gap: 8px; }
  .line > span:not(.tag), .line > small { min-width: 0; flex-basis: 100%; overflow-wrap: anywhere; }
  .actions {
    gap: 8px;
    flex-wrap: wrap;
  }
  .actions > .btn, .actions > a.btn { flex: 1 1 132px; }

  .card:has(> .table), .grade-table-wrap { max-width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .table { min-width: 680px; font-size: 13px; }
  .table th, .table td { padding: 11px 10px; white-space: nowrap; }
  .table th { background: #f3f5f1; }
  .table tr:hover { background: #f7f9f5; }
  .table .btn[style*="color:#6967ef"], .student-name-link, .list-link,
  .homework-student-name .list-link { color: var(--primary-dark) !important; }
  #roster .student-roster-link {
    display: grid;
    min-height: 54px;
    padding: 4px 0;
    border: 0;
    color: var(--ink);
    background: transparent;
    text-align: left;
  }
  #roster .student-roster-link b { font-size: 16px; line-height: 1.35; }
  #roster .student-roster-link small { margin-top: 3px; color: var(--muted); font-size: 13px; font-weight: 500; }
  .tag { background: var(--accent-soft); color: var(--primary-dark); }
  .timeline-dot { background: var(--primary); }

  .modal {
    padding: 0;
    align-items: end;
    place-items: end center;
    background: rgba(26, 42, 58, .46);
    backdrop-filter: blur(5px);
    -webkit-backdrop-filter: blur(5px);
  }
  .dialog, .student-dialog-with-qr {
    width: 100%;
    max-width: none;
    max-height: min(90dvh, 880px);
    margin: 0;
    padding: 22px max(18px, env(safe-area-inset-right)) max(20px, env(safe-area-inset-bottom)) max(18px, env(safe-area-inset-left));
    border-radius: 26px 26px 0 0;
    animation: dsMobileDrawer .32s cubic-bezier(.32, .72, 0, 1);
  }
  .dialog::before {
    content: "";
    display: block;
    width: 42px;
    height: 5px;
    margin: -9px auto 16px;
    border-radius: 5px;
    background: #d8e0e9;
  }
  .dialog h2 { margin: 0 42px 16px 0; font-size: 22px; }
  .dialog-close { top: 14px; right: 16px; width: 44px; height: 44px; }
  .dialog .actions {
    position: sticky;
    z-index: 2;
    bottom: calc(-20px - env(safe-area-inset-bottom));
    margin: 18px -2px 0;
    padding: 12px 2px max(10px, env(safe-area-inset-bottom));
    background: var(--card);
    border-top: 1px solid var(--line);
  }

  .notice {
    bottom: calc(88px + env(safe-area-inset-bottom));
    width: max-content;
    max-width: calc(100vw - 32px);
    border-radius: 14px;
    padding: 12px 16px;
    text-align: center;
    line-height: 1.45;
  }

  .mobile-quick { gap: 11px; }
  .mobile-quick button {
    min-height: 104px;
    border-color: rgba(255, 255, 255, .85);
    border-radius: 24px;
    background: linear-gradient(145deg, #fff, #f4f7f1);
    box-shadow: var(--shadow);
  }
  /* Search remains a floating glass control, matching the existing iOS chrome. */
  .mobile-quick label {
    min-height: 104px;
    border: 1px solid rgba(255, 255, 255, .8);
    border-radius: 24px;
    background: rgba(255, 255, 255, .68);
    box-shadow: 0 10px 30px rgba(45, 58, 39, .11);
    backdrop-filter: blur(22px) saturate(160%);
    -webkit-backdrop-filter: blur(22px) saturate(160%);
  }
  .mobile-quick button b, .mobile-quick label b { color: var(--primary); }
  .today-row { grid-template-columns: 64px minmax(0, 1fr); padding: 13px 0; }
  .today-row small { grid-column: 2; }

  .homework-entry-hero { padding: 24px 20px; border-radius: 26px; background: linear-gradient(145deg, #edf4e9, #fff); border-color: #e1e8dc; }
  .homework-entry-hero h2 { color: var(--ink); font-size: 24px; }
  .homework-entry-grid { grid-template-columns: 1fr; gap: 12px; }
  .homework-entry-grid > button { min-height: 150px; padding: 20px; border-radius: 26px; box-shadow: var(--shadow); }
  .homework-entry-grid b { font-size: 20px; }
  .homework-assignment, .dictation-session, .dictation-history-link { min-height: 64px; border-radius: 16px; }
  .homework-detail-hero { border-radius: 20px; }
  .homework-student-row { min-height: 68px; }

  .seat { min-width: 620px; grid-template-columns: repeat(5, 1fr); }
  .card:has(> .seat) { overflow-x: auto; -webkit-overflow-scrolling: touch; }

  .mobile-hub {
    position: fixed;
    z-index: 80;
    inset: 0;
    display: grid;
    align-items: end;
    background: rgba(26, 42, 58, .42);
    opacity: 0;
    pointer-events: none;
    transition: opacity .22s ease;
    backdrop-filter: blur(5px);
    -webkit-backdrop-filter: blur(5px);
  }
  .mobile-hub.open { opacity: 1; pointer-events: auto; }
  .mobile-hub-panel {
    padding: 10px max(18px, env(safe-area-inset-right)) max(20px, env(safe-area-inset-bottom)) max(18px, env(safe-area-inset-left));
    border-radius: 30px 30px 0 0;
    background: var(--card);
    box-shadow: 0 -8px 40px rgba(0, 0, 0, .12);
    transform: translateY(100%);
    transition: transform .3s cubic-bezier(.32, .72, 0, 1);
  }
  .mobile-hub.open .mobile-hub-panel { transform: translateY(0); }
  .mobile-hub-handle { width: 42px; height: 5px; margin: 0 auto 16px; border-radius: 5px; background: #d8e0e9; }
  .mobile-hub-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
  .mobile-hub-head h2 { margin: 0; font-size: 22px; }
  .mobile-hub-close { width: 44px; height: 44px; border: 0; border-radius: 50%; background: var(--panel-soft); color: var(--ink); font-size: 24px; }
  .mobile-hub-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; }
  .mobile-hub-item {
    min-height: 92px;
    padding: 12px 6px;
    border: 1px solid var(--line);
    border-radius: 20px;
    background: var(--panel-soft);
    color: var(--ink);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 7px;
    font-weight: 700;
  }
  .mobile-hub-item .nav-icon { color: var(--primary); font-size: 22px; }

  html.mobile-menu-open, html.mobile-menu-open body { overflow: hidden; }
  html.homework-embedded-active .side, html.homework-scanner-active .side { display: none !important; }
  html.homework-embedded-active .app, html.homework-scanner-active .app { padding-bottom: 0; }

  html.night {
    color-scheme: dark;
    --bg: #161b15;
    --ink: #f0f3ed;
    --muted: #abb3a6;
    --line: #343c31;
    --card: #222920;
    --panel: #222920;
    --panel-soft: #2a3227;
    --panel-hover: #303a2d;
    --field: #1b2119;
    --accent-soft: #33402f;
    --shadow: 0 14px 36px rgba(0, 0, 0, .2);
  }
  html.night body, html.night .app { background: var(--bg); }
  html.night .top { background: rgba(22, 27, 21, .9); border-color: var(--line); }
  html.night .side { background: rgba(34, 41, 32, .88); border-color: rgba(255,255,255,.1); }
  html.night .nav button.active { background: linear-gradient(135deg, #292958, #183f55); color: #a8a7ff; }
  html.night .card, html.night .dialog, html.night .mobile-hub-panel { background: var(--card) !important; border-color: var(--line); }
  html.night .table th { background: var(--panel-soft); }
  html.night .table tr:hover { background: var(--panel-hover); }
  html.night .dialog::before, html.night .mobile-hub-handle { background: #43516a; }
  html.night .homework-entry-hero { background: linear-gradient(145deg, #293426, #222920); border-color: #3a4636; }
  html.night .mobile-quick label { background: rgba(34, 41, 32, .7); border-color: rgba(255,255,255,.1); }
  html.night .mobile-hub-item { background: var(--panel-soft); border-color: var(--line); }
}

@media (max-width: 380px) {
  .content { padding-left: 12px; padding-right: 12px; }
  .top { padding-left: 12px; padding-right: 12px; }
  .top h1 { font-size: 18px; }
  .top-tools { max-width: 60%; }
  .top-tools .btn { padding-inline: 10px; font-size: 12px; }
  .card { padding: 14px; border-radius: 18px; }
  .grid { gap: 8px; }
  .grid .stat { min-height: 108px; }
  .stat b { font-size: 22px; }
  .nav-label { font-size: 9px; }
  .mobile-hub-grid { gap: 8px; }
  .mobile-hub-item { min-height: 84px; font-size: 12px; }
}

@media (max-width: 768px) {
  .mobile-hub {
    --drawer-progress: 0;
    align-items: stretch;
    justify-items: start;
    overflow: hidden;
    background: transparent;
    opacity: 1;
    transition: none;
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
  }
  .mobile-hub::before {
    content: "";
    position: absolute;
    inset: 0;
    background: rgba(26, 42, 58, .42);
    opacity: var(--drawer-progress);
    transition: opacity .26s ease-out;
  }
  .mobile-hub.open { --drawer-progress: 1; }
  .mobile-hub.dragging { pointer-events: auto; }
  .mobile-hub.dragging::before, .mobile-hub.dragging .mobile-hub-panel { transition: none !important; }
  .mobile-hub-panel {
    position: relative;
    z-index: 1;
    width: min(82vw, 320px);
    height: 100dvh;
    max-height: none;
    padding: max(20px, env(safe-area-inset-top)) 18px max(20px, env(safe-area-inset-bottom)) max(18px, env(safe-area-inset-left));
    overflow-x: hidden;
    overflow-y: auto;
    border-radius: 0 28px 28px 0;
    box-shadow: 16px 0 40px rgba(20, 31, 46, .2);
    transform: translateX(-100%);
    transition: transform .26s cubic-bezier(.22, .8, .24, 1);
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }
  .mobile-hub.open .mobile-hub-panel { transform: translateX(0); }
  .mobile-hub-handle {
    position: absolute;
    top: 50%;
    right: 8px;
    width: 5px;
    height: 42px;
    margin: -21px 0 0;
    border-radius: 5px;
  }
  .mobile-hub-head { padding-right: 8px; }
  .mobile-hub-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .mobile-hub-item { min-height: 78px; }
  html.mobile-drawer-dragging, html.mobile-drawer-dragging body { overflow: hidden; }
}

@keyframes dsMobileEnter {
  to { opacity: 1; transform: translateY(0); }
}

@keyframes dsMobileDrawer {
  from { opacity: .92; transform: translateY(100%); }
  to { opacity: 1; transform: translateY(0); }
}

@media (prefers-reduced-motion: reduce) {
  .page, .dialog, .mobile-hub, .mobile-hub-panel, .side, .nav, .nav button { animation: none !important; transition-duration: .01ms !important; }
  .page { opacity: 1 !important; transform: none !important; }
}

@media (max-width: 820px) and (prefers-reduced-transparency: reduce) {
  .top, .side, .mobile-quick label {
    background: var(--card) !important;
    backdrop-filter: none !important;
    -webkit-backdrop-filter: none !important;
  }
}

@media (max-width: 820px) and (prefers-contrast: more) {
  .side, .mobile-quick label { border-color: color-mix(in srgb, var(--ink) 38%, transparent); }
}
