.mobile-header-actions, .mobile-assets { display: none; }

/* Shared responsive foundation for the C2C workspace.  Keep overflow local to
   the component that actually needs it; the page itself should never scroll
   sideways on a phone. */
html,
body,
#dashboard-view,
.dashboard,
.workspace,
.content-viewport,
.workspace-section,
.panel { min-width: 0; }

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

@media (max-width: 900px) {
  main { width: 100%; max-width: none; margin: 0; padding: 0; }

  .topbar {
    min-height: 56px;
    height: auto;
    align-items: flex-start;
    flex-wrap: wrap;
    gap: 8px 12px;
    padding: 10px 16px;
  }
  .topbar .brand { min-height: 36px; align-items: center; }
  .topbar-right { width: 100%; flex: 1 0 100%; margin-left: 0; }
  .account-nav {
    width: 100%;
    display: grid;
    align-items: stretch;
    gap: 10px;
  }
  .header-account-data {
    width: 100%;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
  }
  .header-metric {
    min-height: 48px;
    display: flex;
    grid-row: auto;
    flex-direction: column;
    align-items: flex-start;
    justify-content: center;
    gap: 2px;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 7px 10px;
    background: var(--surface);
  }
  .header-metric small { font-size: 11px; line-height: 16px; }
  .header-metric strong { font-size: 15px; line-height: 19px; }
  .points-metric { border-left: 1px solid var(--line); padding-left: 10px; }
  .header-identity {
    width: 100%;
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto auto auto;
    align-items: center;
    gap: 8px;
    border-top: 1px solid var(--line);
    border-left: 0;
    padding: 10px 0 0;
  }
  .header-user-id { min-width: 0; }
  .header-user-id small { font-size: 11px; }
  .header-user-id-value { min-width: 0; }
  .header-user-id code { width: 100%; max-width: 100%; font-size: 11px; }
  .header-session { width: auto; display: contents; }
  .header-identity .button { min-height: 40px; }
  #open-gift { min-width: 58px; }

  .dashboard {
    display: flex;
    min-height: calc(100dvh - 56px);
    flex-direction: column;
  }
  .sidebar {
    z-index: 9;
    flex: 0 0 auto;
    border-right: 0;
    border-bottom: 1px solid var(--line);
    padding: 0;
    background: var(--surface);
  }
  .workspace-nav {
    display: flex;
    align-items: stretch;
    justify-content: flex-start;
    gap: 4px;
    overflow-x: auto;
    margin: 0;
    padding: 4px 12px;
    scrollbar-width: none;
  }
  .workspace-nav::-webkit-scrollbar,
  .ai-tabs::-webkit-scrollbar { display: none; }
  .workspace-tab {
    min-width: 104px;
    min-height: 44px;
    flex: 0 0 auto;
    border: 0;
    border-radius: 8px;
    padding: 10px 16px;
    white-space: nowrap;
  }
  .workspace-tab.active { border: 0; background: var(--accent-soft); }
  .workspace { width: 100%; min-height: 0; }
  .ai-tabs {
    position: sticky;
    z-index: 8;
    top: 0;
    height: 48px;
    flex: 0 0 48px;
    align-items: stretch;
    justify-content: flex-start;
    gap: 0;
    overflow-x: auto;
    padding: 0 10px;
    scrollbar-width: none;
  }
  .ai-tab {
    min-width: 78px;
    height: 48px;
    flex: 0 0 auto;
    padding: 0 13px;
    white-space: nowrap;
  }
  .ai-status { margin: 8px 12px 0; }
  .content-viewport {
    min-height: 0;
    overflow: visible;
    padding: 14px 12px 26px;
  }
  .workspace-section { min-height: 0; }
  .panel { padding: 16px; border-radius: 10px; }
  .account-actions { justify-content: stretch; padding: 12px 14px; }
  .account-actions .button { width: 100%; }
  .list-panel { padding-top: 14px; }
  .list-toolbar { align-items: flex-start; flex-wrap: wrap; gap: 8px; }
  .list-toolbar > div:first-child { min-width: 0; }
  .heading-actions,
  .credential-toolbar-actions,
  .credential-toolbar-primary { min-width: 0; }
  .heading-actions { display: flex; flex-wrap: wrap; gap: 6px; }
  .button { min-height: 40px; }
  .button-small { min-height: 36px; }
  .icon-button { width: 40px; height: 40px; min-height: 40px; }

  .drawer {
    top: 0;
    right: 0;
    bottom: 0;
    width: min(100%, 640px);
    max-width: 100vw;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 0 16px calc(24px + env(safe-area-inset-bottom));
  }
  .business-drawer { width: min(100%, 640px); padding: 0 16px calc(24px + env(safe-area-inset-bottom)); }
  .business-drawer > .drawer-header {
    position: sticky;
    z-index: 3;
    top: 0;
    min-height: 64px;
    margin: 0 -16px 14px;
    padding: 10px 16px;
    background: rgba(255, 255, 255, .97);
    backdrop-filter: blur(8px);
  }
  .drawer-header .icon-button { flex: 0 0 40px; width: 40px; height: 40px; }
  .detail-tabs { max-width: 100%; overflow-x: auto; scrollbar-width: none; }
  .detail-tabs button { min-height: 40px; height: 40px; white-space: nowrap; }
  .modal {
    width: min(100% - 20px, 560px);
    max-height: calc(100dvh - 20px);
    margin: 10px;
    overflow: auto;
  }
  .modal-actions { flex-wrap: wrap; }
  .modal-actions .button { min-width: 110px; }

  .section-heading { align-items: flex-start; flex-wrap: wrap; }
  .section-heading > .heading-actions { width: 100%; }
  .form-row,
  .field-grid,
  .limit-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .upload-toolbar { min-width: 0; }
  .upload-toolbar .file-name { overflow-wrap: anywhere; }
  .toast-region {
    right: max(12px, env(safe-area-inset-right));
    bottom: max(12px, env(safe-area-inset-bottom));
    width: min(360px, calc(100vw - 24px));
  }
}

