/* Custom theme layer (GrowCRM-style override approach) */
:root {
  --custom-bg-1: #f7f7f9;
  --custom-bg-2: #fff3de;
  --custom-card: #ffffff;
  --custom-border: #ececec;
  --custom-title: #202020;
  --custom-text: #5f5f5f;
  --custom-primary: #f29e16;
  --custom-primary-2: #e68f07;
  --custom-success: #22a06b;
  --custom-warning: #cc7a00;
}

body[data-theme="light"] {
  --bg: #f7f7f9;
  --bg-soft: #fff3de;
  --surface: #ffffff;
  --surface-alt: #fffaf2;
  --text: #202020;
  --muted: #5f5f5f;
  --line: #ececec;
  --brand: #f29e16;
  --brand-strong: #e68f07;
}

body[data-theme="dark"] {
  --bg: #161515;
  --bg-soft: #2a1f0d;
  --surface: #1f1e1c;
  --surface-alt: #282622;
  --text: #f3efe8;
  --muted: #c8b9a1;
  --line: #3a332b;
  --brand: #f29e16;
  --brand-strong: #d88908;
  --custom-bg-1: #161515;
  --custom-bg-2: #2a1f0d;
  --custom-card: #1f1e1c;
  --custom-border: #3a332b;
  --custom-title: #f3efe8;
  --custom-text: #c8b9a1;
  --custom-primary: #f29e16;
  --custom-primary-2: #d88908;
  --custom-success: #2fb077;
  --custom-warning: #f0a326;
}

@media (prefers-color-scheme: dark) {
  body[data-theme="system"] {
    --bg: #161515;
    --bg-soft: #2a1f0d;
    --surface: #1f1e1c;
    --surface-alt: #282622;
    --text: #f3efe8;
    --muted: #c8b9a1;
    --line: #3a332b;
    --brand: #f29e16;
    --brand-strong: #d88908;
    --custom-bg-1: #161515;
    --custom-bg-2: #2a1f0d;
    --custom-card: #1f1e1c;
    --custom-border: #3a332b;
    --custom-title: #f3efe8;
    --custom-text: #c8b9a1;
    --custom-primary: #f29e16;
    --custom-primary-2: #d88908;
    --custom-success: #2fb077;
    --custom-warning: #f0a326;
  }
}

body {
  background:
    radial-gradient(circle at 20% 0%, var(--custom-bg-2) 0%, rgba(0, 0, 0, 0) 32%),
    var(--custom-bg-1);
  overflow-x: hidden;
}

html {
  overflow-x: hidden;
}

.bg-orb {
  display: none;
}

img,
svg,
video,
canvas {
  max-width: 100%;
  height: auto;
}

a,
button,
input,
select,
textarea,
summary {
  min-width: 0;
}

/* ===== Topbar layout ===== */
.topbar {
  display: flex;
  flex-direction: column;
  gap: 8px;
  border-bottom: 1px solid var(--custom-border);
  background: color-mix(in srgb, var(--custom-card) 90%, transparent);
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.08);
  padding: 14px 28px;
  position: sticky;
  top: 0;
  z-index: 10;
}

.topbar-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  width: 100%;
}

.brand,
.brand span,
h1,
h2,
h3,
h4,
.stat-value,
.entity-description {
  color: var(--custom-title);
}

/* Brand logo */
.brand-logo {
  height: 32px;
  width: auto;
  border-radius: 6px;
  object-fit: contain;
}

.nav a,
.muted,
.stat-meta,
.eyebrow,
.entity-meta,
.meta-small {
  color: var(--custom-text);
}

.nav a:hover {
  color: var(--custom-title);
  background: color-mix(in srgb, var(--custom-primary) 12%, transparent);
}

.brand span,
.entity-description,
.entity-meta span,
.entity-primary-meta span,
.counter-badge,
th,
td {
  overflow-wrap: anywhere;
}

.toolbar-actions {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  justify-content: flex-end;
  flex-wrap: nowrap;
  flex-shrink: 0;
}

/* ===== Notifications fix ===== */
.notifications-menu,
.theme-menu {
  position: relative;
}

.notifications-menu[open] > .notifications-panel {
  display: block;
}

.notifications-panel,
.theme-menu-panel {
  z-index: 100;
}

/* PWA install button */
.pwa-install-btn {
  white-space: nowrap;
}

