/* ==========================================================================
   AIDOWS FDE — MOBILE SHELL  (active while <html class="fde-mobile">)
   --------------------------------------------------------------------------
   The class is set in index.php <head> from FDE_MOBILE_MQ (the same query the
   old @media block in os.css used) and kept in sync by system/mobile.js.
   Desktop mode never sees any of this — every rule is scoped to .fde-mobile.

   Geometry comes from the VISUAL viewport (--vvh / --vvt, written by
   mobile.js), not from 100vh / 100dvh: on iOS those don't shrink when the
   keyboard (or the taller emoji keyboard) opens. Notch, status bar of the
   installed PWA and the home indicator come in through --sat/--sab/--sal/--sar.

   Phones get one app on screen at a time (full-screen, header with
   [switcher] [_] [x]); the bottom bar lives on the home screen only.
   Modules are untouched: they still run in iframes and talk to window.sys.
   (c) 2025 Juzko Network.
   ========================================================================== */

:root {
    --sat: env(safe-area-inset-top, 0px);
    --sab: env(safe-area-inset-bottom, 0px);
    --sal: env(safe-area-inset-left, 0px);
    --sar: env(safe-area-inset-right, 0px);
    /* room kept for the home indicator / gesture bar */
    --sab-eff: var(--sab);
    --m-bar: 46px;      /* bottom bar (home screen) */
    --m-head: 44px;     /* app header */
    /* home screen grid cell (mobile.js counts columns / rows with these):
       min width of a column, height of an icon with a one-line label (8+8 padding,
       2 border, 40 image, 5 gap, 11.5px x 1.2 label) = the row height it always had */
    --m-cell-w: 68px;
    --m-cell-h: 76.8px;
}

/* iOS home-screen app (mobile.js adds .fde-ios-pwa + --ios-band = the bottom band WebKit
   won't paint our elements into, 44 px on the XS Max). The home indicator (a thin pill,
   Lukasz: keep it "almost to the bottom") sits inside that band -> no extra room above it;
   with no band keep only 14 px of the 34 px safe area. */
html.fde-ios-pwa { --sab-eff: max(0px, calc(min(var(--sab), 14px) - var(--ios-band, 0px))); }

/* ...and the band shows only the page background colour -> make it the bar's colour, and
   the bar solid, so bar + band read as one dock down to the edge of the screen */
html.fde-ios-pwa body { background-color: #141414; }
html.fde-ios-pwa body[data-theme="modern"] { background-color: #11161d; }
html.fde-ios-pwa body[data-theme="aero"] { background-color: #1c2f4a; }
html.fde-ios-pwa.fde-mobile #os-taskbar { background: #141414; }
html.fde-ios-pwa.fde-mobile body[data-theme="modern"] #os-taskbar { background: #11161d; }
html.fde-ios-pwa.fde-mobile body[data-theme="aero"] #os-taskbar { background: linear-gradient(to bottom, #3b5577, #1c2f4a); }

/* landscape phones: slimmer chrome (portrait SE in Safari is short too, but keeps full-size controls) */
@media (orientation: landscape) and (max-height: 500px) {
    html.fde-mobile { --m-bar: 40px; --m-head: 36px; }
}

/* mobile-only controls (switcher buttons) stay hidden on the desktop */
.m-only { display: none !important; }
html.fde-mobile .m-only { display: inline-flex !important; }

/* --- ROOT: the page itself never moves ----------------------------------- */
html.fde-mobile,
html.fde-mobile body {
    width: 100%;
    height: 100%;
    overflow: hidden !important;
    overscroll-behavior: none;
}
html.fde-mobile body {
    position: relative;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    -webkit-text-size-adjust: 100%;
}

/* --- BOOT: keep the text out of the notch / status bar ------------------- */
html.fde-mobile #boot-overlay {
    padding-top: calc(20px + var(--sat));
    padding-left: calc(16px + var(--sal));
    padding-right: calc(16px + var(--sar));
}

/* Modern / Aero boot in the system font (SF on iPhone, Roboto on Android): same 1.05rem as
   CRT, but much wider than VT323 -> on 320-360 px "[OK]" dropped under its line (Lukasz, SE).
   Shrink the boot text only as much as the longest line needs; never above the size os.css
   gives (1em = #boot-overlay), CRT untouched. 20.5 em = "MOUNTING VIRTUAL FILE SYSTEM... [OK]"
   in the widest font we measured (DejaVu Sans 20.24 em; Helvetica ~ SF 19.4 em), 38 px = the
   16 + 16 px side padding above + 6 px gap before [OK]. SE: 16.8 -> ~13.8 px, >= 390 px: no change. */
html.fde-mobile body[data-theme="modern"] .boot-terminal,
html.fde-mobile body[data-theme="aero"] .boot-terminal {
    font-size: min(1em, calc((100vw - 38px - var(--sal) - var(--sar)) / 20.5));
}

/* --- HOME SCREEN: the only scroll container ------------------------------ */
html.fde-mobile #os-desktop {
    position: fixed;
    left: 0;
    right: 0;
    top: var(--vvt, 0px);
    width: auto;
    height: var(--vvh, 100vh);
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;

    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(var(--m-cell-w), 1fr));
    align-content: start;
    align-items: start;
    gap: 14px 6px;
    padding: calc(18px + var(--sat)) calc(10px + var(--sar))
             calc(var(--m-bar) + var(--sab-eff) + 24px) calc(10px + var(--sal));
}
html.fde-mobile #os-desktop.os-hidden { display: none; }