@media (max-width: 700px) {
  .header-account-data { gap: 7px; }
  .header-metric { min-height: 46px; padding: 6px 9px; }
  .header-metric strong { font-size: 14px; }
  .workspace-tab { min-width: 96px; padding-inline: 13px; }
  .ai-tab { min-width: 74px; padding-inline: 11px; font-size: 12px; }
  .content-viewport { padding: 12px 10px 22px; }
  .panel { padding: 14px; }
  .form-row,
  .field-grid,
  .limit-grid { grid-template-columns: 1fr; }
  .section-heading h2 { font-size: 18px; }
  .page-section-heading h2 { font-size: 21px; }
  .record-actions { width: auto; }
  .credential-detection-row { grid-template-columns: minmax(0, 1fr) auto; }
  .credential-detection-row > h3 { white-space: normal; }
  .credential-detection-row > button { grid-column: 2; }
  .credential-detection-row > select { grid-column: 1 / -1; grid-row: 2; }
  .detection-feedback { grid-column: 1 / -1; grid-row: 3; margin-top: 4px; }
}

@media (min-width: 901px) and (max-width: 1100px) {
  /* Keep the tablet credential card inside its panel. The previous minimum
     column widths made a 1024px viewport scroll even when every value fit. */
  .credential-list-panel .credential-list-row {
    grid-template-columns: minmax(160px, 1.2fr) minmax(112px, .95fr) repeat(4, minmax(64px, .7fr)) 56px;
  }
  .key-row { grid-template-columns: minmax(170px, 1.2fr) repeat(4, minmax(70px, .7fr)) 76px; }
}

@media (max-width: 600px) {
  .topbar { padding: 8px 12px; }
  .topbar .brand-name { font-size: 17px; }
  .header-identity { grid-template-columns: minmax(0, 1fr) auto auto auto; }
  .header-user-id { grid-column: 1 / -1; }
  .header-user-id code { width: min(230px, calc(100vw - 86px)); }
  .header-identity .button { min-width: 70px; }
  .workspace-nav { padding-inline: 10px; }
  .ai-tabs { padding-inline: 6px; }
  .content-viewport { padding-inline: 10px; }
  .list-toolbar { align-items: stretch; }
  .list-toolbar > div:first-child,
  .heading-actions,
  .credential-toolbar-actions { width: 100%; }
  .heading-actions,
  .credential-toolbar-actions { justify-content: flex-end; }
  .heading-actions .button,
  .credential-toolbar-actions .button { flex: 0 0 auto; }
  .table-row .record-actions { width: auto; }
  .drawer,
  .business-drawer { padding-inline: 12px; }
  .business-drawer > .drawer-header { margin-inline: -12px; padding-inline: 12px; }
  .drawer .modal-actions .button,
  .modal-actions .button { flex: 1 1 140px; }

  .report-host,
  .billing-host,
  .usage-host { width: 100%; min-width: 0; overflow: visible; }
  .report-toolbar,
  .billing-host .report-toolbar { min-width: 0; }
  .report-filters,
  .report-filter-row { min-width: 0; }
  .billing-host .report-table-panel { max-width: 100%; }
  .billing-host .report-table tr { min-width: 0; }
  .report-table td,
  .report-table th { overflow-wrap: anywhere; }
  .api-page { max-height: none; overflow: visible; }
}

