/* =====================================================
   AKASH DASHBOARD - FULL MOBILE RESPONSIVE LAYER
   Keeps desktop layout intact and adds a touch-friendly
   drawer/navigation and responsive module layouts.
===================================================== */

html, body {
  width: 100%;
  max-width: 100%;
  overflow-x: hidden;
}

img, canvas, video, iframe, svg {
  max-width: 100%;
}

.main,
.main > section,
#dashboardCards,
#taskArea,
.panel,
.card {
  min-width: 0;
  max-width: 100%;
}

/* Mobile menu controls are hidden on desktop. */
.mobile-menu-toggle,
.mobile-menu-overlay {
  display: none;
}

/* Prevent long labels/URLs from forcing the page wider. */
.main,
.main * {
  overflow-wrap: anywhere;
}

button,
input,
select,
textarea {
  max-width: 100%;
}

@media (max-width: 700px) {
  :root {
    --mobile-gutter: 12px;
    --mobile-sidebar-width: min(86vw, 330px);
  }

  body {
    width: 100%;
    min-width: 0;
    overflow-x: hidden;
    -webkit-text-size-adjust: 100%;
  }

  /* App must remain a full-width page while the sidebar becomes a drawer. */
  #app.authenticated,
  .app {
    display: block !important;
    width: 100%;
    min-width: 0;
    min-height: 100vh;
  }

  /* -----------------------------------------------
     SIDEBAR / DRAWER
  ----------------------------------------------- */
  .sidebar {
    position: fixed !important;
    inset: 0 auto 0 0 !important;
    width: var(--mobile-sidebar-width) !important;
    max-width: 330px;
    height: 100dvh !important;
    min-height: 100dvh !important;
    max-height: none !important;
    padding: 18px 12px 14px !important;
    transform: translate3d(-105%, 0, 0);
    transition: transform .24s ease, box-shadow .24s ease;
    box-shadow: 12px 0 38px rgba(15, 23, 42, .24);
    overflow: hidden !important;
    z-index: 10001 !important;
    will-change: transform;
  }

  body.mobile-menu-open .sidebar {
    transform: translate3d(0, 0, 0);
    box-shadow: 12px 0 45px rgba(15, 23, 42, .32);
  }

  .brand {
    padding: 2px 8px 14px !important;
    font-size: 19px !important;
  }

  .menu-search-wrap {
    position: sticky;
    top: 0;
    padding-bottom: 10px !important;
  }

  .menu-search {
    height: 44px !important;
    font-size: 14px !important;
  }

  .nav {
    max-height: none !important;
    flex: 1 1 auto !important;
    overflow-y: auto !important;
    padding-bottom: 8px;
    -webkit-overflow-scrolling: touch;
  }

  .nav button {
    min-height: 44px;
    padding: 11px 12px !important;
    margin: 2px 0 !important;
    font-size: 14px !important;
    gap: 10px !important;
  }

  .nav-icon {
    width: 20px !important;
    height: 20px !important;
    flex-basis: 20px !important;
  }

  .nav-icon svg {
    width: 19px !important;
    height: 19px !important;
  }

  .logout {
    min-height: 44px;
    margin-top: 8px !important;
  }

  /* Darkened area outside drawer. */
  .mobile-menu-overlay {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, .48);
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition: opacity .2s ease, visibility .2s ease;
    z-index: 10000;
    -webkit-backdrop-filter: blur(2px);
    backdrop-filter: blur(2px);
  }

  .mobile-menu-overlay.show {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
  }

  /* -----------------------------------------------
     MOBILE MENU BUTTON
  ----------------------------------------------- */
  .mobile-menu-toggle {
    position: fixed;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 7px;
    top: max(10px, env(safe-area-inset-top));
    left: 10px;
    min-width: 46px;
    height: 44px;
    padding: 0 11px;
    border: 1px solid rgba(148, 163, 184, .55);
    border-radius: 11px;
    background: rgba(255, 255, 255, .94);
    color: #172033;
    box-shadow: 0 5px 18px rgba(15, 23, 42, .13);
    cursor: pointer;
    z-index: 10002;
    font: inherit;
    font-size: 12px;
    font-weight: 800;
    touch-action: manipulation;
  }

  .mobile-menu-toggle:active {
    transform: scale(.97);
  }

  .mobile-menu-toggle-icon {
    display: inline-flex;
    flex-direction: column;
    justify-content: center;
    gap: 4px;
    width: 18px;
  }

  .mobile-menu-toggle-icon span {
    display: block;
    width: 18px;
    height: 2px;
    border-radius: 3px;
    background: currentColor;
    transition: transform .2s ease, opacity .2s ease;
  }

  body.mobile-menu-open .mobile-menu-toggle-icon span:nth-child(1) {
    transform: translateY(6px) rotate(45deg);
  }

  body.mobile-menu-open .mobile-menu-toggle-icon span:nth-child(2) {
    opacity: 0;
  }

  body.mobile-menu-open .mobile-menu-toggle-icon span:nth-child(3) {
    transform: translateY(-6px) rotate(-45deg);
  }

  body.mobile-menu-open .mobile-menu-toggle {
    background: #fff;
  }

  /* -----------------------------------------------
     MAIN / HEADER
  ----------------------------------------------- */
  .main {
    width: 100% !important;
    max-width: none !important;
    margin-left: 0 !important;
    padding: 64px var(--mobile-gutter) 18px !important;
    min-width: 0 !important;
  }

  .top {
    width: 100%;
    margin-bottom: 14px !important;
    gap: 12px !important;
    align-items: stretch !important;
  }

  .top-left {
    width: 100%;
    min-width: 0;
  }

  .top h1 {
    font-size: clamp(21px, 6vw, 27px) !important;
    line-height: 1.2;
    padding-left: 2px;
  }

  #todayLabel {
    line-height: 1.4;
  }

  .top-account-actions {
    width: 100% !important;
    display: grid !important;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 7px !important;
  }

  .top-account-actions .top-account-btn {
    width: 100%;
    min-width: 0 !important;
    min-height: 42px;
    padding: 9px 8px !important;
    font-size: 12px !important;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .top-account-actions .ai-assistant-btn {
    grid-column: 1 / -1;
  }

  .theme-popover {
    position: fixed !important;
    top: 112px !important;
    right: 12px !important;
    left: 12px !important;
    width: auto !important;
    max-width: none !important;
    z-index: 10020 !important;
  }

  /* -----------------------------------------------
     DASHBOARD CARDS
  ----------------------------------------------- */
  .cards {
    width: 100%;
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 9px !important;
    margin-bottom: 14px !important;
  }

  .card {
    width: 100%;
    min-width: 0;
    padding: 13px !important;
    border-radius: 11px;
  }

  .card .n {
    font-size: clamp(20px, 6vw, 25px);
  }

  /* -----------------------------------------------
     PANELS / TOOLBARS / BUTTONS
  ----------------------------------------------- */
  .panel {
    width: 100%;
    min-width: 0;
    border-radius: 11px;
  }

  .toolbar {
    width: 100%;
    padding: 11px !important;
    gap: 8px !important;
    flex-wrap: wrap !important;
  }

  .toolbar input,
  .toolbar select,
  .toolbar .search {
    width: 100%;
    min-width: 0 !important;
    flex: 1 1 100% !important;
    height: 42px;
  }

  .btn {
    min-height: 42px;
    touch-action: manipulation;
  }

  .task-toolbar-head {
    width: 100%;
  }

  .task-actions-row {
    width: 100% !important;
    display: grid !important;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 7px !important;
  }

  .task-actions-row .btn {
    width: 100%;
    min-width: 0 !important;
    min-height: 42px;
    flex: none !important;
  }

  .task-filter-total {
    width: 100%;
    white-space: normal !important;
  }

  .task-quick-filters {
    padding: 10px !important;
    gap: 6px !important;
    overflow-x: auto;
    flex-wrap: nowrap !important;
    -webkit-overflow-scrolling: touch;
  }

  .task-filter-btn {
    flex: 0 0 auto;
    min-height: 38px;
    white-space: nowrap;
  }

  /* -----------------------------------------------
     TABLES: preserve all data without breaking page
  ----------------------------------------------- */
  .table-wrap,
  .daily-expenses-table-wrap,
  .daily-expenses-table-container {
    width: 100%;
    max-width: 100%;
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
  }

  table {
    max-width: none;
  }

  th,
  td {
    white-space: normal;
  }

  /* -----------------------------------------------
     GENERIC MODULE CONTAINERS
  ----------------------------------------------- */
  .main > section {
    width: 100% !important;
    min-width: 0 !important;
  }

  .main > section > .panel,
  .main > section .panel {
    max-width: 100%;
    min-width: 0;
  }

  .watermark-layout,
  .image-tool-grid,
  .resize-workspace-grid,
  .wm-compress-grid,
  .diff-tool-layout,
  .daily-expenses-top-grid,
  .daily-expenses-filterbar {
    width: 100% !important;
    min-width: 0 !important;
  }

  .watermark-layout,
  .image-tool-grid,
  .resize-workspace-grid,
  .wm-compress-grid,
  .diff-tool-layout {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  .watermark-layout > *,
  .image-tool-grid > *,
  .resize-workspace-grid > *,
  .wm-compress-grid > *,
  .diff-tool-layout > * {
    min-width: 0 !important;
    max-width: 100%;
  }

  /* Tool upload zones and content boxes */
  .wm-upload,
  .image-upload {
    min-height: 145px !important;
    padding: 20px 12px !important;
  }

  .wm-file,
  .image-file,
  .image-to-pdf-file {
    min-width: 0;
    max-width: 100%;
  }

  .wm-file-name,
  .image-file-main,
  .image-to-pdf-file span {
    min-width: 0;
  }

  .wm-range-row,
  .wm-color-row,
  .image-size-target {
    max-width: 100%;
  }

  .wm-range-row .wm-number-input {
    width: 70px !important;
    flex-basis: 70px !important;
  }

  /* -----------------------------------------------
     IMAGE / PDF / OCR / DIFF
  ----------------------------------------------- */
  .image-two,
  .image-three,
  .image-to-pdf-settings {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  .image-actions,
  .image-to-pdf-actions,
  .pdf-page-remove-actions,
  .pdf-ocr-actions,
  .wm-compress-actions,
  .diff-actions {
    width: 100%;
    gap: 7px;
  }

  .image-actions .btn,
  .image-to-pdf-actions .btn,
  .pdf-page-remove-actions .btn,
  .pdf-ocr-actions .btn,
  .wm-compress-actions .btn,
  .diff-actions .btn {
    flex: 1 1 140px;
    min-width: 0 !important;
  }

  .pdf-page-grid {
    width: 100%;
    grid-template-columns: repeat(3, minmax(0, 1fr)) !important;
  }

  .pdf-ocr-text {
    width: 100%;
    min-width: 0;
  }

  .diff-output,
  .diff-editor,
  .diff-preview-shell,
  .diff-preview-frame {
    max-width: 100%;
  }

  /* -----------------------------------------------
     GOALS
  ----------------------------------------------- */
  .goals-grid,
  .goal-form-grid,
  .goal-two-col {
    width: 100%;
    min-width: 0;
    grid-template-columns: minmax(0, 1fr) !important;
  }

  .goals-tabs {
    width: 100%;
    min-width: 0 !important;
    overflow-x: auto;
    flex-wrap: nowrap !important;
    -webkit-overflow-scrolling: touch;
  }

  .goals-tabs > * {
    flex: 0 0 auto;
  }

  /* -----------------------------------------------
     DAILY EXPENSES
  ----------------------------------------------- */
  .daily-expenses-summary {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  .daily-expenses-form-grid {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  .daily-expenses-form-grid .full {
    grid-column: auto !important;
  }

  .daily-expenses-filterbar {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  /* Keep data tables usable by horizontal scrolling rather than
     shrinking numbers into unreadable columns. */
  .daily-expenses-table {
    min-width: 780px;
  }

  /* -----------------------------------------------
     MODALS
  ----------------------------------------------- */
  .modal {
    padding: 10px !important;
    align-items: flex-start !important;
    overflow-y: auto;
  }

  .modal-box,
  .profile-modal-box,
  .settings-modal-box,
  .single-login-card {
    width: 100% !important;
    max-width: 100% !important;
    max-height: calc(100dvh - 20px);
    margin: 0 auto;
    overflow-y: auto;
    border-radius: 14px;
  }

  .form {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  .field.full {
    grid-column: auto !important;
  }

  .modal-actions,
  .profile-actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 7px;
  }

  .modal-actions .btn,
  .profile-actions .btn {
    width: 100%;
  }

  /* AI assistant and document guide must never exceed viewport. */
  #aiAssistantPanel,
  .ai-assistant-panel,
  .document-guide-modal {
    max-width: 100vw !important;
  }

  /* Safe areas on phones with notches/home indicators. */
  .main {
    padding-bottom: max(18px, env(safe-area-inset-bottom));
  }
}

/* Small phones */
@media (max-width: 420px) {
  .main {
    padding-left: 9px !important;
    padding-right: 9px !important;
  }

  .cards {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  .top-account-actions {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  .top-account-actions .ai-assistant-btn {
    grid-column: auto;
  }

  .task-actions-row {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  .image-actions .btn,
  .image-to-pdf-actions .btn,
  .pdf-page-remove-actions .btn,
  .pdf-ocr-actions .btn,
  .wm-compress-actions .btn,
  .diff-actions .btn {
    flex-basis: 100%;
  }

  .pdf-page-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
  }

  .mobile-menu-toggle {
    left: 8px;
    top: max(8px, env(safe-area-inset-top));
  }
}

/* Landscape phones: use the extra horizontal space without
   returning to the desktop sidebar. */
@media (max-width: 900px) and (orientation: landscape) and (max-height: 600px) {
  .sidebar {
    width: min(54vw, 330px) !important;
  }

  .main {
    padding-top: 60px !important;
  }

  .cards {
    grid-template-columns: repeat(3, minmax(0, 1fr)) !important;
  }

  .top {
    flex-direction: row !important;
    align-items: center !important;
  }

  .top-account-actions {
    width: auto !important;
    display: flex !important;
    flex-wrap: wrap;
    justify-content: flex-end;
  }

  .top-account-actions .ai-assistant-btn {
    grid-column: auto;
  }

  .top-account-actions .top-account-btn {
    min-width: 92px;
  }

  .task-actions-row {
    grid-template-columns: repeat(5, minmax(0, 1fr)) !important;
  }

  .task-actions-row .btn {
    min-width: 0;
  }
}

/* Desktop/tablet: never show the mobile drawer controls. */
@media (min-width: 701px) {
  .mobile-menu-toggle,
  .mobile-menu-overlay {
    display: none !important;
  }

  body.mobile-menu-open {
    overflow: auto;
  }
}

/* =====================================================
   MOBILE ACCOUNT / UTILITY MENU
   Profile, Theme, Documents, AI Assistant and Settings
   stay compact on phones instead of occupying the page.
===================================================== */
.mobile-account-toggle,
.mobile-account-menu {
  display: none;
}

@media (max-width: 700px) {
  .top-account-actions {
    position: static !important;
    width: auto !important;
    display: block !important;
  }

  .mobile-account-toggle {
    position: fixed;
    top: max(10px, env(safe-area-inset-top));
    right: 10px;
    z-index: 10002;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 5px;
    min-width: 62px;
    height: 44px;
    padding: 0 10px;
    border: 1px solid rgba(148, 163, 184, .55);
    border-radius: 11px;
    background: rgba(255, 255, 255, .95);
    color: #172033;
    box-shadow: 0 5px 18px rgba(15, 23, 42, .13);
    cursor: pointer;
    font: inherit;
    font-size: 12px;
    font-weight: 800;
    touch-action: manipulation;
  }

  .mobile-account-toggle:active {
    transform: scale(.97);
  }

  .mobile-account-toggle-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 17px;
    height: 20px;
    font-size: 21px;
    line-height: 1;
  }

  .mobile-account-menu {
    position: fixed;
    top: 62px;
    right: 10px;
    z-index: 10003;
    width: min(250px, calc(100vw - 20px));
    padding: 8px;
    display: none;
    flex-direction: column;
    gap: 6px;
    border: 1px solid rgba(148, 163, 184, .35);
    border-radius: 14px;
    background: rgba(255, 255, 255, .98);
    box-shadow: 0 18px 45px rgba(15, 23, 42, .22);
    -webkit-backdrop-filter: blur(12px);
    backdrop-filter: blur(12px);
  }

  .mobile-account-menu.show {
    display: flex;
  }

  .mobile-account-menu .top-account-btn {
    width: 100% !important;
    min-width: 0 !important;
    min-height: 44px;
    margin: 0 !important;
    padding: 10px 12px !important;
    justify-content: center;
    font-size: 13px !important;
  }

  .mobile-account-menu .ai-assistant-btn {
    grid-column: auto !important;
  }

  /* The theme picker belongs to the account menu, but remains usable
     after Theme is tapped. */
  .theme-popover {
    top: 62px !important;
    right: 10px !important;
    left: auto !important;
    width: min(300px, calc(100vw - 20px)) !important;
    max-width: 300px !important;
  }
}

@media (max-width: 380px) {
  .mobile-menu-toggle {
    min-width: 44px;
    width: 44px;
    padding: 0;
  }

  .mobile-menu-toggle-text {
    display: none;
  }

  .mobile-account-toggle {
    min-width: 44px;
    width: 44px;
    padding: 0;
  }

  .mobile-account-toggle > span:last-child {
    display: none;
  }
}

/* -------------------------------------------------
   MOBILE ACTION BUTTONS — KEEP RELATED ACTIONS IN
   ONE HORIZONTAL ROW WHEN SPACE ALLOWS
   ------------------------------------------------- */
@media (max-width: 700px) {
  .task-actions-row {
    display: flex !important;
    flex-wrap: nowrap !important;
    width: 100% !important;
    gap: 6px !important;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
    padding-bottom: 2px;
  }

  .task-actions-row .btn {
    flex: 1 0 64px !important;
    width: auto !important;
    min-width: 64px !important;
    min-height: 42px;
    padding-left: 8px !important;
    padding-right: 8px !important;
    white-space: nowrap;
  }

  .image-actions,
  .image-to-pdf-actions,
  .pdf-page-remove-actions,
  .pdf-ocr-actions,
  .wm-compress-actions,
  .diff-actions,
  .daily-expenses-category-manager-actions,
  .goal-milestone-actions {
    display: flex !important;
    flex-wrap: nowrap !important;
    align-items: center;
    gap: 6px !important;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
    width: 100%;
  }

  .image-actions .btn,
  .image-to-pdf-actions .btn,
  .pdf-page-remove-actions .btn,
  .pdf-ocr-actions .btn,
  .wm-compress-actions .btn,
  .diff-actions .btn,
  .daily-expenses-category-manager-actions .btn,
  .goal-milestone-actions .btn {
    flex: 0 0 auto !important;
    width: auto !important;
    min-width: 72px !important;
    white-space: nowrap;
  }

  /* Table action cells: keep Edit / Remove / View together instead of
     stacking every button vertically. The cell can scroll on narrow phones. */
  td.actions,
  .actions {
    white-space: nowrap;
  }

  td.actions .btn,
  .actions .btn {
    width: auto !important;
    min-width: 68px;
    display: inline-flex !important;
    margin: 2px;
  }

  /* Goal/detail action clusters stay in one row as well. */
  .goal-detail-top > div:last-child,
  .goal-note-head > div:last-child {
    display: flex !important;
    flex-wrap: nowrap !important;
    gap: 6px !important;
    align-items: center;
    overflow-x: auto;
    max-width: 100%;
  }

  .goal-detail-top > div:last-child .btn,
  .goal-note-head > div:last-child .btn {
    flex: 0 0 auto !important;
    width: auto !important;
    min-width: 72px !important;
    white-space: nowrap;
  }
}

/* Very narrow phones: preserve the single-row interaction while allowing
   horizontal touch scrolling instead of making buttons unusably tiny. */
@media (max-width: 360px) {
  .task-actions-row .btn {
    flex-basis: 62px !important;
    min-width: 62px !important;
    font-size: 11px !important;
    padding-left: 6px !important;
    padding-right: 6px !important;
  }
}
