/* Castle staff design system. Screen-only: printed documents keep their layouts.
 * Opt in with cw-page, cw-heading, cw-panel, cw-toolbar, cw-tabs.
 * --wide is for working tables; --form is for focused data entry.
 * Legacy aliases below let existing workflows share these components while
 * retaining their JS hooks, numeric alignment, and meaningful status colors.
 */
@media screen {
  body.castle-workspace {
    --cw-ink: #22364e;
    --cw-muted: #60748b;
    --cw-primary: #2455cc;
    --cw-border: #dce5ef;
    --cw-soft: #f5f8fc;
    --cw-surface: #fff;
    --cw-radius: 16px;
    --cw-control-radius: 9px;
    --cw-shadow: 0 4px 20px #18345306;
    --cw-width: 1400px;
    --cw-wide: 1600px;
    --cw-form: 960px;
    background: #f4f6fa;
    color: var(--cw-ink);
    font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  }
  .castle-workspace > main { padding: 24px 32px 48px; }
  .castle-workspace .cw-page {
    width: 100%; max-width: var(--cw-width); min-width: 0;
    box-sizing: border-box; margin: 0 auto; padding: 0; color: var(--cw-ink);
  }
  .castle-workspace .cw-page.cw-page--wide { max-width: var(--cw-wide); }
  .castle-workspace .cw-page.cw-page--form { max-width: var(--cw-form); }
  .castle-workspace .cw-page.cw-dailies { max-width: 1000px; }
  .castle-workspace .cw-page.cw-dailies.all-loans { max-width: var(--cw-wide); }
  .castle-workspace .cw-page h1 {
    font-family: inherit; font-size: 2.2rem; font-weight: 700;
    line-height: 1.2; letter-spacing: -.04em; color: var(--cw-ink);
    margin: 0;
  }
  .castle-workspace .cw-page h2 {
    font-family: inherit; font-size: 1.15rem; line-height: 1.4;
    letter-spacing: -.02em;
  }
  .castle-workspace .cw-page > h1 { margin: 12px 0 24px; }
  .castle-workspace .cw-heading {
    display: flex; align-items: center; justify-content: space-between;
    flex-wrap: wrap; gap: 16px; padding: 0; border: 0; margin: 12px 0 24px;
    background: transparent;
  }
  .castle-workspace .cw-heading > div { min-width: 0; }
  .castle-workspace .cw-heading h1 { overflow-wrap: anywhere; }
  .castle-workspace .cw-heading :is(p, .muted) {
    color: var(--cw-muted); font-size: .85rem; line-height: 1.5;
  }
  .castle-workspace .cw-heading form { margin: 0; }
  .castle-workspace .cw-heading.cw-finance-heading { justify-content: flex-start; }
  .castle-workspace :is(.user-page-heading,.reviews-heading).cw-heading { justify-content: flex-start; }
  .castle-workspace .cw-page .bonus-heading { justify-content: flex-start; }
  .castle-workspace .cw-page .bonus-location-toggle { font: inherit; letter-spacing: inherit; }
  .castle-workspace .cw-page :is(.muted, .meta, .editor-help) { color: var(--cw-muted); }

  /* Shared surfaces. Do not replace semantic backgrounds on status cards. */
  .castle-workspace .cw-page :is(.cw-panel, .finance-panel, .finance-card,
    .chart-panel, .po-panel, .panel, .workflow-card, .user-card, .editor-card, .users-panel, .location-card, .order-card,
    .product-panel, .orientation-panel, .advertising-panel, .ad-panel) {
    border: 1px solid var(--cw-border); border-radius: var(--cw-radius);
    background: var(--cw-surface); box-shadow: var(--cw-shadow);
  }
  .castle-workspace .cw-page.cw-panel { padding: 24px; }
  .castle-workspace .cw-page :is(.panel-heading, .chart-toolbar) {
    border-color: var(--cw-border); padding: 20px 24px;
  }
  .castle-workspace .cw-page :is(.card, .co-card, .export-balance-card) {
    border-radius: var(--cw-radius); border-color: var(--cw-border);
    box-shadow: var(--cw-shadow);
  }
  .castle-workspace .castle.cw-page .card { background: #fff; padding: 20px; }
  .castle-workspace .castle.cw-page .card-link:hover { background: var(--cw-soft); border-color: #aabed5; }
  .castle-workspace .cw-page .bonus-summary { background: linear-gradient(120deg,#193a59,#2b587f); }

  /* Controls inherit workflow colors; destructive/status actions remain distinct. */
  .castle-workspace .cw-page :is(.button, .primary-button, .secondary-button, .po-secondary-button, .editor-secondary, .editor-cancel,
    .user-secondary, .back-to-pos) {
    box-sizing: border-box; min-height: 40px; border-radius: var(--cw-control-radius);
    padding: 10px 16px; font-family: inherit; font-size: .875rem;
    font-weight: 600; line-height: 1.35; text-decoration: none;
  }
  .castle-workspace .cw-page :is(input:not([type]), input[type=text],
    input[type=email], input[type=tel], input[type=password], input[type=search],
    input[type=number], input[type=date], input[type=time], input[type=datetime-local],
    input[type=url], select, textarea) {
    box-sizing: border-box; max-width: 100%; min-width: 0;
    border-radius: var(--cw-control-radius); font-family: inherit;
    font-size: .9rem; line-height: 1.4;
  }
  .castle-workspace .cw-page :is(input,select,textarea):not(:disabled):not([readonly]) {
    accent-color: var(--cw-primary);
  }
  .castle-workspace .cw-page :is(input,select,textarea)[aria-invalid=true] { border-color: #b42318; }
  .castle-workspace .cw-page :is(a,button,input,select,textarea,summary):focus-visible {
    outline: 3px solid #8eb8ee; outline-offset: 3px;
  }
  .castle-workspace .cw-page :is(button,.button):disabled {
    opacity: .5; cursor: not-allowed;
  }
  .castle-workspace .cw-page :is(.icon-action,.bonus-print,.page-setup-icon) {
    border-radius: var(--cw-control-radius);
  }
  .castle-workspace .cw-page .primary-button,
  .castle-workspace .history-page.cw-page .button,
  .castle-workspace .labels-page.cw-page .print-button:not(:disabled),
  .castle-workspace .transfers.cw-page .primary {
    background: var(--cw-primary); border-color: var(--cw-primary); color: #fff;
  }
  .castle-workspace .advertising-page .advertising-topbar { border-radius: var(--cw-radius); }
  .castle-workspace .advertising-topbar > .cw-tabs { margin: 0; }
  .castle-workspace .cw-page .cw-toolbar {
    background: #fff; border: 1px solid var(--cw-border); border-radius: 12px; padding: 16px;
  }
  .castle-workspace .castle.cw-page > .filters,
  .castle-workspace .order-helper.cw-page > .options {
    background: #fff; border: 1px solid var(--cw-border); border-radius: 12px; padding: 16px;
  }

  /* Section navigation shares one appearance without changing its destinations. */
  .castle-workspace .cw-page :is(.cw-tabs, .inventory-tabs, .daily-tabs,
    .commission-tabs, .conversion-subtabs, .chart-tabs, .history-tabs, .filter-tabs) {
    display: flex; flex-wrap: wrap; align-items: center; gap: 4px;
    padding: 6px; margin: 16px 0 24px; border: 0; border-radius: 12px;
    background: #eaf0f6; max-width: 100%; box-sizing: border-box;
  }
  .castle-workspace .cw-page :is(.cw-tabs, .inventory-tabs, .daily-tabs,
    .commission-tabs, .conversion-subtabs, .chart-tabs, .history-tabs, .filter-tabs) > a {
    display: inline-flex; align-items: center; gap: 6px; padding: 10px 14px;
    min-height: 40px; box-sizing: border-box; border: 0; border-radius: 8px;
    color: #52677e; background: transparent; text-decoration: none;
    font-size: .875rem; font-weight: 600; line-height: 1.35;
  }
  .castle-workspace .cw-page :is(.cw-tabs, .inventory-tabs, .daily-tabs,
    .commission-tabs, .conversion-subtabs, .chart-tabs, .history-tabs, .filter-tabs) > a[aria-current] {
    background: #fff; color: var(--cw-ink); box-shadow: 0 2px 6px #1834530c;
  }
  .castle-workspace .cw-page :is(.cw-tabs, .inventory-tabs, .daily-tabs,
    .commission-tabs, .conversion-subtabs, .chart-tabs, .history-tabs, .filter-tabs) > a:hover { color: #2455cc; background: #ffffffb3; }
  .castle-workspace .cw-page :is(.co-month,.bonus-card-header,.sales-period,.month-navigation) {
    gap: 12px; color: var(--cw-ink);
  }
  .castle-workspace .cw-page :is(.co-month,.bonus-card-header,.month-navigation) > a,
  .castle-workspace .cw-page .period-arrow {
    display: inline-flex; align-items: center; justify-content: center;
    box-sizing: border-box; flex: 0 0 40px; width: 40px; height: 40px; padding: 0;
    border: 1px solid var(--cw-border); border-radius: 10px; background: #fff; color: #52677e;
  }
  .castle-workspace .cw-page :is(.co-month,.bonus-card-period,.month-range,.sales-period-label) {
    font-size: 1.15rem; font-weight: 600;
  }

  /* Tables preserve their column widths, numeric alignment, and sticky behavior. */
  .castle-workspace .cw-page table,
  .castle-workspace .stocktake-floating-header table { border-collapse: collapse; font-size: .875rem; }
  .castle-workspace .cw-page thead th,
  .castle-workspace .stocktake-floating-header thead th {
    background: #f5f8fc; color: #60748b; font-size: .72rem; font-weight: 650;
    line-height: 1.4; letter-spacing: .035em; text-transform: uppercase;
    border-bottom-color: var(--cw-border);
  }
  .castle-workspace .cw-page :is(th,td) { border-color: var(--cw-border); }
  .castle-workspace .cw-bonus-page .finance-panel .table-wrap > table { min-width: 760px; }
  .castle-workspace .cw-page :is(.table-wrap,.po-table-wrap,.co-table-wrap,.all-loans-table,.user-table) {
    position: relative; max-width: 100%; border-radius: 12px;
  }
  .castle-workspace .cw-page:not(.stocktakes) :is(.table-wrap,.po-table-wrap,.all-loans-table) { overflow: auto; }
  .castle-workspace .cw-page :is(.empty,.empty-applications,.co-empty) {
    padding: 32px 20px; text-align: center; color: var(--cw-muted); line-height: 1.6;
  }
  .castle-workspace .cw-page :is(.notice,.advertising-notice,.bonus-pending,.errorlist) {
    border-radius: 10px; line-height: 1.5; overflow-wrap: anywhere;
  }
  .castle-workspace .cw-page :is(.notice,.advertising-notice) { padding: 14px 18px; }

  /* Dialogs use native focus/keyboard behavior from the existing workflows. */
  .castle-workspace dialog {
    box-sizing: border-box; max-width: calc(100vw - 32px); max-height: calc(100dvh - 48px);
    border: 1px solid var(--cw-border); border-radius: var(--cw-radius);
    color: var(--cw-ink); box-shadow: 0 24px 80px #142b4640; overflow: auto;
    font-family: inherit;
  }
  .castle-workspace dialog::backdrop { background: #172b465c; backdrop-filter: blur(3px); }
  .castle-workspace dialog h2 { font-size: 1.15rem; line-height: 1.4; letter-spacing: -.02em; }

  /* Older Daily forms now use the same cards as the other staff workflows. */
  .castle-workspace .cw-dailies :is(#addon-check,#loanpro-card-fields,
    #paid-outs,#other-income,#daily-notes,.cw-daily-section),
  .castle-workspace .cw-qbo-review {
    background: #fff; border: 1px solid var(--cw-border);
    border-radius: var(--cw-radius); padding: 24px; margin-top: 20px; box-shadow: var(--cw-shadow);
  }
  .castle-workspace .cw-dailies :is(#addon-check,#loanpro-card-fields,
    #paid-outs,#other-income,#daily-notes,.cw-daily-section) h2:first-child { margin-top: 0; }
  .castle-workspace .cw-dailies .button-finalize { background: var(--cw-primary); color: #fff; }
  .castle-workspace .cw-dailies .button-finalize:hover { background: #1b43a5; }
  .castle-workspace .cw-qbo-review table { width: 100%; }
  .castle-workspace .cw-qbo-review :is(th,td) { padding: 12px; border-bottom: 1px solid var(--cw-border); }

  @media (max-width: 800px) {
    .castle-workspace > main { padding: 20px 20px 40px; }
    .castle-workspace .cw-page h1 { font-size: 1.9rem; }
    .castle-workspace .cw-heading { margin-top: 4px; }
  }
  @media (max-width: 600px) {
    .castle-workspace > main { padding: 16px 16px 32px; }
    .castle-workspace .cw-page h1 { font-size: 1.75rem; }
    .castle-workspace .cw-heading { gap: 12px; margin-bottom: 20px; }
    .castle-workspace .cw-page :is(.panel-heading,.chart-toolbar) { padding: 16px; }
    .castle-workspace .cw-page :is(.co-month,.bonus-card-period,.month-range,.sales-period-label) { font-size: 1rem; }
    .castle-workspace .cw-page :is(.co-month,.bonus-card-header,.sales-period,.month-navigation) { gap: 6px; }
    .castle-workspace .cw-dailies :is(#addon-check,#loanpro-card-fields,
      #paid-outs,#other-income,#daily-notes,.cw-daily-section),
    .castle-workspace .cw-qbo-review { padding: 16px; }
    .castle-workspace .cw-page.cw-panel { padding: 16px; }
    .castle-workspace .cw-page .cash-fields { gap: 16px; }
    .castle-workspace .cw-page .daily-date-selector { flex-wrap: wrap; gap: 8px; }
  }
}
