:root {
  --app-mobile-bar-height: 60px;
  --app-mobile-safe-bottom: env(safe-area-inset-bottom, 0px);
}

.app-mobile-overlay,
.app-mobile-topbar,
.app-mobile-sidebar-close,
.app-mobile-tools-toggle,
.app-mobile-tools-close {
  display: none;
}

.app-table-scroll {
  max-width: 100%;
  overflow-x: auto;
  overscroll-behavior-inline: contain;
  -webkit-overflow-scrolling: touch;
}

@media (max-width: 767px) {
  html,
  body {
    width: 100%;
    max-width: 100%;
    overflow-x: hidden;
  }

  body {
    min-height: 100dvh;
  }

  body > .relative.flex.h-screen,
  body > .flex.h-screen,
  body > .relative.flex.min-h-screen {
    width: 100%;
    min-width: 0;
  }

  main {
    width: 100%;
    min-width: 0;
  }

  main > *,
  main section,
  main article,
  main form {
    max-width: 100%;
    min-width: 0;
  }

  h1,
  h2,
  h3,
  p,
  td,
  th,
  label,
  a,
  button {
    overflow-wrap: anywhere;
  }

  input,
  select,
  textarea {
    max-width: 100%;
    font-size: 16px !important;
  }

  button,
  [role="button"],
  a.rounded-lg,
  a.rounded-xl {
    touch-action: manipulation;
  }

  button:not(.app-mobile-sidebar-close):not(.app-mobile-tools-close),
  [role="button"] {
    min-height: 42px;
  }

  main .w-64,
  main .min-w-\[150px\],
  main .min-w-\[170px\],
  main .min-w-\[220px\] {
    width: 100% !important;
    min-width: 0 !important;
  }

  main .p-8,
  main .p-10 {
    padding: 1rem !important;
  }

  main .px-8,
  main .px-10 {
    padding-left: 1rem !important;
    padding-right: 1rem !important;
  }

  main .text-4xl {
    font-size: 1.875rem !important;
    line-height: 2.25rem !important;
  }

  main .text-3xl {
    font-size: 1.625rem !important;
    line-height: 2rem !important;
  }

  .app-mobile-topbar {
    position: sticky;
    top: 0;
    z-index: 35;
    display: flex;
    min-height: var(--app-mobile-bar-height);
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    border-bottom: 1px solid #e2e8f0;
    background: rgba(255, 255, 255, 0.96);
    padding: 0.65rem 1rem;
    color: #0f172a;
    box-shadow: 0 1px 3px rgba(15, 23, 42, 0.08);
    backdrop-filter: blur(10px);
  }

  .dark .app-mobile-topbar {
    border-color: #334155;
    background: rgba(30, 41, 54, 0.96);
    color: #f8fafc;
  }

  .app-mobile-brand {
    min-width: 0;
    overflow: hidden;
    color: #1773cf;
    font-size: 1rem;
    font-weight: 800;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .app-mobile-menu-button,
  [data-mobile-menu-toggle] {
    display: inline-flex;
    width: 44px;
    min-width: 44px;
    height: 44px;
    align-items: center;
    justify-content: center;
    border: 1px solid #dbe4ee;
    border-radius: 8px;
    background: #fff;
    color: #0f172a;
  }

  .dark .app-mobile-menu-button,
  .dark [data-mobile-menu-toggle] {
    border-color: #334155;
    background: #1e2936;
    color: #f8fafc;
  }

  .app-primary-sidebar {
    position: fixed !important;
    inset: 0 auto 0 0;
    z-index: 70;
    display: flex !important;
    width: min(86vw, 304px) !important;
    max-width: 304px;
    height: 100dvh !important;
    overflow-y: auto;
    background: #fff;
    box-shadow: 18px 0 45px rgba(15, 23, 42, 0.22);
    transform: translateX(-105%);
    transition: transform 180ms ease;
  }

  .dark .app-primary-sidebar {
    background: #1e2936;
  }

  body.app-mobile-nav-open .app-primary-sidebar {
    transform: translateX(0);
  }

  .app-mobile-sidebar-close,
  .app-mobile-tools-close {
    position: absolute;
    top: 0.75rem;
    right: 0.75rem;
    z-index: 2;
    display: inline-flex;
    width: 40px;
    height: 40px;
    align-items: center;
    justify-content: center;
    border: 1px solid #dbe4ee;
    border-radius: 8px;
    background: #fff;
    color: #334155;
  }

  .dark .app-mobile-sidebar-close {
    border-color: #475569;
    background: #1e2936;
    color: #f8fafc;
  }

  .app-mobile-overlay {
    position: fixed;
    inset: 0;
    z-index: 60;
    display: block;
    visibility: hidden;
    background: rgba(15, 23, 42, 0.52);
    opacity: 0;
    transition: opacity 180ms ease, visibility 180ms ease;
  }

  body.app-mobile-nav-open .app-mobile-overlay,
  body.app-mobile-tools-open .app-mobile-overlay {
    visibility: visible;
    opacity: 1;
  }

  body.app-mobile-nav-open,
  body.app-mobile-tools-open {
    overflow: hidden !important;
  }

  .app-responsive-table {
    width: max-content;
    min-width: 680px !important;
  }

  .app-table-scroll {
    position: relative;
    display: block;
    width: 100%;
    max-width: calc(100vw - 2rem);
  }

  .app-table-scroll table th,
  .app-table-scroll table td {
    padding-left: 0.75rem;
    padding-right: 0.75rem;
  }

  .app-mobile-scroll-note::after {
    content: "Desliza para ver más";
    position: sticky;
    left: 0;
    display: block;
    width: max-content;
    padding: 0.45rem 0.75rem;
    color: #64748b;
    font-size: 0.7rem;
    font-weight: 600;
  }

  [role="dialog"],
  .app-mobile-modal > div:last-child > div {
    max-width: calc(100vw - 1rem) !important;
  }

  .app-mobile-modal .p-6 {
    padding: 1rem !important;
  }

  .app-mobile-action-row {
    display: grid !important;
    width: 100%;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    align-items: stretch !important;
  }

  .app-mobile-action-row > button,
  .app-mobile-action-row > a {
    width: 100%;
    min-width: 0;
    justify-content: center;
  }

  .app-mobile-action-row > :last-child:nth-child(odd) {
    grid-column: 1 / -1;
  }

  #grades-units-tabs {
    width: 100%;
    overflow-x: auto;
    justify-content: flex-start;
  }

  #grades-units-tabs > * {
    flex: 0 0 auto;
  }

  #grades-student-search {
    width: 100% !important;
  }

  #grades-search-toolbar {
    display: grid !important;
    width: 100%;
    grid-template-columns: minmax(0, 1fr) 44px 44px !important;
    gap: 0.5rem !important;
  }

  #grades-search-toolbar > .relative {
    min-width: 0;
  }

  #grades-search-toolbar > button {
    width: 44px !important;
    min-width: 44px;
    padding: 0 !important;
    justify-content: center;
  }

  #grades-units-tabs,
  nav[aria-label="Tabs"] {
    scrollbar-width: none;
  }

  #grades-units-tabs::-webkit-scrollbar,
  nav[aria-label="Tabs"]::-webkit-scrollbar {
    display: none;
  }

  #grades-table-shell {
    height: auto !important;
    max-height: none !important;
    overflow: visible !important;
    background: transparent !important;
    border: 0 !important;
    box-shadow: none !important;
  }

  #grades-desktop-table {
    display: none !important;
  }

  #grades-mobile-list {
    display: flex !important;
    flex-direction: column;
    gap: 0.75rem;
  }

  .grades-mobile-student {
    box-shadow: 0 1px 3px rgba(15, 23, 42, 0.08);
  }

  .grades-mobile-summary::-webkit-details-marker {
    display: none;
  }

  .grades-mobile-summary::marker {
    content: "";
  }

  .grades-mobile-chevron {
    transition: transform 160ms ease;
  }

  .grades-mobile-student[open] .grades-mobile-chevron {
    transform: rotate(180deg);
  }

  .grades-mobile-category + .grades-mobile-category {
    border-top: 1px solid #e2e8f0;
  }

  .dark .grades-mobile-category + .grades-mobile-category {
    border-color: #334155;
  }

  .grades-mobile-field {
    display: flex;
    min-height: 64px;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    padding-top: 0.6rem;
    padding-bottom: 0.6rem;
  }

  .grades-mobile-field > :first-child {
    min-width: 0;
  }

  .grades-mobile-field input {
    flex: 0 0 80px;
    font-size: 16px !important;
  }

  #grades-table-shell + div {
    flex-wrap: wrap;
    gap: 0.5rem 1rem;
  }

  #editor-grid {
    min-width: 720px !important;
  }

  #editor-grid-shell,
  .app-schedule-scroll {
    overflow: auto !important;
    -webkit-overflow-scrolling: touch;
  }

  .app-editor-tools {
    position: fixed !important;
    inset: auto 0 0 0;
    z-index: 75 !important;
    display: flex !important;
    width: 100% !important;
    max-width: none !important;
    height: min(72dvh, 620px) !important;
    padding-bottom: var(--app-mobile-safe-bottom);
    border-top: 1px solid #dbe4ee;
    border-left: 0 !important;
    border-radius: 8px 8px 0 0;
    transform: translateY(105%);
    transition: transform 180ms ease;
  }

  body.app-mobile-tools-open .app-editor-tools {
    transform: translateY(0);
  }

  .app-editor-tools .app-mobile-action-row {
    display: flex !important;
  }

  .app-editor-tools .app-mobile-action-row > button {
    width: auto;
    flex: 1 1 0;
  }

  .app-mobile-tools-toggle {
    position: fixed;
    right: 1rem;
    bottom: calc(1rem + var(--app-mobile-safe-bottom));
    z-index: 45;
    display: inline-flex;
    min-height: 48px;
    align-items: center;
    gap: 0.5rem;
    border-radius: 8px;
    background: #1773cf;
    padding: 0.7rem 1rem;
    color: #fff;
    font-size: 0.8rem;
    font-weight: 700;
    box-shadow: 0 10px 25px rgba(23, 115, 207, 0.28);
  }

  #editor-autofill-btn span + *,
  #editor-publish-btn span + * {
    min-width: 0;
  }

  header.app-editor-header {
    align-items: flex-start !important;
    flex-direction: column !important;
    gap: 0.75rem;
    padding: 0.75rem !important;
  }

  header.app-editor-header > div {
    min-width: 0;
    flex-wrap: wrap;
  }

  header.app-editor-header > div:last-child {
    width: 100%;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  header.app-editor-header > div:last-child > button {
    width: 100%;
    padding-left: 0.5rem;
    padding-right: 0.5rem;
  }

  #editor-autofill-btn {
    grid-column: 1 / -1;
  }

  header.app-editor-header > div:last-child > div.h-8 {
    display: none;
  }

  .app-mobile-compact-meta {
    flex-wrap: wrap !important;
    row-gap: 0.4rem !important;
  }

  /* Keep the group identity useful without consuming most of the first screen. */
  .group-detail-summary {
    padding: 1rem !important;
    border-radius: 12px !important;
  }

  .group-detail-summary-layout {
    display: grid !important;
    grid-template-columns: 52px minmax(0, 1fr) 70px;
    align-items: center !important;
    gap: 0.5rem !important;
  }

  .group-detail-summary-identity {
    display: contents !important;
  }

  .group-detail-summary-identity > [id$="-group-code"] {
    width: 52px !important;
    height: 52px !important;
    border-radius: 8px !important;
    font-size: 1.25rem !important;
    line-height: 1.5rem !important;
  }

  .group-detail-summary-content {
    min-width: 0;
  }

  .group-detail-summary-content > div:first-child {
    flex-wrap: wrap;
    gap: 0.25rem 0.5rem !important;
    margin-bottom: 0.25rem !important;
  }

  .group-detail-summary-content h2 {
    min-width: 0;
    overflow: hidden;
    font-size: 1.25rem !important;
    line-height: 1.5rem !important;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .group-detail-summary-meta {
    flex-wrap: wrap !important;
    gap: 0.2rem 0.4rem !important;
    font-size: 0.6875rem !important;
    line-height: 1rem !important;
  }

  .group-detail-summary-meta > span {
    min-width: 0;
  }

  .group-detail-summary-meta > span:last-child {
    flex-basis: 100%;
  }

  .group-detail-summary-meta .material-symbols-outlined {
    font-size: 16px !important;
  }

  .group-detail-summary-average {
    align-self: stretch;
    justify-content: center;
    border-left: 1px solid #e2e8f0;
    padding-left: 0.5rem;
  }

  .dark .group-detail-summary-average {
    border-color: #334155;
  }

  .group-detail-summary-average > span:first-child {
    font-size: 0.625rem !important;
    line-height: 0.875rem !important;
    max-width: 60px;
    text-align: right;
    white-space: normal;
  }

  .group-detail-summary-average [id$="-avg-score"] {
    font-size: 1.75rem !important;
    line-height: 2rem !important;
  }

  .ddgc-communication-layout {
    gap: 1rem !important;
  }

  .ddgc-recipients-header {
    gap: 0.75rem !important;
    padding: 0.875rem !important;
  }

  .ddgc-recipients-header > .relative,
  .ddgc-recipients-header input {
    width: 100% !important;
  }

  .ddgc-selection-bar {
    gap: 0.75rem;
    padding: 0.625rem 0.875rem !important;
  }

  #ddgc-recipients-list {
    max-height: min(52dvh, 460px) !important;
    overscroll-behavior: contain;
  }

  .ddgc-recipient-row {
    gap: 0.5rem !important;
    padding: 0.75rem !important;
  }

  .ddgc-recipient-student {
    min-width: 0;
    flex: 1 1 auto;
    gap: 0.5rem !important;
  }

  .ddgc-recipient-check,
  .ddgc-recipient-avatar,
  .ddgc-recipient-whatsapp {
    flex: 0 0 auto;
  }

  .ddgc-recipient-avatar {
    width: 36px !important;
    height: 36px !important;
  }

  .ddgc-recipient-student-info {
    display: block;
    min-width: 0;
  }

  .ddgc-recipient-name {
    display: -webkit-box;
    overflow: hidden;
    line-height: 1.1rem;
    overflow-wrap: anywhere;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
  }

  .ddgc-recipient-guardian {
    min-width: 0 !important;
    flex: 0 0 auto;
    gap: 0.375rem !important;
    border: 0 !important;
    background: transparent !important;
    padding: 0 !important;
  }

  .ddgc-recipient-guardian-copy {
    max-width: 56px;
    text-align: right;
  }

  .ddgc-recipient-guardian-copy > span {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .ddgc-recipient-whatsapp {
    display: inline-flex;
    width: 40px;
    height: 40px;
    align-items: center;
    justify-content: center;
    padding: 0 !important;
  }

  .ddgc-message-input {
    min-height: 120px !important;
  }

  .ddg-attendance-toolbar {
    align-items: stretch !important;
    gap: 0.75rem !important;
  }

  .ddg-date-controls {
    display: grid !important;
    width: 100% !important;
    grid-template-columns: 40px minmax(0, 1fr) 40px !important;
    align-items: center !important;
    gap: 0.375rem !important;
  }

  .ddg-date-controls > button {
    display: inline-flex !important;
    width: 40px !important;
    min-width: 40px !important;
    height: 40px !important;
    align-items: center;
    justify-content: center;
    padding: 0 !important;
  }

  #ddg-month-label {
    min-width: 0;
    overflow: hidden;
    text-align: center;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  #ddg-attendance-date {
    width: 100% !important;
    margin-left: 0 !important;
    grid-column: 1 / -1;
  }
}

@media (max-width: 420px) {
  .app-mobile-action-row {
    grid-template-columns: 1fr;
  }

  .app-mobile-action-row > :last-child:nth-child(odd) {
    grid-column: auto;
  }

  main .rounded-2xl.p-6,
  main .rounded-xl.p-6 {
    padding: 1rem !important;
  }

  .app-table-scroll {
    max-width: calc(100vw - 1rem);
  }
}

@media (max-width: 350px) {
  .ddgc-recipient-guardian-copy {
    display: none !important;
  }
}

@media (prefers-reduced-motion: reduce) {
  .app-primary-sidebar,
  .app-mobile-overlay,
  .app-editor-tools {
    transition: none !important;
  }
}
