/* Galatheus-dash Mobile Shell — all rules apply only below 768px.
 * Desktop (>=769px) is unaffected because every rule in this file
 * sits inside the @media block below.
 *
 * Sections:
 *  - Mobile-only visibility helpers
 *  - Mobile shell layout (top bar, content, bottom tab bar)
 *  - Bottom tab bar
 *  - Chat tab (full-bleed)
 *  - Apps tab (list + workspace + picker + sheet)
 *  - More tab (menu list)
 *  - Responsive adaptation (grids, tables, forms, modals, chart-data)
 */

@media (max-width: 768px) {
  :root {
    --m-topbar-h: 44px;
    --m-tabbar-h: 56px;
    --m-safe-bottom: env(safe-area-inset-bottom, 0px);
    --m-pad: 12px;
  }

  /* Helper: hide on mobile */
  .desktop-only { display: none !important; }

  /* --- Bottom tab bar --- */
  .m-tabbar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    background: rgba(7, 12, 23, 0.96);
    border-top: 1px solid var(--line);
    backdrop-filter: blur(8px);
    height: calc(var(--m-tabbar-h) + var(--m-safe-bottom));
    padding-bottom: var(--m-safe-bottom);
    z-index: 200;
  }
  .m-tabbar .m-tab {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    padding: 6px 0 8px;
    font-size: 10px;
    font-weight: 600;
    color: var(--muted);
    cursor: pointer;
    border: none;
    background: none;
    position: relative;
    min-height: 44px;
  }
  .m-tabbar .m-tab .m-tab-icon {
    font-size: 18px;
    line-height: 1;
  }
  .m-tabbar .m-tab.active { color: var(--brand); }
  .m-tabbar .m-tab.active::before {
    content: "";
    position: absolute;
    top: 0;
    left: 20%;
    right: 20%;
    height: 2px;
    background: var(--brand);
  }
  .m-tabbar .m-tab:focus-visible {
    outline: none;
    box-shadow: inset 0 0 0 2px rgba(74,168,255,0.4);
  }

  /* Lock shell to viewport so body doesn't scroll (sticky header would get
     pushed off-screen). Content scrolls internally. */
  html, body {
    height: 100dvh;
    overflow: hidden;
  }
  .shell-layout {
    height: 100dvh;
    max-height: 100dvh;
    overflow: hidden;
  }

  /* --- Top bar (restyled from existing .shell-header on mobile) --- */
  .shell-header {
    height: var(--m-topbar-h);
    padding: 0 var(--m-pad);
    gap: 8px;
    position: sticky;
    top: 0;
    z-index: 100;
    background: rgba(7, 12, 23, 0.92);
    backdrop-filter: blur(8px);
    border-bottom: 1px solid var(--line);
    display: flex;
    align-items: center;
  }
  .shell-header .brand {
    font-size: 13px;
    letter-spacing: 0;
    flex: 1;
    text-align: center;
  }
  .shell-header .shell-actions { gap: 6px; }

  /* Hide the hamburger — bottom nav replaces it */
  #mobile-menu-btn,
  .mobile-menu-btn { display: none !important; }

  /* Hide the desktop chat-toggle button on mobile — chat has its own tab */
  #chat-toggle { display: none !important; }

  /* Mobile back button (shown only when inside a sub-view) */
  .m-back {
    display: none;
    background: none;
    border: 1px solid var(--line);
    color: var(--muted);
    width: 32px;
    height: 32px;
    border-radius: 6px;
    font-size: 18px;
    line-height: 1;
    padding: 0;
    cursor: pointer;
  }
  .m-back.visible { display: inline-flex; align-items: center; justify-content: center; }
  .m-back:hover { color: var(--text); border-color: var(--muted); }

  .shell-header .m-title {
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text);
    flex: 1;
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .shell-header .m-overflow {
    background: none;
    border: none;
    color: var(--muted);
    font-size: 18px;
    width: 32px;
    height: 32px;
    cursor: pointer;
    padding: 0;
  }

  /* "+" button in header; only visible when the Tickets tab is active. */
  .shell-header .m-ticket-add {
    display: none;
    background: none;
    border: 1px solid var(--line);
    color: var(--text);
    width: 32px;
    height: 32px;
    border-radius: 6px;
    font-size: 20px;
    line-height: 1;
    cursor: pointer;
    padding: 0;
  }
  body.m-tickets-active .shell-header .m-ticket-add {
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }
  .shell-header .m-ticket-add:hover { color: var(--brand); border-color: var(--brand); }

  /* --- Force single-column layout on mobile ---
     The desktop .shell-layout is a 6-column grid (activity/sidebar/gutter/center/
     gutter/chat). Children carry `grid-column: N` hints. On mobile we collapse
     to a single column, but children's grid-column hints create implicit
     columns unless we reset them. Also: shell-resize.js persists --sidebar-w
     and --chat-w inline on .shell-layout from localStorage — these leak into
     mobile grid tracks if not zeroed. --- */
  .shell-layout {
    --activity-w: 0 !important;
    --sidebar-w: 0 !important;
    --gutter-w: 0 !important;
    --chat-w: 0 !important;
    grid-template-columns: 1fr !important;
    grid-template-rows: auto minmax(0, 1fr) !important;
  }
  .shell-layout > * {
    grid-column: 1 / -1 !important;
  }
  .shell-gutter, .shell-gutter-left, .shell-gutter-right {
    display: none !important;
  }

  /* --- Tab surface switching: hide all three panels by default;
     each body.m-*-active class re-enables one.
     !important overrides workspace.css's legacy
     `.shell-layout.chat-open .shell-chat { display: flex }` rule which
     AppShell.init adds on page load. --- */
  .shell-sidebar, .shell-center, .shell-chat { display: none !important; }

  /* --- Chat tab (full-bleed on mobile) --- */
  body.m-chat-active .shell-chat { display: flex !important; }

  /* Hide the activity bar (vertical icon rail) on mobile — the bottom tab bar
     replaces it as the primary navigation surface. */
  .shell-activity { display: none !important; }

  /* Hide ChatPanel's and ChatAgent's internal headers — the mobile top bar
     already shows "Chat". */
  .shell-chat .chat-panel-header,
  .shell-chat .agent-chat-header { display: none !important; }

  /* Session controls row (ChatPanel): tighten spacing, allow wrapping */
  .shell-chat .cp-actions {
    display: flex;
    gap: 6px;
    padding: 6px var(--m-pad);
    flex-wrap: wrap;
  }
  .shell-chat .cp-session-select { max-width: 100%; flex: 1 1 auto; }

  /* Hide the app-context-panel by default on mobile (duplicates the app
     indicator in chat). In the Apps activity, it's reachable as a bottom-sheet
     modal via the Edit-goals affordance when workspace-gradient is available. */
  #app-context-slot,
  .shell-chat .ctx-panel,
  .shell-chat .app-context-panel { display: none !important; }

  /* Displayed-as-modal state (unhides and repositions as bottom sheet) */
  #app-context-slot.m-as-modal {
    display: block !important;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    max-height: 88dvh;
    overflow-y: auto;
    background: var(--panel);
    border-top: 1px solid var(--line);
    border-radius: 16px 16px 0 0;
    padding: 10px var(--m-pad) calc(12px + var(--m-safe-bottom));
    z-index: 280;
    box-shadow: 0 -8px 24px rgba(0, 0, 0, 0.5);
  }
  #app-context-slot.m-as-modal .ctx-panel,
  #app-context-slot.m-as-modal .app-context-panel {
    display: block !important;
  }
  .m-ctx-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 270;
  }
  .m-ctx-backdrop.visible { display: block; }
  .m-ctx-modal-close {
    position: sticky;
    top: 0;
    float: right;
    background: var(--bg);
    border: 1px solid var(--line);
    color: var(--muted);
    width: 32px;
    height: 32px;
    border-radius: 50%;
    font-size: 16px;
    cursor: pointer;
    z-index: 1;
  }
  .m-edit-goals-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: var(--panel);
    border: 1px solid var(--brand);
    color: var(--brand);
    padding: 8px 14px;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 600;
    min-height: 44px;
    cursor: pointer;
    margin: 8px 0 12px;
  }
  .m-edit-goals-btn:hover {
    background: rgba(74,168,255,0.1);
  }

  /* Hide #agent-app-select everywhere by default on mobile.
     The hoisted copy inside #m-chat-select-slot (moved by mobile-shell.js
     into the header) is shown by the rule below. The original copy inside
     .agent-input-row stays hidden, removing the duplicate. */
  #agent-app-select { display: none !important; }
  .m-chat-select-slot > #agent-app-select { display: block !important; }

  /* App-select slot in the mobile header (chat tab only). Replaces the
     .m-title on the chat tab so the dropdown sits flush against the header
     with no whitespace above it. */
  .m-chat-select-slot { display: none; }
  body.m-chat-active .m-chat-select-slot {
    display: flex;
    flex: 1;
    min-width: 0;
    align-items: center;
  }
  body.m-chat-active .shell-header .m-title { display: none; }
  .m-chat-select-slot > #agent-app-select {
    width: 100%;
    min-height: 32px;
    font-size: 14px;
    padding: 4px 8px;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 6px;
    color: var(--text);
  }

  /* Chat agent container fills the chat panel */
  .shell-chat .agent-chat {
    display: flex;
    flex-direction: column;
    height: 100%;
    min-width: 0;
  }
  .shell-chat .agent-chat-messages {
    flex: 1;
    overflow-y: auto;
    min-height: 0;
  }

  /* Chat agent composer: fit viewport, stack if too narrow */
  .shell-chat .agent-chat-input {
    padding: 6px var(--m-pad) calc(6px + var(--m-safe-bottom));
    min-width: 0;
  }
  .shell-chat .agent-input-row {
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
    min-width: 0;
  }
  .shell-chat .agent-input-row > #agent-app-select,
  .shell-chat .agent-input-row > select {
    flex: 1 1 100%;
    max-width: 100%;
    min-width: 0;
  }
  .shell-chat .agent-input-row > #agent-input {
    flex: 1 1 auto;
    min-width: 0;
    width: auto !important;
  }
  .shell-chat .agent-input-row > #agent-send {
    flex: 0 0 auto;
  }
  body.m-chat-active .shell-layout { grid-template-columns: 1fr; }

  .shell-layout .shell-chat {
    grid-column: 1 / -1;
    border-left: none;
    height: calc(100dvh - var(--m-topbar-h) - var(--m-tabbar-h) - var(--m-safe-bottom));
    max-height: calc(100dvh - var(--m-topbar-h) - var(--m-tabbar-h) - var(--m-safe-bottom));
  }

  /* Composer sticks to bottom of chat panel, above tab bar */
  .shell-chat .agent-chat-input {
    position: sticky;
    bottom: 0;
    background: var(--panel);
    padding: 8px var(--m-pad);
    border-top: 1px solid var(--line);
  }

  /* --- Apps tab (sidebar as list) --- */
  body.m-apps-active .shell-sidebar {
    display: flex !important;
    position: static;
    width: 100%;
    height: calc(100dvh - var(--m-topbar-h) - var(--m-tabbar-h) - var(--m-safe-bottom));
    max-height: calc(100dvh - var(--m-topbar-h) - var(--m-tabbar-h) - var(--m-safe-bottom));
    overflow-y: auto;
    padding-top: 0;
    box-shadow: none;
    border-right: none;
    border-top: none;
  }

  /* Center panel scrolls internally */
  body.m-tickets-active .shell-center,
  body.m-more-active .shell-center,
  body.m-apps-workspace .shell-center {
    height: calc(100dvh - var(--m-topbar-h) - var(--m-tabbar-h) - var(--m-safe-bottom));
    max-height: calc(100dvh - var(--m-topbar-h) - var(--m-tabbar-h) - var(--m-safe-bottom));
    overflow-y: auto;
  }
  body.m-apps-active .shell-layout { grid-template-columns: 1fr; }

  /* When an app is selected in the Apps tab, switch from the list view
     to the workspace view (sidebar → hidden, center → visible). Controlled
     by m-apps-workspace body class set by mobile-shell.js on selectItem. */
  body.m-apps-active.m-apps-workspace .shell-sidebar { display: none !important; }
  body.m-apps-active.m-apps-workspace .shell-center {
    display: block !important;
    width: 100%;
  }

  /* Sidebar items as list rows (44px min tap target) */
  body.m-apps-active .shell-sidebar .sb-item,
  body.m-apps-active .shell-sidebar .sb-app {
    min-height: 44px;
    padding: 10px var(--m-pad);
    font-size: 15px;
    border-bottom: 1px solid rgba(255,255,255,0.04);
  }
  body.m-apps-active .shell-sidebar .sb-tenant {
    padding: 10px var(--m-pad) 4px;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--muted);
  }

  /* --- Tickets / More tabs use center panel full-width --- */
  body.m-tickets-active .shell-center,
  body.m-more-active .shell-center {
    display: block !important;
    width: 100%;
  }
  body.m-tickets-active .shell-layout,
  body.m-more-active .shell-layout {
    grid-template-columns: 1fr;
  }

  /* --- Tickets: Create Ticket panel is hidden by default on mobile; shown
     as a bottom-sheet modal when the user taps the "+" button in the header. --- */
  .tp-create-panel { display: none; }
  .tp-create-panel.m-as-modal {
    display: block;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    max-height: 82dvh;
    overflow-y: auto;
    z-index: 310;
    margin: 0;
    border-radius: 14px 14px 0 0;
    border: 1px solid var(--line);
    background: var(--panel);
    padding: 14px var(--m-pad) calc(14px + var(--m-safe-bottom));
  }
  .tp-create-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.45);
    z-index: 300;
  }
  .tp-create-backdrop.visible { display: block; }
  .tp-create-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
  }
  .tp-create-header h2 { margin: 0; }
  .tp-create-close {
    border: 1px solid var(--line);
    background: transparent;
    color: var(--muted);
    border-radius: 6px;
    width: 32px;
    height: 32px;
    font-size: 20px;
    line-height: 1;
  }

  /* --- More tab menu list --- */
  .m-more-list {
    padding: 0;
  }
  .m-more-list .m-more-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px var(--m-pad);
    border-bottom: 1px solid rgba(255,255,255,0.04);
    font-size: 15px;
    color: var(--text);
    cursor: pointer;
    min-height: 48px;
  }
  .m-more-list .m-more-item .m-more-chev {
    margin-left: auto;
    color: var(--muted);
    font-size: 18px;
  }
  .m-more-list .m-more-item:hover {
    background: rgba(255,255,255,0.03);
  }

  /* --- Workspace: hide desktop sub-tab bar, show picker --- */
  .ws-tab-bar { display: none !important; }

  .m-picker-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px var(--m-pad);
    background: rgba(7, 12, 23, 0.6);
    border-bottom: 1px solid var(--line);
    cursor: pointer;
    min-height: 44px;
    position: sticky;
    top: var(--m-topbar-h);
    z-index: 90;
  }
  .m-picker-bar:focus-visible {
    outline: none;
    box-shadow: inset 0 0 0 2px rgba(74,168,255,0.4);
  }
  .m-picker-label {
    font-size: 15px;
    font-weight: 600;
    color: var(--text);
  }
  .m-picker-caret {
    color: var(--muted);
    font-size: 16px;
    transition: transform 0.15s;
  }
  .m-picker-bar.open .m-picker-caret {
    transform: rotate(180deg);
  }

  /* --- Bottom sheet (section picker) --- */
  .m-sheet-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 250;
    display: none;
    opacity: 0;
    transition: opacity 0.18s ease;
  }
  .m-sheet-backdrop.visible {
    display: block;
    opacity: 1;
  }

  .m-sheet {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    max-height: 78dvh;
    background: var(--panel);
    border-top: 1px solid var(--line);
    border-radius: 16px 16px 0 0;
    z-index: 260;
    padding: 10px 0 calc(8px + var(--m-safe-bottom));
    overflow-y: auto;
    transform: translateY(100%);
    transition: transform 0.22s ease;
  }
  .m-sheet.visible {
    transform: translateY(0);
  }
  .m-sheet-handle {
    width: 36px;
    height: 4px;
    background: var(--line);
    border-radius: 2px;
    margin: 0 auto 10px;
  }
  .m-sheet-title {
    font-size: 11px;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    padding: 0 var(--m-pad) 8px;
  }
  .m-sheet-item {
    padding: 12px var(--m-pad);
    font-size: 15px;
    color: var(--text);
    border-bottom: 1px solid rgba(255,255,255,0.04);
    display: flex;
    align-items: center;
    justify-content: space-between;
    cursor: pointer;
    min-height: 48px;
  }
  .m-sheet-item.active { color: var(--brand); }
  .m-sheet-item .m-sheet-check {
    color: var(--brand);
    font-size: 14px;
    opacity: 0;
  }
  .m-sheet-item.active .m-sheet-check {
    opacity: 1;
  }

  /* --- Responsive grids → single column --- */
  .ws-crd-grid,
  .ws-ops-form {
    grid-template-columns: 1fr !important;
  }

  /* --- Generic tables → stacked cards (applies to every table in shell) --- */
  .shell-center table,
  .shell-chat table,
  .workspace-center table {
    display: block;
    max-width: 100%;
    overflow-x: auto;
  }
  .shell-center table thead,
  .shell-chat table thead,
  .workspace-center table thead { display: none; }
  .shell-center table tbody,
  .shell-chat table tbody,
  .workspace-center table tbody { display: block; }
  .shell-center table tr,
  .shell-chat table tr,
  .workspace-center table tr {
    display: block;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 10px 12px;
    margin-bottom: 8px;
    min-width: 0;
  }
  .shell-center table td,
  .shell-chat table td,
  .workspace-center table td {
    display: flex;
    justify-content: space-between;
    gap: 8px;
    padding: 4px 0;
    border: none;
    font-size: 14px;
    min-width: 0;
    word-break: break-word;
  }
  .shell-center table td::before,
  .shell-chat table td::before,
  .workspace-center table td::before {
    content: attr(data-label);
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--muted);
    flex: 0 0 auto;
  }

  /* Flex rows in panels: wrap, full-width */
  .sp-conn-row {
    flex-direction: column !important;
    align-items: stretch !important;
    min-width: 0;
    padding: 8px var(--m-pad) !important;
  }
  .sp-conn-row > * { min-width: 0; }

  /* Inline btn/header rows that overflow: wrap */
  .panel-header-row,
  .ws-app-header,
  .ws-site-links {
    flex-wrap: wrap;
    min-width: 0;
  }

  /* Sub-tab bars: horizontal scroll */
  .sub-tabs,
  .sub-tab-bar {
    display: flex;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    flex-wrap: nowrap;
    padding-bottom: 2px;
  }
  .sub-tab, .sub-tabs .sub-tab {
    flex: 0 0 auto;
    white-space: nowrap;
  }

  /* --- Chart-data table → stacked cards (dash uses /api/chart-data, not Grafana) --- */
  table.cd-table,
  table.cd-table thead,
  table.cd-table tbody,
  table.cd-table tr,
  table.cd-table td {
    display: block;
  }
  table.cd-table thead { display: none; }
  table.cd-table tr {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 10px 12px;
    margin-bottom: 8px;
  }
  table.cd-table td {
    padding: 4px 0;
    border: none;
    font-size: 14px;
  }
  table.cd-table .cd-metric-name {
    font-weight: 600;
    color: var(--text);
  }
  table.cd-table .cd-metric-spark svg,
  table.cd-table svg.cd-sparkline {
    width: 100%;
    height: 40px;
  }

  /* --- Forms: stack, full-width, ≥44px tap targets --- */
  input[type="text"],
  input[type="email"],
  input[type="password"],
  input[type="search"],
  input[type="url"],
  input[type="number"],
  input[type="tel"],
  select,
  textarea {
    width: 100% !important;
    max-width: 100% !important;
    min-height: 44px;
    font-size: 16px; /* prevents iOS zoom-on-focus */
    padding: 10px 12px;
  }

  button, .btn, input[type="submit"] {
    min-height: 44px;
    padding: 10px 16px;
  }

  .sim-row, .ws-ops-form > * {
    flex-direction: column;
    align-items: stretch;
  }
  .sim-row label,
  .ws-ops-form label {
    flex: 0 0 auto;
    text-align: left;
    white-space: normal;
    margin-bottom: 4px;
  }
  .sim-row input {
    max-width: 100%;
  }

  /* --- Modals → bottom sheets --- */
  .gradient-hint-modal .hint-modal-card,
  .sc-modal .sc-modal-card {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    top: auto;
    width: 100% !important;
    max-width: 100% !important;
    max-height: 85dvh;
    border-radius: 16px 16px 0 0;
    margin: 0;
    padding: 16px var(--m-pad) calc(16px + var(--m-safe-bottom));
  }

  /* --- Panel padding tightened --- */
  .panel,
  .workspace-center .panel,
  .shell-center .panel {
    padding: var(--m-pad);
  }

  main {
    padding: var(--m-pad);
  }

  /* --- Overflow safety --- */
  pre, code {
    overflow-x: auto;
    max-width: 100%;
    word-break: break-word;
  }
  .long-id, .mono-id {
    word-break: break-all;
  }
}

@media (min-width: 769px) {
  /* Helper: hide on desktop */
  .mobile-only { display: none !important; }
}
