/* ===== Telefony i iOS Safari: bezpieczne obszary, brak zoomu na polach, wąskie ekrany (320–430 px), dotyk ===== */
:root { --safe-t: env(safe-area-inset-top, 0px); --safe-l: env(safe-area-inset-left, 0px); --safe-r: env(safe-area-inset-right, 0px); }

/* Bezpieczny obszar u góry (notch / Dynamic Island w trybie PWA ze status barem „black-translucent”). */
.topbar { padding-top: var(--safe-t); height: calc(var(--topbar-h) + var(--safe-t)); padding-left: max(clamp(14px, 3vw, 32px), var(--safe-l)); padding-right: max(clamp(14px, 3vw, 32px), var(--safe-r)); }
.sidebar { padding-top: calc(18px + var(--safe-t)); }
.sp-top { padding-top: calc(10px + var(--safe-t)); }
.auth-panel { padding-top: calc(32px + var(--safe-t)); }
.splash { padding-top: calc(24px + var(--safe-t)); }
.page-cal { height: calc(100dvh - var(--topbar-h) - var(--safe-t)); }
.menu { max-width: calc(100vw - 16px); }
.now-tag { white-space: nowrap; }
.notice { flex-wrap: wrap; }

/* Fallback dla przeglądarek bez dvh (iOS < 15.4). */
@supports not (height: 100dvh) {
  body, .app, .student-root, .sp { min-height: 100vh; min-height: -webkit-fill-available; }
  .sidebar { height: 100vh; }
  .page-cal { height: calc(100vh - var(--topbar-h) - var(--safe-t)); }
  .sheet { max-height: 92vh; }
}

/* Nic nie może wystawać poziomo. */
#main, .view, .page, .sheet-body, .sp-main { min-width: 0; }
#main { overflow-x: clip; }
.card, .sheet, .stat, .hist-body, .row-main, .insight-body, .tl-body { overflow-wrap: anywhere; }
.toast-host { left: 12px; right: 12px; width: auto; max-width: 460px; margin: 0 auto; transform: none; }

/* iOS: tekst w polach daty/godziny wyrównany do lewej jak w innych polach. */
input[type="date"], input[type="time"] { text-align: left; -webkit-appearance: none; appearance: none; }
input::-webkit-date-and-time-value { text-align: left; }
.input, .search-input { -webkit-appearance: none; appearance: none; }
select.input { -webkit-appearance: none; appearance: none; }

/* Dotyk: brak „callout” i minimalne cele 40–44 px. */
.btn, .nav-item, .bn-item, .icon-btn, .chip-btn, .segmented button, .status-btn, .ev, .mcell, .ws-day, .ds-day { -webkit-touch-callout: none; -webkit-user-select: none; user-select: none; }
@media (hover: none) and (pointer: coarse) {
  .btn-sm { height: 36px; }
  .icon-btn { width: 42px; height: 42px; }
  .card-link:hover, .ws-day:hover, .ev:hover { transform: none; }
  .row-btn, .trow-btn, .menu-item { min-height: 44px; }
  .chip-btn, .chip-radio span { padding-top: 8px; padding-bottom: 8px; }
  .hw-check input, .prep-hw input { width: 20px; height: 20px; }
}

/* ---- Telefony (≤ 960 px) ---- */
@media (max-width: 960px) {
  /* iOS Safari przybliża stronę, gdy pole ma < 16 px — dlatego 16 px na wszystkich polach formularzy. */
  .input, .input-sm, .search-input, .pal-input, select, textarea, input:not([type="checkbox"]):not([type="radio"]):not([type="range"]) { font-size: 16px; }
  .input-lg { font-size: 22px; }
  .sheet { max-height: calc(100dvh - var(--safe-t) - 10px); }
  .sheet-body { -webkit-overflow-scrolling: touch; }
  .overlay { overscroll-behavior: contain; }
  .sheet-foot { padding-bottom: calc(12px + var(--safe-b)); }
  .sheet-foot .btn { flex: 1 1 auto; min-width: 0; }
  .sheet-foot .btn-ghost { flex: 0 0 auto; }
  .page-head { gap: 12px; margin-bottom: 16px; }
  .page-actions { width: 100%; }
  .page-actions .btn { flex: 1 1 auto; }
  .page-actions .btn-icon-only { flex: 0 0 auto; }
  .toolbar .search { flex: 1 1 100%; max-width: none; min-width: 0; }
  .toolbar .select-inline { margin-left: 0; }
  .bottom-nav { padding-left: max(6px, var(--safe-l)); padding-right: max(6px, var(--safe-r)); }
  .topbar [data-action="toggle-theme"] { display: none; } /* przełącznik motywu jest w menu konta i w Ustawieniach */
  .topbar { gap: 6px; }
  .sync-chip:not([data-state="error"]):not([data-state="offline"]) { display: none; }
  .cal-head, .cal-body { grid-template-columns: 44px repeat(var(--cols), minmax(0, 1fr)); }
  .cal-body { grid-template-columns: 44px minmax(0, 1fr); }
  .cal-time span { right: 5px; font-size: 10px; }
  .cdh-name { font-size: 10px; letter-spacing: .04em; } .cdh-num { font-size: 16px; width: 30px; height: 30px; }
  .cdh-meta span { font-size: 9.5px; padding: 1px 4px; }
  .ev { padding: 3px 4px 2px 6px; border-left-width: 3px; }
  .ev-name { font-size: 11.5px; } .ev-time { font-size: 9.5px; }
  .cal-legend { display: none; }
  .page-cal { height: calc(100dvh - var(--topbar-h) - 76px - var(--safe-b) - var(--safe-t)); }
  .hero-next { padding: 18px; } .hero-title { font-size: 21px; }
  .limit-card { padding: 18px; gap: 18px; }
  .sp-hero { padding: 18px; }
}