/* an app covers the screen -> freeze the home screen behind it */
html.fde-mobile.fde-app-open #os-desktop { overflow: hidden; }

/* CRT scanlines: cover the screen, don't scroll away with the icons */
html.fde-mobile #os-desktop::after { position: fixed; }

/* containers that must not take a grid cell */
html.fde-mobile #os-windows {
    position: absolute;
    top: 0;
    left: 0;
    width: 0;
    height: 0;
}
html.fde-mobile #os-background {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    z-index: -1;
}

/* icons: grid instead of saved x/y */
html.fde-mobile .desktop-icon {
    position: relative !important;
    top: auto !important;
    left: auto !important;
    width: auto !important;
    min-width: 0;
    height: auto !important;
    margin: 0 !important;
    padding: 8px 2px;
    transform: none !important;
    touch-action: manipulation;
}
html.fde-mobile .desktop-icon:hover { background: transparent; border-color: transparent; }
html.fde-mobile .desktop-icon:active { background: rgba(255, 255, 255, 0.12); }
html.fde-mobile .d-icon-label { font-size: 11.5px; overflow-wrap: anywhere; }

/* "Arrange icons" (long press on the desktop; mobile.js section 4): the bottom bar turns
   into [Cancel] hint [Done], empty cells show up as dashed slots, the picked icon glows */
html.fde-mobile.fde-arranging #os-taskbar > :not(#m-arrange) { display: none !important; }
#m-arrange {
    display: flex;
    flex: 1 1 auto;
    align-items: center;
    gap: 8px;
    min-width: 0;
}
.m-ar-hint {
    flex: 1 1 auto;
    min-width: 0;
    text-align: center;
    color: #aaa;
    font-family: var(--font-ui);
    font-size: 12px;
    line-height: 1.25;
}
.m-ar-btn {
    flex-shrink: 0;
    height: 32px;
    padding: 0 12px;
    background: transparent;
    border: 1px solid var(--border-color);
    border-radius: var(--ui-radius, 0);
    color: #ccc;
    font-family: var(--font-ui);
    font-size: 0.9rem;
    touch-action: manipulation;
    cursor: pointer;
}
.m-ar-btn.ok { border-color: var(--accent-green); color: var(--accent-green); }
/* an empty row between icons keeps an icon's height (rows stay content-sized: a fixed
   minimum would let a crowded screen squeeze the sticky-note rows) */
