/* RBG Apps workspace — JACK layout standard with the RBG purple/green palette. Login keeps login.css. */
:root {
  --brand: #703570; --brand-strong: #5A2A5A; --brand-accent: #A64CA6; --brand-soft: #F5EEF5; --brand-line: #E4D3E4;
  --green: #00B050; --green-strong: #007A33; --green-soft: #E6F4EC;
  --text: #2F2F2F; --text-strong: #241A28; --muted: #6B7280; --line: #E3E5EA; --line-strong: #CBD0D8;
  --bg: #F5F6F8; --surface: #FFFFFF; --surface-alt: #FAFAFB;
  --danger: #B42B39; --danger-soft: #FBEAEC; --warn: #8B5B10; --warn-soft: #FBF3E4;
  --s1: 4px; --s2: 8px; --s3: 12px; --s4: 16px; --s5: 20px; --s6: 24px; --s8: 32px; --s10: 40px;
  --r-ctl: 8px; --r-panel: 12px;
  --topbar: 64px; --nav: 224px; --context: 400px;
  --font: "Segoe UI", Inter, system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  --shadow: 0 1px 2px rgb(36 26 40 / 6%), 0 4px 12px rgb(36 26 40 / 5%);
  --focus: 0 0 0 3px rgb(166 76 166 / 35%);
  /* Variables used by login.css (SystemApp login design). */
  --rb-green-primary: var(--green); --rb-green-dark: var(--green-strong); --rb-purple: var(--brand-accent);
  --rb-bg-page: var(--bg); --rb-gray-text: var(--text); --rb-gray-muted: var(--muted);
}
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; font: 15px/1.5 var(--font); color: var(--text); background: var(--bg); }
[hidden] { display: none !important; }
a { color: var(--brand); }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: none; box-shadow: var(--focus); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.skip-link { position: absolute; left: var(--s2); top: -48px; z-index: 50; background: var(--surface); padding: var(--s2) var(--s3); border-radius: var(--r-ctl); }
.skip-link:focus { top: var(--s2); }

/* ---------- Shell ---------- */
.shell { display: grid; min-height: 100vh; grid-template-columns: var(--nav) minmax(0, 1fr); grid-template-rows: var(--topbar) 1fr; grid-template-areas: "top top" "nav main"; }
.shell.context-open { grid-template-columns: var(--nav) minmax(0, 1fr) var(--context); grid-template-areas: "top top top" "nav main context"; }
.topbar { grid-area: top; position: sticky; top: 0; z-index: 30; display: flex; align-items: center; gap: var(--s3); padding: 0 var(--s6); background: var(--surface); border-bottom: 1px solid var(--line); }
.topbar-brand { display: flex; align-items: center; gap: var(--s3); text-decoration: none; color: var(--text-strong); min-width: 0; }
.topbar-brand img { width: 58px; height: auto; display: block; }
.topbar-title { font-size: 19px; font-weight: 700; letter-spacing: -.2px; white-space: nowrap; }
.topbar-title span { font-weight: 400; color: var(--brand); }
.identity { margin-left: auto; display: flex; align-items: center; gap: var(--s3); min-width: 0; }
.identity-text { display: flex; align-items: center; gap: var(--s2); min-width: 0; }
.identity-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 260px; }
.topbar .nav-toggle { display: none; }

