/* =========================================================
 * Mobile & PWA Responsive Enhancements
 * Loaded after components.css so desktop layout stays intact.
 * ========================================================= */

:root {
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

/* Desktop defaults: hide mobile-only chrome */
.mobile-tabbar,
.mobile-drawer-overlay,
.mobile-menu-trigger,
.mobile-theme-trigger {
  display: none;
}

.top-header-left,
.top-header-right {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

/* Desktop sidebar logout button (shown only when access password is enabled) */
.sidebar-logout-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  margin-top: 6px;
  padding: 10px 14px;
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  font-family: inherit;
  border-radius: var(--radius-sm);
  transition: all 0.2s var(--ease-spring);
}

.sidebar-logout-btn:hover {
  color: var(--danger);
  background: rgba(239, 68, 68, 0.08);
  border-color: rgba(239, 68, 68, 0.2);
}

.sidebar-logout-btn.hidden {
  display: none !important;
}

/* Ensure table containers always scroll cleanly on any viewport */
.table-container {
  max-width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

@media (max-width: 768px), (max-width: 960px) and (max-height: 500px) and (pointer: coarse) {
  :root {
    --mobile-tabbar-height: 56px;
    --mobile-gutter: 12px;
    --mobile-left: max(var(--mobile-gutter), var(--safe-left));
    --mobile-right: max(var(--mobile-gutter), var(--safe-right));
  }

  html,
  body {
    height: 100dvh;
    overscroll-behavior-y: none;
    -webkit-tap-highlight-color: transparent;
  }

  .app-container {
    height: 100dvh;
    width: 100%;
    overflow: hidden;
  }

  /* Hide desktop sidebar on mobile; replaced by bottom 5-tab bar + grid drawer */
  .sidebar {
    display: none !important;
  }

  .main-content {
    width: 100%;
    height: 100dvh;
    margin-bottom: 0 !important;
    padding-top: var(--safe-top);
    padding-bottom: calc(var(--mobile-tabbar-height) + var(--safe-bottom));
    overflow: hidden;
  }

  /* Compact sticky mobile top header */
  .top-header {
    height: 54px;
    padding: 0 var(--mobile-right) 0 var(--mobile-left);
    gap: 8px;
    border-bottom: 1px solid var(--border-color);
    background: var(--bg-sidebar);
    backdrop-filter: blur(20px) saturate(1.6);
    -webkit-backdrop-filter: blur(20px) saturate(1.6);
  }

  .top-header-left {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
    flex: 1;
  }

  .top-header-right {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
  }

  .mobile-menu-trigger,
  .mobile-theme-trigger {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border-color);
    background: var(--bg-surface);
    color: var(--text-main);
    cursor: pointer;
    flex-shrink: 0;
    position: relative;
  }

  .mobile-menu-trigger:active,
  .mobile-theme-trigger:active {
    transform: scale(0.95);
    background: var(--bg-input);
  }

  .page-title {
    font-size: 17px;
    letter-spacing: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .status-indicator {
    padding: 5px 10px;
    font-size: 11px;
    gap: 6px;
    max-width: 150px;
    white-space: nowrap;
  }

  .status-indicator .separator,
  .status-indicator .status-time {
    display: none;
  }

  /* Panels */
  .panel {
    min-height: 0;
    padding: 12px var(--mobile-right) 20px var(--mobile-left) !important;
    -webkit-overflow-scrolling: touch;
  }

  /* Prevent iOS Safari zoom on input focus */
  input[type="text"],
  input[type="number"],
  input[type="password"],
  input[type="search"],
  input[type="url"],
  select,
  textarea {
    font-size: 16px !important;
  }

  /* Stats cards on dashboard */
  .stats-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
    margin-bottom: 12px;
  }

  .stat-card {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-content: start;
    align-items: start;
    gap: 4px;
    padding: 10px;
    border-radius: var(--radius-sm);
  }

  .stat-card:hover {
    transform: none;
  }

  .stat-label {
    grid-area: 1 / 1;
    font-size: 12px;
    line-height: 1.4;
    margin: 0;
  }

  .stat-value {
    grid-area: 2 / 1 / 3 / -1;
    font-size: 20px;
    line-height: 1.25;
    letter-spacing: 0;
    overflow-wrap: anywhere;
  }

  .stat-trend {
    grid-area: 1 / 2;
    gap: 3px;
    margin: 0;
    padding: 1px 4px;
    font-size: 10px;
    white-space: nowrap;
  }

  .stat-trend svg {
    width: 10px;
    height: 10px;
  }

  /* Control bar & buttons */
  .control-bar {
    flex-direction: column;
    align-items: stretch;
    padding: 12px;
    gap: 8px;
    margin-bottom: 12px;
    border-radius: var(--radius-md);
  }

  .control-actions {
    width: 100%;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr)) minmax(0, 1.2fr);
    gap: 8px;
  }

  .control-actions .btn {
    width: 100%;
    min-height: 44px;
    padding: 6px;
    gap: 4px;
    font-size: 13px;
  }

  .control-actions .btn svg {
    flex-shrink: 0;
  }

  .btn {
    min-height: 38px;
  }

  .btn-sm {
    min-height: 34px;
    padding: 6px 11px;
  }

  /* Payment banner */
  .payment-alert {
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
    padding: 12px;
    margin-top: 12px;
    border-radius: var(--radius-sm);
  }

  .payment-content {
    flex-direction: row;
    gap: 10px;
  }

  .payment-text {
    flex: 1;
    min-width: 0;
    overflow-wrap: anywhere;
  }

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

  .payment-actions .divider-v {
    display: none;
  }

  .payment-actions > :is(.btn, .link-btn) {
    min-height: 44px;
    justify-content: center;
    display: inline-flex;
    align-items: center;
  }

  #pay-qrcode canvas,
  #pay-qrcode img {
    max-width: min(180px, 100%);
    height: auto !important;
    margin: 0 auto;
    display: block;
  }

  /* Toolbars & summaries */
  .panel-toolbar,
  .panel-toolbar-row {
    flex-wrap: wrap;
    gap: 10px;
  }

  .panel-toolbar {
    margin-bottom: 12px;
  }

  .toolbar-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    width: 100%;
  }

  .purchase-comparison {
    margin-top: 12px;
  }

  .purchase-comparison .toolbar-title {
    flex: 1;
    flex-wrap: wrap;
    gap: 2px 8px;
  }

  .purchase-comparison-controls {
    flex-shrink: 0;
  }

  .purchases-summary {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px 12px;
    width: 100%;
  }

  /* Add purchase bar */
  .add-purchase-bar {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    padding: 12px;
    gap: 8px;
  }

  .add-purchase-bar .label,
  #add-purchase-name,
  #add-purchase-steam-link,
  #btn-add-purchase {
    grid-column: 1 / -1;
    margin-right: 0;
  }

  .add-purchase-bar input[type="text"],
  .add-purchase-bar input[type="number"],
  .add-purchase-bar #add-purchase-steam-link,
  .add-purchase-bar #add-purchase-quantity {
    width: 100%;
    min-width: 0;
    min-height: 44px;
  }

  .add-purchase-bar #btn-add-purchase {
    min-height: 44px;
  }

  /* Tables: crisp horizontal scrolling */
  .table-container {
    border-radius: var(--radius-md);
  }

  .table-container table {
    min-width: 640px;
    font-size: 12.5px;
  }

  .purchase-comparison-scroll {
    max-height: 54dvh;
  }

  .purchase-comparison-scroll table {
    min-width: 680px;
  }

  th,
  td {
    padding: 10px 11px;
    white-space: nowrap;
  }

  td:first-child {
    white-space: normal;
    min-width: 140px;
    max-width: 220px;
    overflow-wrap: anywhere;
  }

  /* Accounts & proxy & settings & forms */
  .accounts-layout,
  .form-grid,
  .kv-grid,
  .proxy-strategy-grid,
  .field-row,
  .field-row--3,
  .field-row--4,
  .strategy-step-param-grid,
  .strategy-create-options,
  .strategy-module-picker-list {
    grid-template-columns: 1fr !important;
  }

  .input-pill {
    min-width: 0 !important;
    width: 100%;
  }

  .proxy-section,
  .settings-card,
  .card {
    padding: 16px 14px;
    border-radius: var(--radius-md);
  }

  .proxy-section-header {
    flex-wrap: wrap;
    gap: 10px;
  }

  #proxy-timeout,
  #proxy-test-url,
  #proxy-add-port {
    max-width: 100% !important;
  }

  /* Strategy center touch friendliness */
  .strategy-workbench {
    grid-template-columns: 1fr !important;
    gap: 14px;
  }

  .strategy-step {
    padding: 12px;
    gap: 8px;
  }

  .strategy-step-actions {
    gap: 8px;
    margin-top: 6px;
    padding-top: 8px;
    border-top: 1px dashed var(--border-color);
  }

  .strategy-step-actions .icon-btn {
    width: 36px;
    height: 36px;
    font-size: 15px;
    border-radius: 8px;
  }

  /* Console / debug log */
  .console-wrapper {
    flex: 1;
    height: auto;
    min-height: 0;
    border-radius: var(--radius-md);
  }

  .console-toolbar {
    padding: 10px 12px;
    flex-wrap: wrap;
    gap: 8px;
  }

  .console-filters {
    flex-basis: 100%;
  }

  .console-filters,
  .console-actions {
    flex-wrap: wrap;
    gap: 6px;
    width: 100%;
  }

  .console-filters #log-search,
  .console-filters select {
    flex: 1 1 108px;
    min-width: 0;
    width: auto;
    padding-inline: 8px;
  }

  .console-actions .icon-btn {
    flex: 1 0 auto;
    height: 40px;
    padding: 0 8px;
  }

  .console-body {
    min-height: 0;
    padding: 10px 12px;
  }

  /* Steam Guard */
  .steam-token-page {
    min-height: 0;
    overflow: hidden;
  }

  .steam-token-card {
    padding: 20px 14px;
  }

  .steam-token-title {
    margin-bottom: 16px;
  }

  .steam-token-circle {
    width: min(100%, 240px);
    height: auto;
    aspect-ratio: 1;
  }

  .steam-token-ring {
    width: 100%;
    height: 100%;
  }

  .steam-code-text {
    font-size: 32px;
    letter-spacing: 0;
    margin-left: 0;
  }

  /* Steam Deals & Gift panels */
  .steam-game-grid {
    grid-template-columns: 1fr !important;
    gap: 14px;
  }

  .gift-hero {
    padding: 18px 16px;
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }

  .gift-grid,
  .sd-filters {
    grid-template-columns: 1fr !important;
  }

  .gift-section {
    padding: 14px;
  }

  .gift-section-header > .gift-friend-tools {
    flex: 1 1 260px;
    min-width: 0;
  }

  .gift-friend-tools .gift-search-input {
    flex: 1;
    width: auto;
    min-width: 0;
    min-height: 44px;
    font-size: 16px;
  }

  .gift-friend-tools .btn {
    flex-shrink: 0;
    min-height: 44px;
  }

  /* Modals & onboarding wizard */
  .app-modal-host {
    padding: max(12px, var(--safe-top)) 12px max(12px, var(--safe-bottom));
    align-items: flex-end;
  }

  .app-modal,
  .relogin-modal,
  .overlay-content,
  .wizard-modal {
    width: 100% !important;
    max-width: 100% !important;
    max-height: calc(100dvh - 24px - var(--safe-top) - var(--safe-bottom)) !important;
    padding: 20px 16px !important;
    border-radius: 20px !important;
  }

  .relogin-overlay,
  .overlay {
    padding: 12px;
  }

  .relogin-actions {
    display: flex;
    flex-direction: column;
    gap: 8px;
  }

  .relogin-actions .btn,
  .wizard-footer-btns .btn {
    width: 100%;
    min-height: 44px;
    justify-content: center;
  }

  .wizard-footer {
    flex-direction: column;
    align-items: stretch;
  }

  .wizard-footer-btns {
    width: 100%;
  }

  /* Floating controls & toasts above tabbar */
  .scroll-to-top {
    right: 14px;
    bottom: calc(var(--mobile-tabbar-height) + var(--safe-bottom) + 14px);
    width: 40px;
    height: 40px;
  }

  #toast-host,
  .toast-host {
    left: 12px !important;
    right: 12px !important;
    bottom: calc(var(--mobile-tabbar-height) + var(--safe-bottom) + 12px) !important;
    max-width: none !important;
  }

  .toast {
    width: 100%;
    max-width: none !important;
  }

  /* Hide keyboard shortcut hints on touch screens */
  .kbd {
    display: none;
  }

  /* =========================================================
   * Mobile Bottom TabBar (5 fixed slots)
   * ========================================================= */
  .mobile-tabbar {
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    align-items: center;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    height: calc(var(--mobile-tabbar-height) + var(--safe-bottom));
    padding: 4px max(6px, var(--safe-right)) calc(4px + var(--safe-bottom)) max(6px, var(--safe-left));
    background: var(--bg-sidebar);
    backdrop-filter: blur(28px) saturate(1.8);
    -webkit-backdrop-filter: blur(28px) saturate(1.8);
    border-top: 1px solid var(--border-color);
    box-shadow: 0 -6px 24px rgba(0, 0, 0, 0.08);
    z-index: 90;
  }

  .mobile-tab {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    height: 100%;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    font-family: inherit;
    font-size: 10.5px;
    font-weight: 500;
    cursor: pointer;
    position: relative;
    padding: 4px 2px;
    border-radius: 10px;
    transition: color 0.18s ease, transform 0.15s ease;
  }

  .mobile-tab:active {
    transform: scale(0.94);
  }

  .mobile-tab svg {
    width: 20px;
    height: 20px;
    flex-shrink: 0;
  }

  .mobile-tab.active {
    color: var(--primary);
    font-weight: 700;
  }

  .mobile-tab .mobile-dot {
    position: absolute;
    top: 4px;
    right: calc(50% - 16px);
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--danger);
    box-shadow: 0 0 0 2px var(--bg-surface);
  }

  .mobile-tab .mobile-dot.hidden {
    display: none;
  }

  /* =========================================================
   * Mobile All-Features Drawer (Bottom Sheet)
   * ========================================================= */
  .mobile-drawer-overlay {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 120;
    background: rgba(0, 0, 0, 0.48);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    align-items: flex-end;
    justify-content: center;
  }

  .mobile-drawer-overlay.open {
    display: flex;
  }

  .mobile-drawer {
    width: 100%;
    max-height: 82dvh;
    background: var(--bg-surface);
    border-top-left-radius: 24px;
    border-top-right-radius: 24px;
    border-top: 1px solid var(--border-color);
    box-shadow: var(--shadow-lg);
    padding: 12px 16px calc(18px + var(--safe-bottom));
    display: flex;
    flex-direction: column;
    gap: 14px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    animation: mobileSheetUp 0.24s var(--ease-out);
  }

  @keyframes mobileSheetUp {
    from {
      transform: translateY(32px);
      opacity: 0.5;
    }
    to {
      transform: translateY(0);
      opacity: 1;
    }
  }

  .mobile-drawer-handle {
    width: 38px;
    height: 4px;
    border-radius: 999px;
    background: var(--border-color);
    margin: 0 auto 2px;
  }

  .mobile-drawer-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }

  .mobile-drawer-title {
    font-size: 16px;
    font-weight: 700;
    color: var(--text-main);
  }

  .mobile-drawer-close {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    border: 1px solid var(--border-color);
    background: var(--bg-input);
    color: var(--text-secondary);
    font-size: 18px;
    line-height: 1;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  .mobile-drawer-grid {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 10px;
  }

  .mobile-drawer-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 14px 8px;
    border-radius: 14px;
    border: 1px solid var(--border-color);
    background: var(--bg-body);
    color: var(--text-main);
    font-family: inherit;
    font-size: 12px;
    font-weight: 500;
    cursor: pointer;
    position: relative;
    text-align: center;
    transition: background 0.15s, border-color 0.15s, transform 0.15s;
  }

  .mobile-drawer-item:active {
    transform: scale(0.96);
  }

  .mobile-drawer-item.active {
    background: var(--primary);
    color: var(--primary-text);
    border-color: transparent;
    font-weight: 600;
  }

  .mobile-drawer-item svg {
    width: 20px;
    height: 20px;
    flex-shrink: 0;
  }

  .mobile-drawer-badge {
    position: absolute;
    top: 8px;
    right: 8px;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--danger);
  }

  .mobile-drawer-badge.hidden {
    display: none;
  }

  .mobile-drawer-footer {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding-top: 10px;
    border-top: 1px solid var(--border-color);
  }

  .mobile-drawer-action {
    flex: 1 1 130px;
    min-height: 42px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 8px 12px;
    border-radius: 12px;
    border: 1px solid var(--border-color);
    background: var(--bg-body);
    color: var(--text-main);
    font-family: inherit;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
  }

  .mobile-drawer-action.danger {
    color: var(--danger);
    border-color: rgba(239, 68, 68, 0.28);
    background: rgba(239, 68, 68, 0.06);
  }

  .mobile-drawer-action.hidden {
    display: none !important;
  }
}

@media (min-width: 540px) and (max-width: 768px),
  (min-width: 540px) and (max-width: 960px) and (max-height: 500px) and (pointer: coarse) {
  .stats-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }
}