/* Notifications panel dropdown */
.notifications-menu .notifications-panel {
  position: absolute;
  top: calc(100% + 10px);
  right: 0;
  width: 380px;
  max-height: 420px;
  overflow-y: auto;
  border-radius: 12px;
  border: 1px solid var(--custom-border);
  background: var(--custom-card);
  box-shadow: 0 8px 24px rgba(0,0,0,0.12);
  z-index: 100;
}

/* Desktop: row 1 = [Brand ... Nav(center) ... Bell|Theme|Logout], row 2 = none */
@media (min-width: 769px) {
  .topbar {
    display: flex;
    flex-direction: column;
    gap: 0;
  }

  .topbar-row {
    display: flex;
    align-items: center;
    width: 100%;
    gap: 16px;
  }

  .nav {
    flex: 1;
    display: flex;
    justify-content: center;
    gap: 6px;
    margin: 0;
    padding: 0;
  }
}

/* Mobile: stack vertically */
@media (max-width: 768px) {
  .notifications-menu .notifications-panel {
    position: relative;
    top: 8px;
    width: 100%;
    max-height: 50vh;
    right: auto;
  }
}

.theme-toggle {
  background: color-mix(in srgb, var(--custom-card) 85%, transparent);
  border-color: var(--custom-border);
}

.theme-toggle button {
  color: var(--custom-text);
}

.theme-toggle button.active {
  background: var(--custom-primary);
  color: #202020;
}

.card,
.hero-panel,
.auth-card {
  background: var(--custom-card);
  border: 1px solid var(--custom-border);
  border-radius: 14px;
  box-shadow: 0 12px 30px rgba(9, 30, 66, 0.08);
}

/* Auth logo on login page */
.auth-logo {
  width: 120px;
  height: auto;
  margin: 0 auto 16px;
  display: block;
  object-fit: contain;
  border-radius: 12px;
}

/* Remove blue tint on dark backgrounds */
.auth-body {
  background:
    radial-gradient(circle at 20% 0%, var(--custom-bg-2) 0%, rgba(0,0,0,0) 32%),
    var(--custom-bg-1);
}

.auth-body .auth-card {
  text-align: center;
}

.auth-body .auth-form {
  text-align: left;
}

.btn {
  border-radius: 10px;
  background: linear-gradient(180deg, var(--custom-primary), var(--custom-primary-2));
  border-color: var(--custom-primary-2);
  color: #1d1d1d;
}

.btn.ghost {
  background: var(--custom-card);
  color: var(--custom-primary);
  border-color: var(--custom-border);
}

.btn.success {
  background: linear-gradient(180deg, #14b982, var(--custom-success));
  border-color: var(--custom-success);
}

.btn.warn {
  background: linear-gradient(180deg, #d69b3c, var(--custom-warning));
  border-color: var(--custom-warning);
}

.admin-table thead th {
  background: color-mix(in srgb, var(--custom-primary) 8%, var(--custom-card));
  color: var(--custom-title);
  border-bottom: 1px solid var(--custom-border);
}

.filters-grid input,
.filters-grid select,
.inline-stack input,
.inline-stack select,
.comment-form input,
.auth-form input,
.entity-inline-form input,
textarea {
  border-color: var(--custom-border);
  background: color-mix(in srgb, var(--custom-card) 92%, var(--custom-bg-2));
  color: var(--custom-title);
}

.page-shell,
.card,
.hero-panel,
.entity-card,
.maintenance-card,
.mini-kpi,
.table-wrap,
.filters-grid,
.toolbar-actions,
.section-head,
.entity-head,
.entity-actions,
.entity-inline-edit-card,
.entity-inline-form,
.comment-form {
  max-width: 100%;
}

.table-wrap {
  width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
}

table {
  width: max(100%, 720px);
  min-width: 720px;
}

.entity-actions > *,
.toolbar-actions > *,
.filters-actions > *,
.inline-stack > *,
.entity-primary-meta > *,
.entity-meta > * {
  min-width: 0;
}

.entity-actions form,
.entity-actions .btn,
.filters-actions .btn,
.hero-actions .btn,
.pager-actions .btn {
  max-width: 100%;
}

.entity-inline-form label,
.filters-grid label,
.inline-stack label,
textarea {
  min-width: 0;
  max-width: 100%;
}

.filters-actions {
  flex-wrap: wrap;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--custom-primary);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--custom-primary) 18%, transparent);
}

.chart-fallback {
  display: grid;
  gap: 8px;
}