html.fde-mobile .m-gap {
    min-height: var(--m-cell-h);
    visibility: hidden;
    pointer-events: none;
}
html.fde-mobile .m-slot {
    min-height: var(--m-cell-h);
    border: 1px dashed rgba(255, 255, 255, 0.22);
    border-radius: var(--ui-radius, 0);
}
html.fde-mobile.fde-arranging .desktop-icon { cursor: pointer; }
html.fde-mobile.fde-arranging .desktop-icon:active { background: transparent; }
html.fde-mobile .desktop-icon.m-picked,
html.fde-mobile .desktop-icon.m-picked:active {
    background: var(--accent-glow);
    border-color: var(--accent-green);
}
html.fde-mobile.fde-arranging .desktop-note { opacity: 0.4; }   /* notes stay where they are */

/* sticky notes: full-width cards */
html.fde-mobile .desktop-note {
    position: relative !important;
    top: auto !important;
    left: auto !important;
    grid-column: 1 / -1;
    width: auto !important;
    height: auto !important;
    min-height: 150px;
    margin: 4px 0;
    transform: none !important;
}
html.fde-mobile .desktop-note:hover { transform: none !important; }
html.fde-mobile .note-handle { height: 32px; }
html.fde-mobile .note-close { font-size: 20px; line-height: 32px; padding: 0 12px; }
html.fde-mobile .note-content { font-size: 16px; }   /* 16px: iOS doesn't zoom in on focus */

/* --- APP WINDOW: full screen inside the safe area ------------------------ */
html.fde-mobile .os-window {
    position: fixed !important;
    top: calc(var(--vvt, 0px) + var(--sat)) !important;
    left: var(--sal) !important;
    right: var(--sar);
    width: auto !important;
    height: calc(var(--vvh, 100vh) - var(--sat) - var(--sab-eff)) !important;
    min-width: 0;
    min-height: 0;
    max-height: none;
    border-radius: 0;
    box-shadow: none;
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
    animation: fdeMobOpen 0.16s ease-out;
}
/* keyboard up: it already covers the home indicator -> use every pixel above it */
html.fde-mobile.fde-kb .os-window {
    height: calc(var(--vvh, 100vh) - var(--sat)) !important;
}
/* aero without blur: give the glass a solid backing */
html.fde-mobile body[data-theme="aero"] .os-window { background: #1a3150; }

@keyframes fdeMobOpen {
    from { opacity: 0; transform: translateY(12px); }
    to   { opacity: 1; transform: none; }
}

html.fde-mobile .window-header {
    height: var(--m-head);
    flex-shrink: 0;
    gap: 8px;
    padding: 0 2px 0 12px;
    cursor: default;
    touch-action: none;          /* dragging the header must not move the page */
}
html.fde-mobile .window-title {
    flex: 1 1 auto;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 0.9rem;
}
html.fde-mobile .window-controls { flex-shrink: 0; gap: 4px; }
html.fde-mobile .win-btn {
    width: 40px;
    height: 34px;
    font-size: 1rem;
    touch-action: manipulation;
}
@media (orientation: landscape) and (max-height: 500px) {
    html.fde-mobile .win-btn { height: 30px; }
}
html.fde-mobile .btn-switch .fde-ico { width: 18px; height: 18px; }
html.fde-mobile .resize-handle { display: none; }

/* landscape + keyboard: only ~110-150 px are left -> give them all to the app
   (the header comes back as soon as the keyboard goes away) */
@media (orientation: landscape) and (max-height: 500px) {
    html.fde-mobile.fde-kb .window-header { display: none; }
}

html.fde-mobile .window-content {
    flex: 1 1 auto;
    min-height: 0;
    position: relative;
}
html.fde-mobile iframe.app-frame {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

/* --- BOTTOM BAR (home screen only) --------------------------------------- */
html.fde-mobile #os-taskbar {
    position: fixed;
    left: 0;
    right: 0;
    width: auto;
    top: calc(var(--vvt, 0px) + var(--vvh, 100vh) - var(--m-bar) - var(--sab-eff));
    bottom: auto;
    height: calc(var(--m-bar) + var(--sab-eff));
    padding: 0 calc(8px + var(--sar)) var(--sab-eff) calc(8px + var(--sal));
    gap: 8px;
    z-index: 6000;
    backdrop-filter: none !important;
    -webkit-backdrop-filter: none !important;
    transform: translateZ(0);
    touch-action: none;
}
html.fde-mobile.fde-app-open #os-taskbar,
html.fde-mobile.fde-kb #os-taskbar { display: none; }

html.fde-mobile #running-apps { display: none; }   /* replaced by the switcher */
html.fde-mobile #start-btn {
    flex-shrink: 0;
    padding: 3px 12px;
    font-size: 1.1rem;
}
html.fde-mobile #switch-btn {
    flex-shrink: 0;
    align-items: center;
    gap: 6px;
    height: 32px;
    padding: 0 10px;
    background: transparent;
    border: 1px solid var(--border-color);
    border-radius: var(--ui-radius, 0);
    color: #888;
    font-family: var(--font-ui);
    font-size: 0.9rem;
    touch-action: manipulation;
    cursor: pointer;
}
html.fde-mobile #switch-btn.has-apps {
    border-color: var(--accent-green);
    color: var(--accent-green);
}
html.fde-mobile #switch-btn .fde-ico { width: 16px; height: 16px; }
html.fde-mobile #system-tray {
    flex-shrink: 0;
    margin-left: auto;
    padding-right: 2px;
}

