/* TheaCRM v2.8.567 – UI layout foundation r3
   Shared width, theme, button, back-action and mobile-modal rules.
   Loaded after the user theme so selected accent colors remain authoritative. */

:root {
  --thea-control-min-height: 40px;
  --thea-touch-min-height: 44px;
  --thea-action-radius: 10px;
  --thea-action-gap: .45rem;
}

/* Every rendered page remains inside the existing Flask/Jinja app shell. */
body.thea-app-shell #mainContent,
body.thea-app-shell #mainContent > * {
  min-width: 0;
}

/* Long entity names must wrap instead of increasing the page width. */
body.thea-app-shell #mainContent :is(
  h1,
  h2,
  h3,
  .page-title,
  .project-page-title,
  .thea-page-title,
  .card-title,
  .modal-title,
  .dropdown-item,
  .list-group-item,
  .table td,
  .table th
) {
  overflow-wrap: anywhere;
  word-break: normal;
}

body.thea-app-shell #mainContent :is(
  .d-flex,
  .row,
  .col,
  [class*="col-"],
  .card,
  .card-body,
  .thea-card,
  .thea-work-card
) > * {
  min-width: 0;
}

/* Dashboard already uses the available shell width. Apply the same rule to
   direct page wrappers which currently re-introduce Bootstrap max-widths. */
@media (min-width: 992px) {
  body.thea-app-shell #mainContent > :is(
    .container,
    .container-sm,
    .container-md,
    .container-lg,
    .container-xl,
    .container-xxl,
    .container-fluid,
    .responsive-container,
    .thea-page,
    .thea-work-page,
    .thea-directory-page,
    .thea-list-page,
    .thea-chat-page,
    .email-app-container,
    .email-page-container,
    .email-detail-container,
    .email-workflow-page,
    .thea-email-signature-page,
    .ai-prework-page,
    .ai-prework-form,
    .ai-prework-detail
  ),
  body.thea-app-shell #mainContent > [class*="max-w-"] {
    width: 100% !important;
    max-width: none !important;
    margin-left: 0 !important;
    margin-right: 0 !important;
  }
}

/* Theme contract: Bootstrap primary and historical success actions both use
   the user-selected TheaCRM accent. Warning, danger and info stay semantic. */
body.thea-app-shell :is(.btn-primary, .btn-success) {
  --bs-btn-color: #fff;
  --bs-btn-bg: var(--primary-color);
  --bs-btn-border-color: var(--primary-color);
  --bs-btn-hover-color: #fff;
  --bs-btn-hover-bg: var(--primary-hover);
  --bs-btn-hover-border-color: var(--primary-hover);
  --bs-btn-active-color: #fff;
  --bs-btn-active-bg: var(--primary-hover);
  --bs-btn-active-border-color: var(--primary-hover);
  --bs-btn-disabled-color: #fff;
  --bs-btn-disabled-bg: var(--primary-color);
  --bs-btn-disabled-border-color: var(--primary-color);
  background-color: var(--primary-color) !important;
  border-color: var(--primary-color) !important;
  color: #fff !important;
}

body.thea-app-shell :is(.btn-primary, .btn-success):is(:hover, :focus-visible, :active, .active) {
  background-color: var(--primary-hover) !important;
  border-color: var(--primary-hover) !important;
  color: #fff !important;
}

body.thea-app-shell :is(.btn-outline-primary, .btn-outline-success) {
  --bs-btn-color: var(--primary-color);
  --bs-btn-border-color: var(--primary-color);
  --bs-btn-hover-color: #fff;
  --bs-btn-hover-bg: var(--primary-color);
  --bs-btn-hover-border-color: var(--primary-color);
  --bs-btn-active-color: #fff;
  --bs-btn-active-bg: var(--primary-hover);
  --bs-btn-active-border-color: var(--primary-hover);
  color: var(--primary-color) !important;
  border-color: var(--primary-color) !important;
  background-color: transparent !important;
}