@media (max-width: 480px) {
  .header-account-data { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .header-metric small { font-size: 10px; }
  .header-metric strong { font-size: 13px; }
  .header-identity { gap: 7px; }
  .header-identity .button { min-width: 0; padding-inline: 10px; }
  .workspace-tab { min-width: 90px; }
  .ai-tab { min-width: 70px; }
  .content-viewport { padding-inline: 8px; }
  .panel { padding: 12px; }
  .modal { width: calc(100% - 16px); margin: 8px; }
  .modal form { padding: 16px; }
  .detail-list div { align-items: flex-start; flex-direction: column; gap: 4px; }
  .detail-list dd { max-width: 100%; text-align: left; }
  .billing-host .report-filter-row { grid-template-columns: 1fr; }
  .billing-host .report-filters [data-custom-range],
  .billing-host .report-filter-actions { grid-column: 1; }
  .usage-host .report-metrics,
  .usage-host .usage-total-strip { grid-template-columns: 1fr 1fr; }
}

@media (max-width: 600px) {
  .password-reset-dialog {
    width: 100%;
    max-width: none;
    max-height: min(88dvh, 720px);
    margin: auto 0 0;
    border: 0;
    border-radius: 8px 8px 0 0;
  }
  .password-reset-dialog form { padding: 20px 18px calc(20px + env(safe-area-inset-bottom)); }
  .password-reset-dialog .modal-heading { margin-bottom: 18px; }
  .password-reset-dialog .modal-heading h2 { font-size: 18px; }
  .password-reset-account-email { margin-bottom: 18px; }
  .password-reset-actions { flex-wrap: nowrap; }
  .password-reset-actions .button { flex: 1 1 0; min-width: 0; }
}

/* Credential cards on phones use an information hierarchy instead of
   shrinking the desktop eleven-column grid into a long, sparse column. */
@media (max-width: 600px) {
  .credential-list-panel .credential-list-row {
    min-width: 0;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-template-rows: none;
    gap: 10px 14px;
    padding: 14px 4px;
  }
  .credential-list-panel .credential-list-row > [data-label] {
    min-width: 0;
    justify-self: stretch;
    text-align: left;
  }
  .credential-list-panel .credential-list-row > .credential-identity {
    grid-column: 1;
    grid-row: auto;
    align-self: start;
    min-width: 0;
  }
  .credential-list-panel .credential-list-row > .credential-actions {
    grid-column: 2;
    grid-row: 1;
    align-self: start;
    justify-self: end;
  }
  .credential-list-panel .credential-list-row > .credential-status-cell {
    grid-column: 1;
    grid-row: auto;
    justify-content: flex-start;
    text-align: left;
  }
  .credential-list-panel .credential-list-row > .credential-request-cell {
    grid-column: 2;
    grid-row: auto;
    justify-items: start;
    text-align: left;
  }
  .credential-list-panel .credential-list-row > .credential-quota-cell {
    grid-column: 1 / -1;
    grid-row: auto;
  }
  .credential-list-panel .credential-list-row > .credential-factor-cell {
    grid-column: 1;
    grid-row: auto;
    justify-self: stretch;
    text-align: left;
  }
  .credential-list-panel .credential-list-row > .credential-standard-cell {
    grid-column: 2;
    grid-row: auto;
    justify-self: stretch;
    text-align: left;
  }
  .credential-list-panel .credential-list-row > .credential-settled-cell {
    grid-column: 1;
    grid-row: auto;
    justify-self: stretch;
    text-align: left;
  }
  .credential-list-panel .credential-list-row > .credential-excluded-cell {
    grid-column: 2;
    grid-row: auto;
    text-align: left;
  }
  .credential-list-panel .credential-list-row > .credential-policy-cell {
    grid-column: 1 / -1;
    grid-row: auto;
    justify-content: flex-start;
    text-align: left;
  }
  .credential-list-panel .credential-list-row > .credential-updated-cell {
    grid-column: 1 / -1;
    grid-row: auto;
    justify-self: stretch;
    text-align: left;
  }
  .credential-list-panel .credential-list-row .credential-lock-summary { text-align: left; }
  .credential-list-panel .credential-list-row .credential-policy-label { display: inline; }
}

@media (prefers-reduced-motion: reduce) {
  .drawer,
  .drawer-backdrop,
  .button,
  .ai-tab,
  .workspace-tab { transition: none !important; }
}

/* Phone refinement: keep the signed-in header compact instead of stacking
   every desktop item into a tall column. */
@media (max-width: 600px) {
  .topbar {
    gap: 7px 10px;
    padding: 9px 16px 8px;
  }
  .topbar .brand { min-height: 30px; }
  .account-nav { gap: 7px; }
  .header-account-data { gap: 6px; }
  .header-metric {
    min-height: 42px;
    justify-content: center;
    border-radius: 7px;
    padding: 5px 9px;
  }
  .header-metric small { font-size: 10px; line-height: 14px; }
  .header-metric strong { font-size: 14px; line-height: 17px; }
  .header-identity {
    grid-template-columns: minmax(0, 1fr) auto auto auto;
    gap: 6px;
    padding-top: 7px;
  }
  .header-user-id {
    display: flex;
    grid-column: auto;
    align-items: center;
    gap: 6px;
  }
  .header-user-id small { flex: 0 0 auto; font-size: 10px; }
  .header-user-id-value { flex: 1 1 auto; gap: 4px; }
  .header-user-id code { width: 100%; font-size: 10px; line-height: 20px; }
  .header-copy-button { width: 28px; height: 32px; min-height: 32px; flex-basis: 28px; }
  .header-identity .button {
    min-width: 0;
    min-height: 36px;
    height: 36px;
    padding: 6px 10px;
    font-size: 12px;
  }
  #open-gift { min-width: 66px; }
  .header-session .button { min-width: 42px; }
  .content-viewport { padding-top: 10px; }
  .account-page { border-radius: 9px; }
  .account-details { padding: 0 14px; }
  .account-details > div { gap: 3px; padding: 10px 0; }
  .account-details dt { font-size: 11px; }
  .account-details dd { font-size: 13px; }
  .workspace-nav { padding-top: 3px; padding-bottom: 3px; }
}

@media (max-width: 360px) {
  .header-user-id {
    grid-column: 1 / -1;
  }
  .header-user-id code { width: min(180px, calc(100vw - 92px)); }
  .header-identity .button { padding-inline: 8px; }
}

/* Mobile first hierarchy for the signed-in shell and key cards. */
@media (max-width: 600px) {
  .header-account-data {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0;
    border: 1px solid #d9e2f2;
    border-radius: 10px;
    overflow: hidden;
    background: #fff;
  }
  .header-metric {
    min-height: 42px;
    border: 0;
    border-radius: 0;
    padding: 6px 8px;
    background: transparent;
  }
  .header-metric.balance-metric {
    grid-column: 1 / -1;
    min-height: 56px;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid #d9e2f2;
    padding: 9px 12px;
    background: #f2f6ff;
  }
  .header-metric.balance-metric small { font-size: 11px; color: #52627d; }
  .header-metric.balance-metric strong { color: #1f4fae; font-size: 21px; line-height: 24px; }
  .header-metric.balance-total,
  .header-metric.balance-reserved,
  .header-metric.points-metric { align-items: center; text-align: center; }
  .header-metric.balance-total,
  .header-metric.balance-reserved { border-right: 1px solid #e6ebf3; }
  .header-metric.points-metric { border-left: 0; padding-left: 8px; }
  .header-metric.balance-total small,
  .header-metric.balance-reserved small,
  .header-metric.points-metric small { font-size: 9px; }
  .header-metric.balance-total strong,
  .header-metric.balance-reserved strong,
  .header-metric.points-metric strong { font-size: 13px; }

  .key-row {
    min-width: 0;
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-rows: auto auto auto;
    gap: 9px 12px;
    padding: 14px 4px;
  }
  .key-row > .table-cell:nth-child(1) {
    grid-column: 1 !important;
    grid-row: 1 !important;
    align-self: start;
    min-width: 0;
  }
  .key-row > .table-cell:nth-child(2) {
    grid-column: 1 !important;
    grid-row: 2 !important;
    justify-self: start;
    text-align: left;
  }
  .key-row > .table-cell:nth-child(3) {
    grid-column: 2 !important;
    grid-row: 2 !important;
    justify-self: end;
    text-align: right;
  }
  .key-row > .table-cell:nth-child(4) {
    grid-column: 1 !important;
    grid-row: 3 !important;
    justify-self: start;
    text-align: left;
  }
  .key-row > .table-cell:nth-child(5) {
    grid-column: 2 !important;
    grid-row: 3 !important;
    justify-self: end;
    text-align: right;
  }
  .key-row > .table-cell:nth-child(6),
  .key-row > .table-cell:nth-child(7),
  .key-row > .table-cell:nth-child(8),
  .key-row > .table-cell:nth-child(9) { display: none; }
  .key-row > .record-actions {
    grid-column: 2;
    grid-row: 1;
    align-self: start;
    justify-self: end;
  }
  .key-row > .record-actions .button { min-height: 34px; }
  .key-row > .table-cell:first-child strong { max-width: 190px; font-size: 16px; }
  .key-row > [data-label]::before { display: block; margin-bottom: 2px; color: var(--subtle); font-size: 10px; line-height: 1.2; content: attr(data-label); }
}

/* Phone header has its own compact structure; desktop metrics remain intact. */
@media (max-width: 600px) {
  .topbar:has(#account-nav:not([hidden])) {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    gap: 0;
    min-height: 0;
    padding: 0 16px 4px;
    background: #fff;
  }
  .topbar:has(#account-nav:not([hidden])) .brand { min-height: 44px; }
  .topbar:has(#account-nav:not([hidden])) > .topbar-right { display: none; }
  .topbar:has(#account-nav:not([hidden])) > .mobile-header-actions { display: flex; }
  .topbar:has(#account-nav:not([hidden])) > .mobile-assets { display: flex; }
  .mobile-header-actions { position: relative; align-items: center; gap: 4px; }
  .mobile-header-actions > button {
    min-width: 44px; min-height: 44px; padding: 0 8px;
    background: transparent; color: var(--ink); font-size: 14px; cursor: pointer;
  }
  #mobile-gift { color: var(--accent); font-weight: 600; }
  #mobile-recharge { color: var(--accent); font-weight: 600; }
  #mobile-account-toggle { font-size: 24px; }
  #mobile-account-menu {
    position: absolute; top: 44px; right: 0; z-index: 15;
    min-width: 132px; padding: 4px; border: 1px solid var(--line);
    border-radius: 10px; background: #fff; box-shadow: 0 6px 20px #15203318;
  }
  #mobile-account-menu button {
    width: 100%; min-height: 44px; padding: 0 14px; border-radius: 6px;
    text-align: left; background: transparent; color: var(--red); font-size: 14px; cursor: pointer;
  }
  #mobile-account-menu button:hover { background: var(--surface-soft); }
  .mobile-assets {
    grid-column: 1 / -1; min-width: 0; align-items: center;
    justify-content: space-between; flex-wrap: wrap; column-gap: 12px;
  }
  #mobile-balance-toggle {
    display: flex; align-items: baseline; flex-wrap: wrap; gap: 5px;
    min-width: 0; max-width: 100%; min-height: 44px; padding: 10px 0;
    background: transparent; color: var(--muted); font-size: 14px; cursor: pointer;
    text-align: left; line-height: 24px;
  }
  #mobile-balance { color: var(--ink); font-size: 14px; font-weight: 650; overflow-wrap: anywhere; font-variant-numeric: tabular-nums; }
  .balance-chevron { font-size: 14px; }
  #mobile-balance-toggle[aria-expanded="true"] .balance-chevron { transform: rotate(180deg); }
  #mobile-reserved-dot { width: 6px; height: 6px; border-radius: 50%; background: #d58621; align-self: center; }
  .mobile-points-label { font-size: 14px; color: var(--muted); white-space: nowrap; }
  #mobile-points { color: var(--ink); margin-left: 4px; font-weight: 650; }
  #mobile-balance-details {
    display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px;
    width: 100%; margin: 0 0 8px; padding: 10px 12px;
    border-radius: 8px; background: var(--surface-soft);
  }
  #mobile-balance-details dt { font-size: 12px; color: var(--muted); }
  #mobile-balance-details dd { margin: 4px 0 0; font-size: 14px; color: var(--ink); overflow-wrap: anywhere; font-variant-numeric: tabular-nums; }
}