/* ---- Wąskie telefony (iPhone SE / mini: 320–400 px) ---- */
@media (max-width: 420px) {
  body { font-size: 14px; }
  .page { padding: 14px 12px calc(14px + var(--safe-b)); }
  .card { padding: 14px; border-radius: 14px; }
  .page-title { font-size: 22px; } .page-sub { font-size: 13px; }
  .stat-grid { gap: 8px; } .stat { padding: 12px 12px; } .stat-value { font-size: 22px; } .stat-label { font-size: 12px; } .stat-sub { font-size: 12px; }
  .stat-ico { width: 26px; height: 26px; border-radius: 8px; }
  .section-title h2 { font-size: 15px; }
  .sheet-head { padding: 12px 14px 4px; } .sheet-body { padding: 12px 14px 8px; } .sheet-foot { padding-left: 14px; padding-right: 14px; }
  .sheet-title { font-size: 17px; }
  .sheet-body.flush .lesson-section { padding: 0 14px; } .lesson-hero { padding: 14px; }
  .form-row, .form-row-3 { grid-template-columns: 1fr; }
  .form-row-3 .field:first-child { grid-column: auto; }
  .form-row.when-weekday { grid-template-columns: 1fr; }
  .trow { grid-template-columns: minmax(0, 1fr) auto; gap: 8px; padding: 10px 4px; }
  .table-entries .trow { grid-template-columns: 64px minmax(0, 1fr) auto; }
  .hist-item { grid-template-columns: 52px minmax(0, 1fr); gap: 10px; }
  .hist-side { grid-column: 2; flex-direction: row; align-items: center; justify-content: flex-start; flex-wrap: wrap; gap: 8px; }
  .week-strip { gap: 4px; } .ws-day { padding: 8px 2px 6px; border-radius: 10px; } .ws-date { font-size: 14px; } .ws-letter { font-size: 10px; }
  .ds-day { min-height: 52px; font-size: 10px; } .ds-day b { font-size: 14px; }
  .sc-meta { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; padding: 8px 10px; } .sc-meta div { font-size: 12.5px; }
  .student-grid { grid-template-columns: 1fr; gap: 10px; }
  .ph-name { font-size: 22px; } .profile-hero { padding: 16px; } .ph-main { gap: 12px; } .avatar-xl { width: 52px; height: 52px; font-size: 18px; border-radius: 16px; }
  .ph-actions { width: 100%; } .ph-actions .btn { flex: 1 1 auto; }
  .hero-main { gap: 12px; } .hero-title { font-size: 19px; }
  .hero-actions .btn { flex: 1 1 auto; }
  .tl-time { width: 42px; }
  .limit-grid { grid-template-columns: 1fr 1fr; gap: 10px; } .limit-grid b { font-size: 15px; }
  .segmented { flex-wrap: wrap; } .segmented button { padding: 6px 10px; font-size: 12.5px; }
  .setting-row { gap: 10px; } .setting-row > div:first-child { min-width: 0; flex: 1 1 100%; }
  .auth-panel { padding-left: 16px; padding-right: 16px; } .auth-card h1 { font-size: 24px; }
  .bn-item { font-size: 10.5px; height: 52px; }
  .btn-row .btn { flex: 1 1 auto; }
  .status-grid { grid-template-columns: 1fr 1fr; }
  .price b { font-size: 38px; }
  .plan-status { padding: 16px; } .plan-status h2 { font-size: 18px; }
  .pt-code { font-size: 26px; letter-spacing: .08em; }
  .sp-claim { padding: 22px 14px; }
  .month-body { grid-template-rows: repeat(var(--rows), minmax(60px, 1fr)); }
  .mcell-num { width: 24px; height: 24px; font-size: 12px; }
  .cal-title { font-size: 17px; }
  .cal-toolbar { gap: 8px; } .cal-tools { flex-wrap: wrap; }
  .chart-foot { gap: 6px 16px; }
  .report-form { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 340px) {
  .stat-grid { grid-template-columns: 1fr; }
  .bn-item span { font-size: 10px; }
  .tb-title { font-size: 15px; }
  .topbar { gap: 4px; }
  .sync-chip { display: none; }
}

/* Telefon poziomo: niższa nawigacja i okna na całą wysokość. */
@media (max-width: 960px) and (max-height: 520px) {
  .bn-item { height: 44px; gap: 1px; } .bn-item span { display: none; }
  #main { padding-bottom: calc(58px + var(--safe-b)); }
  .page-cal { height: calc(100dvh - var(--topbar-h) - 58px - var(--safe-b) - var(--safe-t)); }
  .sheet { max-height: 100dvh; border-radius: 0; }
}