body.thea-app-shell :is(.btn-outline-primary, .btn-outline-success):is(:hover, :focus-visible, :active, .active) {
  color: #fff !important;
  border-color: var(--primary-hover) !important;
  background-color: var(--primary-hover) !important;
}

body.thea-app-shell :is(.text-primary, .text-success, .link-primary, .link-success) {
  color: var(--primary-color) !important;
}

body.thea-app-shell :is(.bg-primary, .bg-success) {
  background-color: var(--primary-color) !important;
  color: #fff;
}

body.thea-app-shell :is(.border-primary, .border-success) {
  border-color: var(--primary-color) !important;
}

/* Common button geometry without changing semantic variants. */
body.thea-app-shell .btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--thea-action-gap);
  min-height: var(--thea-control-min-height);
  max-width: 100%;
  border-radius: var(--thea-action-radius);
  line-height: 1.2;
  white-space: normal;
  text-align: center;
}

body.thea-app-shell .btn.btn-sm {
  min-height: 36px;
}

body.thea-app-shell .btn:focus-visible,
body.thea-app-shell [role="button"]:focus-visible,
body.thea-app-shell a:focus-visible {
  outline: 3px solid rgba(var(--thea-primary-rgb), .28);
  outline-offset: 2px;
}

/* Back/cancel actions retain their current href/history behavior, but share
   one neutral visual treatment. The selectors include existing legacy names. */
body.thea-app-shell :is(
  .btn-secondary,
  .btn-outline-secondary,
  .btn-outline-brown,
  .email-detail-back-btn,
  .stock-back-link,
  .thea-chat-back,
  [title^="Zpět"],
  [aria-label^="Zpět"]
) {
  background-color: transparent !important;
  border: 1px solid #6c757d !important;
  color: #495057 !important;
  box-shadow: none !important;
}

body.thea-app-shell :is(
  .btn-secondary,
  .btn-outline-secondary,
  .btn-outline-brown,
  .email-detail-back-btn,
  .stock-back-link,
  .thea-chat-back,
  [title^="Zpět"],
  [aria-label^="Zpět"]
):is(:hover, :focus-visible, :active) {
  background-color: #f1f3f5 !important;
  border-color: #495057 !important;
  color: #212529 !important;
}

/* Generic near-fullscreen mobile modal fallback. Existing explicitly marked
   fullscreen modals keep their stronger rules from the current foundation. */
@media (max-width: 767.98px) {
  body.thea-app-shell .btn {
    min-height: var(--thea-touch-min-height);
  }

  body.thea-app-shell .btn.btn-sm {
    min-height: 40px;
  }

  body.thea-app-shell .modal:not(.thea-ui-mobile-fullscreen-modal) .modal-dialog {
    width: calc(100% - .7rem);
    max-width: none;
    margin: .35rem auto;
  }

  body.thea-app-shell .modal:not(.thea-ui-mobile-fullscreen-modal) .modal-content {
    max-height: calc(100dvh - .7rem);
    border-radius: 14px;
    overflow: hidden;
  }

  body.thea-app-shell .modal:not(.thea-ui-mobile-fullscreen-modal) .modal-body {
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
}

@media (prefers-color-scheme: dark) {
  body.thea-app-shell :is(
    .btn-secondary,
    .btn-outline-secondary,
    .btn-outline-brown,
    .email-detail-back-btn,
    .stock-back-link,
    .thea-chat-back,
    [title^="Zpět"],
    [aria-label^="Zpět"]
  ) {
    border-color: #77808a !important;
    color: #e0e5ea !important;
  }

  body.thea-app-shell :is(
    .btn-secondary,
    .btn-outline-secondary,
    .btn-outline-brown,
    .email-detail-back-btn,
    .stock-back-link,
    .thea-chat-back,
    [title^="Zpět"],
    [aria-label^="Zpět"]
  ):is(:hover, :focus-visible, :active) {
    background-color: #2a2f3a !important;
    border-color: #aeb7c0 !important;
    color: #fff !important;
  }
}