.nav { grid-area: nav; position: sticky; top: var(--topbar); height: calc(100vh - var(--topbar)); overflow-y: auto; display: flex; flex-direction: column; background: var(--surface); border-right: 1px solid var(--line); padding: var(--s4) var(--s3); }
.nav-group + .nav-group { margin-top: var(--s5); }
.nav-heading { margin: 0 0 var(--s1); padding: 0 var(--s3); font-size: 11px; font-weight: 700; letter-spacing: .8px; text-transform: uppercase; color: var(--muted); }
.nav-link { display: flex; align-items: center; justify-content: space-between; gap: var(--s2); min-height: 40px; padding: var(--s2) var(--s3); border-radius: var(--r-ctl); color: #4B5563; text-decoration: none; font-size: 14px; }
.nav-link:hover { background: var(--brand-soft); color: var(--brand); }
.nav-link.is-active { background: var(--brand-soft); color: var(--brand); font-weight: 600; box-shadow: inset 3px 0 0 var(--green); }
.nav-count { font-size: 12px; color: var(--muted); background: var(--bg); border-radius: 10px; padding: 0 var(--s2); }
.nav-footer { margin-top: auto; padding: var(--s8) var(--s2) var(--s2); }
.nav-footer img { display: block; width: 110px; height: auto; margin: 0 auto; opacity: .9; }

.workspace { grid-area: main; min-width: 0; padding: var(--s8) var(--s8) var(--s10); outline: none; }
.workspace > * { max-width: 1280px; }
.context { grid-area: context; z-index: 5; position: sticky; top: var(--topbar); height: calc(100vh - var(--topbar)); overflow-y: auto; background: var(--surface); border-left: 1px solid var(--line); }
.context-head { position: sticky; top: 0; z-index: 2; display: flex; align-items: flex-start; justify-content: space-between; gap: var(--s3); padding: var(--s5) var(--s5) var(--s3); background: var(--surface); border-bottom: 1px solid var(--line); }
.context-head h2 { margin: 0; font-size: 18px; line-height: 1.3; color: var(--text-strong); }
.context-head p { margin: var(--s1) 0 0; font-size: 13px; }
.context-body { padding: var(--s4) var(--s5) var(--s6); }
.backdrop { display: none; }

/* ---------- Page header ---------- */
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: var(--s4); flex-wrap: wrap; margin-bottom: var(--s6); }
.page-title h1 { margin: 0; font-size: 28px; line-height: 1.2; letter-spacing: -.4px; color: var(--text-strong); }
.page-title p { margin: var(--s2) 0 0; color: var(--muted); max-width: 72ch; }
.page-actions { display: flex; gap: var(--s2); flex-wrap: wrap; }
.notice-inline { padding: var(--s3) var(--s4); border-left: 3px solid var(--brand-accent); background: var(--surface); border-radius: var(--r-ctl); font-size: 14px; }

