/* ============================================
   DASHBOARD MOBILE - Fixed panel architecture
   Following builder.html/success.html patterns

   Key principles:
   1. Viewport lock (100vh/100dvh, overflow: hidden)
   2. Fixed header (app-header with safe area)
   3. Fixed footer (mobile tabs with safe area)
   4. Content area - fixed between panels with internal scroll
   ============================================ */

:root {
  --dashboard-head-h: 64px;  /* app-header height (increased for larger Create button) */
  --dashboard-foot-h: 56px;  /* mobile tabs height (icon 20 + gap 2 + text 14 + padding 16 + 4 buffer) */
}

/* Hidden on desktop */
.header-cta-create {
  display: none;
}

/* ============================================
   MOBILE BREAKPOINT (< 768px)
   ============================================ */
@media (max-width: 767px) {
  /* Viewport lock - prevent body scroll */
  html,
  body.dashboard-page {
    height: 100vh !important;
    height: 100dvh !important;  /* Dynamic viewport height for iOS */
    overflow: hidden !important;
  }

  /* ============================================
     LIGHT BACKGROUND - Like builder mobile layout
     ============================================ */
  body.dashboard-page {
    background: var(--dashboard-surface-light) !important;
    background-attachment: scroll !important;
  }

  /* Header - add safe area padding, reset bottom padding for centering */
  body.dashboard-page .app-header {
    padding: env(safe-area-inset-top, 0px) 0 0 0;
    height: calc(var(--dashboard-head-h) + env(safe-area-inset-top, 0px));
  }

  /* Footer - enhanced mobile tabs (light lavender like builder) */
  .dashboard-mobile-tabs {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    /* Total height = base + safe-area (matches content bottom calculation) */
    height: calc(var(--dashboard-foot-h) + env(safe-area-inset-bottom, 0px));
    z-index: 1100;
    background: rgba(244, 243, 255, 0.95);  /* Uses --dashboard-surface-light with opacity */
    -webkit-backdrop-filter: blur(12px);
    backdrop-filter: blur(12px);
    border-top: 1px solid var(--dashboard-border);
    box-shadow: var(--dashboard-card-shadow);
    /* Padding: content centered in base height, safe-area below */
    padding: 0 0 env(safe-area-inset-bottom, 0px) 0;
    /* GPU acceleration */
    transform: translateZ(0);
    will-change: transform;
    /* Prevent touch-induced shifts */
    touch-action: pan-x pinch-zoom;
    overscroll-behavior: none;
  }

  /* Header - light lavender like builder panels */
  body.dashboard-page .app-header {
    background: rgba(244, 243, 255, 0.95) !important;  /* Uses --dashboard-surface-light with opacity */
    -webkit-backdrop-filter: blur(12px);
    backdrop-filter: blur(12px);
    box-shadow: var(--dashboard-card-shadow);
  }

  /* Hide user widget in header on mobile (replaced by stager menu) */
  body.dashboard-page .user-widget--header,
  body.dashboard-page .user-widget__login-btn {
    display: none !important;
  }

  /* CTA button in header - "+ Create" */
  .header-cta-create {
    display: flex !important;
    align-items: center;
    gap: 8px;
    /* Fixed position in header area */
    position: fixed;
    top: calc(env(safe-area-inset-top, 0px) + 10px);
    right: 16px;
    z-index: 1050;
    /* Styling - matching btn-header-preview from builder */
    padding: 12px 20px;
    background: linear-gradient(135deg, #f472b6, var(--dashboard-accent), var(--dashboard-primary));
    border: none;
    border-radius: 24px;
    font-family: var(--font-family, 'Gilroy', 'Manrope', system-ui, sans-serif);
    font-size: 16px;
    font-weight: 700;
    color: var(--dashboard-dark-text);
    text-decoration: none;
    box-shadow: 0 4px 12px rgba(168, 85, 247, 0.3);
    cursor: pointer;
    transition: all 0.2s ease;
    white-space: nowrap;
  }

  .header-cta-create:active {
    transform: scale(0.96);
    box-shadow: 0 2px 8px rgba(168, 85, 247, 0.25);
  }

  .header-cta-create svg {
    flex-shrink: 0;
  }

  /* Content area - fixed between header and footer */
  .dashboard-container {
    position: fixed !important;
    top: calc(var(--dashboard-head-h) + env(safe-area-inset-top, 0px));
    left: 0;
    right: 0;
    bottom: calc(var(--dashboard-foot-h) + env(safe-area-inset-bottom, 0px));
    width: 100% !important;
    margin: 0 !important;
    padding: 0 !important;
    overflow: hidden;
    display: block;
    z-index: 1;
  }

  /* Scrollable main content */
  .dashboard-main {
    width: 100%;
    height: 100%;
    max-width: none;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;  /* Momentum scrolling on iOS */
    overflow-anchor: none;  /* Prevent scroll anchoring jumps */
    padding: 16px 16px 24px;
  }

  /* Sidebar - hidden on mobile (already in layout.css) */
  .dashboard-sidebar {
    display: none !important;
  }

  /* ============================================
     CONTRASTING TEXT ON LIGHT BACKGROUND
     ============================================ */

  /* Section titles - dark text instead of white */
  .dashboard-section__title {
    color: var(--dashboard-text) !important;
  }

  .dashboard-section__subtitle {
    color: var(--dashboard-text-muted) !important;
  }

  /* Overview greeting title */
  .overview__greeting {
    color: var(--dashboard-text) !important;
  }

  /* Overview events and reactions titles */
  .overview__events-title,
  .overview__reactions-title {
    color: var(--dashboard-text) !important;
  }

  /* Pill tabs - dark theme for light background */
  .pill-tabs {
    background: var(--dashboard-hover) !important;
  }

  .pill-tabs__tab {
    color: var(--dashboard-text-hint) !important;
  }

  .pill-tabs__tab:hover:not(.pill-tabs__tab--active) {
    color: var(--dashboard-text) !important;
  }

  .pill-tabs__tab--active,
  .pill-tabs__tab--active:hover {
    color: var(--dashboard-text) !important;
  }

  /* Stat cards - ensure contrast */
  .stat-card {
    background: var(--dashboard-surface);
    box-shadow: var(--dashboard-card-shadow);
  }

  /* Settings view title */
  .settings-view__title {
    color: var(--dashboard-text) !important;
  }

  .settings-view__subtitle {
    color: var(--dashboard-text-muted) !important;
  }

  /* ============================================
     SETTINGS PAGE - Mobile Layout
     Order: Profile → Personal Info → Credits → Subscription → History → Delete
     ============================================ */

  /* Settings grid - single column, flex for ordering */
  .settings-grid {
    display: flex !important;
    flex-direction: column;
    gap: 16px;
  }

  .settings-grid__left,
  .settings-grid__right,
  .settings-grid__cards-row {
    display: contents; /* Let children participate in parent flex */
  }

  /* Settings cards order on mobile */
  .settings-card--profile-centered {
    order: 1;
  }

  .settings-card--personal {
    order: 2;
  }

  .settings-card--credits-balance {
    order: 3;
  }

  .settings-card--subscription {
    order: 4;
  }

  .settings-card--history {
    order: 5;
  }

  .settings-card--danger {
    order: 6;
  }

  /* ============================================
     PURCHASE HISTORY - Mobile Compact View
     Card-based layout instead of table
     ============================================ */

  /* Reset table display to allow card layout */
  .history-table {
    display: block;
  }

  /* Hide table headers on mobile */
  .history-table__head {
    display: none;
  }

  /* Transform table rows into cards */
  .history-table__body {
    display: flex;
    flex-direction: column;
    gap: 12px;
  }

  .history-table__body tr:not(.history-table__loading) {
    display: grid;
    grid-template-columns: 1fr auto;
    grid-template-rows: auto auto;
    gap: 4px 12px;
    background: var(--dashboard-card-bg, #ffffff);
    border: 1px solid var(--dashboard-border, rgba(0, 0, 0, 0.06));
    border-radius: 12px;
    padding: 14px 16px;
    box-shadow: 0 2px 6px rgba(31, 34, 56, 0.04);
  }

  /* Hide Credits and Status columns on mobile */
  .history-table__body td:nth-child(4), /* Credits */
  .history-table__body td:nth-child(5)  /* Status */ {
    display: none;
  }

  /* Date - top left, smaller */
  .history-table__body td:nth-child(1) {
    grid-column: 1;
    grid-row: 1;
    font-size: 12px;
    color: var(--dashboard-text-secondary, rgba(31, 34, 56, 0.6));
    padding: 0;
    font-weight: 500;
  }

  /* Description - bottom left, main text */
  .history-table__body td:nth-child(2) {
    grid-column: 1;
    grid-row: 2;
    font-size: 15px;
    font-weight: 600;
    color: var(--dashboard-text, #1f2238);
    padding: 0;
    line-height: 1.3;
  }

  /* Amount - right side, spans both rows */
  .history-table__body td:nth-child(3) {
    grid-column: 2;
    grid-row: 1 / 3;
    display: flex;
    align-items: center;
    font-size: 16px;
    font-weight: 700;
    color: var(--dashboard-text, #1f2238);
    padding: 0;
    text-align: right;
  }

  /* Loading state - keep full width */
  .history-table__body tr.history-table__loading {
    display: block;
    background: none;
    border: none;
    box-shadow: none;
    padding: 24px;
    text-align: center;
  }

  /* Respect hidden attribute on loading row */
  .history-table__body tr.history-table__loading[hidden] {
    display: none;
  }

  .history-table__body tr.history-table__loading td {
    display: block;
  }

  /* History card header - stack on mobile */
  .history-card__header {
    flex-direction: column;
    gap: 12px;
    align-items: flex-start;
  }

  .history-card__invoices-btn {
    width: 100%;
    justify-content: center;
  }

  /* History footer - full width button */
  .history-card__footer {
    padding-top: 12px;
    border-top: 1px solid var(--dashboard-border, rgba(0, 0, 0, 0.06));
    margin-top: 4px;
  }

  .history-card__view-all {
    width: 100%;
    text-align: center;
    padding: 12px;
    font-size: 14px;
  }

  /* Smaller empty state icon */
  .history-card__empty-icon {
    font-size: 36px;
  }

  /* Table wrapper - remove horizontal scroll (cards don't need it) */
  .history-card__table-wrap {
    overflow: visible;
  }

  /* ============================================
     SETTINGS HEADER - Mobile (left-aligned like other pages)
     ============================================ */
  .settings-header {
    margin-bottom: 16px;
    text-align: left;
  }

  .settings-header__title {
    font-size: 28px !important;
    font-weight: 700 !important;
    color: var(--dashboard-text) !important;
  }

  .settings-header__subtitle {
    font-size: 14px !important;
    color: var(--dashboard-text-hint) !important;
  }

  /* ============================================
     SETTINGS CARDS - Full width on mobile
     ============================================ */
  .settings-card {
    width: 100% !important;
    max-width: none !important;
    margin: 0 !important;
  }

  /* ============================================
     PROFILE CARD - Mobile (centered, compact)
     ============================================ */
  .settings-card--profile-centered {
    padding: 20px !important;
  }

  .profile-centered {
    flex-direction: column !important;
    text-align: center;
    gap: 12px;
  }

  .profile-centered__info {
    align-items: center;
  }

  .profile-centered__avatar {
    width: 80px;
    height: 80px;
  }

  .profile-centered__avatar svg {
    width: 36px;
    height: 36px;
  }

  .profile-centered__name {
    font-size: 20px;
  }

  .profile-centered__username {
    font-size: 14px;
  }

  /* ============================================
     PERSONAL INFO FORM - Mobile
     ============================================ */
  .settings-card--personal {
    padding: 20px !important;
  }

  .personal-form__field {
    margin-bottom: 16px;
  }

  .personal-form__input {
    padding: 12px 14px;
    font-size: 16px; /* Prevent iOS zoom on focus */
  }

  .personal-form__submit {
    margin-top: 12px;
    height: 50px;
  }

  /* ============================================
     CREDITS CARD - Mobile Compact
     ============================================ */
  .settings-card--credits-balance {
    padding: 20px !important;
  }

  .credits-card__header {
    margin-bottom: 12px;
  }

  .credits-card__title {
    font-size: 15px;
  }

  .credits-card__value {
    font-size: 40px !important;
  }

  .credits-card__breakdown {
    flex-direction: row !important;
    gap: 10px;
  }

  .credits-card__box {
    flex: 1;
    padding: 10px 12px;
  }

  .credits-card__box-header {
    font-size: 10px;
    gap: 4px;
  }

  .credits-card__box-header svg {
    width: 12px;
    height: 12px;
  }

  .credits-card__box-value {
    font-size: 20px;
  }

  .credits-card__btn {
    height: 46px;
    font-size: 14px;
  }

  /* ============================================
     SUBSCRIPTION CARD - Mobile Compact
     ============================================ */
  .settings-card--subscription {
    padding: 20px !important;
  }

  .subscription-card__header {
    margin-bottom: 12px;
  }

  .subscription-card__title {
    font-size: 15px;
  }

  .subscription-card__badge {
    font-size: 11px;
    padding: 4px 10px;
  }

  .subscription-card__status-row {
    font-size: 14px;
    padding: 10px 14px;
  }

  .subscription-card__info {
    padding: 12px 14px;
    gap: 10px;
  }

  .subscription-card__info-row {
    font-size: 13px;
  }

  .subscription-card__actions {
    flex-direction: column;
    gap: 10px;
  }

  .subscription-card__btn {
    width: 100%;
    justify-content: center;
    padding: 12px 16px;
    font-size: 14px;
  }

  .subscription-card__empty {
    padding: 20px;
  }

  .subscription-card__empty-text {
    font-size: 15px;
  }

  /* ============================================
     DANGER ZONE - Mobile (subtle)
     ============================================ */
  .settings-card--danger {
    padding: 16px 20px !important;
    flex-direction: column;
    gap: 12px;
    text-align: center;
  }

  .settings-card--danger__title {
    color: var(--dashboard-text-hint);
  }

  .settings-card--danger__text {
    font-size: 13px;
    color: var(--dashboard-text-hint);
  }

  .btn-delete-account {
    width: 100%;
    justify-content: center;
    padding: 12px 16px;
    font-size: 14px;
  }

  /* Subscription section title */
  .subscription-header .dashboard-section__title,
  .subscription-header .dashboard-section__subtitle {
    color: var(--dashboard-text) !important;
  }

  .subscription-header .dashboard-section__subtitle {
    color: var(--dashboard-text-muted) !important;
  }

  /* Billing toggle - adapt for light background */
  .billing-toggle {
    background: var(--dashboard-hover);
    border-color: var(--dashboard-border);
  }

  .billing-toggle__tab {
    color: var(--dashboard-text-hint);
  }

  .billing-toggle__tab:hover {
    color: var(--dashboard-text);
  }

  .billing-toggle__tab.is-active {
    color: var(--dashboard-dark-text);
  }

  /* Dashboard loader - light background for mobile */
  .dashboard-loader {
    background: var(--dashboard-surface-light) !important;
  }

  .dashboard-loader__spinner {
    border-color: var(--dashboard-border) !important;
    border-top-color: var(--dashboard-accent) !important;
  }


  /* Cards panel - transparent on mobile (cards have their own backgrounds) */
  .cards-panel {
    background: transparent;
    box-shadow: none;
    padding: 0;
    border-radius: 0;
  }

  /* Event rows - ensure contrast */
  .event-row {
    background: var(--dashboard-surface);
  }

  /* ============================================
     SUBSCRIPTION PRICING - Mobile layout
     Inspired by upsell-modal.js card design
     3-column: [Checkbox] | [Name+Features] | [Price]
     ============================================ */

  /* Single column layout */
  .pricing-grid {
    grid-template-columns: 1fr;
    gap: 14px;
    max-width: none;
  }

  /* ===== Card: 3-column grid layout ===== */
  .pricing-card {
    display: grid !important;
    grid-template-columns: 28px 1fr auto;
    grid-template-rows: auto auto;
    gap: 0;
    padding: 16px;
    border-radius: 16px;
    position: relative;
    align-items: start;
    background: var(--dashboard-surface);
    border: 1px solid var(--dashboard-border);
    box-shadow: var(--dashboard-card-shadow);
  }

  /* ===== Pro card - most attractive ===== */
  .pricing-card--pro {
    border: 2px solid var(--dashboard-accent);
    box-shadow: 0 4px 20px rgba(168, 85, 247, 0.15);
  }

  /* Ultra card - orange accent */
  .pricing-card--ultra {
    border: 2px solid transparent;
    background: linear-gradient(#ffffff, #ffffff) padding-box,
      linear-gradient(135deg, #f59e0b, #ea580c) border-box;
  }

  /* ===== Column 1: Checkbox indicator ===== */
  .pricing-card::before {
    content: '';
    grid-column: 1;
    grid-row: 1 / 3;
    width: 22px;
    height: 22px;
    border: 2px solid var(--dashboard-border-strong);
    border-radius: 50%;
    margin-top: 2px;
    box-sizing: border-box;
    position: relative;
  }

  /* Checkmark - hidden by default */
  .pricing-card::after {
    content: '';
    position: absolute;
    /* Position centered in the checkbox circle */
    left: 23px;  /* 16px padding + 7px offset */
    top: 22px;   /* 16px padding + 2px margin + 4px offset */
    width: 5px;
    height: 9px;
    border: solid transparent;
    border-width: 0 2px 2px 0;
    transform: rotate(45deg);
    opacity: 0;
  }

  /* Current plan - filled checkbox with checkmark */
  .pricing-card.is-current::before {
    background: linear-gradient(135deg, #22c55e, #16a34a);
    border-color: #22c55e;
  }

  .pricing-card.is-current::after {
    border-color: #fff;
    opacity: 1;
  }

  /* Pro card checkbox */
  .pricing-card--pro::before {
    border-color: rgba(168, 85, 247, 0.3);
  }

  .pricing-card--pro.is-current::before {
    background: linear-gradient(135deg, #a855f7, #7c3aed);
    border-color: #a855f7;
  }

  /* Ultra card checkbox */
  .pricing-card--ultra::before {
    border-color: rgba(245, 158, 11, 0.3);
  }

  .pricing-card--ultra.is-current::before {
    background: linear-gradient(135deg, #f59e0b, #ea580c);
    border-color: #f59e0b;
  }

  /* ===== Column 2: Header (Name + Features) ===== */
  .pricing-card__header {
    grid-column: 2;
    grid-row: 1;
    display: flex;
    flex-direction: column;
    margin: 0;
    text-align: left;
    padding: 0 12px 0 0;
  }

  .pricing-card__name {
    font-size: 17px;
    font-weight: 700;
    margin-bottom: 2px;
    color: var(--dashboard-text);
  }

  .pricing-card--pro .pricing-card__name {
    color: var(--dashboard-primary);
  }

  .pricing-card--ultra .pricing-card__name {
    color: #ea580c;
  }

  /* Hide original price and billing note in header */
  .pricing-card__header .pricing-card__price,
  .pricing-card__header .pricing-card__billing-note {
    display: none !important;
  }

  .pricing-card__tagline {
    display: none; /* Hide tagline, show features instead */
  }

  /* ===== Column 2, Row 2: Features ===== */
  .pricing-card__body {
    display: block !important;
    grid-column: 2;
    grid-row: 2;
    padding: 0 12px 0 0;
    margin-top: 6px;
  }

  .pricing-card__features {
    margin: 0;
    padding: 0;
    list-style: none;
  }

  /* Show only first 3 features */
  .pricing-card__features li {
    font-size: 12px;
    color: var(--dashboard-text-hint);
    padding: 2px 0;
    display: flex;
    align-items: center;
    gap: 6px;
    line-height: 1.3;
  }

  .pricing-card__features li:nth-child(n+4) {
    display: none;
  }

  .feature-icon {
    font-size: 10px;
    width: auto;
    height: auto;
    background: none;
    border-radius: 0;
    display: inline;
    padding: 0;
  }

  .feature-icon--pro {
    color: var(--dashboard-accent);
  }

  .feature-icon--ultra {
    color: #ea580c;
  }

  /* Disabled features - dimmer */
  .pricing-card__features li.feature-disabled {
    color: var(--dashboard-text-secondary);
  }

  /* ===== Column 3: Price + CTA ===== */
  .pricing-card__cta {
    grid-column: 3;
    grid-row: 1 / 3;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 8px;
    margin: 0;
    padding-left: 14px;
    border-left: 1px solid var(--dashboard-border);
    min-width: 80px;
    align-self: stretch;
  }

  .pricing-card--pro .pricing-card__cta {
    border-left-color: rgba(168, 85, 247, 0.15);
  }

  .pricing-card--ultra .pricing-card__cta {
    border-left-color: rgba(245, 158, 11, 0.15);
  }

  /* Mobile price display */
  .pricing-card__cta::before {
    content: attr(data-mobile-price);
    display: block;
    font-size: 20px;
    font-weight: 700;
    color: var(--dashboard-text);
    line-height: 1.1;
    text-align: center;
  }

  .pricing-card--pro .pricing-card__cta::before {
    color: var(--dashboard-primary);
  }

  .pricing-card--ultra .pricing-card__cta::before {
    color: #ea580c;
  }

  /* ===== Buttons ===== */
  .pricing-card__btn {
    padding: 8px 14px;
    font-size: 12px;
    font-weight: 600;
    border-radius: 10px;
    white-space: nowrap;
    min-width: 65px;
    text-align: center;
  }

  .pricing-card__btn-text {
    font-size: inherit;
  }

  /* Current plan - hide button, checkbox shows status */
  .pricing-card__btn--current {
    display: none !important;
  }

  /* Upgrade Pro button */
  .pricing-card__btn--upgrade-pro {
    padding: 8px 14px;
    font-size: 12px;
    box-shadow: 0 4px 12px rgba(168, 85, 247, 0.25);
  }

  /* Upgrade Ultra button */
  .pricing-card__btn--upgrade-ultra {
    padding: 8px 14px;
    font-size: 12px;
    box-shadow: 0 4px 12px rgba(245, 158, 11, 0.25);
  }

  /* Downgrade button */
  .pricing-card__btn--downgrade {
    padding: 6px 10px;
    font-size: 11px;
    background: transparent !important;
    border: 1px solid var(--dashboard-border-strong) !important;
    color: var(--dashboard-text-secondary) !important;
    box-shadow: none !important;
  }

  /* ===== Badge ===== */
  .pricing-card__badge {
    position: absolute;
    top: -9px;
    right: 14px;
    left: auto;
    transform: none;
    font-size: 10px;
    font-weight: 700;
    padding: 4px 10px;
    border-radius: 12px;
  }

  /* ===== Billing toggle ===== */
  .billing-toggle {
    margin-bottom: 16px;
  }

  .billing-toggle__tab {
    padding: 8px 18px;
    font-size: 13px;
  }

  /* Reactions cards - keep original gradient colors from desktop */
  /* No override needed - colors work well on light background */
}

/* ============================================
   SMALL MOBILE (< 380px)
   ============================================ */
@media (max-width: 380px) {
  :root {
    --dashboard-foot-h: 52px;  /* Slightly smaller footer */
  }

  .dashboard-main {
    padding: 12px 12px 20px;
  }

  /* Smaller CTA button on small screens */
  .header-cta-create {
    padding: 8px 12px;
    font-size: 13px;
    right: 12px;
  }
}