.chart-fallback-item {
  display: grid;
  gap: 4px;
}

.chart-fallback-label {
  font-size: 12px;
  color: var(--custom-text);
}

.chart-fallback-bar {
  height: 10px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--custom-primary) 22%, transparent);
  overflow: hidden;
}

.chart-fallback-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--custom-primary), var(--custom-primary-2));
}

/* Large displays */
.page-shell {
  width: min(96vw, 2200px);
  max-width: 2200px;
}

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

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

.infographic-grid {
  align-items: stretch;
}

.infographic-card {
  background:
    radial-gradient(120% 120% at 0% 0%, color-mix(in srgb, var(--custom-primary) 16%, transparent) 0%, transparent 60%),
    linear-gradient(160deg, color-mix(in srgb, var(--custom-card) 95%, transparent), color-mix(in srgb, var(--custom-primary) 4%, var(--custom-card)));
}

.infographic-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 12px;
}

.infographic-head h3 {
  margin: 0;
}

.ring-wrap {
  display: grid;
  grid-template-columns: 160px 1fr;
  gap: 14px;
  align-items: center;
}

.ring {
  width: 150px;
  height: 150px;
  border-radius: 999px;
  display: grid;
  place-content: center;
  text-align: center;
  background:
    radial-gradient(closest-side, var(--custom-card) 72%, transparent 73% 100%),
    conic-gradient(
      from -90deg,
      #fb923c 0 var(--ring-open, 0%),
      #22a06b var(--ring-open, 0%) 100%
    );
}

.ring span {
  font-size: 24px;
  font-weight: 800;
  color: var(--custom-title);
}

.ring small {
  color: var(--custom-text);
  font-size: 11px;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.ring-meta p {
  margin: 4px 0;
  color: var(--custom-text);
}

.ring-meta strong {
  color: var(--custom-title);
}

.infographic-list {
  display: grid;
  gap: 12px;
}

.infographic-list.compact {
  gap: 10px;
}

.metric-row {
  display: grid;
  gap: 6px;
}

.metric-label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  color: var(--custom-title);
  font-size: 13px;
  font-weight: 700;
}

.metric-label span {
  color: var(--custom-text);
  font-family: "IBM Plex Mono", monospace;
  font-size: 12px;
}

.metric-track {
  height: 12px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--custom-primary) 10%, var(--custom-card));
  overflow: hidden;
  display: flex;
}

.metric-fill {
  height: 100%;
  display: block;
}

