/* ============================================================
   MATHEZEIT — Polish Module
   Patches: Mobile (Bottom-Sheets, Touch-Targets, Stack-Layouts)
            A11y (Focus, Labels, Live-Regions, Contrast)
            Reduced-Motion (Animations off, Confetti off)

   Loaded AFTER the per-file inline <style> block so its rules
   override defaults. Logic and layout are untouched on desktop.
   ============================================================ */


/* ──────────────────────────────────────────────────────────
   FOCUS — visible, consistent across the app
   ────────────────────────────────────────────────────────── */
*:focus { outline: none; }

button:focus-visible,
a:focus-visible,
[role="button"]:focus-visible,
[tabindex]:focus-visible,
.demo-btn:focus-visible,
.tabs button:focus-visible {
  outline: 2px solid var(--violet, #8B5CF6);
  outline-offset: 3px;
  border-radius: 8px;
  box-shadow: 0 0 0 4px rgba(139, 92, 246, 0.22);
}

input:focus-visible,
textarea:focus-visible,
select:focus-visible {
  /* fields already have a violet border + glow on :focus — keep that visual.
     We add a stronger outer halo for keyboard users. */
  outline: 2px solid rgba(139, 92, 246, 0.6);
  outline-offset: 2px;
}

.code-slot:focus-visible,
.field-input:focus-visible,
.field-textarea:focus-visible,
.field-select:focus-visible,
.teacher-search-input:focus-visible {
  outline: 2px solid rgba(139, 92, 246, 0.6);
  outline-offset: 2px;
}


/* ──────────────────────────────────────────────────────────
   TOUCH — min 44×44 targets on coarse pointers
   ────────────────────────────────────────────────────────── */
@media (hover: none), (pointer: coarse) {
  .cc-menu-btn,
  .drawer-close,
  .drawer-icon-btn,
  .modal-close,
  .panel-close,
  .pass-toggle,
  .email-chip-remove,
  .domain-item-remove,
  .detected-chip-remove,
  .jw-new-row .rm,
  .file-clear,
  #file-clear {
    min-width: 44px;
    min-height: 44px;
  }
  .demo-btn,
  .sort-item,
  .tabs button,
  .add-item,
  .cc-menu-item,
  .pm-row,
  .pick-chip,
  .sort-btn,
  .verwaltung-btn,
  .foot-link,
  .pill,
  .rc-action,
  .auto-item,
  .ts-item {
    min-height: 44px;
  }
  .code-slot { min-height: 48px; }
}


/* ──────────────────────────────────────────────────────────
   KEYBOARD INSET — modal stays visible while typing on mobile
   ────────────────────────────────────────────────────────── */
.modal-stage,
.drawer-stage {
  padding-bottom: env(keyboard-inset-height, 0);
}
.modal-body,
.drawer-body {
  scroll-padding-bottom: env(keyboard-inset-height, 24px);
}


/* ──────────────────────────────────────────────────────────
   ACTIVE STATES — give touch users visual feedback
   that desktop hover hides
   ────────────────────────────────────────────────────────── */
