@media (max-width: 850px) {
  /* Phone surfaces are a touch greyer than the desktop's lit cream (the light theme only) */
  :root:not([data-theme="dark"]) {
    --surface: #F3EFE4;
    --surface-solid: #F3EFE4;
  }

  .app {
    grid-template-columns: 1fr;
    grid-template-rows: var(--m-header-h) 1fr;
  }

  /* Flat lighting on the phone: no light band on cards or blocks, no sidebar bevel strip */
  .card, .event-block, .list-item, .todo-item, .sidebar { background-image: none; }
  .sidebar::after { display: none; }

  .sidebar {
    display: none;
    position: fixed;
    inset: 0 auto 0 0;
    width: 220px;
    z-index: 25;
  }
  .sidebar.is-open { display: flex; }
  .topbar { display: none; }

  /* Header: sits directly on the page background, no bar */
  .mobile-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: var(--m-header-h);
    padding: 0 14px 0 12px;
    background: transparent;
    border: 0;
  }
  .mobile-header .logo { margin: 0; height: auto; padding: 0; gap: 7px; }
  .mobile-header .logo .icon-logo { width: 34px; height: 34px; }
  .mobile-header .logo-text { font-size: 22px; }
  .mobile-header .topbar-right { gap: 10px; }
  .user-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    border: 0;
    background: none;
    padding: 0;
    min-height: 44px;
    color: var(--text-2);
  }
  .user-circle {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: var(--avatar-bg);
    color: var(--text);
  }
  .user-circle .icon { width: 17px; height: 17px; stroke-width: 1.8; }
  .mobile-header .logout-btn { height: 30px; padding: 0 14px; font-size: 13px; border-radius: 7px; }
  .menu { top: calc(var(--m-header-h) + 2px); right: 12px; }
  .menu-actions.m-only { display: flex; }
  .only-m { display: inline; }
  .only-d { display: none; }

  /* Content: full-bleed schedule card, inset Upcoming cards */
  .content {
    display: block;
    padding: 0 4px calc(var(--nav-h) + 96px + env(safe-area-inset-bottom));
  }
  .schedule-card {
    display: block;
    min-height: 0;
    border-radius: 14px;
    padding: 14px 12px 3px;                    /* the last class card sits flush against the bottom edge */
  }
  /* One phone type scale for every card heading (Course Schedule, Today, Deadlines, My Tasks, Quiz) */
  .card-head { margin-bottom: 16px; }
  .card-head h2 { font-size: 23px; line-height: 28px; font-weight: 600; letter-spacing: 0; }
  /* Week arrows (Schedule) and day arrows (Clock): real touch controls. The day strip only
     covers one week, so the arrows are how you reach the next one. */
  .cal-card .sub-row, .tb-card .sub-row { flex-wrap: wrap; gap: 6px; margin-top: 6px; }
  .cal-card .sub-row .sub, .tb-card .sub-row .sub { font-size: 12.5px; }
  .week-nav {
    width: 32px;
    height: 32px;
    border-radius: 8px;
    color: var(--control-text);
  }
  .week-nav .icon { width: 13px; height: 13px; }
  .sub-row .today-chip { min-height: 32px; }
  .card-head .add-btn { display: none; }       /* the floating button replaces every in-card "+ Add" */
  .cal-week-wrap { display: none; }
  .week-wrap { flex: none; min-height: 0; overflow-x: auto; }
  .week-grid.timeblock-grid { display: grid; grid-template-columns: 84px minmax(0, 1fr); padding-bottom: 8px; }
  .timeblock-grid .hour-label { margin-left: 0; }

  /* Day tabs: bordered strip, regular weight, hairline separators, 4px underline */
  .day-tabs {
    display: flex;
    align-items: stretch;
    gap: 0;
    height: 52px;
    margin: 0 0 16px;
    padding: 0 4px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: transparent;
    overflow-x: auto;
    overflow-y: hidden;
    scrollbar-width: none;
  }
  .day-tabs::-webkit-scrollbar { display: none; }
  .day-tab {
    position: relative;
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    min-height: 0;
    padding: 0 16px;
    border: 0;
    background: none;
    border-radius: 0;
    font-size: 18px;
    font-weight: 400;
    color: var(--text-2);
    white-space: nowrap;
  }
  .day-tab + .day-tab::before {
    content: "";
    position: absolute;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
    width: 1px;
    height: 22px;
    background: var(--tab-rule);
  }
  .day-tab.is-active { color: var(--brand); box-shadow: none; }
  .day-tab.is-active::after {
    content: "";
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    height: 4px;
    border-radius: 2px 2px 0 0;
    background: var(--brand);
  }

  .mobile-list { display: block; }

  /* Upcoming cards */
  .rail { margin: 16px 14px 0; gap: 12px; }
  .rail .card { padding: 12px 14px 12px; border-radius: 12px; background-color: var(--rail-card); }
  .rail h2 { font-size: 20px; line-height: 26px; margin: 0 0 8px; }
  .upcoming-card .empty-state {
    flex-direction: row;
    align-items: center;
    gap: 14px;
    padding: 2px 6px 0;
    text-align: center;
  }
  .upcoming-card .empty-state .art { width: 70px; height: 70px; margin: 0; flex: 0 0 70px; }
  .upcoming-card .empty-state strong { flex: 1; margin: 0; font-size: 17px; line-height: 1.3; text-wrap: pretty; }  /* no orphaned last word */
  .upcoming-card .empty-state .hint { display: none; }

  /* Other views' cards share the schedule card's shape and side padding */
  .content > .card { border-radius: 14px; }
  .content > .card:not(.schedule-card) { padding: 14px 12px 12px; }
  .content > .card > .empty-state { padding: 18px 0 12px; }

  /* Task and deadline rows: tags move out of the side column, which stacks status over the check */
  .list-scroll { overflow: visible; margin: 0; padding: 0; }
  .list-toolbar { flex-direction: column; align-items: stretch; }
  .progress-bar { flex: 1; width: auto; }
  .todo-item { gap: 10px; padding: 9px 10px; }
  .row-side .tags { display: none; }
  .deadline-row .row-side { flex-direction: column; align-items: flex-end; gap: 6px; }
  .row-title strong { font-size: 14px; }

  /* Quiz cards take the phone class-row anatomy (thick red bar, 10px corners, large red title) */
  .cd-card { border-left-width: 5px; border-radius: 10px; padding: 12px 14px; }
  .cd-card strong { font-size: 17px; line-height: 22px; }
  .cd-card .sub { font-size: 12.5px; }
  .cd-card .days { font-size: 22px; }

  /* Floating "+ Add …" (label and action follow the current view) */
  .fab {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    position: fixed;
    right: 14px;
    bottom: calc(var(--nav-h) + 18px + env(safe-area-inset-bottom));
    z-index: 16;
    height: 44px;
    padding: 0 18px 0 16px;
    background: var(--fab);
    color: var(--control-text);
    border: 0;
    border-radius: 999px;
    font-size: 16px;
    font-weight: 400;
    box-shadow: var(--shadow-float);
  }
  .fab .icon { width: 18px; height: 18px; stroke-width: 1.8; }

  /* Bottom nav: five equal cells; the active cell carries a light tan pill that spans the bar's height */
  .bottom-nav {
    display: flex;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 15;
    height: calc(var(--nav-h) + env(safe-area-inset-bottom));
    padding: 0 4px env(safe-area-inset-bottom);
    background: var(--surface-solid);
    border: 0;
    box-shadow: var(--shadow-nav);
    justify-content: stretch;
  }
  .bottom-nav button {
    flex: 1 1 0;
    min-width: 0;
    min-height: 0;
    height: var(--nav-h);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    padding: 6px 0 4px;
    border: 0;
    border-radius: 8px 8px 0 0;
    background: none;
    color: var(--text-2);
    font-size: 13px;
    line-height: 16px;
    white-space: nowrap;
  }
  .bottom-nav button .icon { width: 23px; height: 23px; stroke-width: 1.5; }
  .bottom-nav button.is-active { background: var(--active-tab); color: var(--brand); }
}

