/* ═══════════════════════════════════════════════════════════════
   KOSA Mobile Shell v1.0
   Bottom nav + hamburger drawer + top bar + bottom sheet för mobil.
   Aktiveras endast vid (max-width: 768px). Desktop påverkas inte.

   Inkludera i <head> efter kosa-design.css:
     <link rel="stylesheet" href="../shared/kosa-mobile-shell.css">
   Och scriptet före </body>:
     <script src="../shared/kosa-mobile-shell.js" defer></script>

   Touch-targets: alla interaktiva element ≥ 44×44px (mobile-design skill).
   Safe-area: respekterar notch/home-indicator via env(safe-area-inset-*).
═══════════════════════════════════════════════════════════════ */

/* Default: helt dolt — desktop påverkas inte */
.kosa-ms-topbar,
.kosa-ms-bottom-nav,
.kosa-ms-drawer,
.kosa-ms-drawer-backdrop,
.kosa-ms-sheet,
.kosa-ms-sheet-backdrop { display: none; }

/* Aktiveras endast på mobil */
@media (max-width: 768px) {

  /* ── Top bar ────────────────────────────────────────────────── */
  .kosa-ms-topbar {
    position: fixed;
    top: 0; left: 0; right: 0;
    height: calc(52px + env(safe-area-inset-top, 0px));
    padding-top: env(safe-area-inset-top, 0px);
    background: var(--card, #F2EDE4);
    border-bottom: 1px solid var(--border, #D6CFC2);
    z-index: 1000;
    display: flex;
    align-items: center;
    gap: 8px;
    padding-left: 8px;
    padding-right: 8px;
    font-family: 'Inter', -apple-system, system-ui, sans-serif;
  }
  .kosa-ms-topbar-btn {
    width: 44px;
    height: 44px;
    border: none;
    background: transparent;
    color: var(--text, #1A1714);
    font-size: 22px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background .1s;
  }
  .kosa-ms-topbar-btn:active { background: var(--card2, #E2DDD2); }
  .kosa-ms-topbar-title {
    flex: 1;
    font-size: 16px;
    font-weight: 600;
    color: var(--text, #1A1714);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    text-align: center;
    padding: 0 4px;
  }

  /* ── Bottom nav ─────────────────────────────────────────────── */
  .kosa-ms-bottom-nav {
    position: fixed;
    bottom: 0; left: 0; right: 0;
    height: calc(64px + env(safe-area-inset-bottom, 0px));
    padding-bottom: env(safe-area-inset-bottom, 0px);
    background: var(--card, #F2EDE4);
    border-top: 1px solid var(--border, #D6CFC2);
    z-index: 1000;
    display: flex;
    align-items: stretch;
    font-family: 'Inter', -apple-system, system-ui, sans-serif;
    box-shadow: 0 -1px 8px rgba(0,0,0,.04);
  }
  .kosa-ms-nav-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    padding: 6px 2px 4px;
    text-decoration: none;
    color: var(--muted, #7A7268);
    font-size: 10px;
    font-weight: 500;
    min-height: 44px;
    border: none;
    background: transparent;
    cursor: pointer;
    position: relative;
    transition: color .1s;
  }
  .kosa-ms-nav-item:active { background: var(--card2, #E2DDD2); }
  .kosa-ms-nav-item .kosa-ms-nav-icon {
    font-size: 22px;
    line-height: 1;
    display: block;
  }
  .kosa-ms-nav-item .kosa-ms-nav-label {
    font-size: 10px;
    line-height: 1.1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100%;
  }
  .kosa-ms-nav-item.active {
    color: var(--gold, #B8952A);
  }
  .kosa-ms-nav-item.active::before {
    content: '';
    position: absolute;
    top: 0;
    left: 25%;
    right: 25%;
    height: 3px;
    background: var(--gold, #B8952A);
    border-radius: 0 0 3px 3px;
  }
  .kosa-ms-nav-badge {
    position: absolute;
    top: 4px;
    right: 50%;
    transform: translateX(14px);
    min-width: 16px;
    height: 16px;
    border-radius: 999px;
    background: #df2f4a;
    color: #fff;
    font-size: 9px;
    font-weight: 700;
    padding: 0 4px;
    display: none;
    align-items: center;
    justify-content: center;
    border: 2px solid var(--card, #F2EDE4);
  }
  .kosa-ms-nav-badge.visible { display: flex; }

  /* ── Drawer (hamburger menu) ────────────────────────────────── */
  .kosa-ms-drawer-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.45);
    z-index: 1100;
    opacity: 0;
    pointer-events: none;
    transition: opacity .2s;
    display: block;
  }
  .kosa-ms-drawer-backdrop.open {
    opacity: 1;
    pointer-events: auto;
  }
  .kosa-ms-drawer {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    width: min(320px, 85vw);
    background: var(--card, #F2EDE4);
    z-index: 1101;
    transform: translateX(-100%);
    transition: transform .25s cubic-bezier(.4,0,.2,1);
    display: flex;
    flex-direction: column;
    font-family: 'Inter', -apple-system, system-ui, sans-serif;
    padding-top: env(safe-area-inset-top, 0px);
    padding-bottom: env(safe-area-inset-bottom, 0px);
    box-shadow: 2px 0 20px rgba(0,0,0,.15);
  }
  .kosa-ms-drawer.open { transform: translateX(0); }
  .kosa-ms-drawer-header {
    padding: 16px 20px 12px;
    display: flex;
    align-items: center;
    gap: 12px;
    border-bottom: 1px solid var(--border, #D6CFC2);
  }
  .kosa-ms-drawer-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--gold, #B8952A);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 16px;
    flex-shrink: 0;
  }
  .kosa-ms-drawer-user {
    flex: 1;
    min-width: 0;
  }
  .kosa-ms-drawer-name {
    font-size: 14px;
    font-weight: 600;
    color: var(--text, #1A1714);
    line-height: 1.2;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .kosa-ms-drawer-email {
    font-size: 11px;
    color: var(--muted, #7A7268);
    margin-top: 2px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .kosa-ms-drawer-list {
    flex: 1;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 8px 0;
  }
  .kosa-ms-drawer-section {
    font-size: 11px;
    font-weight: 600;
    color: var(--muted, #7A7268);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    padding: 14px 20px 6px;
  }
  .kosa-ms-drawer-item {
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 12px 20px;
    min-height: 48px;
    text-decoration: none;
    color: var(--text, #1A1714);
    font-size: 14px;
    font-weight: 500;
    transition: background .1s;
    border: none;
    background: transparent;
    width: 100%;
    cursor: pointer;
    text-align: left;
  }
  .kosa-ms-drawer-item:active { background: var(--card2, #E2DDD2); }
  .kosa-ms-drawer-item.active {
    background: var(--gold-bg, #EFE7CC);
    color: var(--gold, #B8952A);
  }
  .kosa-ms-drawer-item .kosa-ms-drawer-icon {
    font-size: 20px;
    width: 28px;
    text-align: center;
    flex-shrink: 0;
  }
  .kosa-ms-drawer-divider {
    height: 1px;
    background: var(--border, #D6CFC2);
    margin: 8px 16px;
  }
  .kosa-ms-drawer-footer {
    padding: 12px 20px;
    border-top: 1px solid var(--border, #D6CFC2);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
  }

  /* ── Bottom sheet ───────────────────────────────────────────── */
  .kosa-ms-sheet-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.45);
    z-index: 1200;
    opacity: 0;
    pointer-events: none;
    transition: opacity .2s;
    display: block;
  }
  .kosa-ms-sheet-backdrop.open {
    opacity: 1;
    pointer-events: auto;
  }
  .kosa-ms-sheet {
    position: fixed;
    left: 0; right: 0; bottom: 0;
    background: var(--card, #F2EDE4);
    z-index: 1201;
    border-radius: 16px 16px 0 0;
    transform: translateY(100%);
    transition: transform .25s cubic-bezier(.4,0,.2,1);
    max-height: 85vh;
    display: flex;
    flex-direction: column;
    padding-bottom: env(safe-area-inset-bottom, 0px);
    box-shadow: 0 -4px 20px rgba(0,0,0,.15);
    font-family: 'Inter', -apple-system, system-ui, sans-serif;
  }
  .kosa-ms-sheet.open { transform: translateY(0); }
  .kosa-ms-sheet-handle {
    width: 40px;
    height: 4px;
    background: var(--border-strong, #BDB5A8);
    border-radius: 2px;
    margin: 10px auto 6px;
    flex-shrink: 0;
  }
  .kosa-ms-sheet-header {
    padding: 6px 20px 12px;
    font-size: 16px;
    font-weight: 600;
    color: var(--text, #1A1714);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    flex-shrink: 0;
  }
  .kosa-ms-sheet-body {
    flex: 1;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 0 20px 20px;
  }

  /* ── Auto-padding för body när shell är aktiv ──────────────── */
  html[data-kosa-mobile-shell="1"] body {
    padding-top: calc(52px + env(safe-area-inset-top, 0px)) !important;
    padding-bottom: calc(64px + env(safe-area-inset-bottom, 0px)) !important;
  }

  /* Göm hubb-railen när mobile-shell är aktiv (mutex) */
  html[data-kosa-mobile-shell="1"] #kosa-hubb-rail { display: none !important; }
  html[data-kosa-mobile-shell="1"][data-kosa-rail="1"] body,
  html[data-kosa-mobile-shell="1"][data-kosa-rail="expanded"] body {
    padding-left: 0 !important;
  }

  /* Göm chat-bubblan när mobile-shell är aktiv (kollision med bottom nav) */
  html[data-kosa-mobile-shell="1"] #kosa-chat-bubble,
  html[data-kosa-mobile-shell="1"] #kosa-chat-bubble-panel { display: none !important; }

  /* Body scroll-lock när drawer eller sheet är öppen */
  html[data-kosa-ms-locked="1"] body {
    overflow: hidden;
    overscroll-behavior: contain;
  }
}

/* ═══════════════════════════════════════════════════════════════
   Catch-all mobil-light för verktyg utan egen mobil-CSS
   Selektorer riktar in sig på vanliga KOSA-klasser. Verktyg som har
   egna @media-block för dessa selektorer overridar via specificity.
   Aktiveras endast när data-kosa-mobile-shell="1" på <html>.
═══════════════════════════════════════════════════════════════ */
@media (max-width: 768px) {
  /* Sidopaneler: göms automatiskt (mobile-shell drawer ersätter dem) */
  html[data-kosa-mobile-shell="1"] aside.sidebar,
  html[data-kosa-mobile-shell="1"] .sidebar:not(.kosa-ms-keep),
  html[data-kosa-mobile-shell="1"] .left-rail,
  html[data-kosa-mobile-shell="1"] .right-rail,
  html[data-kosa-mobile-shell="1"] .right-panel:not(.kosa-ms-keep),
  html[data-kosa-mobile-shell="1"] .side-panel:not(.kosa-ms-keep) {
    /* Tools kan opt-out med klassen kosa-ms-keep */
    display: none;
  }

  /* Generic topbar/header: göms NÄR verktyget inte har klassen kosa-ms-keep
     (vi vill ha mobile-shells egen top bar, inte två stacked) */
  html[data-kosa-mobile-shell="1"] header.topbar.kosa-ms-hide,
  html[data-kosa-mobile-shell="1"] .topbar.kosa-ms-hide,
  html[data-kosa-mobile-shell="1"] .app-header.kosa-ms-hide { display: none !important; }

  /* KPI-rader stackar */
  html[data-kosa-mobile-shell="1"] .kpi-row,
  html[data-kosa-mobile-shell="1"] .kpi-grid,
  html[data-kosa-mobile-shell="1"] .kpis,
  html[data-kosa-mobile-shell="1"] .stat-grid {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 10px !important;
  }
  html[data-kosa-mobile-shell="1"] .kpi-card,
  html[data-kosa-mobile-shell="1"] .stat-card { padding: 12px !important; }
  html[data-kosa-mobile-shell="1"] .kpi-card .kpi-value,
  html[data-kosa-mobile-shell="1"] .kpi-card .value { font-size: 18px !important; }

  /* Filter-rad: wrap + minst 36px touch */
  html[data-kosa-mobile-shell="1"] .filter-row,
  html[data-kosa-mobile-shell="1"] .filters,
  html[data-kosa-mobile-shell="1"] .tabs {
    flex-wrap: wrap !important;
    gap: 6px !important;
  }
  html[data-kosa-mobile-shell="1"] .filter-chip,
  html[data-kosa-mobile-shell="1"] .filter-btn,
  html[data-kosa-mobile-shell="1"] .tab {
    min-height: 36px;
    padding: 6px 12px !important;
  }

  /* Tabeller — horisontell scroll om de ligger i en wrapper */
  html[data-kosa-mobile-shell="1"] .table-wrap,
  html[data-kosa-mobile-shell="1"] .table-scroll,
  html[data-kosa-mobile-shell="1"] .data-table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    max-width: 100%;
  }
  html[data-kosa-mobile-shell="1"] table.kosa-table,
  html[data-kosa-mobile-shell="1"] table.data-table,
  html[data-kosa-mobile-shell="1"] table.responsive-table {
    min-width: 600px;
  }
  html[data-kosa-mobile-shell="1"] table.kosa-table td,
  html[data-kosa-mobile-shell="1"] table.kosa-table th,
  html[data-kosa-mobile-shell="1"] table.data-table td,
  html[data-kosa-mobile-shell="1"] table.data-table th {
    padding: 8px 10px !important;
    font-size: 13px;
  }

  /* Modaler/dialogs → bottom-sheet känsla */
  html[data-kosa-mobile-shell="1"] .modal-overlay,
  html[data-kosa-mobile-shell="1"] .dialog-overlay {
    align-items: flex-end !important;
  }
  html[data-kosa-mobile-shell="1"] .modal,
  html[data-kosa-mobile-shell="1"] .modal-content,
  html[data-kosa-mobile-shell="1"] .dialog {
    max-width: 100vw !important;
    width: 100vw !important;
    margin: 0 !important;
    border-radius: 16px 16px 0 0 !important;
    max-height: 90vh;
  }

  /* Knappar — touch-target */
  html[data-kosa-mobile-shell="1"] button:not(.kosa-ms-topbar-btn):not(.kosa-ms-nav-item):not(.kosa-ms-install-hint-close):not(.icon-btn-sm) {
    min-height: 40px;
  }

  /* Formulär: stack två-kol grid */
  html[data-kosa-mobile-shell="1"] .form-grid,
  html[data-kosa-mobile-shell="1"] .form-2col,
  html[data-kosa-mobile-shell="1"] .two-col {
    grid-template-columns: 1fr !important;
  }

  /* Input-storlek — iOS auto-zoom blockeras vid font-size ≥ 16px */
  html[data-kosa-mobile-shell="1"] input[type="text"],
  html[data-kosa-mobile-shell="1"] input[type="email"],
  html[data-kosa-mobile-shell="1"] input[type="number"],
  html[data-kosa-mobile-shell="1"] input[type="search"],
  html[data-kosa-mobile-shell="1"] input[type="tel"],
  html[data-kosa-mobile-shell="1"] input[type="url"],
  html[data-kosa-mobile-shell="1"] input[type="password"],
  html[data-kosa-mobile-shell="1"] select,
  html[data-kosa-mobile-shell="1"] textarea {
    font-size: 16px;
  }

  /* Main-content: ge generösa padding */
  html[data-kosa-mobile-shell="1"] main:not(.kosa-ms-skip-padding),
  html[data-kosa-mobile-shell="1"] .page-main:not(.kosa-ms-skip-padding) {
    padding: 12px !important;
  }

  /* Finance-pattern: .hdr stack vertikalt (RR+BR, Prognos, Budget använder) */
  html[data-kosa-mobile-shell="1"] .hdr {
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 10px !important;
    grid-template-columns: 1fr !important;
  }
  html[data-kosa-mobile-shell="1"] .hdr-right,
  html[data-kosa-mobile-shell="1"] .hdr-actions,
  html[data-kosa-mobile-shell="1"] .hdr-center,
  html[data-kosa-mobile-shell="1"] .hdr-left {
    width: 100%;
    align-items: flex-start !important;
    justify-content: flex-start !important;
    flex-wrap: wrap;
    text-align: left !important;
  }
  html[data-kosa-mobile-shell="1"] .hdr-btn {
    min-height: 40px;
    padding: 8px 14px !important;
  }

  /* Finance-pattern: KPI som flex (inte grid) — stack 50/50 */
  html[data-kosa-mobile-shell="1"] .kpis,
  html[data-kosa-mobile-shell="1"] .kpis-row,
  html[data-kosa-mobile-shell="1"] .kpi-list {
    flex-wrap: wrap !important;
    gap: 8px !important;
  }
  html[data-kosa-mobile-shell="1"] .kpis .kpi,
  html[data-kosa-mobile-shell="1"] .kpis-row .kpi,
  html[data-kosa-mobile-shell="1"] .kpi-list .kpi {
    min-width: calc(50% - 4px) !important;
    flex: 1 1 calc(50% - 4px) !important;
  }

  /* Drill-down modals i finance — fullscreen overlay */
  html[data-kosa-mobile-shell="1"] .drill-modal,
  html[data-kosa-mobile-shell="1"] .drill-overlay {
    width: 100vw !important;
    height: calc(100dvh - 52px - 64px - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px)) !important;
    max-width: 100vw !important;
    max-height: none !important;
    border-radius: 0 !important;
    top: calc(52px + env(safe-area-inset-top, 0px)) !important;
    left: 0 !important;
    bottom: calc(64px + env(safe-area-inset-bottom, 0px)) !important;
  }
}

@media (max-width: 480px) {
  html[data-kosa-mobile-shell="1"] .kpi-row,
  html[data-kosa-mobile-shell="1"] .kpi-grid,
  html[data-kosa-mobile-shell="1"] .kpis,
  html[data-kosa-mobile-shell="1"] .stat-grid {
    grid-template-columns: 1fr !important;
  }
}

/* ── PWA-install hint (visas en gång, kan dismissas) ─────────── */
@media (max-width: 768px) {
  .kosa-ms-install-hint {
    position: fixed;
    bottom: calc(80px + env(safe-area-inset-bottom, 0px));
    left: 12px;
    right: 12px;
    background: var(--card, #F2EDE4);
    border: 1px solid var(--gold-border, #D8C282);
    border-radius: 12px;
    padding: 12px 14px;
    z-index: 1050;
    box-shadow: 0 4px 16px rgba(0,0,0,.12);
    display: none;
    align-items: center;
    gap: 10px;
    font-family: 'Inter', -apple-system, system-ui, sans-serif;
  }
  .kosa-ms-install-hint.visible { display: flex; }
  .kosa-ms-install-hint-text {
    flex: 1;
    font-size: 12px;
    color: var(--text, #1A1714);
    line-height: 1.3;
  }
  .kosa-ms-install-hint-text b { color: var(--gold, #B8952A); }
  .kosa-ms-install-hint-close {
    width: 32px;
    height: 32px;
    border: none;
    background: transparent;
    color: var(--muted, #7A7268);
    font-size: 18px;
    cursor: pointer;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .kosa-ms-install-hint-close:active { background: var(--card2, #E2DDD2); }
}
