/* Narrow-screen refinements. Loaded last so desktop layout remains unchanged. */
.mobile-nav-close {
  display: none;
}

.sidebar-preferences {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 5px 14px;
}

.sidebar-preferences .theme-control {
  flex: 1;
}

.sidebar-preferences .theme-control select {
  width: 100%;
  min-height: 38px;
  border-color: rgb(255 255 255 / 18%) !important;
  background: #303a4c !important;
  color: #fff !important;
  color-scheme: light;
}

.sidebar-preferences .theme-control select option {
  background: #fff !important;
  color: #172033 !important;
}

.sidebar-lang-switch {
  min-width: 48px;
  min-height: 38px;
  border: 1px solid rgb(255 255 255 / 18%);
  border-radius: 9px;
  background: rgb(255 255 255 / 9%);
  color: #dfe5f3;
}

@media (max-width: 820px) {
  :root[data-theme="dark"] .sidebar .nav,
  :root[data-theme="dark"] .sidebar .sidebar-lang-switch,
  :root[data-theme="dark"] .sidebar-preferences .theme-control select,
  :root[data-theme="dark"] .sidebar .mobile-nav-close,
  :root[data-theme="dark"] .mobile-menu {
    border-color: #35445c !important;
    background: #162235 !important;
    color: #dce5f3 !important;
  }

  :root[data-theme="dark"] .sidebar .nav.active,
  :root[data-theme="dark"] .sidebar .nav:hover,
  :root[data-theme="dark"] .sidebar .nav:focus-visible {
    background: #292553 !important;
    color: #fff !important;
  }

  body.menu-open {
    overflow: hidden;
  }

  body.menu-open::before {
    content: "";
    position: fixed;
    z-index: 14;
    inset: 0;
    background: rgb(5 12 24 / 48%);
  }

  .sidebar {
    width: min(278px, calc(100vw - 42px));
    left: min(-292px, calc(42px - 100vw));
    height: 100dvh;
    min-height: 100dvh;
    padding: 22px 16px 16px;
    box-shadow: 16px 0 36px rgb(4 10 22 / 28%);
    overflow-x: hidden;
    overflow-y: scroll;
    overscroll-behavior: contain;
    touch-action: pan-y;
    -webkit-overflow-scrolling: touch;
  }

  .sidebar nav,
  .sidebar .storage,
  .sidebar .profile {
    flex: 0 0 auto;
  }

  .menu-open .sidebar {
    left: 0;
  }

  .sidebar .mobile-nav-close {
    display: grid;
    position: absolute;
    top: 12px;
    right: 12px;
    width: 40px;
    height: 40px;
    padding: 0;
    place-items: center;
    border: 1px solid rgb(255 255 255 / 18%);
    border-radius: 10px;
    background: rgb(255 255 255 / 9%);
    color: #fff;
    font-size: 25px;
    line-height: 1;
  }

  .sidebar .brand {
    padding-right: 48px;
  }

  main {
    padding: 0 14px 30px;
  }

  header {
    min-height: 78px;
    height: auto;
    margin-bottom: 18px;
    padding: 12px 0;
    gap: 8px;
    flex-wrap: wrap;
  }

  header > div:not(.header-actions) {
    min-width: 0;
  }

  header h1 {
    overflow: hidden;
    font-size: 19px;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .panel-head h2,
  .student-invoice-panel h2,
  .dashboard-panel h2 {
    font-size: 18px;
  }

  .mobile-menu {
    display: grid;
    width: 42px;
    min-width: 42px;
    height: 42px;
    margin-right: 0;
    padding: 0;
    place-items: center;
    color: var(--ink);
  }

  .header-actions {
    display: flex;
    flex-basis: auto;
    width: auto;
    min-width: 0;
    margin-left: auto;
    justify-content: flex-end;
    gap: 6px;
  }

  .header-actions .upload-button {
    display: grid;
    min-width: 124px;
    min-height: 42px;
    padding: 0 10px;
    place-items: center;
    color: #fff !important;
    font-size: 12px;
    white-space: nowrap;
  }

  /* Keep the mobile label visible even if another stylesheet targets its span. */
  .header-actions .upload-button > span {
    display: none !important;
  }

  .header-actions .upload-button::after {
    content: "↑ 上传文件";
    display: block;
    color: #fff;
    font-size: 12px;
    font-weight: 700;
  }

  :root[lang="en"] .header-actions .upload-button::after {
    content: "↑ Upload files";
  }

  .header-actions .upload-button[hidden] {
    display: none !important;
  }

  .panel,
  .table-wrap {
    border-radius: 12px;
  }

  .panel-head {
    flex-wrap: wrap;
    padding: 17px 16px;
  }

  .panel-head > div {
    min-width: 0;
  }

  .panel-head > :last-child:not(:first-child),
  .panel-head > div:last-child:not(:first-child) {
    max-width: 100%;
  }

  .panel-head .secondary,
  .invoice-items-head .secondary,
  #refreshDashboard {
    display: inline-flex;
    min-height: 42px;
    align-items: center;
    justify-content: center;
  }

  .toolbar,
  .search {
    width: 100%;
  }

  .search {
    min-height: 44px;
  }

  .table-wrap {
    overflow-x: auto;
    overscroll-behavior-inline: contain;
    -webkit-overflow-scrolling: touch;
  }

  .table-wrap table {
    min-width: 640px;
  }

  .folder-announcement {
    padding: 15px;
  }

  .announcement-heading {
    align-items: flex-start;
    flex-wrap: wrap;
  }

  .announcement-heading .secondary {
    display: inline-flex;
    min-height: 40px;
    margin-left: 40px;
  }

  .grant-user-filter {
    width: 100%;
    margin: 0;
  }

  .grant-user-filter select {
    min-width: 0;
    flex: 1;
  }

  .student-invoice-panel form,
  .dashboard-content {
    padding: 16px;
  }

  .invoice-items-head {
    align-items: flex-start;
    flex-direction: column;
  }

  .invoice-items-head > div:last-child {
    display: flex;
    width: 100%;
    flex-wrap: wrap;
    gap: 8px;
  }

  .invoice-items-head .secondary {
    flex: 1 1 135px;
  }

  .invoice-total {
    max-width: none;
    min-height: 58px;
  }

  .student-invoice-actions .primary,
  .login-submit {
    min-height: 46px;
  }

  .dashboard-grid,
  .network-status {
    grid-template-columns: 1fr !important;
  }

  .metric-pie {
    gap: 14px;
  }

  dialog {
    width: min(calc(100vw - 24px), 560px);
    max-height: calc(100dvh - 24px);
    padding: 18px;
  }

  .dialog-actions {
    flex-wrap: wrap;
  }

  .dialog-actions button {
    min-height: 42px;
  }
}

@media (max-width: 520px) {
  .login-screen {
    align-items: flex-start;
    overflow-y: auto;
    padding: 18px 12px;
  }

  .login-card {
    width: 100%;
    margin: auto 0;
    padding: 24px 20px;
  }

  .login-preferences {
    gap: 10px;
  }

  .invoice-item {
    grid-template-columns: 1fr 1fr;
    gap: 10px;
  }

  .invoice-item > :first-child {
    grid-column: 1 / -1;
  }

  .invoice-item .remove-item {
    min-height: 42px;
    justify-self: start;
  }

  .people-list > div,
  .grant-list > div {
    padding-right: 16px;
    padding-left: 16px;
  }
}