/* --- START MENU: sheet above the bar ------------------------------------- */
html.fde-mobile .os-start-menu {
    position: fixed;
    left: calc(8px + var(--sal));
    right: calc(8px + var(--sar));
    width: auto;
    /* anchored by its bottom edge WITHOUT `bottom:` (that one follows WebKit's own,
       sometimes wrong, viewport height on iOS): top at the anchor + translate -100% */
    top: calc(var(--vvt, 0px) + var(--vvh, 100vh) - var(--m-bar) - var(--sab-eff) - 6px);
    bottom: auto;
    translate: 0 -100%;
    max-height: calc(var(--vvh, 100vh) - var(--sat) - var(--m-bar) - var(--sab-eff) - 18px);
    z-index: 6001;
    backdrop-filter: none !important;
    -webkit-backdrop-filter: none !important;
}
html.fde-mobile .start-list {
    flex: 1 1 auto;
    min-height: 0;
    max-height: none;
    overflow-y: auto;
    overscroll-behavior: contain;
    touch-action: pan-y;
}
html.fde-mobile .start-item { min-height: 46px; padding: 10px 14px; }
html.fde-mobile .start-item:hover { padding-left: 14px; background: transparent; }
html.fde-mobile .start-item:active { background: rgba(255, 255, 255, 0.12); }