/* ---------- Buttons, badges ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: var(--s2); min-height: 40px; padding: var(--s2) var(--s4); border: 1px solid transparent; border-radius: var(--r-ctl); font-weight: 600; font-size: 14px; cursor: pointer; white-space: nowrap; text-decoration: none; }
.btn-sm { min-height: 32px; padding: var(--s1) var(--s3); font-size: 13px; }
.btn-primary { background: var(--green-strong); color: #fff; }
.btn-primary:hover { background: #006529; }
.btn-secondary { background: var(--surface); color: var(--brand); border-color: var(--brand-line); }
.btn-secondary:hover { background: var(--brand-soft); }
.btn-ghost { background: transparent; color: var(--muted); }
.btn-ghost:hover { background: var(--bg); color: var(--text); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger-outline { background: var(--surface); color: var(--danger); border-color: #EBC2C7; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.is-busy { cursor: progress; }
.btn.is-busy::after { content: ""; width: 14px; height: 14px; border-radius: 50%; border: 2px solid currentColor; border-right-color: transparent; animation: spin .8s linear infinite; }
.icon-btn { display: inline-grid; place-items: center; width: 40px; height: 40px; border: 0; border-radius: var(--r-ctl); background: transparent; color: var(--brand); cursor: pointer; flex-shrink: 0; }
.icon-btn:hover { background: var(--brand-soft); }
.badge { display: inline-flex; align-items: center; padding: 2px var(--s2); border-radius: 6px; font-size: 12px; font-weight: 600; line-height: 1.5; white-space: nowrap; }
.badge-ok { background: var(--green-soft); color: var(--green-strong); }
.badge-bad { background: var(--danger-soft); color: var(--danger); }
.badge-warn { background: var(--warn-soft); color: var(--warn); }
.badge-muted { background: #EEF0F3; color: #4B5563; }
.badge-brand { background: var(--brand-soft); color: var(--brand); }

/* ---------- States ---------- */
.state { display: flex; flex-direction: column; align-items: flex-start; gap: var(--s2); padding: var(--s8) var(--s6); background: var(--surface); border: 1px dashed var(--line-strong); border-radius: var(--r-panel); }
.state strong { color: var(--text-strong); }
.state p { margin: 0; color: var(--muted); }
.state-loading { flex-direction: row; align-items: center; border-style: solid; border-color: var(--line); color: var(--muted); }
.state-error { border: 1px solid #EBC2C7; background: #FFFAFA; }
.spinner { width: 18px; height: 18px; border-radius: 50%; border: 2px solid var(--brand-line); border-top-color: var(--brand); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Application launcher ---------- */
.app-search { max-width: 420px; margin-bottom: var(--s5); }
.app-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: var(--s4); }
.app-card { position: relative; display: grid; grid-template-columns: auto 1fr auto; align-items: start; gap: var(--s4); padding: var(--s5); background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-panel); text-decoration: none; color: var(--text); box-shadow: var(--shadow); transition: border-color .15s, box-shadow .15s; }
.app-card:hover { border-color: var(--brand-accent); box-shadow: 0 6px 20px rgb(112 53 112 / 12%); }
.app-card.is-busy { cursor: progress; opacity: .8; }
.app-card-text { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.app-card-name { font-size: 17px; font-weight: 650; color: var(--text-strong); line-height: 1.3; }
.app-card-category { font-size: 12px; font-weight: 600; color: var(--green-strong); text-transform: uppercase; letter-spacing: .6px; }
.app-card-desc { margin-top: var(--s1); font-size: 13px; color: var(--muted); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.app-card-status { font-size: 12px; color: var(--brand); min-height: 0; }
.app-card-status:empty { display: none; }
.app-card-go { display: grid; place-items: center; width: 32px; height: 32px; border-radius: 50%; background: var(--brand-soft); color: var(--brand); }
.app-logo { --size: 48px; display: inline-grid; place-items: center; width: var(--size); height: var(--size); flex-shrink: 0; border-radius: calc(var(--size) * .22); overflow: hidden; background: var(--surface); border: 1px solid var(--line); }
.app-logo img { width: 100%; height: 100%; object-fit: contain; padding: calc(var(--size) * .06); }
.app-logo-mono { border: 0; font-weight: 700; font-size: calc(var(--size) * .38); letter-spacing: -.5px; }

/* ---------- Data table (RBG admin table standard, as 5R / SystemApp) ---------- */
.datatable { display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; min-width: 0; }
.filter-bar { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 8px; }
.filter-field { display: grid; gap: 2px; font-size: 11px; font-weight: 600; color: var(--muted); min-width: 0; }
.filter-field .input { width: 190px; max-width: 100%; font-weight: 400; color: var(--text); }
.table-toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.spacer { flex: 1; }
.search { position: relative; width: 280px; max-width: 100%; color: var(--muted); }
.search svg { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); pointer-events: none; }
.search input { padding-left: 32px; }
input, select, textarea { width: 100%; min-height: 40px; padding: var(--s2) var(--s3); background: var(--surface); border: 1px solid var(--line-strong); border-radius: var(--r-ctl); color: var(--text); }
.input { min-height: 34px; padding: 5px 8px; font-size: 13px; }
select.input { padding-right: 24px; }
.input-inline { width: auto; }
.page-size { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); white-space: nowrap; }
input:hover, select:hover, textarea:hover { border-color: #AEB5C0; }
input[readonly], select:disabled { background: var(--surface-alt); color: var(--muted); }
input[type=checkbox] { width: 20px; height: 20px; min-height: 0; padding: 0; accent-color: var(--brand); cursor: pointer; }
.table-wrap { position: relative; overflow: auto; max-height: calc(100vh - 230px); background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-ctl); }
.context .table-wrap { max-height: none; }
table { width: 100%; border-collapse: collapse; text-align: left; font-size: 13px; }
th { position: sticky; top: 0; z-index: 1; padding: 8px 10px; background: var(--surface-alt); border-bottom: 1px solid var(--line); font-size: 12px; font-weight: 600; color: var(--muted); white-space: nowrap; }
td { padding: 9px 10px; border-bottom: 1px solid #EEF0F3; vertical-align: top; }
tbody tr:last-child td { border-bottom: 0; }
td.cell-primary { font-weight: 600; color: var(--text-strong); min-width: 150px; }
td.nowrap { white-space: nowrap; }
.col-no { width: 44px; text-align: right; color: var(--muted); }
td.empty-cell { padding: 28px 10px; text-align: center; color: var(--muted); }
tr.is-clickable { cursor: pointer; }
tr.is-clickable:hover td { background: #FBF8FB; }
.sort { display: inline-flex; align-items: center; gap: 4px; padding: 0; border: 0; background: none; font: inherit; color: inherit; cursor: pointer; }
.sort.is-active { color: var(--brand); }
.sort-mark { font-size: 10px; opacity: .7; }
.col-actions { text-align: right; white-space: nowrap; }
.table-foot { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; font-size: 12px; }
.pager-buttons { display: flex; align-items: center; gap: 4px; }
.pager-buttons .btn-sm { min-width: 32px; padding: 2px 8px; }

/* ---------- Context panel content ---------- */
.detail-list { display: grid; grid-template-columns: minmax(110px, 40%) 1fr; gap: var(--s2) var(--s3); margin: 0; font-size: 14px; }
.detail-list dt { color: var(--muted); }
.detail-list dd { margin: 0; overflow-wrap: anywhere; }
.panel-actions { display: flex; flex-wrap: wrap; gap: var(--s2); margin-top: var(--s5); }
.sticky-actions { position: sticky; bottom: 0; padding: var(--s3) 0; background: linear-gradient(transparent, var(--surface) 30%); }
.panel-section { margin: var(--s6) 0 var(--s1); font-size: 15px; color: var(--text-strong); }
.access-table td, .access-table th { padding: var(--s2) var(--s3); }
.access-table th:not(:first-child), .access-table td:not(:first-child) { width: 1%; text-align: center; white-space: nowrap; }
.access-table th { white-space: normal; line-height: 1.3; }
.access-table tr.is-dirty td { background: var(--warn-soft); }
.access-app { display: inline-flex; align-items: center; gap: var(--s2); }
.check { display: inline-flex; align-items: center; gap: var(--s2); cursor: pointer; }
.diff-table td { font-size: 13px; overflow-wrap: anywhere; }

/* ---------- Modal and forms ---------- */
.modal { width: min(560px, calc(100% - 32px)); max-height: calc(100vh - 32px); padding: 0; border: 1px solid var(--line); border-radius: var(--r-panel); color: var(--text); box-shadow: 0 20px 50px rgb(36 26 40 / 25%); }
.modal.modal-wide { width: min(760px, calc(100% - 32px)); }
.modal::backdrop { background: rgb(36 26 40 / 45%); }
.modal-head { display: flex; align-items: center; justify-content: space-between; gap: var(--s3); padding: var(--s4) var(--s5); border-bottom: 1px solid var(--line); }
.modal-head h2 { margin: 0; font-size: 18px; color: var(--text-strong); }
.modal-body { padding: var(--s5); overflow-y: auto; max-height: calc(100vh - 180px); }
.modal-body p { margin-top: 0; }
.modal-foot { display: flex; justify-content: flex-end; gap: var(--s2); padding: var(--s3) var(--s5); border-top: 1px solid var(--line); background: var(--surface-alt); }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--s4); }
.modal:not(.modal-wide) .form-grid { grid-template-columns: 1fr; }
.field { display: flex; flex-direction: column; gap: var(--s1); min-width: 0; }
.field-wide { grid-column: 1 / -1; }
.field > label { font-size: 13px; font-weight: 600; color: var(--text-strong); }
.field .req { color: var(--danger); }
.field-check label { display: flex; align-items: center; gap: var(--s2); font-weight: 600; font-size: 14px; }
.help { font-size: 12px; color: var(--muted); }
.field-error { font-size: 12px; color: var(--danger); }
.field-error:empty { display: none; }
.field.has-error input, .field.has-error select, .field.has-error textarea { border-color: var(--danger); }
.form-error { margin: var(--s3) 0 0; color: var(--danger); font-size: 14px; }
.form-error:empty { display: none; }
.token { display: block; margin: var(--s3) 0; padding: var(--s3); background: var(--brand-soft); border-radius: var(--r-ctl); word-break: break-all; font-size: 13px; }

