/* ForgeFit v6 - layout lock, responsive tables, safe controls. */

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

body {
  min-width: 0;
}

.app-shell,
.app-frame,
.app-main,
.main,
.page,
.container,
main,
section,
article,
form,
.card {
  max-width: 100%;
  min-width: 0;
}

.app-main,
.main,
main {
  width: 100%;
  /*
   * overflow-x: clip em vez de hidden.
   * clip corta visualmente mas NÃO cria scroll-container,
   * portanto não bloqueia touch-scroll de filhos (ex: .mobile-rail) no iOS.
   * Fallback: hidden em navegadores sem suporte a clip (pré-Safari 16).
   */
  overflow-x: hidden;
}
@supports (overflow: clip) {
  .app-main,
  .main,
  main {
    overflow-x: clip;
  }
}

img,
video,
iframe,
embed,
object,
svg {
  max-width: 100%;
  height: auto;
}

input,
textarea,
select {
  max-width: 100%;
  min-width: 0;
}

input[type="file"] {
  width: 100%;
}

pre,
code {
  max-width: 100%;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  word-break: break-word;
  overflow-x: auto;
}

.break-word,
.card,
.feed-caption,
.profile-bio,
.drawer-name,
.drawer-username,
.user-card-main,
.comment-item,
.comment-item * {
  overflow-wrap: break-word;
  word-break: break-word;
}

.chip-list,
.tag-list,
.segmented {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2, .5rem);
}

.scroll-x,
.mobile-rail,
.tabs,
.feed-tabs {
  max-width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}

.scroll-x::-webkit-scrollbar,
.mobile-rail::-webkit-scrollbar,
.tabs::-webkit-scrollbar,
.feed-tabs::-webkit-scrollbar {
  display: none;
}

.scroll-x > *,
.mobile-rail > * {
  flex-shrink: 0;
  scroll-snap-align: start;
}

.mobile-rail {
  width: 100%;
  margin-inline: 0;
  padding-inline: 0;
  /* Espaço à direita para que o último card não fique rente à borda ao scrollar */
  padding-inline-end: var(--space-4, 1rem);
  /* scroll-padding garante que o snap posicione cards com margem visual correta */
  scroll-padding-left: 0;
  scroll-padding-right: var(--space-4, 1rem);
}

.dashboard-v5,
.dashboard-v5-hero,
.metric-card-wide,
.discipline-card,
.dashboard-streak,
.profile-hero,
.profile-main,
.profile-edit-grid,
.profile-edit-avatar,
.search-card,
.feed-layout,
.feed-center {
  min-width: 0;
  max-width: 100%;
}

.dashboard-v5-hero {
  flex-wrap: wrap;
}

.metric-card-line,
.flex-between {
  min-width: 0;
}

.metric-card-line strong,
.metric-card-line span,
.flex-between > * {
  min-width: 0;
}

.drawer-backdrop,
.sheet-backdrop,
.comments-sheet-backdrop,
.bottom-nav,
.sheet,
.comments-sheet,
.composer-backdrop,
.ff-modal-backdrop {
  width: 100%;
  max-width: 100vw;
  left: 0;
  right: 0;
}

.sheet,
.comments-sheet,
.ff-modal {
  box-sizing: border-box;
}

.sheet-grid {
  min-width: 0;
}

.sheet-action {
  min-width: 0;
}

.bottom-nav {
  overflow: hidden;
}

.bottom-nav-item,
.bottom-nav-fab,
.header-btn,
.icon-btn,
.feed-menu-btn {
  min-width: 44px;
  min-height: 44px;
}

.header-avatar,
.header-avatar-img {
  width: 44px;
  height: 44px;
}