/* --- CONTEXT MENU (long press): sheet above the bar ---------------------- */
html.fde-mobile .os-context-menu {
    position: fixed;
    left: calc(8px + var(--sal)) !important;
    right: calc(8px + var(--sar));
    top: calc(var(--vvt, 0px) + var(--vvh, 100vh) - var(--m-bar) - var(--sab-eff) - 6px) !important;
    bottom: auto;
    translate: 0 -100%;
    width: auto;
    border-radius: var(--ui-radius, 0);
}
html.fde-mobile .ctx-item { padding: 14px 16px; font-size: 1rem; }
html.fde-mobile .ctx-item:hover { background: transparent; color: #ccc; }
html.fde-mobile .ctx-item:active { background: #0e639c; color: #fff; }

/* --- DIALOGS: fit the width, stay above the keyboard --------------------- */
html.fde-mobile .os-dialog-overlay {
    top: var(--vvt, 0px);
    height: var(--vvh, 100vh);
    padding: calc(12px + var(--sat)) calc(12px + var(--sar)) calc(12px + var(--sab-eff)) calc(12px + var(--sal));
    backdrop-filter: none;
}
html.fde-mobile.fde-kb .os-dialog-overlay { padding-bottom: 12px; }
html.fde-mobile .os-dialog {
    width: 100% !important;
    max-width: 420px;
    max-height: 100%;
    overflow-y: auto;
}
html.fde-mobile .dialog-content { padding: 16px; }
html.fde-mobile .dialog-content input,
html.fde-mobile .dialog-content select { font-size: 16px; }   /* no iOS focus zoom */
html.fde-mobile .btn-dialog { min-height: 40px; padding: 8px 16px; }

/* --- TOAST: top of the screen (never under the keyboard) ----------------- */
html.fde-mobile .os-toast {
    left: calc(12px + var(--sal));
    right: calc(12px + var(--sar));
    top: calc(var(--vvt, 0px) + var(--sat) + 10px);
    bottom: auto;
    text-align: center;
    transform: translateY(-16px);
}
html.fde-mobile.fde-app-open .os-toast {
    top: calc(var(--vvt, 0px) + var(--sat) + var(--m-head) + 8px);
}
html.fde-mobile .os-toast.show { transform: none; }

/* --- APP SWITCHER (built by mobile.js) ----------------------------------- */
.m-switcher { display: none; }
html.fde-mobile .m-switcher.open {
    display: flex;
    flex-direction: column;
    position: fixed;
    left: 0;
    right: 0;
    top: var(--vvt, 0px);
    height: var(--vvh, 100vh);
    padding: calc(10px + var(--sat)) calc(10px + var(--sar)) calc(10px + var(--sab-eff)) calc(10px + var(--sal));
    background: rgba(0, 0, 0, 0.72);
    z-index: 9000;
    font-family: var(--font-ui);
}
.m-sw-panel {
    margin-top: auto;
    display: flex;
    flex-direction: column;
    min-height: 0;
    max-height: 100%;
    background: linear-gradient(var(--win-bg), var(--win-bg)), var(--bg-color);
    border: 1px solid var(--accent-green);
    border-radius: var(--ui-radius, 0);
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.8);
    overflow: hidden;
}
.m-sw-head {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 46px;
    padding: 0 2px 0 14px;
    border-bottom: 1px solid var(--border-color);
    color: var(--accent-green);
    font-weight: 700;
    letter-spacing: 1px;
}
.m-sw-x {
    width: 46px;
    height: 44px;
    background: none;
    border: 0;
    color: var(--text-main);
    font-size: 24px;
    line-height: 1;
    cursor: pointer;
}
.m-sw-list {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-y;
}
.m-sw-row {
    display: flex;
    align-items: stretch;
    border-bottom: 1px solid var(--border-color);
}
.m-sw-open {
    flex: 1 1 auto;
    min-width: 0;
    display: flex;
    align-items: center;
    gap: 12px;
    min-height: 52px;
    padding: 0 14px;
    background: none;
    border: 0;
    border-left: 3px solid transparent;
    color: var(--text-main);
    font-family: var(--font-ui);
    font-size: 15px;
    text-align: left;
    cursor: pointer;
}
.m-sw-row.is-active .m-sw-open {
    border-left-color: var(--accent-green);
    color: var(--accent-green);
}
.m-sw-open .t {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.m-sw-ico {
    flex-shrink: 0;
    width: 22px;
    display: inline-flex;
    justify-content: center;
    font-size: 20px;
}
.m-sw-ico .fde-ico,
.m-sw-ico img { width: 20px; height: 20px; object-fit: contain; }
.m-sw-close {
    flex-shrink: 0;
    width: 50px;
    background: none;
    border: 0;
    color: #e06060;
    font-size: 22px;
    cursor: pointer;
}
.m-sw-empty { padding: 22px 14px; color: #888; text-align: center; }
.m-sw-foot {
    flex-shrink: 0;
    display: flex;
    gap: 8px;
    padding: 10px;
    border-top: 1px solid var(--border-color);
}
.m-sw-btn {
    flex: 1 1 0;
    min-height: 42px;
    background: #222;
    color: #ddd;
    border: 1px solid #444;
    border-radius: var(--ui-radius, 0);
    font-family: var(--font-ui);
    font-size: 14px;
    cursor: pointer;
}
.m-sw-btn.danger { color: #ff6b6b; border-color: #5a2a2a; }

/* --- DIAGNOSTICS (hold the clock ~1 s; mobile.js) ------------------------ */
#m-diag {
    position: fixed;
    left: calc(4px + var(--sal));
    right: calc(4px + var(--sar));
    top: calc(var(--vvt, 0px) + var(--sat) + 4px);
    z-index: 99999;
    margin: 0;
    padding: 6px 8px;
    background: rgba(0, 0, 0, 0.82);
    color: #00ff41;
    border: 1px solid #00ff41;
    font: 10px/1.35 monospace;
    white-space: pre-wrap;
    pointer-events: none;
}
