/*
 * Project-wide phone fixes.
 * Loaded on every page from templates/head.php, after vendor CSS.
 * Every rule is inside a max-width media query, so the desktop layout is unchanged.
 * Opt-in classes used in markup: .sticky-col (pinned name column), .mobile-list-box (fixed-height lists),
 * .mobile-input-line (borderless inputs), .ln-actions (lesson note toolbar).
 */

/* Surface colour for elements outside a .card (the theme only defines --bs-card-bg inside .card).
   Light #fff / dark #30334e match the theme's card and modal backgrounds. */
:root {
  --mobile-surface: #fff;
  --mobile-shadow: rgba(46, 38, 61, 0.18);
}

.dark-style,
[data-bs-theme="dark"] {
  --mobile-surface: #30334e;
  --mobile-shadow: rgba(0, 0, 0, 0.45);
}

/* ------------------------------------------------------------------ */
/* Phones (< 768px)                                                    */
/* ------------------------------------------------------------------ */
@media (max-width: 767.98px) {

  /* Never let the page itself scroll sideways; wide content scrolls in its own box.
     `clip` (not `hidden`) so position:sticky headers keep working. */
  html,
  body {
    overflow-x: clip;
  }

  /* ---------- Tables ---------- */
  .table-responsive,
  div.dt-container div.dt-layout-table,
  div.dt-container div.dt-layout-table > div {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* `table-responsive` put on the <table> itself (used in several pages) only scrolls if the table is a block. */
  table.table-responsive {
    display: block;
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .table {
    font-size: 0.8125rem;
  }

  .table > :not(caption) > * > * {
    padding-left: 0.5rem;
    padding-right: 0.5rem;
  }

  /* Sticky first/name column for wide entry grids. Add .sticky-col to the <th> and <td>. */
  .sticky-col {
    position: sticky !important;
    left: 0;
    z-index: 2;
    background-color: var(--mobile-surface) !important;
    box-shadow: 2px 0 3px -2px rgba(0, 0, 0, 0.25);
  }

  thead .sticky-col {
    z-index: 3;
  }

  /* DataTables controls: stack and fill the width. */
  div.dt-container div.dt-search input {
    width: 100% !important;
    margin-left: 0 !important;
  }

  div.dt-container div.dt-search {
    width: 100%;
  }

  div.dt-container .dt-paging .pagination {
    flex-wrap: wrap;
    justify-content: center;
  }

  /* ---------- Card headers & toolbars ---------- */
  .card-header.d-flex,
  .card-header .d-flex,
  .btn-toolbar {
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  .card-header .btn-group {
    flex-wrap: wrap;
  }

  /* Header filters with inline widths (e.g. width:20%, min-width:260px) take the full row. */
  .card-header .form-select,
  .card-header .form-control,
  .card-header .select2-container {
    width: 100% !important;
    min-width: 0 !important;
    max-width: 100% !important;
  }

  .select2-container {
    max-width: 100%;
  }

  .modal-footer {
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  /* ---------- Fixed-height list boxes (student/staff/class lists): shorter on phones ---------- */
  .mobile-list-box {
    height: auto !important;
    max-height: 60vh !important;
  }

  /* ---------- Modals: large ones go fullscreen ---------- */
  .modal-dialog.modal-lg,
  .modal-dialog.modal-xl,
  .modal-dialog.modal-fullscreen-md-down {
    width: 100vw;
    max-width: none !important;
    height: 100%;
    margin: 0 !important;
  }

  .modal-dialog.modal-lg .modal-content,
  .modal-dialog.modal-xl .modal-content {
    min-height: 100%;
    border: 0;
    border-radius: 0;
  }

  .modal-dialog.modal-lg .modal-body,
  .modal-dialog.modal-xl .modal-body {
    overflow-y: auto;
  }

  .modal-body {
    padding-left: 1rem;
    padding-right: 1rem;
  }

  /* ---------- Tap targets ---------- */
  .form-check-input {
    width: 1.25rem;
    height: 1.25rem;
  }

  .btn-icon.btn-sm,
  .btn-sm.btn-icon {
    min-width: 2.25rem;
    min-height: 2.25rem;
  }

  td > a > i[class^="ri-"],
  td > a > i[class*=" ri-"] {
    display: inline-block;
    padding: 0.35rem;
  }

  /* ---------- Tabs: scroll sideways instead of stacking tall ---------- */
  .nav-tabs,
  .nav-pills:not(.flex-column) {
    flex-wrap: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
    white-space: nowrap;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
  }

  .nav-tabs .nav-item,
  .nav-pills:not(.flex-column) .nav-item {
    flex-shrink: 0;
  }

  /* ---------- Charts ---------- */
  .apexcharts-canvas,
  .apexcharts-canvas svg,
  canvas {
    max-width: 100% !important;
  }

  /* ---------- Layout chrome ---------- */
  /* Theme customizer button is a demo leftover and covers content on phones. */
  .template-customizer-open-btn {
    display: none !important;
  }

  /* Long branch names must not push the user menu off screen. */
  #layout-navbar > .btn-group > .btn {
    max-width: 45vw;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    display: inline-block !important;
  }

  .container-xxl.container-p-y,
  .container-p-y:not([class^="pt-"]):not([class*=" pt-"]) {
    padding-top: 1rem !important;
  }

  .card-body {
    padding: 1rem;
  }

  h4.fw-bold,
  h4.py-3 {
    font-size: 1.125rem;
  }

  /* Register page: captcha image on its own line, code field full width below it. */
  #captcha_refresh {
    flex-wrap: wrap;
    row-gap: 0.5rem;
  }

  #captcha_refresh > .form-floating {
    flex: 1 1 100%;
  }

  #captcha_refresh > .form-floating > .form-control {
    border-radius: var(--bs-border-radius, 0.375rem) !important;
  }

  /* Lesson note editor: up to 9 action buttons; keep them compact so they fit in 2-3 rows. */
  .ln-actions .btn {
    padding: 0.375rem 0.75rem;
    font-size: 0.8125rem;
    flex: 1 1 auto;
  }

  /* Remark/score inputs that were made borderless need a visible edge to be tappable. */
  .mobile-input-line {
    border-bottom: 1px solid var(--bs-border-color, #d1d0d4) !important;
  }
}

/* ------------------------------------------------------------------ */
/* Bottom navigation bar (templates/mobile-bottom-nav.php), below lg   */
/* ------------------------------------------------------------------ */
@media (max-width: 991.98px) {
  .mobile-bottom-nav {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 1030; /* under modals (1055) and the open sidebar overlay (1099) */
    padding: 0 0.75rem calc(0.5rem + env(safe-area-inset-bottom, 0px));
    pointer-events: none;
  }

  .mobile-bottom-nav .mbn-bar {
    pointer-events: auto;
    display: flex;
    align-items: flex-end;
    justify-content: space-around;
    max-width: 560px;
    margin: 0 auto;
    padding: 0.4rem 0.25rem 0.45rem;
    background: var(--mobile-surface);
    border: 1px solid var(--bs-border-color, #e6e6e8);
    border-radius: 1.25rem;
    box-shadow: 0 6px 24px var(--mobile-shadow);
  }

  .mobile-bottom-nav .mbn-item {
    flex: 1 1 0;
    min-width: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.15rem;
    padding: 0.25rem 0.1rem;
    color: var(--bs-secondary-color, #8a8d93);
    text-decoration: none;
    font-size: 0.6875rem;
    font-weight: 500;
    line-height: 1.1;
    -webkit-tap-highlight-color: transparent;
  }

  .mobile-bottom-nav .mbn-item > i {
    font-size: 1.375rem;
    line-height: 1;
  }

  .mobile-bottom-nav .mbn-label {
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .mobile-bottom-nav .mbn-item.active {
    color: var(--bs-primary);
    font-weight: 600;
  }

  /* Raised centre action */
  .mobile-bottom-nav .mbn-center {
    margin-top: -1.9rem;
  }

  .mobile-bottom-nav .mbn-center-btn {
    width: 3.5rem;
    height: 3.5rem;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bs-primary);
    color: #fff;
    font-size: 1.5rem;
    border: 4px solid var(--mobile-surface);
    box-shadow: 0 4px 14px rgba(var(--bs-primary-rgb, 102, 108, 255), 0.45);
    transition: transform 0.15s ease;
  }

  .mobile-bottom-nav .mbn-center:active .mbn-center-btn {
    transform: scale(0.94);
  }

  .mobile-bottom-nav .mbn-center.active,
  .mobile-bottom-nav .mbn-center {
    color: var(--bs-body-color);
    font-weight: 600;
  }

  /* Keep page content (and the footer) clear of the bar */
  .layout-page:has(.mobile-bottom-nav) .content-wrapper {
    padding-bottom: calc(5.5rem + env(safe-area-inset-bottom, 0px));
  }

  /* Toasts sit above the bar */
  #toast-container.toast-bottom-left,
  #toast-container.toast-bottom-right,
  #toast-container.toast-bottom-center,
  #toast-container.toast-bottom-full-width {
    bottom: calc(6rem + env(safe-area-inset-bottom, 0px));
  }
}
