/* ==========================================================================
   Millisa Developer Console - Responsive Layer
   Loaded after style.css. Layout-only fixes for phones and tablets.
   Never changes `display` of elements that JS shows/hides.
   ========================================================================== */

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

/* Long code, URLs and keys never push the page sideways */
.tab-content {
  overflow-wrap: anywhere;
}

.tab-content pre,
.tab-content code {
  overflow-wrap: normal;
}

.tab-content pre {
  max-width: 100%;
  overflow-x: auto;
}

/* ──────────────────────────────────────────────────────────────────────────
   TABLETS & PHONES (matches the drawer breakpoint in style.css)
   ────────────────────────────────────────────────────────────────────────── */
@media (max-width: 860px) {
  /* Top bar: one clean row */
  .top-header {
    padding: 0 0.75rem;
    gap: 0.5rem;
  }

  .header-left {
    flex: 1;
    min-width: 0;
    gap: 0.6rem;
  }

  .header-breadcrumbs {
    min-width: 0;
  }

  .breadcrumb-root,
  .breadcrumb-sep {
    display: none;
  }

  .breadcrumb-current {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: 0.95rem;
  }

  .header-actions {
    flex-shrink: 0;
    gap: 0.4rem;
  }

  .env-pill {
    display: none;
  }

  /* "Millisa Chat" link becomes an icon button (also in the drawer) */
  .header-actions > a.btn[href="/"] {
    width: 34px;
    height: 34px;
    padding: 0 !important;
    justify-content: center;
  }

  .header-actions > a.btn[href="/"] span {
    display: none;
  }

  .header-wallet-chip {
    padding: 0.32rem 0.6rem;
    white-space: nowrap;
  }

  /* Profile badge: avatar + sign out only */
  .user-profile-badge {
    padding: 0.2rem 0.3rem 0.2rem 0.25rem;
    gap: 0.25rem;
  }

  .user-profile-badge > span,
  .user-profile-badge .role-badge {
    display: none;
  }

  .user-profile-badge .btn {
    white-space: nowrap;
  }

  /* Bottom tab bar respects the iPhone home indicator */
  .mobile-bottom-nav {
    height: calc(var(--bottom-nav-height) + env(safe-area-inset-bottom, 0px));
  }

  .tab-content {
    padding-bottom: calc(var(--bottom-nav-height) + env(safe-area-inset-bottom, 0px) + 1.25rem);
  }

  /* The chat tab stretches to full height behind the fixed tab bar,
     so reserve the bar's space at the bottom */
  #tab-chat {
    padding-bottom: calc(var(--bottom-nav-height) + env(safe-area-inset-bottom, 0px) + 0.5rem);
  }

  .stats-grid {
    grid-template-columns: repeat(auto-fit, minmax(165px, 1fr));
  }

  /* Chat tab */
  .chat-subbar {
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  .chat-subbar > div {
    flex: 1;
    min-width: 0;
  }

  .model-badge-select {
    flex: 1;
    min-width: 0;
    max-width: 100%;
  }

  /* Page headers: title, then actions on their own wrapping row */
  .tab-header,
  .page-header,
  .section-header {
    flex-wrap: wrap;
    gap: 0.75rem;
  }

  /* Tables in card mode: no double card frame, values wrap inside the card */
  .panel-card:has(> .table-responsive) {
    padding: 0;
    background: transparent;
    border: none;
    box-shadow: none;
  }

  .table-responsive:has(> .data-table) {
    border: none;
    background: transparent;
    overflow-x: visible;
  }

  .data-table td {
    flex-wrap: wrap;
    gap: 0.35rem 0.75rem;
    min-width: 0;
  }

  .data-table td > * {
    max-width: 100%;
    min-width: 0;
  }

  .data-table td > div,
  .data-table td > span {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 0.3rem;
    text-align: right;
  }

  /* Sub-tab button rows scroll sideways instead of overflowing */
  #tab-users > div:has(> #subtab-btn-devs) {
    overflow-x: auto;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }

  #tab-users > div:has(> #subtab-btn-devs) > .btn {
    flex-shrink: 0;
    white-space: nowrap;
  }

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

/* ──────────────────────────────────────────────────────────────────────────
   PHONES
   ────────────────────────────────────────────────────────────────────────── */
@media (max-width: 480px) {
  /* Two stat cards per row instead of one tall column */
  .stats-grid,
  .wallet-stats-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.6rem;
  }

  .stats-grid .stat-card,
  .wallet-stats-grid .stat-card {
    padding: 0.85rem;
    min-width: 0;
  }

  /* Hero balance card and any leftover last card span the full row */
  .wallet-stats-grid .wallet-hero-card,
  .wallet-stats-grid > .stat-card:last-child:nth-child(even) {
    grid-column: 1 / -1;
  }

  .stat-value {
    font-size: 1.35rem;
  }

  .stat-title {
    font-size: 0.68rem;
  }

  .stat-subtext {
    font-size: 0.72rem;
  }

  .top-header {
    padding: 0 0.6rem;
  }

  .mobile-menu-btn {
    flex-shrink: 0;
  }
}

@media (max-width: 360px) {
  .header-wallet-chip {
    display: none !important;
  }
}