@media (pointer: coarse) {
  .nav-btn, .chip {
    min-height: 44px;
  }
  .icon-btn { width: 40px; height: 40px; }
  /* Bigger round check for fingers; width and height must move together or the circle turns into an oval */
  .check { width: 26px; height: 26px; min-height: 0; flex: 0 0 26px; }
  .check:checked { background-size: 17px; }
}

@media (max-width: 600px) {
  .row-2 { grid-template-columns: 1fr; }
}

@media (max-width: 850px) {
  /* Search: header button + sheet */
  .round-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: var(--avatar-bg);
    color: var(--text);
  }
  .round-btn .icon { width: 18px; height: 18px; }
  .user-btn { padding: 4px; min-height: 44px; }
  .user-circle { width: 34px; height: 34px; }
  .user-circle .icon { width: 19px; height: 19px; }

  .search-sheet {
    display: block;
    position: fixed;
    inset: 0 0 auto 0;
    z-index: 35;
    padding: 10px 12px 12px;
    background: var(--surface-solid);
    box-shadow: var(--shadow-float);
  }
  .search-sheet[hidden] { display: none; }
  .search-m { width: 100%; height: 42px; }
  .search-m input { font-size: 16px; }             /* 16px keeps iOS from zooming the page */
  .sheet-close { border: 0; background: none; color: var(--brand); font-size: 14px; font-weight: 600; padding: 0 4px; }
  .search-sheet .search-results {
    position: static;
    width: 100%;
    max-height: calc(100dvh - 90px);
    margin-top: 8px;
    padding: 0;
    box-shadow: none;
  }
  .hit-type { flex-basis: 64px; }
}

@media (max-width: 1100px) and (min-width: 851px) {
  .search { width: 200px; }
  .search-kbd { display: none; }
}
