/*
  Phone layout — loaded with media="(max-width: 768px)" (see index.html), so none of this
  reaches desktop. It restyles the existing markup into an app-like shell:
    - bottom tab bar + "Lainnya" sheet (modules/mobile-nav.js) instead of the slide-in sidebar
    - compact app bar with the page title
    - modals as bottom sheets, 16px form fields (no iOS zoom), 44px touch targets
    - tables as cards (mobile-nav.js adds data-label to every cell)
  Much of the app's markup carries inline styles, so several rules need !important.
*/

:root {
    --tabbar-h: 60px;
    --safe-b: env(safe-area-inset-bottom, 0px);
    --safe-t: env(safe-area-inset-top, 0px);
}

html, body { overflow-x: clip; -webkit-tap-highlight-color: transparent; }
body { padding-bottom: calc(var(--tabbar-h) + var(--safe-b)); overscroll-behavior-y: none; }
img, video, canvas, svg { max-width: 100%; }
.app-view > *, .main-content > * { min-width: 0; }

/* ── Shell: no sidebar, app bar on top, tab bar at the bottom ─────────────── */
.mobile-toggle { display: none !important; }
.sidebar { display: none !important; }

.top-header {
    height: calc(56px + var(--safe-t)) !important;
    padding: var(--safe-t) 14px 0 !important;
    gap: 10px;
}
.mobile-page-title {
    flex: 1; min-width: 0;
    font-size: 1.15rem; font-weight: 700; color: var(--primary-dark);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.header-actions { gap: 10px !important; flex-shrink: 0; }
.user-info, .user-profile > .fa-chevron-down { display: none !important; }
.user-profile { gap: 0 !important; }
#offlineBadge { padding: 4px 8px !important; font-size: 0.7rem !important; }
#syncNowBtn { padding: 4px 10px !important; }
.avatar, #activeUserAvatar { width: 34px !important; height: 34px !important; }
#accountSwitcher { position: fixed !important; top: calc(56px + var(--safe-t)) !important; right: 10px !important; left: 10px !important; min-width: 0 !important; max-height: 70dvh; overflow-y: auto; }
.notification-btn { min-width: 40px; min-height: 40px; }

.app-view { padding: 14px 14px 18px !important; gap: 14px !important; }
.main-content { min-height: 100dvh; }

.mobile-tabbar {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 950;
    display: flex; align-items: stretch;
    height: calc(var(--tabbar-h) + var(--safe-b)); padding-bottom: var(--safe-b);
    background: rgba(255, 255, 255, 0.96);
    -webkit-backdrop-filter: blur(14px); backdrop-filter: blur(14px);
    border-top: 1px solid rgba(0, 0, 0, 0.08);
    box-shadow: 0 -4px 18px rgba(0, 0, 0, 0.06);
}
.mtab {
    flex: 1; min-width: 0; border: 0; background: none; cursor: pointer;
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
    color: var(--text-muted); font: inherit; font-size: 0.68rem; font-weight: 600;
    position: relative; touch-action: manipulation;
}
.mtab i { font-size: 1.25rem; line-height: 1; }
.mtab span { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding: 0 2px; }
.mtab.active { color: var(--primary); }
.mtab.active::before {
    content: ''; position: absolute; top: 0; left: 28%; right: 28%; height: 3px;
    border-radius: 0 0 3px 3px; background: var(--primary);
}
.mtab:active { background: rgba(45, 106, 79, 0.08); }

/* "Lainnya" sheet */
.mobile-sheet { position: fixed; inset: 0; z-index: 960; visibility: hidden; pointer-events: none; }
.mobile-sheet.open { visibility: visible; pointer-events: auto; }
.mobile-sheet__backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, 0.4); opacity: 0; transition: opacity 0.2s ease; }
.mobile-sheet.open .mobile-sheet__backdrop { opacity: 1; }
.mobile-sheet__panel {
    position: absolute; left: 0; right: 0; bottom: 0;
    max-height: 82dvh; overflow-y: auto; -webkit-overflow-scrolling: touch;
    background: #fff; border-radius: 22px 22px 0 0;
    padding: 8px 16px calc(16px + var(--safe-b));
    transform: translateY(100%); transition: transform 0.25s cubic-bezier(0.25, 0.8, 0.25, 1);
}
.mobile-sheet.open .mobile-sheet__panel { transform: translateY(0); }
.mobile-sheet__grab { width: 40px; height: 4px; border-radius: 4px; background: rgba(0, 0, 0, 0.15); margin: 4px auto 12px; }
.mobile-sheet__title { font-weight: 700; color: var(--primary-dark); margin: 0 4px 12px; font-size: 1.05rem; }
.mobile-sheet__grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.mobile-sheet__tile {
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px;
    min-height: 84px; padding: 10px 6px; border: 1px solid rgba(0, 0, 0, 0.07); border-radius: 14px;
    background: #f7faf8; color: var(--text-dark); font: inherit; font-size: 0.74rem; font-weight: 600;
    text-align: center; cursor: pointer; touch-action: manipulation;
}
.mobile-sheet__tile i { font-size: 1.4rem; color: var(--primary); }
.mobile-sheet__tile.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.mobile-sheet__tile.active i { color: #fff; }
.mobile-sheet__tile:active { transform: scale(0.97); }
body.mobile-sheet-open { overflow: hidden; }

/* Toasts sit above the tab bar */
#tumber-toast-container { bottom: calc(var(--tabbar-h) + var(--safe-b) + 12px) !important; }

/* ── Touch targets and forms ─────────────────────────────────────────────── */
button, .btn, .icon-btn, select, .btn-outline-sm { min-height: 40px; touch-action: manipulation; }
.btn-sm { min-height: 34px; }
input:not([type="checkbox"]):not([type="radio"]):not([type="range"]), select, textarea { font-size: 16px !important; min-height: 44px; }
textarea { min-height: 88px; }
.form-row { flex-direction: column !important; gap: 0 !important; }

/* ── Modals as bottom sheets ─────────────────────────────────────────────── */
.modal-overlay { align-items: flex-end !important; justify-content: center; padding: 0 !important; }
.modal-overlay .modal-content {
    width: 100% !important; max-width: none !important; max-height: 92dvh !important;
    border-radius: 22px 22px 0 0 !important; padding: 18px 16px calc(20px + var(--safe-b)) !important;
    transform: translateY(100%) !important; -webkit-overflow-scrolling: touch;
}
.modal-overlay.active .modal-content { transform: translateY(0) !important; }
.modal-header {
    position: sticky; top: -18px; z-index: 5; background: #fff;
    margin: -18px -16px 14px !important; padding: 14px 16px !important; border-bottom: 1px solid rgba(0, 0, 0, 0.06);
}
.modal-header h2 { font-size: 1.15rem !important; }

/* ── Cards, grids ────────────────────────────────────────────────────────── */
.dashboard-splits, .summary-grid { grid-template-columns: 1fr !important; }
.welcome-banner { padding: 18px !important; }
.glass { border-radius: 16px; }

/* ── Tables as cards (rows get data-label from mobile-nav.js) ─────────────── */
.app-view table.m-cards { display: block !important; width: 100% !important; white-space: normal !important; overflow: visible !important; border: 0 !important; }
.app-view table.m-cards thead { display: none !important; }
.app-view table.m-cards tbody { display: flex; flex-direction: column; gap: 10px; }
.app-view table.m-cards tr {
    display: flex; flex-direction: column; gap: 2px;
    background: #fff; border: 1px solid rgba(0, 0, 0, 0.07); border-radius: 14px;
    padding: 10px 14px; box-shadow: var(--shadow-sm);
}
.app-view table.m-cards td {
    display: flex !important; justify-content: space-between; align-items: center; gap: 12px;
    padding: 6px 0 !important; border: 0 !important; border-bottom: 1px dashed rgba(0, 0, 0, 0.07) !important;
    text-align: right !important; white-space: normal !important; word-break: break-word; min-width: 0;
}
.app-view table.m-cards td:last-child { border-bottom: 0 !important; }
.app-view table.m-cards td[data-label]::before {
    content: attr(data-label); flex: 0 0 38%; max-width: 38%; text-align: left;
    font-size: 0.72rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.3px; color: var(--text-muted);
}
.app-view table.m-cards td:not([data-label]) { justify-content: flex-end; }
.app-view table.m-cards tr.m-full { text-align: center; }
.app-view table.m-cards tr.m-full td { justify-content: center; text-align: center !important; }

/* ── Rows that do not fit wrap instead of overflowing ─────────────────────── */
.app-view div[style*="display:flex"], .app-view div[style*="display: flex"],
.app-view [class*="header"][style*="flex"], .modal-content div[style*="display:flex"], .modal-content div[style*="display: flex"] { flex-wrap: wrap; }
.app-view select, .app-view input, .app-view textarea, .modal-content select, .modal-content input, .modal-content textarea { max-width: 100%; min-width: 0; }
.app-view select[style*="width"], .app-view input[style*="width"] { max-width: 100% !important; }
/* fixed-min-width grids of cards become a single column */
.app-view [style*="minmax(3"], .app-view [style*="minmax(2"], .app-view [style*="minmax(4"] { grid-template-columns: 1fr !important; }
.land-plots-grid { padding-left: 12px !important; margin-left: 8px !important; grid-template-columns: 1fr !important; }

/* ── Compact stat tiles (2 per row) and page headers ─────────────────────── */
.stats-grid { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; gap: 10px !important; }
.stat-card { padding: 14px !important; flex-direction: column; gap: 10px !important; }
.stat-icon-wrap { width: 38px !important; height: 38px !important; font-size: 1.1rem !important; border-radius: 10px !important; }
.stat-details { min-width: 0; width: 100%; }
.stat-details h3 { font-size: 0.74rem !important; margin-bottom: 4px !important; }
.stat-details h2 { font-size: 1.15rem !important; margin-bottom: 4px !important; flex-wrap: wrap; overflow-wrap: anywhere; }
.stat-details .unit, .trend { font-size: 0.72rem !important; }
.project-header { padding: 14px !important; gap: 12px !important; flex-direction: column !important; align-items: stretch !important; }
.project-header h1 { font-size: 1.25rem !important; }
.project-header p { font-size: 0.85rem !important; }
.project-header > div { width: 100%; }
.project-header select, .project-header input:not([type="checkbox"]) { width: 100% !important; }
.project-header .btn, .project-header button:not(.icon-btn) { width: 100%; justify-content: center; }
.card-header { padding: 12px 14px !important; }
.split-card .card-header h2 { font-size: 1rem !important; }
.split-card { min-width: 0; }
.stats-grid > .stat-card.m-span-2 { grid-column: 1 / -1; }

/* ── Tab strips and filter chips ─────────────────────────────────────────────
   Every item stays visible: they wrap onto a second line instead of hiding behind a sideways scroll
   (a scroll needs a swipe, which a mouse or a narrow desktop window cannot do). */
.app-view :is(.gudang-tab, .purchasing-tab, .pekerja-tab, .tab-btn, .users-tab-btn, .settings-tab-btn) { white-space: nowrap; }
.app-view :is(div, nav):has(> :is(.gudang-tab, .purchasing-tab, .pekerja-tab, .tab-btn, .users-tab-btn, .settings-tab-btn)) { flex-wrap: wrap !important; max-width: 100%; }

/* Filter rows: selects and search boxes fill the width instead of hanging at odd sizes */
.app-view div[style*="flex"] > select, .app-view div[style*="flex"] > input[type="text"], .app-view div[style*="flex"] > input[type="search"],
.app-view div[style*="flex"] > input:not([type]) { flex: 1 1 100% !important; width: 100% !important; }

/* Filter chips: smaller, and they wrap (see above) */
.jadwal-filters { flex-wrap: wrap !important; gap: 8px !important; }
.jadwal-filters > .btn { white-space: nowrap; min-height: 36px; padding: 6px 12px; font-size: 0.8rem; }

.jadwal-filters > select { flex: 1 1 100% !important; }
.view-toggle { width: 100%; }
.view-toggle .btn { flex: 1; justify-content: center; }

/* Task cards: status dot + text on the first line, actions below */
.jadwal-task-card { flex-wrap: wrap; gap: 10px 12px !important; padding: 14px !important; }
.jadwal-task-card:hover { transform: none; }
.task-status-dot { width: 36px !important; height: 36px !important; }
.task-card-body { flex: 1 1 calc(100% - 60px) !important; }
.task-card-meta { gap: 6px 12px !important; }
.task-card-actions { flex: 1 1 100% !important; justify-content: flex-start !important; }

/* One scroll direction: lists inside cards grow with the page instead of scrolling on their own */
.app-view [style*="max-height"][style*="overflow-y"], .app-view [style*="max-height"][style*="overflow: auto"] { max-height: none !important; overflow-y: visible !important; }
.modal-header button, .modal-header .icon-btn, .modal-header .close-modal { min-width: 40px; min-height: 40px; display: inline-flex; align-items: center; justify-content: center; }
#closeImagePreviewBtn { top: 8px !important; right: 8px !important; }
#loginOverlay { padding: 16px !important; }

/* Content of a table cell (wrapped by mobile-nav.js): one right-aligned block next to the label */
.app-view table.m-cards td > .m-cell {
    flex: 1 1 0; min-width: 0; display: flex; flex-wrap: wrap; justify-content: flex-end; align-items: center;
    gap: 4px 8px; text-align: right; overflow-wrap: anywhere;
}
.app-view table.m-cards td > .m-cell > div, .app-view table.m-cards td > .m-cell > p { flex: 1 1 100%; min-width: 0; }
.app-view table.m-cards td > .m-cell > * { max-width: 100%; }

/* Cards inside cards: less padding so tables and lists use the width */
.split-card { padding: 14px 10px !important; }
.split-card > div[style*="padding"] { padding: 4px !important; }
.view-toggle .btn { white-space: nowrap; padding-left: 6px; padding-right: 6px; font-size: 0.78rem; }

/* ── Catalogue cards: one per row (they carry a photo, lists and buttons) ──── */
#masterKomoditasGrid, #saprotanGrid { grid-template-columns: 1fr !important; gap: 14px !important; }
#masterKomoditasGrid > .stat-card, #saprotanGrid > .stat-card { padding: 0 !important; overflow: hidden; gap: 0 !important; }

/* ── Repeating form rows (schedule steps, blocks, fertiliser needs) ─────────
   .form-row stacks its fields on phones; a row that ends in a remove button is a compact card instead. */
.form-row:has(> .icon-btn:last-child) {
    flex-direction: row !important; flex-wrap: wrap; align-items: center; gap: 8px !important;
    padding: 10px; background: rgba(45, 106, 79, 0.05); border: 1px solid rgba(0, 0, 0, 0.06); border-radius: 12px;
}
.form-row:has(> .icon-btn:last-child) > .form-group { flex: 1 1 0; margin-bottom: 0 !important; }
.form-row:has(> .icon-btn:last-child) > :is(input, select, div:not(.form-group)) { flex: 1 1 100%; width: 100% !important; margin: 0; }
.form-row:has(> .icon-btn:last-child) > .icon-btn { flex: 0 0 auto; margin-left: auto; }
/* schedule step: HST | type | days | remove on the first line, the activity name below */
.schedule-row > .sched-day, .schedule-row > .sched-duration { flex: 0 0 60px !important; width: 60px !important; padding-left: 8px; padding-right: 8px; }
.schedule-row > .sched-type { flex: 1 1 110px !important; width: auto !important; min-width: 110px; }
.schedule-row > .remove-row-btn { order: 4; }
.schedule-row > .sched-task { order: 5; flex: 1 1 100% !important; width: 100% !important; }

/* Land block row (4 fields in a grid on desktop): one field per line, remove button at the end */
.project-block-row { grid-template-columns: minmax(0, 1fr) !important; gap: 8px !important; padding: 10px !important; }
.project-block-row > .remove-block-btn { justify-self: end; }
.project-block-row > div[style*="display:flex"] { flex-wrap: nowrap !important; }
.project-block-row .block-land-unit { width: 84px !important; flex: 0 0 84px; }
/* Fertiliser needs: name on its own line, quantity and unit side by side; the column header row is redundant */
.saprotan-need-row { flex-direction: row !important; flex-wrap: wrap; gap: 8px !important; padding: 10px; background: rgba(45, 106, 79, 0.05); border-radius: 12px; }
.saprotan-need-row > .form-group:first-child { flex: 1 1 100% !important; }
.saprotan-need-row > .form-group { flex: 1 1 40% !important; margin-bottom: 0 !important; }
#saprotanNeedsContainer > .form-row:first-child:not(.saprotan-need-row) { display: none !important; }

/* ── Modal action buttons: stacked, full width, primary on top, always reachable ──
   (a desktop row of 2-3 buttons ran off the left edge). The bar sticks to the bottom of the sheet. */
.modal-content .form-actions {
    display: flex !important; flex-direction: column-reverse !important; align-items: stretch !important; gap: 8px !important;
    position: sticky; bottom: -20px; z-index: 4; background: #fff;
    margin: 14px -16px -20px !important; padding: 12px 16px calc(12px + var(--safe-b)) !important;
    border-top: 1px solid rgba(0, 0, 0, 0.06);
}
.modal-content .form-actions > .btn, .modal-content .form-actions > button {
    width: 100% !important; margin: 0 !important; justify-content: center; min-height: 46px; white-space: normal; text-align: center;
}

/* Daily-worker rows (field check): name on its own line, then count | rate | total | remove */
.worker-row { grid-template-columns: 64px minmax(0, 1fr) auto auto !important; gap: 6px !important; }
.worker-row > .worker-name { grid-column: 1 / -1; width: 100%; }
.worker-row input { min-width: 0; }
div:has(+ #workerRowsContainer) { display: none !important; }

/* Tab labels that are not selected: darker grey so they stay readable on the light strip */
.app-view :is(.gudang-tab, .purchasing-tab, .pekerja-tab, .users-tab-btn, .settings-tab-btn, .tab-btn):not(.active):not([style*="background:white"]):not([style*="background: white"]):not([style*="var(--primary)"]):not([style*="rgba(45,106,79,0.12)"]) { color: #4b5563 !important; }

/* ── Pengaturan: the section menu runs along the top so the form gets the full width ── */
div:has(> div > #pengaturanTabNav) { flex-direction: column !important; }
div:has(> #pengaturanTabNav) { width: 100% !important; border-right: 0 !important; border-bottom: 1px solid var(--border-glass); padding: 10px 6px !important; }
#pengaturanTabNav { flex-direction: row !important; flex-wrap: nowrap; gap: 4px !important; }
#pengaturanTabNav .settings-tab-btn {
    flex: 1 1 0 !important; min-width: 0; width: auto !important; flex-direction: column; justify-content: center; gap: 4px !important;
    padding: 8px 2px !important; font-size: 0.72rem !important; text-align: center !important; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
#pengaturanTabNav .settings-tab-btn i { width: auto !important; font-size: 1.05rem; }
#pengaturanTabBody { padding: 14px 6px !important; max-height: none !important; overflow-y: visible !important; }
