/* Interfaz tipo app, activa unicamente en celulares y tablets pequenas. */
@media (max-width: 768px) {
  html {
    width: 100%;
    min-height: 100%;
    overflow-x: hidden;
    background: #151517;
    -webkit-text-size-adjust: 100%;
  }

  body {
    width: 100%;
    min-width: 0;
    min-height: 100dvh;
    overflow-x: hidden;
    background: #151517;
  }

  /* Barra superior de la app. */
  .navbar {
    position: sticky;
    top: 0;
    z-index: 100;
    width: 100%;
    height: 60px;
    min-width: 0;
    gap: 0.6rem;
    padding: 0 max(0.9rem, env(safe-area-inset-right)) 0 max(0.9rem, env(safe-area-inset-left));
    background: rgba(8, 8, 9, 0.95);
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
    backdrop-filter: blur(18px);
    -webkit-backdrop-filter: blur(18px);
  }

  .navbar .logo {
    flex: 0 0 auto;
    max-width: 38vw;
    overflow: hidden;
    font-size: 1.1rem;
    line-height: 1;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .navbar .user-info {
    display: flex;
    flex: 1 1 auto;
    min-width: 0;
    gap: 0.45rem;
    align-items: center;
    justify-content: flex-end;
  }

  .navbar .username {
    min-width: 0;
    max-width: min(30vw, 8rem);
    overflow: hidden;
    font-size: 0.75rem;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .navbar .btn {
    flex: 0 0 auto;
    min-height: 34px;
    padding: 0.38rem 0.65rem;
    font-size: 0 !important;
    border-width: 1px;
    border-radius: 8px;
  }

  .navbar .btn::after {
    content: "Salir";
    font-size: 0.75rem !important;
  }

  /* Navegacion inferior fija, como una app nativa. */
  .tabs {
    position: fixed;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 200;
    display: flex;
    width: 100%;
    max-width: 100vw;
    height: calc(68px + env(safe-area-inset-bottom));
    padding: 4px max(4px, env(safe-area-inset-right)) env(safe-area-inset-bottom) max(4px, env(safe-area-inset-left));
    overflow-x: auto;
    overflow-y: hidden;
    overscroll-behavior-x: contain;
    scroll-snap-type: x mandatory;
    scrollbar-width: none;
    background: rgba(10, 10, 11, 0.97);
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    border-bottom: 0;
    box-shadow: 0 -12px 32px rgba(0, 0, 0, 0.34);
    backdrop-filter: blur(20px) saturate(135%);
    -webkit-backdrop-filter: blur(20px) saturate(135%);
    -webkit-overflow-scrolling: touch;
  }

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

  .tab {
    position: relative;
    display: flex;
    flex: 1 0 60px;
    min-width: 60px;
    min-height: 60px;
    padding: 4px 3px 5px;
    gap: 2px;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    color: #8d8d96;
    font-size: 0.64rem;
    font-weight: 600;
    line-height: 1.05;
    text-align: center;
    white-space: normal;
    border: 0;
    border-radius: 10px;
    scroll-snap-align: start;
    touch-action: manipulation;
    transition: color 160ms ease, background 160ms ease, transform 160ms ease;
  }

  .tab::before {
    display: block;
    height: 22px;
    color: currentColor;
    font-size: 1.25rem;
    font-weight: 400;
    line-height: 22px;
  }

  .tab[onclick*="dashboard"]::before { content: "⌂"; }
  .tab[onclick*="search"]::before { content: "⌕"; }
  .tab[onclick*="inbox"]::before { content: "✉"; font-size: 1.04rem; }
  .tab[onclick*="accounts"]::before { content: "▣"; font-size: 1.06rem; }
  .tab[onclick*="events"]::before { content: "◷"; font-size: 1.16rem; }
  .tab[onclick*="subusers"]::before { content: "♙"; font-size: 1.08rem; }

  .tab.active {
    color: #ff2635;
    background: rgba(229, 9, 20, 0.09);
    border-bottom-color: transparent;
  }

  .tab.active::after {
    position: absolute;
    top: 0;
    width: 22px;
    height: 3px;
    content: "";
    background: var(--red);
    border-radius: 0 0 4px 4px;
  }

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

  /* Area principal con espacio para la barra inferior. */
  .content {
    width: 100%;
    max-width: 100%;
    margin: 0;
    padding: 1rem 1rem calc(84px + env(safe-area-inset-bottom));
  }

  .tab-pane,
  .card,
  .stat-card,
  .table-wrap {
    min-width: 0;
    max-width: 100%;
  }

  /* Resumen compacto en una sola fila. */
  .stats-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0.55rem;
    margin-bottom: 1rem;
  }

  .stat-card,
  .stat-card:last-child:nth-child(odd) {
    grid-column: auto;
    min-height: 105px;
    padding: 0.8rem 0.7rem;
    background: #202022;
    border-color: rgba(255, 255, 255, 0.08);
    border-radius: 13px;
    box-shadow: 0 5px 18px rgba(0, 0, 0, 0.13);
  }

  .stat-label {
    margin-bottom: 0.55rem;
    font-size: clamp(0.55rem, 2.35vw, 0.68rem);
    line-height: 1.35;
    letter-spacing: 0.035em;
  }

  .stat-value {
    font-size: clamp(1.45rem, 7vw, 1.85rem);
    line-height: 1;
  }

  .card {
    margin-bottom: 1rem;
    padding: 1rem;
    background: #202022;
    border-color: rgba(255, 255, 255, 0.08);
    border-radius: 14px;
    box-shadow: 0 6px 22px rgba(0, 0, 0, 0.14);
  }

  .card h3 { font-size: 1.05rem; line-height: 1.35; }

  .section-header {
    min-width: 0;
    gap: 0.65rem;
    align-items: center;
  }

  .section-title {
    min-width: 0;
    font-size: 0.92rem;
    line-height: 1.35;
  }

  .btn {
    min-height: 42px;
    padding: 0.62rem 0.9rem;
    border-radius: 8px;
    touch-action: manipulation;
  }

  .btn-sm { min-height: 38px; padding: 0.5rem 0.72rem; }

  /* Controles tactiles y sin zoom automatico en iPhone. */
  input[type="text"],
  input[type="email"],
  input[type="password"],
  input[type="number"],
  input[type="search"],
  select,
  textarea {
    max-width: 100%;
    min-height: 44px;
    font-size: 16px !important;
  }

  .input-clearable { width: 100%; min-width: 0; }
  .input-clearable .clear-btn { min-width: 42px; min-height: 42px; }

  #tab-search .card > div[style*="display:flex"] {
    display: grid !important;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.55rem !important;
  }

  #tab-search .card > div[style*="display:flex"] .btn {
    width: 100%;
    height: auto;
    white-space: normal;
  }

  #tab-subusers .card > div[style*="grid-template-columns"] {
    display: grid !important;
    grid-template-columns: 1fr !important;
    gap: 0.8rem !important;
  }

  #tab-subusers #su-create-btn { width: 100%; }

  #scope-card > div,
  #email-owner-card > div,
  #bulk-add-card > div { align-items: stretch !important; }

  #scope-selector { width: 100%; min-width: 0 !important; }

  /* Tablas desplazables sin ensanchar toda la pagina. */
  .table-wrap {
    width: 100%;
    overflow-x: auto !important;
    overscroll-behavior-x: contain;
    scrollbar-width: thin;
    border-radius: 8px;
    -webkit-overflow-scrolling: touch;
  }

  .table-wrap table {
    width: max-content;
    min-width: 100%;
    font-size: 0.8rem;
  }

  th,
  td {
    max-width: 16rem;
    padding: 0.65rem 0.75rem;
    overflow-wrap: anywhere;
  }

  tbody tr:last-child td { border-bottom: 0; }

  #tab-inbox .section-header {
    padding: 1rem !important;
    align-items: stretch !important;
    flex-direction: column;
  }

  #inbox-search {
    width: 100% !important;
    max-width: none !important;
    min-width: 0 !important;
  }

  #inbox-list { max-height: calc(100dvh - 220px) !important; }
  .code-box { padding: 1rem; }
  .code-value { font-size: clamp(1.35rem, 8vw, 2rem); overflow-wrap: anywhere; }

  .toast {
    top: max(0.75rem, env(safe-area-inset-top));
    right: 0.75rem;
    left: 0.75rem;
    width: auto;
    max-width: none;
    padding: 0.8rem 1rem;
  }
}

