/* Phone-only overrides. Desktop rules stay in the existing stylesheets.*/
@media (max-width: 600px), (pointer: coarse) and (orientation: landscape) and (max-width: 960px) and (max-height: 500px) {
  .container {
    width: calc(100% - 32px);
  }

  .topbar-inner {
    min-height: 56px;
    padding-block: 10px;
  }

  .brand {
    font-size: 20px;
  }

  .intro {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    padding: 16px 0;
    gap: 12px;
  }

  .intro h1 {
    font-size: 26px;
    line-height: 1.2;
  }

  .intro p {
    margin-top: 6px;
    font-size: 13px;
  }

  .intro .actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    width: 100%;
    gap: 8px;
    margin-top: 12px;
  }

  .intro .actions .button {
    width: 100%;
    min-height: 48px;
    padding: 8px;
    white-space: normal;
  }

  button, .button, .button.small, .text-button, .filter-container button,
  .custom-select-trigger, .custom-select-option, .schedule-view-switch, .group-toggle {
    min-height: 44px;
  }

  .icon-button, .row-icon-button, .table-actions button {
    width: 44px;
    min-width: 44px;
    height: 44px;
    min-height: 44px;
  }

  input:not([type="checkbox"]):not([type="color"]), textarea, select {
    font-size: 16px;
  }

  input:not([type="checkbox"]):not([type="color"]), select {
    min-height: 44px;
  }

  input:focus, textarea:focus {
    border-color: var(--mint);
  }

  input::placeholder, textarea::placeholder {
    color: #8aa297;
  }

  .custom-select-native {
    min-height: 0 !important;
  }

  .custom-select-trigger {
    padding: 9px 36px 9px 12px;
    font-size: 14px;
  }

  .section-context .custom-select-trigger,
  .meeting-row .custom-select-trigger,
  .section-curriculum-tools .custom-select-trigger {
    font-size: 14px;
  }

  .table-actions, .group-actions, .period-actions {
    opacity: 1;
    pointer-events: auto;
  }

  .table-actions, .group-actions {
    display: flex;
    gap: 6px;
    flex-shrink: 0;
  }

  .view-nav {
    display: block;
    min-width: 0;
    padding-block: 4px;
  }

  .view-nav [role="tablist"] {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 4px;
    width: 100%;
    overflow: visible;
  }

  .view-nav [role="tab"] {
    min-width: 0;
    min-height: 34px;
    padding: 4px 2px;
    border: 1px solid var(--line);
    border-radius: 7px;
    background: var(--surface);
    font-size: 12px;
    letter-spacing: -0.2px;
    text-align: center;
    white-space: nowrap;
  }

  .view-nav [role="tab"][aria-selected="true"] {
    border-color: #6d9b78;
    background: #224732;
    color: var(--mint);
  }

  #dataset-summary {
    display: none;
  }

  .tools.has-period-selectors {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 8px;
    margin: 12px 0;
  }

  .tools.has-period-selectors .context-fields {
    display: grid;
    grid-template-columns: minmax(0, 1.45fr) repeat(2, minmax(0, 1fr));
    gap: 6px;
    width: 100%;
    grid-column: 1 / -1;
  }

  .tools .context-fields label, .tools .context-fields label:first-child {
    min-width: 0;
    width: auto;
  }

  .tools .context-fields .custom-select {
    height: 100%;
  }

  .tools .context-fields .custom-select-trigger {
    height: 100%;
    padding: 8px 30px 8px 10px;
    font-size: 13px;
  }

  .tools .context-fields .custom-select-value {
    white-space: normal;
    line-height: 1.2;
    overflow-wrap: anywhere;
  }

  .tools .search-wrap, .tools.has-period-selectors .search-wrap {
    width: 100%;
    min-width: 0;
    min-height: 44px;
    height: auto;
  }

  #search {
    width: 100%;
    min-width: 0;
    margin: 0;
    font-size: 16px;
  }

  .tools .sort-wrap, .tools.has-period-selectors .sort-wrap {
    width: min(100%, 220px);
    min-height: 44px;
    height: auto;
    justify-self: end;
    align-self: auto;
  }

  .tools:not(.has-period-selectors) {
    display: flex;
    flex-direction: column;
    align-items: stretch;
  }

  .tools:not(.has-period-selectors) .sort-wrap {
    align-self: flex-end;
  }

  .explore-toolbar {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
  }

  .explore-toolbar .filter-container {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    width: 100%;
    margin: 0;
  }

  .filter-container button {
    padding: 8px 10px;
    font-size: 12px;
  }

  #view-schedules {
    align-self: flex-end;
  }

  .filter-help {
    margin: 8px 0 12px;
    font-size: 12px;
  }

  #section-grid, #section-grid.is-schedule-view {
    grid-template-columns: minmax(0, 1fr);
    gap: 12px;
  }

  .section-card, .section-schedule-preview {
    padding: 12px;
  }

  .section-card:hover {
    transform: none;
  }

  .preview-heading {
    grid-template-columns: minmax(0, 1fr) 44px;
    gap: 10px;
  }

  .preview-heading > button {
    grid-column: 2;
    grid-row: 1;
  }

  .preview-heading h2 {
    font-size: 20px;
  }

  .preview-summary {
    grid-column: 1 / -1;
    grid-row: 2;
    gap: 6px;
    width: 100%;
    max-width: 348px;
    padding-top: 8px;
  }

  .preview-summary > div {
    min-height: 58px;
    padding: 8px 4px;
  }

  .preview-summary dt {
    font-size: 11px;
  }

  .preview-summary dd {
    font-size: clamp(14px, 4vw, 16px);
  }

  .card-header > div:first-child {
    min-width: 0;
  }

  .card-header h2 {
    font-size: 23px;
    overflow-wrap: anywhere;
  }

  .course-row {
    display: grid;
    grid-template-columns: 8px minmax(0, 1fr) auto;
    gap: 3px 8px;
    padding-block: 8px;
  }

  .course-row > .swatch {
    grid-column: 1;
    grid-row: 1;
    width: 8px;
    height: 8px;
  }

  .course-row .course-meta {
    grid-column: 2 / -1;
    grid-row: 2;
    white-space: normal;
    overflow-wrap: anywhere;
    overflow: visible;
  }

  .course-row strong {
    grid-column: 2;
    grid-row: 1;
    max-width: none;
    min-width: 0;
    white-space: normal;
    overflow-wrap: anywhere;
  }

  .course-row .course-days {
    grid-column: 3;
    grid-row: 1;
    max-width: 90px;
    white-space: normal;
  }

  .week-scroll {
    max-width: 100%;
    overscroll-behavior-x: contain;
    scroll-padding-left: 58px;
  }

  .week-grid > .time-column {
    position: sticky;
    left: 0;
    z-index: 2;
    background: var(--bg);
    box-shadow: 3px 0 7px #0003;
  }

  .preview-week {
    scroll-padding-left: 44px;
  }

  .phone-swipe-hint {
    display: block;
    margin: 8px 0 0;
    color: var(--muted);
    font-size: 11px;
    text-align: center;
  }

  .table-heading {
    flex-wrap: wrap;
    gap: 10px;
  }

  .table-heading > div, .table-heading .actions {
    min-width: 0;
  }

  .table-heading h2 {
    font-size: 21px;
  }

  .table-heading .actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-left: auto;
  }

  .group-heading {
    gap: 6px;
    padding: 6px 8px;
  }

  .group-heading h3 .group-title {
    font-size: 14px;
  }

  .group-heading h3 .group-meta {
    display: block;
    margin: 3px 0 0;
  }

  .table-scroll {
    width: 100%;
    max-width: 100%;
    overflow-x: auto;
    overscroll-behavior-x: contain;
  }

  .offering-group, .curriculum-group {
    min-width: 0;
    max-width: 100%;
  }

  .offering-group table {
    min-width: 700px;
  }

  .curriculum-group table {
    width: 100%;
    min-width: 0;
    table-layout: fixed;
  }

  .curriculum-group th:first-child, .curriculum-group td:first-child {
    width: 30%;
    min-width: 0;
  }

  .curriculum-group th:last-child, .curriculum-group td:last-child {
    width: 58px;
    min-width: 0;
    padding-inline: 6px;
  }

  .curriculum-group .course-title-cell {
    min-width: 0;
    white-space: normal;
    overflow-wrap: anywhere;
  }

  .curriculum-group .table-course, .curriculum-group .table-course strong {
    min-width: 0;
    overflow-wrap: anywhere;
  }

  .curriculum-group th, .curriculum-group td {
    padding: 8px;
    font-size: 12px;
  }

  .curriculum-toolbar {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
  }

  .curriculum-toolbar label {
    min-width: 0;
  }

  .curriculum-toolbar label:first-child {
    grid-column: 1 / -1;
  }

  .list-pagination {
    flex-wrap: wrap;
    gap: 8px;
  }

  .list-pagination > span {
    order: 3;
    width: 100%;
    text-align: center;
  }

  body:has(dialog[open]) {
    overflow: hidden;
  }

  dialog[open] {
    position: fixed;
    inset: auto 8px;
    top: calc(var(--phone-viewport-top, 0px) + 8px);
    width: calc(100% - 16px);
    max-width: none;
    max-height: calc(var(--phone-viewport-height, 100dvh) - 16px);
    margin: 0;
    border-radius: 10px;
  }

  dialog::backdrop {
    background: #000a;
  }

  .dialog-heading, .schedule-dialog > .dialog-heading {
    padding: 12px;
    gap: 8px;
    align-items: center;
  }

  .dialog-heading > div {
    min-width: 0;
  }

  .dialog-heading h2 {
    font-size: 19px;
    line-height: 1.3;
    overflow-wrap: anywhere;
  }

  .dialog-heading p {
    font-size: 12px;
  }

  .dialog-content, .schedule-dialog .dialog-content {
    padding: 12px;
    min-height: 0;
    overscroll-behavior-y: contain;
    scroll-padding-block: 16px 112px;
  }

  dialog > form {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior-y: contain;
    scroll-padding-block: 16px 112px;
  }

  #course-form, #section-form {
    padding: 12px 12px 0;
  }

  .dialog-footer {
    position: sticky;
    bottom: 0;
    z-index: 3;
    padding: 10px 12px max(10px, env(safe-area-inset-bottom));
    gap: 8px;
    flex-wrap: wrap;
    background: var(--bg);
  }

  #course-form .dialog-footer, #section-form > .dialog-footer {
    margin-inline: -12px;
  }

  .dialog-footer .button {
    flex: 1 1 110px;
    padding-inline: 10px;
    white-space: normal;
  }

  dialog input, dialog textarea, dialog select {
    scroll-margin-block: 16px 112px;
  }

  .form-grid {
    grid-template-columns: minmax(0, 1fr);
    gap: 10px;
  }

  .form-grid .span-2 {
    grid-column: auto;
  }

  .section-context, .period-create, .curriculum-context {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
  }

  .section-context > label:first-child, .curriculum-context > label:first-child, .period-create > label:first-child, .period-create > label:last-of-type, .period-create > .button {
    grid-column: 1 / -1;
  }

  .curriculum-context .curriculum-name-label {
    min-width: 0;
  }

  .section-curriculum-tools {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 8px;
  }

  .section-curriculum-tools label {
    min-width: 0;
  }

  .section-course {
    padding: 10px;
    min-width: 0;
  }

  .section-code-label input {
    max-width: none;
  }

  .manual-course-fields {
    grid-template-columns: minmax(0, 1fr) 76px 44px;
    gap: 8px;
  }

  .manual-course-fields label:nth-child(2) {
    grid-column: 1 / -1;
    grid-row: 2;
  }

  .section-course .remove-course {
    width: 44px;
    min-width: 44px;
    height: 44px;
  }

  .meeting-row, .section-course .meeting-row {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) 44px;
    gap: 8px;
  }

  .meeting-row label:nth-child(1) {
    grid-column: 1 / 3;
    grid-row: 1;
  }

  .meeting-row label:nth-child(2) {
    grid-column: 1;
    grid-row: 2;
  }

  .meeting-row label:nth-child(3) {
    grid-column: 2;
    grid-row: 2;
  }

  .meeting-row label:nth-child(4) {
    grid-column: 1 / 3;
    grid-row: 3;
  }

  .meeting-row .remove-meeting {
    grid-column: 3;
    grid-row: 3;
    width: 44px;
    min-width: 44px;
    margin: 0;
  }

  .curriculum-row {
    grid-template-columns: minmax(0, 1fr) 76px 44px;
    gap: 8px;
  }

  .curriculum-choice {
    min-height: 44px;
  }

  .meeting-heading {
    gap: 8px;
    flex-wrap: wrap;
  }

  .period-row {
    gap: 8px;
    flex-wrap: wrap;
  }

  .period-row > div:first-child {
    flex-basis: 60%;
    overflow-wrap: anywhere;
  }

  .period-row .period-actions {
    margin-left: auto;
  }

  .schedule-title-row {
    gap: 8px;
    align-items: flex-start;
  }

  .schedule-title-row h2 {
    font-size: 23px;
    overflow-wrap: anywhere;
  }

  .schedule-stats {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
  }

  .schedule-meeting {
    grid-template-columns: minmax(0, 1fr);
    gap: 4px;
  }

  .schedule-meeting > div {
    min-width: 0;
    overflow-wrap: anywhere;
  }

  .data-transfer-controls {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 8px;
  }

  .data-transfer-controls > label:first-child, .data-transfer-controls > label:nth-child(2), .data-transfer-controls > .actions {
    width: 100%;
  }

  .data-transfer-controls .actions, .template-downloads .actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
  }

  .data-transfer-controls .actions > .button {
    flex: 1;
  }

  .template-downloads {
    grid-template-columns: minmax(0, 1fr);
    gap: 16px;
  }

  .backup-period-target {
    padding: 10px;
  }

  .backup-period-target .context-fields {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
  }

  .backup-period-target .context-fields label:first-child {
    grid-column: 1 / -1;
  }

  .backup-period-target .context-fields label, .backup-period-target .context-fields label:first-child {
    min-width: 0;
  }

  .file-zone {
    padding: 12px;
    overflow-wrap: anywhere;
  }

  .import-format-fields {
    grid-template-columns: minmax(0, 1fr);
  }

  .import-options {
    flex-wrap: wrap;
  }

  .file-zone input {
    max-width: 100%;
  }

  #toast {
    max-width: calc(100% - 24px);
    bottom: max(12px, env(safe-area-inset-bottom));
    flex-wrap: wrap;
  }

  main > footer {
    flex-wrap: wrap;
    gap: 6px;
    font-size: 11px;
  }


  /* Four compact phone rows. The original controls are moved back on desktop. */
  .phone-actions-row {
    margin: 8px 0 6px;
  }

  .phone-actions-row .actions {
    display: grid;
    grid-template-columns: 88px minmax(0, 1fr);
    gap: 6px;
    width: 100%;
    margin: 0;
  }

  .phone-actions-row .button {
    min-width: 0;
    min-height: 38px;
    padding: 4px 10px;
    font-size: 12px;
    white-space: nowrap;
  }

  .phone-actions-row #manage-terms {
    color: var(--muted);
    font-weight: 500;
  }

  .phone-actions-row #add-section {
    font-size: 13px;
    font-weight: 650;
  }

  .tools, .tools.has-period-selectors, .tools:not(.has-period-selectors) {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    align-items: stretch;
    gap: 6px;
    margin: 0 0 12px;
  }

  .phone-preference-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 44px;
    align-items: center;
    gap: 6px;
    min-width: 0;
  }

  .phone-preference-row .filter-container {
    display: flex;
    flex-wrap: nowrap;
    align-items: center;
    min-width: 0;
    width: 100%;
    min-height: 44px;
    gap: 6px;
    margin: 0;
    overflow-x: auto;
    scrollbar-width: none;
    -ms-overflow-style: none;
  }

  .phone-preference-row .filter-container::-webkit-scrollbar {
    display: none;
    width: 0;
    height: 0;
  }

  .phone-preference-row .filter-container button {
    flex: 0 0 auto;
    min-height: 36px;
    padding: 6px 10px;
    font-size: 12px;
    border-radius: 8px;
    white-space: nowrap;
  }

  .phone-preference-row.has-filter-overflow .filter-container {
    -webkit-mask-image: linear-gradient(to right, #000 calc(100% - 18px), transparent);
    mask-image: linear-gradient(to right, #000 calc(100% - 18px), transparent);
  }

  .tools .phone-preference-row .sort-wrap,
  .phone-section-actions .sort-wrap {
    grid-column: 2;
    grid-row: 1;
    justify-self: end;
    width: 44px;
    min-width: 44px;
    height: 44px;
    min-height: 44px;
    z-index: 1;
    background: var(--bg);
  }

  .phone-preference-row .sort-wrap .custom-select-trigger,
  .phone-section-actions .sort-wrap .custom-select-trigger {
    display: flex;
    justify-content: center;
    width: 44px;
    min-width: 44px;
    height: 44px;
    padding: 0;
  }

  .phone-preference-row .sort-wrap .custom-select-leading-icon,
  .phone-section-actions .sort-wrap .custom-select-leading-icon {
    position: static;
    transform: none;
  }

  .phone-preference-row .sort-wrap .custom-select-value,
  .phone-preference-row .sort-wrap .custom-select-arrow,
  .phone-section-actions .sort-wrap .custom-select-value,
  .phone-section-actions .sort-wrap .custom-select-arrow {
    display: none;
  }

  .phone-period-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 88px;
    align-items: stretch;
    gap: 6px;
    min-width: 0;
  }

  .tools .phone-period-row .context-fields {
    display: grid;
    grid-column: 1;
    grid-row: 1;
    grid-template-columns: minmax(0, 1.4fr) repeat(2, minmax(0, 0.9fr));
    align-items: stretch;
    gap: 4px;
    width: 100%;
    min-width: 0;
  }

  .tools .phone-period-row .context-fields label,
  .tools .phone-period-row .context-fields label:first-child {
    width: auto;
    min-width: 0;
  }

  .phone-period-row #period-selectors .custom-select-trigger {
    min-height: 36px;
    height: 36px;
    padding: 4px 20px 4px 6px;
    font-size: 12px;
  }

  .phone-period-row .custom-select-arrow {
    right: 5px;
  }

  .phone-period-row .custom-select-arrow svg {
    width: 12px;
    height: 12px;
  }

  .phone-period-row .custom-select-value {
    white-space: nowrap;
    line-height: 1.2;
  }

  .phone-period-row #view-schedules {
    display: flex;
    flex-direction: row;
    grid-column: 2;
    grid-row: 1;
    align-items: center;
    justify-content: center;
    gap: 1px;
    width: 88px;
    min-width: 88px;
    min-height: 36px;
    height: 36px;
    padding: 4px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--surface);
    color: var(--text);
    font-size: 11px;
    line-height: 1.2;
    white-space: nowrap;
  }

  .phone-period-row #view-schedules[aria-pressed="true"] {
    border-color: var(--mint);
    background: #224732;
    color: var(--mint);
  }

  .phone-period-row #view-schedules .switch-track {
    display: none;
  }

  .phone-period-row.without-schedule {
    grid-template-columns: minmax(0, 1fr);
  }

  .explore-toolbar, .filter-help {
    display: none;
  }

  .curriculum-toolbar {
    grid-template-columns: minmax(0, 1.4fr) repeat(2, minmax(0, 1fr));
    gap: 6px;
    align-items: stretch;
  }

  .curriculum-toolbar label:first-child {
    grid-column: auto;
  }

  .curriculum-toolbar .custom-select-trigger {
    min-height: 44px;
    padding: 8px 24px 8px 8px;
    font-size: 12px;
  }

  .curriculum-toolbar .custom-select-arrow {
    right: 6px;
  }

  .table-heading .phone-section-actions {
    flex-wrap: nowrap;
    align-items: center;
    gap: 6px;
  }

  .tools > .curriculum-toolbar {
    margin: 0;
  }

}
