/*
 * Portal site-wide stylesheet — warm-glass palette ported from idr-onboard
 * (brand orange #e75733/#fc6d00, warm stone neutrals), recast as a dense
 * app shell: fixed left sidebar (brand + nav + scope switcher), fluid main
 * column, compact type scale, side-by-side panels instead of stacked blocks.
 *
 * Layered so per-tenant brand overrides (inline, unlayered, from the Brand
 * table) beat everything here — the same mechanism onboard uses for per-org
 * branding. Brand CSS targets .navbar, .nav-link, section, button and the
 * --color-* variables; keep those names stable.
 */
@layer theme, base, components;

@layer theme {
  :root {
    --color-primary: #e75733;
    --color-primary-hover: #cf4526;
    --color-primary-2: #fc6d00;
    --color-danger: #dc3545;
    --color-success: #198754;
    --color-warning: #ffc107;

    --color-ink: #1c1917;
    --color-muted: #6f6a66;
    --color-surface: #ffffff;
    --color-edge: #e7e0da;

    --radius-field: 0.375rem;
    --radius-card: 12px;
    --sidebar-w: 220px;
  }
}

@layer base {
  * { box-sizing: border-box; }

  body {
    font-family: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    font-size: 0.925rem;
    line-height: 1.45;
    margin: 0;
    color: var(--color-ink);
    /* Warm-glass page wash — near-white with a whisper of brand warmth. */
    background: linear-gradient(135deg, #fdf5f0, #ffffff 45%, #fdf5f0);
    background-attachment: fixed;
    -webkit-text-size-adjust: 100%;
  }

  h1, h2, h3, h4, h5, h6 {
    margin-top: 0;
    margin-bottom: 0.5rem;
    font-weight: 600;
    line-height: 1.25;
  }
  h1 { font-size: 1.35rem; }
  h2 { font-size: 1.2rem; }
  h3 { font-size: 1rem; }
  h4 { font-size: 0.85rem; color: var(--color-muted); }

  p { margin-top: 0; margin-bottom: 0.75rem; }

  a { color: var(--color-primary); text-decoration: underline; }
  a:hover { color: var(--color-primary-hover); }

  code { font-size: 0.875em; color: #d63384; word-wrap: break-word; }
  small { font-size: 0.875em; color: var(--color-muted); }
  label { display: inline-block; }
  ::placeholder { color: var(--color-muted); opacity: 1; }
}

@layer components {
  /* App shell: sticky sidebar + fluid main column. */
  .app-shell {
    display: grid;
    grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
    min-height: 100vh;
  }

  /* Warm-glass backdrop atmosphere: two soft blobs behind the frosted chrome. */
  .app-backdrop {
    position: fixed;
    inset: 0;
    z-index: -1;
    overflow: hidden;
    pointer-events: none;
  }
  .app-blob {
    position: absolute;
    width: 26rem;
    height: 26rem;
    border-radius: 50%;
    filter: blur(64px);
  }
  .app-blob-a { top: -6rem; left: 22%; background: rgba(252, 160, 90, 0.2); }
  .app-blob-b { bottom: -8rem; right: 18%; background: rgba(120, 130, 160, 0.13); }

  /* Frosted sidebar, orange-tinted hairline — onboard's admin chrome, rotated 90°. */
  .app-sidebar {
    position: sticky;
    top: 0;
    height: 100vh;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 1rem;
    padding: 0.9rem 0.7rem;
    background: rgba(255, 255, 255, 0.7);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-right: 1px solid rgba(231, 120, 80, 0.22);
  }
  .navbar {
    display: flex;
    flex-direction: column;
    gap: 0.9rem;
    min-height: 0;
  }
  /* Brand block: logo on top, product name underneath. */
  .navbar-brand {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 0.35rem;
    text-decoration: none;
    font-size: 0.9rem;
    line-height: 1.25;
    font-weight: 600;
    color: #212529;
    padding-inline: 0.35rem;
  }
  .navbar-brand img { height: 30px; width: auto; }
  .navbar-nav {
    display: flex;
    flex-direction: column;
    gap: 2px;
    list-style: none;
    margin: 0;
    padding: 0;
  }
  .nav-link, .scope-nav a {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    text-decoration: none;
    padding: 0.4rem 0.6rem;
    border-radius: 8px;
    font-size: 0.9rem;
    font-weight: 500;
    color: rgba(0, 0, 0, 0.62);
  }
  .nav-link:hover, .scope-nav a:hover { color: var(--color-ink); background: rgba(0, 0, 0, 0.045); }
  .nav-link.active, .scope-nav a.active {
    background: linear-gradient(135deg, var(--color-primary-2), var(--color-primary));
    color: #fff;
  }

  /* Scope switcher: the resolved partner/customer workspace, in the sidebar. */
  .scope-head {
    border-top: 1px solid var(--color-edge);
    padding-top: 0.8rem;
    display: flex;
    flex-direction: column;
    gap: 0.55rem;
  }
  .scope-title {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.4rem;
    padding-inline: 0.35rem;
  }
  .scope-title h1 { font-size: 0.95rem; margin: 0; font-weight: 650; }
  .scope-nav { display: flex; flex-direction: column; gap: 2px; }

  .sidebar-foot {
    margin-top: auto;
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
    padding-inline: 0.35rem;
    font-size: 0.8rem;
  }
  .sidebar-foot form {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 0.3rem;
    margin: 0;
  }
  .sidebar-foot .legal { font-size: 0.72rem; }

  main { padding: 1rem 1.3rem 2.5rem; min-width: 0; }

  /* Side-by-side panels: the anti-scroll workhorse. Sections placed in a
     .cols wrapper sit next to each other and wrap on narrow screens. */
  .cols {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(360px, 1fr));
    gap: 0.9rem;
    align-items: start;
    margin-bottom: 0.9rem;
  }
  /* min-width: 0 lets a panel shrink below its table's content width instead
     of forcing the whole page to scroll horizontally. */
  .cols > section { margin-bottom: 0; min-width: 0; }

  /* Cards — every <section> is a warm-glass card, like onboard's panels.
     overflow-x: auto contains a too-wide table inside its own panel scrollbar
     instead of bleeding across the page. */
  section {
    background: var(--color-surface);
    border: 1px solid var(--color-edge);
    border-radius: var(--radius-card);
    padding: 0.85rem 1rem;
    margin-bottom: 0.9rem;
    box-shadow: 0 1px 2px rgb(28 25 23 / 0.04), 0 8px 24px -16px rgb(28 25 23 / 0.12);
    overflow-x: auto;
  }

  table { border-collapse: collapse; width: 100%; }
  th {
    color: var(--color-muted);
    font-weight: 600;
    font-size: 0.72rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
  }
  th, td {
    text-align: left;
    padding: 0.42rem 0.55rem;
    border-bottom: 1px solid color-mix(in srgb, var(--color-edge) 60%, transparent);
  }

  form.inline {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
    align-items: center;
    margin-top: 0.6rem;
  }
  input, select, textarea {
    padding: 0.4rem 0.65rem;
    border: 1px solid var(--color-edge);
    border-radius: var(--radius-field);
    font-size: 0.875rem;
    background: var(--color-surface);
    color: var(--color-ink);
  }
  input:focus, select:focus, textarea:focus {
    outline: 2px solid color-mix(in srgb, var(--color-primary) 40%, transparent);
    outline-offset: 1px;
  }

  /* Primary surfaces: from-primary-2 to-primary gradient, per onboard tokens. */
  button {
    padding: 0.4rem 0.8rem;
    border: none;
    border-radius: var(--radius-field);
    font-size: 0.85rem;
    font-weight: 600;
    color: #fff;
    background: linear-gradient(135deg, var(--color-primary-2), var(--color-primary));
    cursor: pointer;
  }
  button:hover { filter: brightness(0.94); }

  /* Quiet buttons: secondary/row actions. The gradient button is reserved
     for the primary action of a panel; everything else stays calm. */
  button.quiet {
    background: var(--color-surface);
    color: var(--color-ink);
    border: 1px solid var(--color-edge);
    font-weight: 500;
    font-size: 0.8rem;
    padding: 0.24rem 0.6rem;
  }
  button.quiet:hover {
    filter: none;
    border-color: color-mix(in srgb, var(--color-primary) 45%, white);
    background: color-mix(in srgb, var(--color-primary) 7%, white);
    color: var(--color-primary-hover);
  }
  button.quiet.danger:hover {
    border-color: color-mix(in srgb, var(--color-danger) 45%, white);
    background: color-mix(in srgb, var(--color-danger) 7%, white);
    color: var(--color-danger);
  }

  /* Disabled reads as inert. Without this a blocked primary action keeps its
     gradient and looks clickable. */
  button:disabled,
  button:disabled:hover {
    background: var(--color-edge);
    border-color: var(--color-edge);
    color: var(--color-muted);
    filter: none;
    cursor: not-allowed;
  }

  /* Id chips: truncated identifier, click to copy the full value. */
  .id-chip {
    font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
    font-size: 0.7rem;
    color: var(--color-muted);
    background: #f6f4f2;
    border: 1px solid var(--color-edge);
    border-radius: 6px;
    padding: 0.08rem 0.4rem;
    cursor: copy;
    white-space: nowrap;
  }
  .id-chip:hover { color: var(--color-primary-hover); border-color: color-mix(in srgb, var(--color-primary) 35%, white); }
  .id-chip.copied { color: var(--color-success); border-color: #cbe5d6; background: #e7f3ec; }

  /* Inline checkbox sets (e.g. partner entitlements). */
  .check-row {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.15rem 0.75rem;
    margin: 0;
  }
  .check-row label {
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    font-size: 0.8rem;
    color: var(--color-muted);
    white-space: nowrap;
  }

  /* Generic list rows — table-free panels (Admin partners, org lists). */
  .list-row {
    padding: 0.5rem 0.2rem;
    border-bottom: 1px solid color-mix(in srgb, var(--color-edge) 60%, transparent);
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
  }
  .list-row:last-child { border-bottom: none; }
  .list-row .row-line {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.45rem;
  }

  .notice {
    background: #eef6ee;
    border: 1px solid #bfdcbf;
    padding: 0.6rem 0.85rem;
    border-radius: 10px;
    word-break: break-all;
    margin-bottom: 0.9rem;
  }
  .notice.error { background: #fbeeee; border-color: #dcbfbf; }
  /* Something is blocked until an action elsewhere; prose, so words stay whole. */
  .notice.warning { background: #fdf6e3; border-color: #f0e2b6; word-break: normal; overflow-wrap: anywhere; }

  .icon { vertical-align: -0.125em; flex-shrink: 0; }

  /* Org lists — compact rows (avatar, name, quick actions), not cards. */
  .org-list { display: flex; flex-direction: column; }
  .org-row {
    display: flex;
    align-items: center;
    gap: 0.7rem;
    padding: 0.45rem 0.2rem;
    border-bottom: 1px solid color-mix(in srgb, var(--color-edge) 60%, transparent);
  }
  .org-row:last-child { border-bottom: none; }
  .org-row .org-avatar {
    display: grid;
    place-items: center;
    width: 30px;
    height: 30px;
    border-radius: 8px;
    color: #fff;
    background: linear-gradient(135deg, var(--color-primary-2), var(--color-primary));
    flex-shrink: 0;
  }
  .org-name { font-weight: 600; font-size: 0.925rem; color: var(--color-ink); text-decoration: none; }
  .org-name:hover { color: var(--color-primary-hover); }
  .org-sub { color: var(--color-muted); font-size: 0.78rem; }
  .org-row-actions { margin-left: auto; display: flex; flex-wrap: wrap; gap: 0.3rem; }

  .badge {
    display: inline-block;
    padding: 0.12rem 0.5rem;
    border-radius: 999px;
    font-size: 0.72rem;
    font-weight: 600;
    letter-spacing: 0.02em;
    background: color-mix(in srgb, var(--color-primary) 12%, white);
    color: var(--color-primary-hover);
    border: 1px solid color-mix(in srgb, var(--color-primary) 25%, white);
  }
  .badge.neutral { background: #f1efed; color: var(--color-muted); border-color: var(--color-edge); }
  .badge.success { background: #e7f3ec; color: var(--color-success); border-color: #cbe5d6; }
  .badge.warning { background: #fdf6e3; color: #8a6d00; border-color: #f0e2b6; }

  /* Chips: compact icon+label links (row quick actions). */
  .chip {
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    padding: 0.22rem 0.55rem;
    border-radius: 999px;
    border: 1px solid var(--color-edge);
    background: var(--color-surface);
    color: var(--color-ink);
    text-decoration: none;
    font-size: 0.78rem;
    font-weight: 500;
    transition: border-color 0.12s ease, background 0.12s ease, color 0.12s ease;
  }
  .chip:hover {
    border-color: color-mix(in srgb, var(--color-primary) 45%, white);
    background: color-mix(in srgb, var(--color-primary) 7%, white);
    color: var(--color-primary-hover);
  }

  /* Stat strip: one compact row of label + semibold value (+ optional note). */
  .stat-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
    gap: 0.6rem;
    margin-bottom: 0.9rem;
  }
  .stat {
    background: var(--color-surface);
    border: 1px solid var(--color-edge);
    border-radius: 10px;
    padding: 0.5rem 0.8rem;
    box-shadow: 0 1px 2px rgb(28 25 23 / 0.04);
  }
  .stat .stat-label { color: var(--color-muted); font-size: 0.74rem; margin-bottom: 0.1rem; }
  .stat .stat-value { font-size: 1.3rem; font-weight: 650; line-height: 1.1; }
  .stat .stat-note { font-size: 0.72rem; color: var(--color-muted); margin-top: 0.15rem; }
  .stat .stat-note.warning { color: #8a6d00; }

  .field-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: 0.7rem;
    margin: 0.6rem 0;
  }
  .field label {
    display: block;
    font-size: 0.78rem;
    font-weight: 600;
    color: var(--color-muted);
    margin-bottom: 0.2rem;
  }
  .field input, .field select { width: 100%; }
  .field .hint { font-size: 0.72rem; color: var(--color-muted); margin-top: 0.2rem; }

  /* Sidebar sub-nav: sections within a top-level area (Admin). Indented and
     one step quieter than .nav-link so the hierarchy reads at a glance. */
  .subnav {
    display: flex;
    flex-direction: column;
    gap: 2px;
    margin: 0.15rem 0 0 0.7rem;
    padding-left: 0.55rem;
    border-left: 1px solid var(--color-edge);
  }
  .subnav a {
    display: flex;
    align-items: center;
    gap: 0.45rem;
    text-decoration: none;
    padding: 0.3rem 0.5rem;
    border-radius: 7px;
    font-size: 0.82rem;
    color: rgba(0, 0, 0, 0.58);
  }
  .subnav a:hover { color: var(--color-ink); background: rgba(0, 0, 0, 0.045); }
  .subnav a.active {
    color: var(--color-primary);
    background: color-mix(in srgb, var(--color-primary) 9%, transparent);
    font-weight: 600;
  }
  .subnav .icon { opacity: 0.75; }

  /* ---------------------------------------------------------------------
     Blades: configuration slides in from the right instead of expanding
     inline. The list stays put and readable underneath, so you keep your
     place — the reason the old all-inline admin page was unusable.
     --------------------------------------------------------------------- */
  .list-head {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 1rem;
    margin-bottom: 0.6rem;
  }
  .list-head h1 { margin: 0; }
  .list-head .sub { color: var(--color-muted); font-size: 0.82rem; }

  /* Row is the whole click target — the chevron is affordance, not the button. */
  .pick-row {
    display: flex;
    align-items: center;
    gap: 0.7rem;
    width: 100%;
    padding: 0.5rem 0.6rem;
    border: 1px solid transparent;
    border-bottom-color: color-mix(in srgb, var(--color-edge) 60%, transparent);
    border-radius: 8px;
    text-decoration: none;
    color: inherit;
    background: none;
    text-align: left;
  }
  .pick-row:hover { background: rgba(0, 0, 0, 0.03); border-color: var(--color-edge); }
  .pick-row.selected {
    background: color-mix(in srgb, var(--color-primary) 7%, transparent);
    border-color: color-mix(in srgb, var(--color-primary) 30%, transparent);
  }
  .pick-row .grow { flex: 1; min-width: 0; }
  .pick-row .title { font-weight: 600; font-size: 0.9rem; }
  .pick-row .meta {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.35rem;
    margin-top: 0.15rem;
    font-size: 0.76rem;
    color: var(--color-muted);
  }
  .pick-row .chev { color: var(--color-muted); flex: none; }

  /* Tiles: a catalog of things you could configure, where each is picked
     rather than compared. Rows would imply an ordering that does not exist. */
  .tile-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(13rem, 1fr));
    gap: 0.7rem;
    align-items: stretch;
  }
  .tile {
    display: flex;
    flex-direction: column;
    gap: 0.3rem;
    padding: 0.85rem 0.9rem;
    background: var(--color-surface);
    border: 1px solid var(--color-edge);
    border-radius: var(--radius-card);
    text-decoration: none;
    color: inherit;
    box-shadow: 0 1px 2px rgba(28, 25, 23, 0.04);
    transition: border-color 0.12s, box-shadow 0.12s, transform 0.12s;
  }
  .tile:hover {
    border-color: color-mix(in srgb, var(--color-primary) 45%, var(--color-edge));
    box-shadow: 0 4px 14px -6px rgba(28, 25, 23, 0.22);
    transform: translateY(-1px);
  }
  .tile.selected {
    border-color: var(--color-primary);
    background: color-mix(in srgb, var(--color-primary) 6%, var(--color-surface));
  }
  /* Configured tiles carry a quiet accent edge so the eye finds them first. */
  .tile.configured { border-left: 3px solid color-mix(in srgb, var(--color-primary) 55%, transparent); }
  .tile-icon { color: var(--color-primary); }
  .tile-title { font-weight: 650; font-size: 0.95rem; line-height: 1.2; }
  .tile-key { font-size: 0.72rem; color: var(--color-muted); }
  .tile-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 0.3rem;
    margin-top: auto;
    padding-top: 0.4rem;
  }

  @media (prefers-reduced-motion: reduce) {
    .tile { transition: none; }
    .tile:hover { transform: none; }
  }

  .blade-scrim {
    position: fixed;
    inset: 0;
    background: rgba(28, 25, 23, 0.18);
    backdrop-filter: blur(1px);
    z-index: 40;
    animation: blade-fade 0.14s ease-out;
  }
  .blade {
    position: fixed;
    top: 0;
    right: 0;
    height: 100vh;
    width: min(30rem, 100vw);
    z-index: 41;
    display: flex;
    flex-direction: column;
    background: var(--color-surface);
    border-left: 1px solid var(--color-edge);
    box-shadow: -18px 0 40px -18px rgba(28, 25, 23, 0.35);
    animation: blade-in 0.18s cubic-bezier(0.22, 0.61, 0.36, 1);
  }
  .blade-head {
    display: flex;
    align-items: flex-start;
    gap: 0.6rem;
    padding: 0.85rem 1rem;
    border-bottom: 1px solid var(--color-edge);
  }
  .blade-head .grow { flex: 1; min-width: 0; }
  .blade-head h2 { margin: 0; font-size: 1.05rem; }
  .blade-head .sub { color: var(--color-muted); font-size: 0.78rem; margin-top: 0.1rem; }
  .blade-close {
    flex: none;
    line-height: 1;
    padding: 0.25rem 0.45rem;
    border-radius: 6px;
    text-decoration: none;
    color: var(--color-muted);
    font-size: 1.1rem;
  }
  .blade-close:hover { background: rgba(0, 0, 0, 0.06); color: var(--color-ink); }
  .blade-body { padding: 0.9rem 1rem 1.4rem; overflow-y: auto; flex: 1; }
  .blade-body h4 { margin-top: 1.1rem; }
  .blade-body h4:first-child { margin-top: 0; }
  .blade-body .field-grid { grid-template-columns: 1fr; }
  .blade-foot {
    display: flex;
    gap: 0.5rem;
    align-items: center;
    padding: 0.7rem 1rem;
    border-top: 1px solid var(--color-edge);
    background: color-mix(in srgb, var(--color-edge) 18%, var(--color-surface));
  }

  @keyframes blade-in { from { transform: translateX(100%); } to { transform: translateX(0); } }
  @keyframes blade-fade { from { opacity: 0; } to { opacity: 1; } }

  @media (prefers-reduced-motion: reduce) {
    .blade, .blade-scrim { animation: none; }
  }

  /* Narrow screens: sidebar folds into a top bar. */
  @media (max-width: 900px) {
    .blade { width: 100vw; border-left: none; }
    .subnav { margin-left: 0; border-left: none; padding-left: 0; flex-direction: row; flex-wrap: wrap; }
    .app-shell { display: block; }
    .app-sidebar {
      position: static;
      height: auto;
      flex-direction: row;
      flex-wrap: wrap;
      align-items: center;
      gap: 0.6rem;
      border-right: none;
      border-bottom: 1px solid rgba(231, 120, 80, 0.22);
    }
    .navbar { flex-direction: row; align-items: center; flex-wrap: wrap; gap: 0.6rem; flex: 1; }
    .navbar-brand { flex-direction: row; align-items: center; }
    .navbar-nav { flex-direction: row; }
    .scope-head { border-top: none; padding-top: 0; flex-direction: row; align-items: center; flex-wrap: wrap; }
    .scope-nav { flex-direction: row; flex-wrap: wrap; }
    .sidebar-foot { margin-top: 0; flex-direction: row; align-items: center; }
    .sidebar-foot form { flex-direction: row; align-items: center; }
    .sidebar-foot .legal { display: none; }
    main { padding: 0.9rem 0.75rem 2rem; }
  }
}