@media (max-width: 350px) {
  .content { padding-inline: 0.7rem; }
  .navbar .logo { max-width: 32vw; font-size: 1rem; }
  .navbar .username { max-width: 21vw; }
  .stats-grid { gap: 0.35rem; }
  .stat-card { padding-inline: 0.5rem; }
  #tab-search .card > div[style*="display:flex"] { grid-template-columns: 1fr; }
}

@media (max-width: 768px) and (orientation: landscape) {
  .navbar { height: 54px; }
}

/* Tablas convertidas en tarjetas para eliminar el scroll horizontal. */
@media (max-width: 768px) {
  /* Solo cuatro secciones principales y el acceso Mas en la barra. */
  .tabs > #tab-link-accounts,
  .tabs > #tab-link-subusers {
    display: none !important;
  }

  .tabs > .tab[onclick*="dashboard"] { order: 1; }
  .tabs > .tab[onclick*="search"] { order: 2; }
  .tabs > .tab[onclick*="inbox"] { order: 3; }
  .tabs > .tab[onclick*="events"] { order: 4; }
  .tabs > .mobile-nav-more { order: 5; }

  .tabs > .tab,
  .tabs > .mobile-nav-more {
    flex: 1 1 20%;
    min-width: 0;
    max-width: 20%;
  }

  .mobile-nav-more {
    position: relative;
    display: flex;
    min-height: 60px;
    padding: 4px 3px 5px;
    gap: 2px;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    color: #8d8d96;
    font-family: inherit;
    font-size: 0.64rem;
    font-weight: 600;
    line-height: 1.05;
    text-align: center;
    background: transparent;
    border: 0;
    border-radius: 10px;
    touch-action: manipulation;
  }

  .mobile-nav-more::before {
    display: block;
    height: 22px;
    content: "•••";
    color: currentColor;
    font-size: 1.05rem;
    line-height: 16px;
    letter-spacing: 0.08em;
  }

  .mobile-nav-more.active {
    color: #ff2635;
    background: rgba(229, 9, 20, 0.09);
  }

  .mobile-nav-more.active::after {
    position: absolute;
    top: 0;
    width: 22px;
    height: 3px;
    content: "";
    background: var(--red);
    border-radius: 0 0 4px 4px;
  }

  .mobile-more-backdrop {
    position: fixed;
    inset: 0;
    z-index: 210;
    display: none;
    background: rgba(0, 0, 0, 0.48);
    opacity: 0;
    transition: opacity 160ms ease;
  }

  .mobile-more-backdrop.open {
    display: block;
    opacity: 1;
  }

  .mobile-more-menu {
    position: fixed;
    right: 0.75rem;
    bottom: calc(76px + env(safe-area-inset-bottom));
    left: 0.75rem;
    z-index: 220;
    display: none;
    max-height: calc(100dvh - 160px - env(safe-area-inset-bottom));
    padding: 0.75rem;
    overflow-x: hidden;
    overflow-y: auto;
    background: rgba(31, 31, 33, 0.98);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 16px;
    box-shadow: 0 20px 55px rgba(0, 0, 0, 0.5);
    transform: translateY(10px);
    opacity: 0;
    backdrop-filter: blur(22px);
    -webkit-backdrop-filter: blur(22px);
  }

  .mobile-more-menu.open {
    display: block;
    transform: translateY(0);
    opacity: 1;
    animation: mobile-more-in 170ms ease-out;
  }

  .mobile-more-title {
    padding: 0.35rem 0.45rem 0.65rem;
    color: var(--text2);
    font-size: 0.7rem;
    font-weight: 700;
    letter-spacing: 0.07em;
    text-transform: uppercase;
  }

  .mobile-more-item {
    display: flex;
    width: 100%;
    min-height: 50px;
    margin-top: 0.35rem;
    padding: 0.75rem 0.9rem;
    gap: 0.75rem;
    align-items: center;
    color: var(--text);
    font-family: inherit;
    font-size: 0.9rem;
    font-weight: 600;
    text-align: left;
    background: rgba(255, 255, 255, 0.045);
    border: 1px solid rgba(255, 255, 255, 0.065);
    border-radius: 10px;
  }

  .mobile-more-item::before {
    width: 24px;
    color: var(--red);
    font-size: 1.15rem;
    text-align: center;
  }

  .mobile-more-item[data-tab="dashboard"]::before { content: "⌂"; }
  .mobile-more-item[data-tab="search"]::before { content: "⌕"; }
  .mobile-more-item[data-tab="inbox"]::before { content: "✉"; font-size: 1rem; }
  .mobile-more-item[data-tab="accounts"]::before { content: "▣"; }
  .mobile-more-item[data-tab="events"]::before { content: "◷"; }
  .mobile-more-item[data-tab="subusers"]::before { content: "♙"; }
  .mobile-more-item:active { background: rgba(229, 9, 20, 0.12); }

  @keyframes mobile-more-in {
    from { transform: translateY(10px); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
  }

  #tab-dashboard .table-wrap,
  #tab-events .table-wrap,
  #tab-subusers .table-wrap,
  #tab-accounts .table-wrap {
    overflow: visible !important;
    border-radius: 0;
  }

  #tab-dashboard table,
  #tab-events table,
  #tab-subusers table,
  #tab-accounts table,
  #accounts-table,
  #events-table,
  #subusers-table,
  #accounts-tbody {
    display: block;
    width: 100%;
    min-width: 0;
  }

  #tab-dashboard thead,
  #tab-events thead,
  #tab-subusers thead,
  #tab-accounts thead {
    display: none;
  }

  #accounts-table tr,
  #events-table tr,
  #subusers-table tr,
  #accounts-tbody tr {
    display: grid;
    width: 100%;
    margin-bottom: 0.7rem;
    padding: 0.75rem;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    column-gap: 0.75rem;
    row-gap: 0.55rem;
    background: #272729;
    border: 1px solid rgba(255, 255, 255, 0.075);
    border-radius: 11px;
    box-shadow: 0 3px 12px rgba(0, 0, 0, 0.1);
  }

  #accounts-table tr:last-child,
  #events-table tr:last-child,
  #subusers-table tr:last-child,
  #accounts-tbody tr:last-child {
    margin-bottom: 0;
  }

  #accounts-table td,
  #events-table td,
  #subusers-table td,
  #accounts-tbody td {
    display: flex;
    min-width: 0;
    max-width: none;
    padding: 0;
    gap: 0.2rem;
    align-items: flex-start;
    flex-direction: column;
    overflow-wrap: anywhere;
    border: 0;
    background: transparent !important;
    color: var(--text);
    font-size: 0.8rem;
    line-height: 1.35;
  }

  #accounts-table td::before,
  #events-table td::before,
  #subusers-table td::before,
  #accounts-tbody td::before {
    display: block;
    margin-bottom: 0.08rem;
    color: var(--text3);
    font-size: 0.61rem;
    font-weight: 700;
    line-height: 1.2;
    letter-spacing: 0.055em;
    text-transform: uppercase;
  }

  /* Resumen de cuentas del dashboard. */
  #accounts-table td:nth-child(1) { grid-column: 1 / -1; }
  #accounts-table td:nth-child(1)::before { content: "Email"; }
  #accounts-table td:nth-child(2)::before { content: "Eventos"; }
  #accounts-table td:nth-child(3)::before { content: "Ultimo codigo"; }
  #accounts-table td:nth-child(4)::before { content: "Desde"; }

  /* Historial de eventos. */
  #events-table td:nth-child(1) { grid-column: 1 / -1; }
  #events-table td:nth-child(1)::before { content: "Email"; }
  #events-table td:nth-child(2)::before { content: "Tipo"; }
  #events-table td:nth-child(3)::before { content: "Detalles"; }
  #events-table td:nth-child(4)::before { content: "Fecha"; }
  #events-table td:nth-child(3) { grid-column: 1 / -1; }

  /* Lista de subusuarios. */
  #subusers-table td:nth-child(1)::before { content: "Usuario"; }
  #subusers-table td:nth-child(2)::before { content: "Email"; }
  #subusers-table td:nth-child(3)::before { content: "Creado"; }
  #subusers-table td:nth-child(4)::before { content: "Acciones"; }
  #subusers-table td:nth-child(1),
  #subusers-table td:nth-child(2),
  #subusers-table td:nth-child(4) {
    grid-column: 1 / -1;
  }

  #subusers-table td:nth-child(4) {
    display: flex;
    gap: 0.45rem;
    align-items: stretch;
    flex-flow: row wrap;
  }

  #subusers-table td:nth-child(4)::before {
    width: 100%;
  }

  #subusers-table td:nth-child(4) .btn {
    flex: 1 1 9rem;
  }

  /* Gestion de cuentas. */
  #accounts-tbody td:nth-child(1)::before { content: "Seleccionar"; }
  #accounts-tbody td:nth-child(2)::before { content: "Email"; }
  #accounts-tbody td:nth-child(3)::before { content: "Agregado"; }
  #accounts-tbody td:nth-child(4)::before { content: "Acciones"; }
  #accounts-tbody td:nth-child(2),
  #accounts-tbody td:nth-child(4) {
    grid-column: 1 / -1;
  }

  #accounts-tbody td:nth-child(4) {
    display: flex !important;
    gap: 0.45rem !important;
    align-items: stretch;
    flex-flow: row wrap !important;
  }

  #accounts-tbody td:nth-child(4)::before { width: 100%; }
  #accounts-tbody td:nth-child(4) .btn { flex: 1 1 8rem; }

  /* Mensajes de carga y estados vacios no necesitan etiquetas. */
  #accounts-table td[colspan],
  #events-table td[colspan],
  #subusers-table td[colspan],
  #accounts-tbody td[colspan] {
    display: block;
    grid-column: 1 / -1;
    padding: 1rem;
    text-align: center;
  }

  #accounts-table td[colspan]::before,
  #events-table td[colspan]::before,
  #subusers-table td[colspan]::before,
  #accounts-tbody td[colspan]::before {
    display: none;
  }
}