@media (hover: none) {
  .pill:active,
  .add-item:active,
  .pm-row:active,
  .school-row:active,
  .teacher-row:active,
  .class-card:active,
  .rc-action:active,
  .verwaltung-btn:active,
  .dr-action:active,
  .ts-item:active,
  .auto-item:active,
  .foot-link:active,
  .sort-item:active,
  .cc-menu-item:active,
  .pick-chip:active,
  .school-pick:active {
    background: rgba(139, 92, 246, 0.10);
    transform: scale(0.985);
    transition: background 0.08s ease, transform 0.08s ease;
  }
  .cc-menu-btn:active,
  .drawer-close:active,
  .modal-close:active,
  .panel-close:active {
    background: var(--card-3, #ECE7DE);
    transform: scale(0.94);
  }
}


/* ──────────────────────────────────────────────────────────
   BOTTOM SHEETS — generic, applies to every modal in the app
   ────────────────────────────────────────────────────────── */
@media (max-width: 480px) {
  .modal-stage {
    padding: 0 !important;
    align-items: flex-end !important;
  }
  .modal-stage .modal {
    width: 100% !important;
    max-width: 100% !important;
    border-radius: 18px 18px 0 0 !important;
    max-height: 92vh !important;
    transform: translateY(40px) !important;
    transition: transform 0.32s cubic-bezier(.22, 1.1, .32, 1),
                opacity 0.22s ease !important;
  }
  .modal-stage.is-on .modal {
    transform: translateY(0) !important;
  }
  .modal-stage.is-closing .modal {
    transform: translateY(40px) !important;
    opacity: 0 !important;
  }
  .modal::before {
    content: '';
    position: absolute;
    top: 8px;
    left: 50%;
    transform: translateX(-50%);
    width: 40px;
    height: 4px;
    background: var(--rule, rgba(63, 55, 40, 0.18));
    border-radius: 2px;
    z-index: 10;
  }
  .modal-head {
    padding-top: 26px !important;
    position: sticky;
    top: 0;
    background: var(--card, #FFF);
    z-index: 5;
    border-bottom: 1px solid var(--hairline, rgba(63,55,40,0.07));
  }

  /* QR lightbox bottom-sheet too */
  .qr-lightbox-stage {
    padding: 0 !important;
    align-items: flex-end !important;
  }
  .qr-lightbox-stage .qr-lightbox-card {
    width: 100%;
    padding-bottom: 18px;
  }
  .qr-lightbox-stage .qr-lightbox-frame {
    width: 100% !important;
    max-width: 100% !important;
    border-radius: 18px 18px 0 0;
  }
}


/* ──────────────────────────────────────────────────────────
   DRAWER — bottom-sheet on mobile (S-9, S-10)
   ────────────────────────────────────────────────────────── */
@media (max-width: 720px) {
  .drawer-stage .drawer {
    top: auto !important;
    width: 100% !important;
    max-width: 100% !important;
    height: 92vh !important;
    border-radius: 18px 18px 0 0 !important;
    transform: translateY(100%) !important;
    transition: transform 0.36s cubic-bezier(.22, .9, .32, 1) !important;
  }
  .drawer-stage.is-on .drawer {
    transform: translateY(0) !important;
  }
  .drawer-stage .drawer.is-closing {
    transform: translateY(100%) !important;
  }
  .drawer-stage .drawer::before {
    content: '';
    position: absolute;
    top: 8px;
    left: 50%;
    transform: translateX(-50%);
    width: 40px;
    height: 4px;
    background: var(--rule, rgba(63, 55, 40, 0.18));
    border-radius: 2px;
    z-index: 10;
  }
  .drawer-top,
  .drawer-topbar {
    padding-top: 22px !important;
    position: sticky;
    top: 0;
    background: var(--card, #FFF);
    z-index: 5;
  }
}


/* ──────────────────────────────────────────────────────────
   TABLE → CARD on narrow viewports for the teacher list
   ────────────────────────────────────────────────────────── */
@media (max-width: 600px) {
  .teacher-row {
    grid-template-columns: 44px 1fr !important;
    gap: 12px !important;
    padding: 14px 18px !important;
    align-items: flex-start;
  }
  .teacher-row > .teacher-av { grid-row: span 2; }
  .teacher-row .teacher-email,
  .teacher-row .teacher-classes,
  .teacher-row .teacher-status {
    grid-column: 2 / -1;
  }
  .teacher-row .teacher-email {
    font-size: 12px !important;
    margin-top: 4px;
  }
  .teacher-row .teacher-classes {
    margin-top: 6px;
  }
  .teacher-row .teacher-status {
    margin-top: 6px;
    justify-self: flex-start;
  }
}


/* ──────────────────────────────────────────────────────────
   BENTO STATS — stack 4 → 2 cols on small phones
   ────────────────────────────────────────────────────────── */
@media (max-width: 520px) {
  .school-stats,
  .dr-quick-stats {
    grid-template-columns: repeat(2, 1fr) !important;
  }
  .school-stats > .s-stat,
  .dr-quick-stats > .dr-stat {
    border-bottom: 1px solid var(--hairline, rgba(63,55,40,0.07));
  }
  .school-stats > .s-stat:nth-last-child(-n+2),
  .dr-quick-stats > .dr-stat:nth-last-child(-n+2) {
    border-bottom: 0;
  }
  .school-stats > .s-stat:nth-child(2n),
  .dr-quick-stats > .dr-stat:nth-child(2n) {
    border-right: 0;
  }
}


/* ──────────────────────────────────────────────────────────
   HORIZONTAL SCROLL for stat rows on very narrow screens
   when stacking would feel awkward
   ────────────────────────────────────────────────────────── */
@media (max-width: 380px) {
  .stats,
  .activity-labels {
    overflow-x: auto;
    scroll-snap-type: x mandatory;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  .stats::-webkit-scrollbar,
  .activity-labels::-webkit-scrollbar {
    display: none;
  }
}


/* ──────────────────────────────────────────────────────────
   TOAST — live region styling reinforcement
   (the actual aria-live="polite" is set in HTML)
   ────────────────────────────────────────────────────────── */
.toast[aria-live="polite"]:not(.is-on) {
  /* keep the element rendered but visually hidden — screen-readers
     can still pick up content changes */
  visibility: visible;
}


/* ──────────────────────────────────────────────────────────
   CODE SLOT FIELDSET — keyboard-friendly inputs
   ────────────────────────────────────────────────────────── */
.code-slots .code-slot,
.code-wrap .code-slot {
  caret-color: var(--violet, #8B5CF6);
}


/* ──────────────────────────────────────────────────────────
   FILTER PILLS — large enough tap area
   ────────────────────────────────────────────────────────── */
@media (hover: none), (pointer: coarse) {
  .list-filter-pill,
  .list-search input {
    min-height: 44px;
  }
}


/* ──────────────────────────────────────────────────────────
   COLOR CONTRAST — bump muted greys one notch on very low
   contrast displays via accent CSS
   ────────────────────────────────────────────────────────── */
@media (prefers-contrast: more) {
  :root {
    --ink-mute: #5C5440 !important;
    --ink-dim: #7A7060 !important;
    --hairline: rgba(63, 55, 40, 0.18) !important;
    --rule: rgba(63, 55, 40, 0.30) !important;
  }
  .field-input::placeholder,
  .field-textarea::placeholder,
  input::placeholder,
  textarea::placeholder {
    color: #6C6450 !important;
  }
}


/* ──────────────────────────────────────────────────────────
   REDUCED MOTION — everything calms down to opacity-only
   ────────────────────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {

  /* Disable all transitions, animations, scroll smoothing */
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-delay: 0ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    transition-delay: 0ms !important;
    scroll-behavior: auto !important;
  }

  /* Confetti completely off */
  .confetti,
  .confetti-stage,
  .confetti i {
    display: none !important;
  }

  /* Drawer: simple fade, no slide */
  .drawer {
    transform: none !important;
    opacity: 0;
    transition: opacity 0.15s ease !important;
  }
  .drawer-stage.is-on .drawer {
    opacity: 1 !important;
    transform: none !important;
  }
  .drawer-stage.is-closing .drawer,
  .drawer.is-closing {
    opacity: 0 !important;
    transform: none !important;
  }

  /* Modals: simple fade */
  .modal-stage .modal {
    transform: none !important;
    opacity: 0;
    transition: opacity 0.15s ease !important;
  }
  .modal-stage.is-on .modal {
    opacity: 1 !important;
    transform: none !important;
  }
  .modal-stage.is-closing .modal {
    opacity: 0 !important;
    transform: none !important;
  }

  /* QR lightbox: fade only */
  .qr-lightbox-card,
  .qr-lightbox {
    transform: none !important;
  }

  /* Live-pulse dots become solid colored dots, no pulse */
  .stat-live::before,
  .cc-meta .heart::before,
  .activity-day.is-today::after,
  .activity-day.is-today,
  .kp-tile-live::before,
  .s-stat.live .s-stat-lbl::before,
  .live-dot,
  .dr-chart-bar.is-today {
    animation: none !important;
    box-shadow: none !important;
  }

  /* Stagger delays off */
  [style*="animation-delay"] {
    animation-delay: 0ms !important;
  }
  .class-card,
  .choice,
  .pm-row,
  .section,
  .detected-chip,
  .school-row {
    animation: none !important;
    opacity: 1 !important;
    transform: none !important;
  }

  /* Activity bars: draw instantly */
  .activity-day::after,
  .dr-chart-bar {
    transition: none !important;
  }

  /* Card-vanish + card-arrive: instant */
  .class-card.is-vanishing,
  .class-card.is-arriving,
  .teacher-row.is-new {
    animation: none !important;
    opacity: 1 !important;
    transform: none !important;
  }

  /* Hero-card live-pulses off */
  @keyframes livePulse { 0%, 100% { box-shadow: none; } }
  @keyframes dayPulse  { 0%, 100% { box-shadow: none; } }
  @keyframes kpPulse   { 0%, 100% { box-shadow: none; } }
  @keyframes liveDot   { 0%, 100% { box-shadow: none; } }
}


/* ──────────────────────────────────────────────────────────
   FORCED COLORS / WINDOWS HIGH-CONTRAST
   ────────────────────────────────────────────────────────── */
@media (forced-colors: active) {
  .modal,
  .drawer,
  .panel,
  .section,
  .card,
  .list-card,
  .recommend-card,
  .form-card {
    border: 1px solid CanvasText !important;
  }
  .btn-primary,
  .btn-danger,
  .submit-btn,
  .add-class-btn {
    forced-color-adjust: none;
    border: 2px solid ButtonText !important;
  }
  .cc-grade,
  .role-tag,
  .teacher-status,
  .class-chip {
    border: 1px solid CanvasText;
  }
}


/* ──────────────────────────────────────────────────────────
   SKIP-NAV (optional, hidden until focused)
   ────────────────────────────────────────────────────────── */
.skip-link {
  position: absolute;
  left: -9999px;
  top: 0;
  z-index: 9999;
  padding: 10px 14px;
  background: var(--ink, #3F3728);
  color: #FFF;
  border-radius: 0 0 11px 0;
  font-size: 13px;
  font-weight: 600;
  text-decoration: none;
}
.skip-link:focus {
  left: 0;
}