/* ──────────────────────────────────────────────────────────────────────────
   ADMIN → IMAGE GENERATION MODELS (per-audience checkbox grid)
   ────────────────────────────────────────────────────────────────────────── */
.image-models-table-wrap {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border: 1px solid var(--border-medium);
  border-radius: var(--radius-md);
}

.image-models-table {
  width: 100%;
  min-width: 520px;
  border-collapse: collapse;
  font-size: 0.82rem;
}

.image-models-table th {
  text-align: center;
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-tertiary);
  padding: 0.6rem 0.5rem;
  border-bottom: 1px solid var(--border-medium);
  white-space: nowrap;
}

.image-models-table th:first-child,
.image-models-table td:first-child {
  text-align: left;
  padding-left: 0.85rem;
}

.image-models-table td {
  text-align: center;
  padding: 0.6rem 0.5rem;
  border-bottom: 1px solid var(--border-subtle);
  vertical-align: middle;
}

.image-models-table tbody tr:last-child td {
  border-bottom: none;
}

.image-models-table input[type="checkbox"] {
  width: 17px;
  height: 17px;
  cursor: pointer;
  accent-color: #10b981;
}

.image-model-name {
  font-weight: 600;
  color: var(--text-primary);
}

.image-model-meta {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin-top: 0.2rem;
  font-size: 0.72rem;
  color: var(--text-tertiary);
}

.image-model-badge {
  font-size: 0.64rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 0.08rem 0.4rem;
  border-radius: 999px;
}

.image-model-badge.free {
  color: #34d399;
  background: rgba(16, 185, 129, 0.12);
  border: 1px solid rgba(16, 185, 129, 0.3);
}

.image-model-badge.paid {
  color: #fbbf24;
  background: rgba(245, 158, 11, 0.12);
  border: 1px solid rgba(245, 158, 11, 0.3);
}

.image-model-badge.nokey {
  color: #f87171;
  background: rgba(239, 68, 68, 0.1);
  border: 1px solid rgba(239, 68, 68, 0.3);
}

.image-models-empty {
  text-align: center !important;
  color: var(--text-tertiary);
  padding: 1rem !important;
}

.image-models-note {
  font-size: 0.74rem;
  color: var(--text-tertiary);
  margin-top: 0.75rem;
  line-height: 1.45;
}

/* ──────────────────────────────────────────────────────────────────────────
   ADMIN → USERS: group badge, usage & cost, per-user model assignment
   ────────────────────────────────────────────────────────────────────────── */
.audience-badge {
  display: inline-block;
  font-size: 0.66rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 0.12rem 0.5rem;
  border-radius: 999px;
  border: 1px solid transparent;
}

.audience-badge.audience-paid { color: #fbbf24; background: rgba(245, 158, 11, 0.12); border-color: rgba(245, 158, 11, 0.3); }
.audience-badge.audience-user { color: #38bdf8; background: rgba(56, 189, 248, 0.12); border-color: rgba(56, 189, 248, 0.3); }
.audience-badge.audience-super_admin { color: #f87171; background: rgba(239, 68, 68, 0.12); border-color: rgba(239, 68, 68, 0.3); }
.audience-badge.audience-guest { color: #a1a1aa; background: rgba(255, 255, 255, 0.06); border-color: rgba(255, 255, 255, 0.12); }

.usage-line {
  font-size: 0.76rem;
  line-height: 1.5;
  white-space: nowrap;
}

.usage-api {
  font-size: 0.74rem;
  margin-top: 0.3rem;
  padding: 0.25rem 0.45rem;
  border-radius: 6px;
  background: rgba(56, 189, 248, 0.08);
  border: 1px solid rgba(56, 189, 248, 0.2);
  white-space: nowrap;
}

.model-assign-cell {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  min-width: 200px;
  max-width: 260px;
}

.model-assign-label {
  font-size: 0.66rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-tertiary);
  margin-top: 0.15rem;
}

.model-assign-select {
  padding: 0.25rem 0.45rem !important;
  font-size: 0.74rem !important;
  width: 100%;
}

.model-defaults-table td:first-child {
  white-space: nowrap;
}

.model-defaults-table td {
  text-align: left;
}

.model-defaults-table .model-assign-select,
.model-defaults-table select {
  min-width: 190px;
}

@media (max-width: 860px) {
  .data-table td .model-assign-cell {
    min-width: 0;
    max-width: none;
    width: 100%;
    align-items: stretch;
  }

  .data-table td:has(> .model-assign-cell) {
    flex-direction: column;
    align-items: stretch;
  }

  .usage-line,
  .usage-api {
    white-space: normal;
    text-align: right;
  }
}

/* Users & guest-users tables: room for the new columns, scroll sideways instead of squeezing */
@media (min-width: 861px) {
  #users-view-developers .data-table {
    min-width: 1280px;
  }

  #users-view-guests .data-table:has(#guest-users-table-body) {
    min-width: 1000px;
  }

  #users-view-developers .data-table td:first-child,
  #users-view-guests .data-table td:first-child {
    min-width: 180px;
    word-break: normal;
    overflow-wrap: anywhere;
  }
}

@media (max-width: 860px) {
  .data-table td:has(> .usage-line) {
    flex-direction: column;
    align-items: stretch;
    gap: 0.15rem;
  }

  .data-table td:has(> .usage-line)::before {
    margin-bottom: 0.2rem;
  }
}