/* ---------- Toasts ---------- */
.toasts { position: fixed; right: var(--s5); top: calc(var(--topbar) + var(--s3)); z-index: 100; display: flex; flex-direction: column; gap: var(--s2); max-width: min(420px, calc(100% - 32px)); }
.toast { display: flex; align-items: flex-start; gap: var(--s3); padding: var(--s3) var(--s3) var(--s3) var(--s4); background: var(--surface); border: 1px solid var(--line); border-left: 4px solid var(--green); border-radius: var(--r-ctl); box-shadow: var(--shadow); font-size: 14px; }
.toast-error { border-left-color: var(--danger); }
.toast span { flex: 1; padding-top: 2px; }
.toast-close { border: 0; background: none; color: var(--muted); cursor: pointer; padding: 2px; border-radius: 4px; }

/* ---------- Responsive ---------- */
@media (max-width: 1199px) {
  .shell.context-open { grid-template-columns: var(--nav) minmax(0, 1fr); grid-template-areas: "top top" "nav main"; }
  .context { position: fixed; top: 0; right: 0; bottom: 0; z-index: 60; width: min(440px, 100%); height: auto; box-shadow: -12px 0 32px rgb(36 26 40 / 18%); }
  .shell.context-open .backdrop-context { display: block; position: fixed; inset: 0; z-index: 55; background: rgb(36 26 40 / 35%); }
}
@media (max-width: 759px) {
  :root { --topbar: 56px; }
  .shell, .shell.context-open { grid-template-columns: minmax(0, 1fr); grid-template-areas: "top" "main"; }
  .topbar { padding: 0 var(--s2) 0 var(--s1); gap: var(--s2); }
  .topbar .nav-toggle { display: inline-grid; }
  .topbar-brand img { width: 46px; }
  .topbar-title { font-size: 17px; }
  .identity-name, .identity .badge { display: none; }
  .identity .btn span { display: none; }
  .nav { position: fixed; top: var(--topbar); left: 0; bottom: 0; z-index: 45; width: min(280px, 85vw); height: auto; transform: translateX(-100%); transition: transform .2s ease; visibility: hidden; }
  .shell.nav-open .nav { transform: none; visibility: visible; box-shadow: 12px 0 32px rgb(36 26 40 / 18%); }
  .shell.nav-open .backdrop-nav { display: block; position: fixed; inset: var(--topbar) 0 0 0; z-index: 40; background: rgb(36 26 40 / 35%); }
  .workspace { padding: var(--s5) var(--s4) var(--s8); }
  .page-title h1 { font-size: 24px; }
  .page-actions { width: 100%; }
  .page-actions .btn { flex: 1; }
  .app-grid { grid-template-columns: 1fr; gap: var(--s3); }
  .app-card { padding: var(--s4); gap: var(--s3); }
  .search { width: 100%; }
  .filter-field { flex: 1 1 140px; }
  .filter-field .input { width: 100%; }
  .table-wrap { max-height: none; }
  /* Stacked, labelled records instead of a horizontally scrolling table. */
  .datatable .table-wrap { overflow: visible; background: none; border: 0; }
  .datatable table, .datatable tbody, .datatable tr, .datatable td { display: block; width: 100%; }
  .datatable thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  .datatable tr { margin-bottom: var(--s3); padding: var(--s3) var(--s4); background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-panel); }
  .datatable td { display: grid; grid-template-columns: minmax(96px, 38%) 1fr; gap: var(--s3); padding: var(--s1) 0; border: 0; white-space: normal; overflow-wrap: anywhere; }
  .datatable td > .badge, .datatable td > a { justify-self: start; }
  .datatable td.cell-primary { min-width: 0; }
  .datatable td.col-no { display: none; }
  .datatable tr.row-empty td::before { display: none; }
  .datatable tr.row-empty td { display: block; text-align: center; }
  .datatable td::before { content: attr(data-label); color: var(--muted); font-size: 12px; font-weight: 600; padding-top: 2px; }
  .datatable td.cell-primary { display: block; padding-bottom: var(--s2); font-size: 15px; }
  .datatable td.cell-primary::before { display: none; }
  .datatable td.col-actions { display: flex; justify-content: flex-start; padding-top: var(--s2); }
  .datatable td.col-actions::before { display: none; }
  .datatable tr.is-clickable:hover td { background: none; }
  .form-grid { grid-template-columns: 1fr; }
  .modal, .modal.modal-wide { width: 100%; max-width: 100%; max-height: 100%; height: 100%; margin: 0; border-radius: 0; }
  .modal-body { max-height: none; flex: 1; }
  .modal[open] { display: flex; flex-direction: column; }
  .modal-confirm[open] { height: auto; margin: auto var(--s4); width: calc(100% - 32px); border-radius: var(--r-panel); }
  .toasts { left: var(--s3); right: var(--s3); top: calc(var(--topbar) + var(--s2)); max-width: none; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition: none !important; }
}