.header-title {
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn,
.btn--primary,
.btn--secondary,
.btn--ghost,
.btn--danger {
  min-height: 44px;
  min-width: 44px;
  max-width: 100%;
  white-space: normal;
  overflow: hidden;
  text-overflow: ellipsis;
}

.btn--primary {
  background: var(--btn-primary-bg);
  color: var(--btn-primary-fg);
  border-color: var(--btn-primary-bg);
}

.btn--secondary {
  background: var(--bg-surface);
  color: var(--text-primary);
  border-color: var(--border);
}

.btn--ghost {
  background: transparent;
  color: var(--text-primary);
  border-color: var(--btn-ghost-border);
}

.btn--danger {
  color: #fff;
  background: var(--danger);
  border-color: var(--danger);
}

.btn--block {
  width: 100%;
}

.btn--lg {
  min-height: 56px;
}

.btn--sm {
  min-height: 36px;
}

.btn--icon {
  width: 44px;
  height: 44px;
  padding: 0;
}

.btn:disabled,
.btn[aria-disabled="true"] {
  opacity: .5;
  pointer-events: none;
  cursor: not-allowed;
}

.btn--loading {
  position: relative;
  color: transparent !important;
  pointer-events: none;
}

.btn--loading::after {
  content: "";
  position: absolute;
  width: 20px;
  height: 20px;
  border: 2px solid currentColor;
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin .6s linear infinite;
  color: #fff;
}

.table-responsive {
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.table {
  max-width: 100%;
}

@media (max-width: 768px) {
  .table,
  .table-stack {
    display: block;
    width: 100%;
  }

  .table thead,
  .table-stack thead {
    display: none;
  }

  .table tbody,
  .table tr,
  .table-stack tbody,
  .table-stack tr {
    display: block;
    width: 100%;
  }

  .table tr,
  .table-stack tr {
    background: var(--bg-card);
    border-radius: var(--radius-md);
    padding: var(--space-3);
    margin-bottom: var(--space-3);
    border: 1px solid var(--border-subtle);
  }

  .table th,
  .table td,
  .table-stack th,
  .table-stack td {
    width: 100% !important;
  }

  .table td,
  .table-stack td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-2) 0;
    border-bottom: 1px solid var(--border-subtle);
    text-align: right !important;
  }

  .table td:last-child,
  .table-stack td:last-child {
    border-bottom: 0;
  }

  .table td::before,
  .table-stack td::before {
    content: attr(data-label);
    flex: 0 0 min(45%, 150px);
    text-align: left;
    font-weight: 700;
    color: var(--text-secondary);
    font-size: var(--text-sm);
    text-transform: uppercase;
    letter-spacing: .04em;
  }

  .table td:empty::before,
  .table-stack td:empty::before {
    content: "";
  }

  .table td > *,
  .table-stack td > * {
    max-width: 100%;
  }

  .grid-2,
  .grid-3,
  .grid-4,
  .grid-5,
  .discipline-mini,
  .profile-edit-grid,
  .achievement-grid {
    grid-template-columns: 1fr !important;
  }

  .metric-card-line {
    align-items: flex-start;
    flex-direction: column;
    gap: var(--space-1);
  }

  .profile-edit-avatar {
    flex-direction: column;
    align-items: flex-start;
  }

  .search-card {
    grid-template-columns: 1fr !important;
  }
}

.field-input.is-valid,
.form-control.is-valid,
input.is-valid,
textarea.is-valid,
select.is-valid {
  border-color: var(--success);
}

.field-input.is-invalid,
.form-control.is-invalid,
input.is-invalid,
textarea.is-invalid,
select.is-invalid {
  border-color: var(--danger);
}

.field-error {
  display: none;
  font-size: var(--text-xs);
  color: var(--danger);
  margin-top: var(--space-1);
}

.is-invalid + .field-error,
.field-input.is-invalid + .field-error {
  display: block;
}

.empty-state {
  text-align: center;
  padding: var(--space-12) var(--space-6);
  color: var(--text-secondary);
}

.empty-state-icon {
  width: 80px;
  height: 80px;
  margin: 0 auto var(--space-4);
  color: var(--text-tertiary);
  opacity: .5;
}

.empty-state h3 {
  margin-bottom: var(--space-2);
  color: var(--text-primary);
}

.empty-state p {
  margin-bottom: var(--space-5);
  max-width: 280px;
  margin-inline: auto;
}

.mobile-back-header {
  display: grid;
  grid-template-columns: 44px 1fr 44px;
  align-items: center;
  gap: var(--space-2);
  min-height: 52px;
  margin-bottom: var(--space-4);
}

.mobile-back-header strong {
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text-primary);
}

body.keyboard-open .bottom-nav {
  transform: translateY(100%);
  transition: transform var(--duration-fast);
}

@media (max-width: 768px) and (orientation: landscape) {
  body::before {
    content: 'Gire o celular para o modo vertical.';
    position: fixed;
    inset: 0;
    background: var(--bg-base);
    color: var(--text-primary);
    display: grid;
    place-items: center;
    text-align: center;
    padding: var(--space-8);
    z-index: var(--z-max);
    font-family: var(--font-display);
    font-size: var(--text-xl);
  }
}
