/*
 * RS-TH09 — Unified Cards, Hero, Sections & Content Surfaces
 * Final surface and icon-aware control layer for Admin/Public/Auth/Standalone.
 */

:root {
  --th09-surface: var(--theme-surface, #ffffff);
  --th09-surface-soft: color-mix(in srgb, var(--theme-brand, #137a54) 4%, var(--theme-surface, #ffffff));
  --th09-surface-strong: color-mix(in srgb, var(--theme-brand, #137a54) 8%, var(--theme-surface, #ffffff));
  --th09-border: var(--theme-border, #d6e1db);
  --th09-border-strong: var(--theme-border-strong, #b7c8bf);
  --th09-text: var(--theme-text, #10251b);
  --th09-text-soft: var(--theme-text-soft, #3e574b);
  --th09-muted: var(--theme-muted, #5d6f66);
  --th09-icon: var(--theme-brand, #137a54);
  --th09-radius: 1.1rem;
  --th09-radius-lg: 1.35rem;
  --th09-shadow: 0 14px 34px rgba(16, 37, 27, .08);
  --th09-shadow-soft: 0 8px 22px rgba(16, 37, 27, .06);
  --th09-icon-slot: 2.75rem;
  --th09-icon-control-pad: 2.95rem;
}

html[data-theme="dark"] {
  --th09-surface: #10271f;
  --th09-surface-soft: #142f25;
  --th09-surface-strong: #19392d;
  --th09-border: #38594a;
  --th09-border-strong: #537766;
  --th09-text: #ffffff;
  --th09-text-soft: #ffffff;
  --th09-muted: #ffffff;
  --th09-icon: #ffffff;
  --th09-shadow: 0 18px 44px rgba(0, 0, 0, .30);
  --th09-shadow-soft: 0 10px 28px rgba(0, 0, 0, .22);
}

/* Shared content surfaces. Layout dimensions remain page-owned. */
body:is(.admin-body, .public-shell, .auth-shell, .theme-scope-standalone) :where(
  .card,
  .content-card,
  .section-card,
  .panel,
  .surface-card,
  .analytics-card,
  .kpi-card,
  .metric-card,
  .summary-card,
  .stat-card
) {
  border-color: var(--th09-border);
  color: var(--th09-text);
}

body:is(.admin-body, .public-shell, .auth-shell, .theme-scope-standalone) :where(
  .card-header,
  .panel-header,
  .section-header,
  .content-card__header
) {
  border-color: var(--th09-border);
}

html[data-theme="dark"] body:is(.admin-body, .public-shell, .auth-shell, .theme-scope-standalone) :where(
  .card,
  .content-card,
  .section-card,
  .panel,
  .surface-card,
  .analytics-card,
  .kpi-card,
  .metric-card,
  .summary-card,
  .stat-card
) {
  background: var(--th09-surface) !important;
  border-color: var(--th09-border) !important;
  color: var(--th09-text) !important;
  box-shadow: var(--th09-shadow-soft);
}

html[data-theme="dark"] body:is(.admin-body, .public-shell, .auth-shell, .theme-scope-standalone) :where(
  .card-header,
  .panel-header,
  .section-header,
  .content-card__header,
  .card-footer,
  .panel-footer,
  .content-card__footer
) {
  background: var(--th09-surface-soft) !important;
  border-color: var(--th09-border) !important;
  color: var(--th09-text) !important;
}

html[data-theme="dark"] body:is(.admin-body, .public-shell, .auth-shell, .theme-scope-standalone) :where(
  .card-body,
  .panel-body,
  .content-card__body
) {
  background-color: var(--th09-surface) !important;
  color: var(--th09-text) !important;
}

/* System page hero and its statistics use one reliable contract. */
body:is(.admin-body, .public-shell) .sys-page-hero {
  border-color: color-mix(in srgb, var(--theme-brand, #137a54) 22%, var(--th09-border));
  box-shadow: var(--th09-shadow);
}

html[data-theme="dark"] body:is(.admin-body, .public-shell) .sys-page-hero {
  background:
    radial-gradient(circle at top right, rgba(159, 240, 200, .10), transparent 34%),
    linear-gradient(145deg, #10271f 0%, #0c2018 100%) !important;
  border-color: #456b59 !important;
  color: var(--th09-text) !important;
}

html[data-theme="dark"] body:is(.admin-body, .public-shell) :where(
  .sys-page-hero__title,
  .sys-page-hero__stat-value
) {
  color: #ffffff !important;
  -webkit-text-fill-color: #ffffff !important;
}

html[data-theme="dark"] body:is(.admin-body, .public-shell) :where(
  .sys-page-hero__subtitle,
  .sys-page-hero__stat-label
) {
  color: #ffffff !important;
  -webkit-text-fill-color: #ffffff !important;
}

html[data-theme="dark"] body:is(.admin-body, .public-shell) :where(
  .sys-page-hero__badge,
  .sys-page-hero__stat
) {
  background: #18372b !important;
  border-color: #4c7763 !important;
  color: #ffffff !important;
  -webkit-text-fill-color: #ffffff !important;
  box-shadow: inset 0 1px 0 rgba(255,255,255,.035);
}

/* Icon-aware form controls. JS adds these classes for legacy and dynamic markup. */
.herb-control-icon-wrap {
  position: relative;
  min-width: 0;
}

.herb-control-icon-wrap .herb-control-icon {
  position: absolute;
  inset-block: 0;
  z-index: 4;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: var(--th09-icon-slot);
  color: var(--th09-icon);
  pointer-events: none;
  line-height: 1;
}

.herb-control-icon-wrap .herb-control-icon--start {
  inset-inline-start: 0;
}

.herb-control-icon-wrap .herb-control-icon--end {
  inset-inline-end: 0;
}

body:is(.admin-body, .public-shell, .auth-shell, .theme-scope-standalone) :where(
  .herb-control-has-icon-start,
  .input-icon > .form-control,
  .input-icon > .form-select,
  .search-input-wrap > .form-control,
  .perm-search-wrap > .form-control,
  .perm-filter-wrap > .form-select
) {
  padding-inline-start: var(--th09-icon-control-pad) !important;
}

body:is(.admin-body, .public-shell, .auth-shell, .theme-scope-standalone) .herb-control-has-icon-end {
  padding-inline-end: var(--th09-icon-control-pad) !important;
}

body:is(.admin-body, .public-shell, .auth-shell, .theme-scope-standalone) .search-input-wrap > .form-control {
  padding-inline-end: var(--th09-icon-control-pad) !important;
}

body:is(.admin-body, .public-shell, .auth-shell, .theme-scope-standalone) .herb-control-icon-wrap :where(.form-select.herb-control-has-icon-start, select.herb-control-has-icon-start) {
  padding-inline-start: var(--th09-icon-control-pad) !important;
  padding-inline-end: 2.8rem !important;
}

.herb-control-icon-wrap:focus-within .herb-control-icon {
  color: var(--theme-brand-hover, #0f6546);
}

html[data-theme="dark"] .herb-control-icon-wrap:focus-within .herb-control-icon {
  color: #ffffff;
}

/* Known legacy wrappers receive immediate no-JS support. */
:is(.input-icon, .search-input-wrap, .perm-search-wrap, .perm-filter-wrap) {
  position: relative;
  min-width: 0;
}

:is(.input-icon, .search-input-wrap, .perm-search-wrap, .perm-filter-wrap) > :is(i, .search-icon),
:is(.perm-search-wrap, .perm-filter-wrap) > i {
  z-index: 4;
  color: var(--th09-icon) !important;
  pointer-events: none;
}

/* Role/permission matrix toolbar — final page-local dark-mode correction. */
html[data-theme="dark"] body.admin-body .admin-page--roles .matrix-toolbar {
  background:
    linear-gradient(180deg, rgba(25, 57, 45, .98), rgba(13, 35, 26, .98)) !important;
  border-color: #496f5e !important;
  color: #ffffff !important;
  box-shadow: 0 12px 28px rgba(0,0,0,.20);
}

html[data-theme="dark"] body.admin-body .admin-page--roles .matrix-toolbar :where(
  .form-label,
  label,
  .small,
  .text-muted
) {
  color: #ffffff !important;
  -webkit-text-fill-color: #ffffff !important;
}

html[data-theme="dark"] body.admin-body .admin-page--roles .matrix-toolbar :where(
  .form-control,
  .form-select
) {
  background-color: #0d241b !important;
  border-color: #5d836f !important;
  color: #ffffff !important;
  -webkit-text-fill-color: #ffffff !important;
}

html[data-theme="dark"] body.admin-body .admin-page--roles .matrix-toolbar .form-control::placeholder {
  color: #ffffff !important;
  -webkit-text-fill-color: #ffffff !important;
}

html[data-theme="dark"] body.admin-body .admin-page--roles .matrix-toolbar :where(.form-control, .form-select):focus {
  background-color: #112f24 !important;
  border-color: #9ff0c8 !important;
  box-shadow: 0 0 0 .22rem rgba(159, 240, 200, .20) !important;
}

html[data-theme="dark"] body.admin-body .admin-page--roles .matrix-toolbar :where(
  .btn-outline-secondary,
  .btn-outline-primary,
  .btn-outline-warning
) {
  color: #ffffff !important;
  -webkit-text-fill-color: #ffffff !important;
  background: #19382c !important;
  border-color: #648a76 !important;
}

html[data-theme="dark"] body.admin-body .admin-page--roles .matrix-toolbar :where(
  .btn-outline-secondary,
  .btn-outline-primary,
  .btn-outline-warning
):is(:hover, :focus-visible) {
  color: #061f16 !important;
  -webkit-text-fill-color: #061f16 !important;
  background: #9ff0c8 !important;
  border-color: #9ff0c8 !important;
}

html[data-theme="dark"] body.admin-body .admin-page--roles :where(
  .matrix-shell,
  .matrix-floating-actions
) {
  background: #10271f !important;
  border-color: #456b59 !important;
  color: #ffffff !important;
}

html[data-theme="dark"] body.admin-body .admin-page--roles .matrix-table :where(th:first-child, td:first-child) {
  background: #10271f !important;
  color: #ffffff !important;
}

html[data-theme="dark"] body.admin-body .admin-page--roles .matrix-table thead th,
html[data-theme="dark"] body.admin-body .admin-page--roles .matrix-table thead th:first-child {
  background: #18372b !important;
  color: #ffffff !important;
  border-color: #456b59 !important;
}

html[data-theme="dark"] body.admin-body .admin-page--roles .matrix-module-row td {
  background: #1b3d30 !important;
  color: #ffffff !important;
}

/* Section headings and stat/summary blocks. */
body:is(.admin-body, .public-shell, .auth-shell, .theme-scope-standalone) :where(
  .section-title,
  .panel-title,
  .card-title,
  .summary-title,
  .kpi-title,
  .metric-title
) {
  color: var(--th09-text);
}

body:is(.admin-body, .public-shell, .auth-shell, .theme-scope-standalone) :where(
  .section-subtitle,
  .panel-subtitle,
  .card-subtitle,
  .summary-label,
  .kpi-label,
  .metric-label,
  .muted-note
) {
  color: var(--th09-muted) !important;
}

@media (max-width: 767.98px) {
  :root {
    --th09-icon-slot: 2.65rem;
    --th09-icon-control-pad: 2.85rem;
  }

  body:is(.admin-body, .public-shell, .auth-shell, .theme-scope-standalone) :where(
    .card,
    .content-card,
    .section-card,
    .panel,
    .surface-card
  ) {
    border-radius: 1rem;
  }

  body.admin-body .admin-page--roles .matrix-toolbar {
    gap: .75rem !important;
    padding: .9rem !important;
  }

  body.admin-body .admin-page--roles .matrix-toolbar > * {
    min-width: 0 !important;
    width: 100%;
  }

  body.admin-body .admin-page--roles .matrix-toolbar .d-flex {
    width: 100%;
  }

  body.admin-body .admin-page--roles .matrix-toolbar .btn,
  body.admin-body .admin-page--roles .matrix-toolbar .bulk-role-select {
    width: 100%;
  }
}

@media (max-width: 390px) {
  :root {
    --th09-icon-control-pad: 2.75rem;
  }

  body:is(.admin-body, .public-shell, .auth-shell, .theme-scope-standalone) :where(
    .sys-page-hero,
    .card,
    .content-card,
    .section-card,
    .panel
  ) {
    border-radius: .9rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  body:is(.admin-body, .public-shell, .auth-shell, .theme-scope-standalone) :where(
    .card,
    .sys-page-hero,
    .herb-control-icon
  ) {
    transition: none !important;
  }
}

@media print {
  body:is(.admin-body, .public-shell, .auth-shell, .theme-scope-standalone) :where(
    .card,
    .content-card,
    .section-card,
    .panel,
    .sys-page-hero,
    .sys-page-hero__stat
  ) {
    background: #fff !important;
    color: #111 !important;
    border-color: #bbb !important;
    box-shadow: none !important;
  }
}
