/* TW IP BARE Portal — Mobile UX */

:root {
  --tw-topbar-h: 60px;
  --tw-admin-bar-h: 0px;
  --tw-safe-top: env(safe-area-inset-top, 0px);
  --tw-safe-bottom: env(safe-area-inset-bottom, 0px);
  --tw-safe-left: env(safe-area-inset-left, 0px);
  --tw-safe-right: env(safe-area-inset-right, 0px);
}

/* iOS: 16px+ inputs avoid focus zoom */
@media (max-width: 768px) {
  input:not([type="checkbox"]):not([type="radio"]):not([type="range"]),
  select,
  textarea,
  .tw-form-input,
  .tw-admin-input,
  .auth-form input {
    font-size: 16px !important;
  }
}

/* Touch-friendly tap targets */
html.tw-touch .tw-btn,
html.tw-touch .tw-sidebar-link,
html.tw-touch .tw-admin-nav-item,
html.tw-touch .tw-nav-link,
html.tw-touch .tw-order-tab,
html.tw-touch .tw-inbox-filter {
  min-height: 44px;
}

html.tw-touch .tw-sidebar-link {
  padding-top: 12px;
  padding-bottom: 12px;
}

/* Safe area padding */
body.tw-site {
  padding-left: var(--tw-safe-left);
  padding-right: var(--tw-safe-right);
}

.tw-topbar,
.tw-nav,
.tw-admin-topbar {
  padding-top: max(0px, var(--tw-safe-top));
}

.tw-sidebar-foot,
.tw-form-sticky,
.tw-admin-toast {
  padding-bottom: max(12px, var(--tw-safe-bottom));
}

/* ─── Public nav ───────────────────────────────────────────── */

body.tw-nav-open {
  overflow: hidden;
}