.metric-open {
  background: linear-gradient(90deg, #f97316, #fb7185);
}

.metric-closed {
  background: linear-gradient(90deg, #22a06b, #2dd4bf);
}

@media (min-width: 1600px) {
  .page-shell {
    width: min(98vw, 2600px);
    max-width: 2600px;
    padding: 20px 24px;
  }

  .card,
  .hero-panel {
    padding: 26px;
  }

  .filters-grid {
    grid-template-columns: repeat(6, minmax(150px, 1fr));
  }
}

/* ===== Admin table mobile ===== */
.admin-table {
  width: 100%;
}

.admin-table .table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

/* ===== Mobile: <= 1024px ===== */
@media (max-width: 1024px) {
  .page-shell {
    width: 100%;
    max-width: 100%;
    padding: 16px;
  }

  .topbar {
    padding: 12px 16px;
    gap: 8px;
  }

  .nav {
    width: 100%;
    flex-wrap: nowrap;
    justify-content: flex-start;
    overflow-x: auto;
    overflow-y: hidden;
    padding-bottom: 2px;
    scrollbar-width: none;
  }

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

  .nav a {
    flex: 0 0 auto;
    white-space: nowrap;
  }

  .filters-grid,
  .filters-grid.compact,
  .stats-grid,
  .charts-grid,
  .maintenance-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .search-box,
  .entity-inline-form-full {
    grid-column: 1 / -1;
  }

  .filters-actions {
    grid-column: 1 / -1;
    flex-wrap: wrap;
  }

  .inline-stack {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .entity-inline-form,
  .entity-assign-row {
    grid-template-columns: 1fr;
  }

  .entity-primary-meta,
  .entity-meta {
    display: grid;
    grid-template-columns: 1fr;
    gap: 8px;
  }

  /* Admin table responsive */
  .admin-table table {
    min-width: 600px;
  }

  .admin-table td,
  .admin-table th {
    padding: 8px 10px;
    font-size: 13px;
  }
}

/* ===== Admin mobile cards ===== */
@media (max-width: 768px) {
  .table-wrap table thead {
    display: none;
  }

  .table-wrap table,
  .table-wrap table tbody,
  .table-wrap table tr,
  .table-wrap table td {
    display: block;
    width: 100%;
    min-width: 0;
  }

  .table-wrap table tr {
    background: var(--custom-card);
    border: 1px solid var(--custom-border);
    border-radius: 12px;
    padding: 12px;
    margin-bottom: 10px;
  }

  .table-wrap table td {
    padding: 4px 0;
    font-size: 13px;
    border: none;
  }

  .table-wrap table td:before {
    content: attr(data-label);
    font-weight: 700;
    font-size: 11px;
    color: var(--custom-text);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    display: block;
    margin-bottom: 2px;
  }

  .table-wrap table td .inline-stack {
    grid-template-columns: 1fr;
    gap: 6px;
  }

  .table-wrap table td .inline-stack input,
  .table-wrap table td .inline-stack select {
    width: 100%;
  }

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

  .maintenance-card {
    padding: 14px;
  }

  .stats-grid.compact-grid {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 8px;
  }

  .mini-kpi {
    padding: 10px;
    font-size: 13px;
  }

  .filters-grid.compact {
    grid-template-columns: 1fr !important;
  }
}

/* ===== Mobile: <= 720px ===== */
@media (max-width: 720px) {
  .page-shell {
    padding-top: 12px;
    padding-right: max(12px, env(safe-area-inset-right));
    padding-bottom: calc(16px + env(safe-area-inset-bottom));
    padding-left: max(12px, env(safe-area-inset-left));
  }

  .topbar {
    padding-top: calc(10px + env(safe-area-inset-top));
    padding-right: max(12px, env(safe-area-inset-right));
    padding-left: max(12px, env(safe-area-inset-left));
  }

  .card,
  .hero-panel,
  .auth-card {
    padding: 14px;
    border-radius: 14px;
  }

  .toolbar-actions {
    gap: 6px;
  }

  .toolbar-actions .btn,
  .toolbar-actions summary {
    padding: 6px 8px;
    font-size: 12px;
  }

  .filters-grid,
  .filters-grid.compact,
  .stats-grid,
  .charts-grid,
  .maintenance-grid,
  .inline-stack,
  .comment-form,
  .entity-inline-form {
    grid-template-columns: 1fr;
  }

  .section-head,
  .entity-head,
  .pager {
    align-items: flex-start;
  }

  .filters-actions,
  .hero-actions,
  .pager-actions {
    width: 100%;
    flex-direction: column;
    align-items: stretch;
  }

  .filters-actions .btn,
  .hero-actions .btn,
  .pager-actions .btn,
  .entity-actions .btn,
  .entity-actions form {
    width: 100%;
  }

  .entity-actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    align-items: stretch;
  }

  .entity-assign-row,
  .comment-form {
    gap: 10px;
  }

  .notifications-row .notifications-panel {
    position: relative;
    top: 8px;
    width: 100%;
    max-height: 50vh;
    overflow-y: auto;
    border-radius: 12px;
  }

  table {
    width: max(100%, 640px);
    min-width: 640px;
  }

  .ring-wrap {
    grid-template-columns: 1fr;
    justify-items: center;
  }

  .ring {
    width: 130px;
    height: 130px;
  }

  .hero-panel {
    grid-template-columns: 1fr;
  }

  /* Admin cards stack */
  .admin-grid,
  .maintenance-grid {
    grid-template-columns: 1fr;
  }

  .admin-table td,
  .admin-table th {
    padding: 6px 8px;
    font-size: 12px;
    white-space: nowrap;
  }
}

/* ===== Small mobile: <= 560px ===== */
@media (max-width: 560px) {
  .brand {
    gap: 6px;
  }

  .brand span {
    font-size: 14px;
  }

  .brand-logo {
    height: 26px;
  }

  .toolbar-actions {
    gap: 4px;
  }

  .toolbar-actions .btn,
  .toolbar-actions summary {
    padding: 5px 6px;
    font-size: 11px;
  }

  .entity-actions {
    grid-template-columns: 1fr;
  }

  .entity-main {
    align-items: flex-start;
  }

  .entity-main strong,
  .meta-small {
    width: 100%;
  }

  table {
    width: max(100%, 560px);
    min-width: 560px;
  }
}
