:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="dark"] { color-scheme: dark; --ink: #f5f5f7; --muted: #b6b6bf; --canvas: #000; --line: #38383c; --bs-body-color: #f5f5f7; --bs-border-color: #38383c; }

/* Desktop surfaces; the mobile theme retains its iOS-specific colors. */
@media (min-width: 851px) {
    [data-theme="dark"] body, [data-theme="dark"] .auth-layout { background: #000; color: #f5f5f7; }
    [data-theme="dark"] :is(.sidebar, .stat-card, .week-panel, .settings-card, .modal-content, .update-banner) { background: #1c1c1e; color: #f5f5f7; border-color: #38383c; }
    [data-theme="dark"] :is(.stat-label, .stat-value, .day-name, .form-label, .auth-story .brand, .auth-form h2, .settings-card h2) { color: #f5f5f7; }
    [data-theme="dark"] :is(.stat-footer, .stat-value small, .day-date, .entry-note, .page-footnote, .form-text, .schedule-row .form-label, .time-table th, .period-title>span, .muted-dash, .divider) { color: #b6b6bf; }
    [data-theme="dark"] :is(.form-control, .form-select, .date-tile, .status-badge, .avatar, .demo-info, .entry-preview) { background-color: #2c2c2e; color: #e5e5ea; border-color: #48484a; }
    [data-theme="dark"] :is(.form-control, .form-select):focus { border-color: #8ba7c8; }
    [data-theme="dark"] :is(.time-table td, .schedule-row) { border-color: #38383c; }
    [data-theme="dark"] .row-rest td { background: #1c1c1e; color: #b6b6bf; }
    [data-theme="dark"] .time-table tfoot td { background: #2c2c2e; color: #f5f5f7; }
    [data-theme="dark"] :is(.nav-item, .icon-btn) { color: #b6b6bf; }
    [data-theme="dark"] :is(.nav-item.active, .nav-item:hover, .today-row .date-tile, .today-chip, .row-action) { background: #24303e; color: #a5c1e0; border-color: #425264; }
    [data-theme="dark"] .balance-card { background: #16293e; }
    [data-theme="dark"] :is(.btn-light, .btn-outline-secondary) { background: #2c2c2e; color: #e5e5ea; border-color: #48484a; }
    [data-theme="dark"] .btn-outline-danger { color: #ff9b92; }
    [data-theme="dark"] :is(.notice, .status-badge.warning) { background: #3e3014; color: #f4d98e; border-color: #69532b; }
    [data-theme="dark"] .delta-positive { color: #83a9d4; }
    [data-theme="dark"] .delta-negative { color: #ffac98; }
    [data-theme="dark"] .swal2-popup { background: #1c1c1e; color: #f5f5f7; }
}

/* Shared identity for installation, launch and in-app branding. */
.brand-icon{display:block;flex:none;width:40px;height:40px;border-radius:10px}
.loading-screen{position:fixed;inset:0;z-index:2000;min-height:100dvh;background:#f2f2f7;gap:0}
.loading-screen .launch-icon{display:block;width:clamp(144px,44vmin,200px);height:clamp(144px,44vmin,200px);border-radius:22.9167%;overflow:hidden}
.loading-screen .launch-icon>svg{display:block;width:100%;height:100%}
.loading-screen .launch-hands{transform-box:view-box;transform-origin:46.875% 56.25%;animation:launch-clock-turn 1.6s linear infinite}
@keyframes launch-clock-turn{to{transform:rotate(360deg)}}
@media(prefers-reduced-motion:reduce){.loading-screen .launch-hands{animation:none}}
[data-theme="dark"] .loading-screen{background:#000}

/* iOS 27 refinement: opaque content, restrained glass on navigation and sheets. */
:root {
    --ui-accent: #0066dc;
    --ui-glass: rgba(255,255,255,.92);
    --ui-glass-edge: rgba(255,255,255,.85);
    --ui-shadow: 0 8px 28px rgba(15,30,55,.12);
}
:root[data-theme="dark"] {
    --ui-accent: #75b5ff;
    --ui-glass: rgba(34,34,37,.95);
    --ui-glass-edge: rgba(255,255,255,.16);
    --ui-shadow: 0 8px 28px rgba(0,0,0,.35);
}
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; }
:is(button,a,input,select,textarea,summary):focus-visible { outline: 3px solid var(--ui-accent); outline-offset: 3px; }
:is(.btn,.icon-btn,.row-action,.btn-close,.nav-item) { touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
:is(.icon-btn,.row-action,.btn-close) { min-width: 44px; min-height: 44px; flex-shrink: 0; }
.btn { min-height: 44px; white-space: normal; }
:is(.stat-value,.entry-time,.entry-preview,.form-control[type="time"],.chart-total) { font-variant-numeric: tabular-nums; }
.modal-content>form { display: flex; flex-direction: column; min-height: 0; overflow: hidden; }
.modal-header,.modal-footer { flex-shrink: 0; }
.modal-body { min-height: 0; overflow-y: auto; overscroll-behavior: contain; }
.modal-title { padding-right: 12px; }
.btn-close { box-sizing: border-box; padding: 12px; margin: 0!important; }
.swal2-popup { border-radius: 28px; font-family: inherit; }
.swal2-actions { gap: 8px; }
.swal2-actions button { min-height: 44px; border-radius: 24px!important; margin: 0!important; }
.swal2-cancel { background: #e9e9ee!important; color: #303038!important; }
[data-theme="dark"] .swal2-cancel { background: #38383c!important; color: #f5f5f7!important; }
@media (max-width:850px) {
    html { font: -apple-system-body; font-size: 17px; }
    body { line-height: 1.35; }
    .page-content { padding-left: max(20px,env(safe-area-inset-left)); padding-right: max(20px,env(safe-area-inset-right)); }
    h1 { font-size: 2rem; font-weight: 700; letter-spacing: -.025em; }
    .page-heading { margin-bottom: 24px; }
    .sidebar nav { background: var(--ui-glass); border-color: var(--ui-glass-edge); box-shadow: var(--ui-shadow), inset 0 1px 0 var(--ui-glass-edge); padding: 6px; border-radius: 36px; }
    .nav-item { font-size: .765rem; line-height: 1.2; min-height: 62px; gap: 5px; border-radius: 29px; transition: background-color .18s ease,color .18s ease; }
    .nav-item.active { background: var(--ios-selected); box-shadow: inset 0 1px 1px #ffffff38; }
    .nav-item .app-icon { width: 26px; height: 26px; }
    .btn { font-weight: 600; border-radius: 999px; padding: 11px 18px; }
    .btn-light,.btn-outline-secondary { background: var(--ios-selected); color: var(--ios-blue); }
    .btn-link { text-decoration: none; }
    .period-controls { width: max-content; max-width: 100%; margin: 12px auto 0; padding: 4px; border-radius: 999px; background: var(--ios-surface); gap: 6px; flex-wrap: nowrap; }
    .period-controls .btn { min-width: 7rem; background: transparent; }
    .period-controls .icon-btn { background: transparent; }
    .overview-period { gap: 0; padding-bottom: 20px; }
    .period-title h2 { font-size: 1.294rem; }
    .overview-stats .stat-card { border-radius: 24px; padding: 18px 16px; gap: 8px; }
    .overview-stats .stat-label { font-size: .882rem; font-weight: 500; }
    .overview-stats .stat-value { font-size: 1.882rem; letter-spacing: -.02em; }
    .overview-stats .stat-footer { font-size: .824rem; line-height: 1.35; }
    .entries-heading h2,.chart-card h2 { font-size: 1.176rem; letter-spacing: -.015em; }
    .week-panel,.chart-card,.settings-card { border-radius: 26px; }
    .settings-grid { gap: 24px; }
    .settings-card { padding: 22px 20px; }
    .settings-card h2 { font-size: 1.176rem; line-height: 1.3; margin-bottom: 18px; }
    .settings-card p { line-height: 1.45; }
    .settings-card .section-heading h2 { margin-bottom: 0; }
    .form-control,.form-select { min-height: 48px; border-radius: 14px; padding: 12px 14px; font-size: 1rem; box-shadow: none; }
    .form-label { font-size: .882rem; font-weight: 500; margin-bottom: 8px; }
    .form-check { min-height: 44px; display: flex; align-items: center; padding-left: 0; gap: 12px; }
    .form-check .form-check-input { float: none; flex-shrink: 0; width: 22px; height: 22px; margin: 0; }
    .form-switch .form-check-input { width: 51px; height: 31px; border-radius: 999px; }
    .form-check-label { line-height: 1.3; }
    .schedule-row { padding: 18px 0; gap: 12px; }
    .schedule-row:last-child { border-bottom: 0; }
    .schedule-row .form-check { margin: 0; }
    .absence-row { min-height: 68px; padding: 18px 20px; }
    .absence-row:last-child { border-bottom: 0; }
    .absence-row .app-icon { width: 16px; color: var(--ios-secondary); }
    .modal { top: var(--sheet-top,0px); height: var(--sheet-height,100dvh); bottom: auto; }
    .modal-dialog { min-height: calc(100% - max(12px,env(safe-area-inset-top))); margin-top: max(12px,env(safe-area-inset-top)); }
    .modal-content { max-height: calc(var(--sheet-height,100dvh) - max(12px,env(safe-area-inset-top))); border-radius: 32px 32px 0 0; border: 1px solid var(--ui-glass-edge); box-shadow: var(--ui-shadow); }
    .modal-header { padding: 18px 20px 14px; border-bottom: 0; }
    .modal-title { font-size: 1.176rem; line-height: 1.3; font-weight: 650; }
    .modal-body { padding: 12px 20px 24px; }
    .modal-footer { padding: 12px 20px max(16px,env(safe-area-inset-bottom)); gap: 8px; }
    .modal-header,.modal-footer { background: var(--ui-glass); -webkit-backdrop-filter: blur(20px) saturate(1.4); backdrop-filter: blur(20px) saturate(1.4); }
    .modal-footer .btn { flex: 0 1 auto; padding: 10px 16px; }
    .entry-preview { padding: 16px; font-size: 1rem; line-height: 1.4; }
    .auth-story { padding: max(32px,env(safe-area-inset-top)) 24px 8px; }
    .auth-form-wrap { padding-top: 28px; }
    .auth-form h2 { margin-bottom: 28px; }
    .auth-form .form-control { min-height: 54px; }
    .auth-form .btn { min-height: 50px; }
    .undo-notice { background: var(--ui-glass); border-color: var(--ui-glass-edge); box-shadow: var(--ui-shadow); -webkit-backdrop-filter: blur(20px); backdrop-filter: blur(20px); }
    .update-banner { gap: 10px; padding: max(12px,env(safe-area-inset-top)) 16px 12px; }
    .notice { font-size: .882rem; line-height: 1.4; }
    .swal2-container { padding: max(16px,env(safe-area-inset-top)) 20px max(16px,env(safe-area-inset-bottom)); }
    .swal2-popup:not(.swal2-toast) { width: 21rem; max-width: 100%; padding-bottom: 20px; }
    .swal2-html-container { margin: 16px 20px 20px; line-height: 1.4; }
    .swal2-title { padding: 24px 20px 0; }
    .swal2-actions { max-width: calc(100% - 40px); }
    .swal2-actions button { flex: 1 1 auto; }
    #sms-text { font-size: 1rem; line-height: 1.6; }
}
@media(max-width:360px) {
    #overview-view .page-heading>div:last-child { grid-template-columns: minmax(0,1fr)!important; }
    .overview-stats { gap: 8px; }
    .overview-stats .stat-card { padding: 16px 12px; }
    .overview-stats .stat-value { font-size: 1.65rem; }
}
@media(hover:hover) { .btn:not(:disabled):hover,.icon-btn:hover,.row-action:hover { filter: brightness(.96); } }
@media(prefers-reduced-motion:no-preference) {
    .btn,.icon-btn,.row-action { transition: transform .14s ease,background-color .14s ease; }
    .btn:active,.icon-btn:active,.row-action:active { transform: scale(.97); }
}
@media(prefers-reduced-motion:reduce) {
    *,*::before,*::after { animation-duration: .01ms!important; animation-iteration-count: 1!important; transition-duration: .01ms!important; scroll-behavior: auto!important; }
}
@media(prefers-reduced-transparency:reduce),(prefers-contrast:more) {
    .sidebar nav,.modal-header,.modal-footer,.undo-notice { background: var(--ios-surface,#fff)!important; -webkit-backdrop-filter: none!important; backdrop-filter: none!important; }
    [data-theme="dark"] :is(.sidebar nav,.modal-header,.modal-footer,.undo-notice) { background: #1c1c1e!important; }
    .sidebar nav { border: 1px solid var(--muted); }
    .swal2-backdrop-show { -webkit-backdrop-filter: none; backdrop-filter: none; }
}
.modal-header .btn-close { margin-left: auto!important; }
/* Bootstrap's dark icon filter would also invert our library icon's surface. */
[data-bs-theme="dark"] .btn-close { filter: none; }

/* Period navigation: keep feedback space stable while the next period loads. */
.period-feedback { min-height: 24px; margin: -12px 0 12px; color: var(--ui-accent); font-size: .882rem; }
#period-loading { display: inline-flex; align-items: center; gap: 8px; }
#period-loading .app-icon { width: 18px; height: 18px; animation: period-loading-turn .8s linear infinite; }
@keyframes period-loading-turn { to { transform: rotate(360deg); } }
#overview-view[aria-busy="true"] #day-details { opacity: .65; pointer-events: none; }
@media(prefers-reduced-motion:reduce) { #period-loading .app-icon { animation: none; } }

/* Two inert page surfaces track the finger without changing the selected period. */
#period-viewport { position: relative; overflow: clip; }
#period-page { position: relative; }
.period-copy { position: absolute; top: 0; left: 0; width: 100%; pointer-events: none; background: var(--ios-background,var(--canvas,#f4f6f8)); }
.is-swiping { user-select: none; -webkit-user-select: none; }
.is-swiping #period-page,.is-swiping .period-copy { will-change: transform; }
.is-swiping #period-page { pointer-events: none; }
.swipe-placeholder { min-height: 300px; padding: 24px 20px; border-radius: 24px; background: var(--ios-surface,#fff); }
.swipe-placeholder h2 { font-size: 22px; }
.swipe-placeholder p { color: var(--muted); }
[data-period="week"] .period-copy details>summary { display: none; }
.period-copy [data-period-id="day-count"] { font-weight: 400; font-size: 14px; color: var(--muted); }