@media (max-width: 640px) {
  .tw-nav-inner {
    width: min(100% - 16px, var(--tw-container));
  }

  .tw-brand-text {
    max-width: 42vw;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .tw-nav-menu {
    max-height: calc(100dvh - var(--tw-nav-h) - var(--tw-safe-top));
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .tw-nav-link,
  .tw-nav-auth .tw-btn {
    width: 100%;
    justify-content: center;
  }

  .tw-footer-grid {
    grid-template-columns: 1fr;
    gap: 24px;
  }

  .tw-page {
    padding: 32px 0 48px;
  }

  .tw-container {
    width: min(100% - 20px, var(--tw-container));
  }
}

/* ─── User app shell ───────────────────────────────────────── */

@media (max-width: 768px) {
  .tw-topbar {
    height: auto;
    min-height: var(--tw-topbar-h);
    padding: 8px 12px;
    gap: 8px;
    flex-wrap: wrap;
  }

  .tw-topbar-brand .tw-brand-text {
    display: none;
  }

  .tw-topbar-actions {
    margin-left: auto;
    flex-wrap: wrap;
    gap: 6px;
  }

  .tw-topbar-actions > .tw-btn-primary.tw-btn-sm {
    padding: 0 12px;
    font-size: 0.8rem;
  }

  .tw-sidebar {
    position: fixed;
    top: calc(var(--tw-topbar-h) + var(--tw-safe-top));
    bottom: 0;
    left: 0;
    z-index: 80;
    width: min(88vw, var(--tw-sidebar-w));
    transform: translateX(-105%);
    transition: transform 0.28s cubic-bezier(0.22, 1, 0.36, 1);
    box-shadow: 8px 0 40px rgba(0, 0, 0, 0.45);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .tw-sidebar.is-open {
    transform: translateX(0);
  }

  .tw-sidebar-backdrop {
    top: calc(var(--tw-topbar-h) + var(--tw-safe-top));
    backdrop-filter: blur(6px);
  }

  .tw-main {
    padding: 16px 14px calc(20px + var(--tw-safe-bottom));
  }

  .tw-dash-hero {
    padding: 20px 16px 18px;
    margin-bottom: 20px;
  }

  .tw-dash-hero h1 {
    font-size: 1.45rem;
  }

  .tw-dash-hero-meta {
    gap: 8px;
  }

  .tw-dash-pill {
    font-size: 0.7rem;
    padding: 5px 10px;
  }

  .tw-dash-grid {
    grid-template-columns: 1fr;
    gap: 16px;
  }

  .tw-dash-actions {
    flex-direction: column;
  }

  .tw-dash-actions .tw-btn {
    width: 100%;
  }

  .tw-settings-grid {
    grid-template-columns: 1fr 1fr;
  }

  .tw-table-wrap,
  .tw-admin-table-wrap {
    margin: 0 -2px;
    -webkit-overflow-scrolling: touch;
    overflow-x: auto;
    border-radius: var(--tw-r);
  }

  .tw-table {
    min-width: 560px;
  }

  .tw-table th,
  .tw-table td {
    padding: 10px 10px;
    white-space: nowrap;
  }

  .tw-mail-popup {
    position: fixed;
    left: 8px;
    right: 8px;
    width: auto;
    max-height: min(70vh, 420px);
  }
}

@media (max-width: 400px) {
  .tw-settings-grid {
    grid-template-columns: 1fr;
  }

  .tw-topbar-actions > .tw-btn-ghost:not(.tw-mail-btn):not(.tw-admin-topbar-link) {
    display: none;
  }
}

/* ─── Order / form pages ───────────────────────────────────── */

@media (max-width: 768px) {
  .tw-order-tabs {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    gap: 8px;
    padding-bottom: 4px;
    margin-bottom: 1rem;
    scrollbar-width: none;
  }

  .tw-order-tabs::-webkit-scrollbar {
    display: none;
  }

  .tw-order-tab {
    flex-shrink: 0;
    white-space: nowrap;
  }

  .tw-form-sticky {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 40;
    margin: 0;
    padding: 10px 14px calc(10px + var(--tw-safe-bottom));
    background: linear-gradient(180deg, transparent, rgba(3, 5, 8, 0.92) 24%, rgba(3, 5, 8, 0.98));
    border-top: 1px solid rgba(0, 212, 255, 0.12);
  }

  .tw-form-sticky .tw-turnstile-wrap {
    display: flex;
    margin: 0 0 8px;
    min-height: 0;
    justify-content: center;
  }

  .tw-order-page .tw-main,
  body.app-shell .tw-order-page {
    padding-bottom: calc(140px + var(--tw-safe-bottom));
  }

  .tw-order-panel[hidden] .tw-turnstile-wrap,
  form[hidden] .tw-turnstile-wrap {
    display: none !important;
  }
}

/* ─── Inbox ────────────────────────────────────────────────── */

@media (max-width: 860px) {
  .tw-inbox-head {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }

  .tw-inbox-toolbar {
    width: 100%;
    display: flex;
    gap: 8px;
  }

  .tw-inbox-toolbar .tw-btn {
    flex: 1;
  }

  .tw-inbox-filters {
    overflow-x: auto;
    flex-wrap: nowrap;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 4px;
  }

  .tw-inbox-filter {
    flex-shrink: 0;
  }
}

/* ─── Auth pages ───────────────────────────────────────────── */

@media (max-width: 480px) {
  .auth-shell {
    padding: 24px 14px calc(32px + var(--tw-safe-bottom));
    align-items: flex-start;
  }

  .auth-card {
    padding: 24px 18px;
    border-radius: var(--tw-r);
  }

  .auth-card h1 {
    font-size: 1.3rem;
  }
}

/* ─── Home page ────────────────────────────────────────────── */

@media (max-width: 900px) {
  .tw-hero {
    padding: 40px 0 56px;
  }

  .tw-hero-actions {
    flex-direction: column;
  }

  .tw-hero-actions .tw-btn {
    width: 100%;
  }

  .tw-hero-stats {
    gap: 20px;
    justify-content: space-between;
  }

  .tw-hero-stats li {
    min-width: calc(50% - 12px);
  }

  .tw-stats-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
  }
}

@media (max-width: 480px) {
  .tw-hero-stats li {
    min-width: 100%;
  }

  .tw-stats-grid {
    grid-template-columns: 1fr;
  }

  .tw-jupiter-canvas {
    width: min(100%, 260px) !important;
  }
}

/* ─── Admin panel ──────────────────────────────────────────── */

.tw-admin-sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 95;
  border: none;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(4px);
  cursor: pointer;
}

body.tw-admin-sidebar-open .tw-admin-sidebar-backdrop {
  display: block;
}

body.tw-admin-sidebar-open {
  overflow: hidden;
}

@media (max-width: 900px) {
  .tw-admin-sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 100;
    width: min(88vw, var(--tw-admin-sidebar-w));
    transform: translateX(-105%);
    transition: transform 0.28s cubic-bezier(0.22, 1, 0.36, 1);
    box-shadow: 8px 0 48px rgba(0, 0, 0, 0.5);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .tw-admin-sidebar.is-open {
    transform: translateX(0);
  }

  .tw-admin-menu-btn {
    display: flex !important;
    align-items: center;
    justify-content: center;
    min-width: 44px;
    min-height: 44px;
  }

  .tw-admin-topbar {
    padding: 10px 14px;
    flex-wrap: wrap;
    gap: 10px;
  }

  .tw-admin-topbar h1 {
    font-size: 1.05rem;
    flex: 1 1 100%;
    order: 2;
  }

  .tw-admin-topbar-actions {
    margin-left: auto;
    order: 1;
  }

  .tw-admin-live span:not(.tw-live-dot) {
    display: none;
  }

  .tw-admin-panels {
    padding: 14px 12px calc(16px + var(--tw-safe-bottom));
  }

  .tw-admin-toolbar {
    flex-direction: column;
    align-items: stretch;
  }

  .tw-admin-toolbar .tw-admin-input,
  .tw-admin-toolbar select {
    width: 100%;
    min-width: 0;
  }

  .tw-admin-stats {
    grid-template-columns: repeat(2, 1fr);
    gap: 10px;
  }

  .tw-stat-card {
    padding: 14px;
  }

  .tw-stat-card strong {
    font-size: 1.25rem !important;
  }

  .tw-admin-table-wrap .tw-rd-table .tw-admin-table {
    min-width: 680px;
  }

  .tw-admin-user-actions .tw-btn,
  .tw-data-card-foot .tw-btn.tw-btn-primary,
  .tw-data-card-foot .tw-btn.tw-btn-sm[data-kyc-action] {
    flex: 1 1 100%;
    min-height: 44px;
  }

  .tw-data-card-head {
    flex-direction: column;
    align-items: stretch;
  }

  .tw-admin-user-badges,
  .tw-data-card-head .tw-badge {
    justify-content: flex-start;
  }

  .tw-data-card-head .tw-admin-user-badges,
  .tw-data-card-head > span:not(:only-child) {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
  }

  .kyc-status-tabs {
    gap: 6px;
  }

  .kyc-status-tab {
    flex: 1 1 calc(50% - 6px);
    min-height: 44px;
    text-align: center;
  }

  .tw-admin-modal {
    padding: 0;
    align-items: flex-end;
  }

  .tw-admin-modal-card,
  .tw-admin-modal-wide {
    width: 100%;
    max-width: 100%;
    max-height: 92dvh;
    border-radius: 16px 16px 0 0;
    margin: 0;
  }

  .tw-admin-toast {
    left: 12px;
    right: 12px;
    bottom: calc(12px + var(--tw-safe-bottom));
    text-align: center;
  }

  .tw-admin-form-grid {
    grid-template-columns: 1fr !important;
  }
}

@media (max-width: 400px) {
  .tw-admin-stats {
    grid-template-columns: 1fr;
  }
}

/* ─── Community ────────────────────────────────────────────── */

@media (max-width: 540px) {
  .tw-community-hero h1 {
    font-size: 1.5rem;
  }
}

/* Modals (user dashboard delivery) */
@media (max-width: 640px) {
  .tw-dash-modal {
    padding: 0;
    align-items: flex-end;
  }

  .tw-dash-modal-card {
    width: 100%;
    max-width: 100%;
    max-height: 92dvh;
    border-radius: 16px 16px 0 0;
    margin: 0;
  }
}

/* Admin bar + app shell stacking */
body:has(.tw-admin-bar) {
  --tw-admin-bar-h: 40px;
}

body:has(.tw-admin-bar) .tw-topbar-actions a.tw-btn-ghost[href*="admin"] {
  display: none;
}

body:has(.tw-admin-bar) .tw-sidebar,
body:has(.tw-admin-bar) .tw-sidebar-backdrop {
  top: calc(var(--tw-topbar-h) + var(--tw-admin-bar-h) + var(--tw-safe-top));
}

@media (max-width: 768px) {
  body:has(.tw-admin-bar) .tw-admin-bar {
    position: sticky;
    top: 0;
    z-index: 95;
    padding: 8px 12px;
  }

  body:has(.tw-admin-bar) .tw-topbar {
    top: var(--tw-admin-bar-h);
  }

  .tw-topbar-actions {
    flex-wrap: nowrap;
    gap: 6px;
  }

  .tw-topbar-actions .tw-btn-primary.tw-btn-sm {
    padding: 0 10px;
    font-size: 0.75rem;
    white-space: nowrap;
  }

  .tw-mail-btn {
    min-width: 44px;
    min-height: 44px;
  }

  .tw-sidebar-toggle {
    min-width: 44px;
    min-height: 44px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .tw-sidebar,
  .tw-admin-sidebar {
    transition: none !important;
  }
}
