/* Common component appearance for the existing authenticated workspace.
 * Scope is the canvas and its sibling dialog host, gated by #app.shell-ui.
 * Navigation, header, login, driver pages and application behavior are outside
 * this layer. Private tokens do not override the original semantic variables.
 * Existing grids, table columns, overflow, compact variants, .body.tight,
 * responsive table corrections and modal focus guards remain in place.
 */
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) {
  --ui-surface: #faf9f7;
  --ui-soft: #eae8e6;
  --ui-pill: #e3e0e4;
  --ui-ink: #202020;
  --ui-secondary: #55535a;
  --ui-muted: #68656d;
  --ui-line: #d7d4d8;
  /* Controls need a stronger boundary than decorative panel dividers. */
  --ui-control-line: #857d8e;
  --ui-accent: #544094;
  --ui-accent-soft: #e6dff5;
  --ui-primary: linear-gradient(110deg, #8290ff, #dc8ce9 65%, #f3a7c0);
  --ui-on-primary: #171023;
  --ui-font: "Helvetica Neue", Helvetica, Arial, sans-serif;
  --ui-card-radius: 18px;
  --ui-field-radius: 12px;
  --ui-dialog-radius: 20px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) :is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) {
    --ui-surface: #161719;
    --ui-soft: #1d1e21;
    --ui-pill: #27272c;
    --ui-ink: #f3f1f0;
    --ui-secondary: #c1bdc7;
    --ui-muted: #aaa8af;
    --ui-line: #323236;
    --ui-control-line: #89828f;
    --ui-accent: #d4c5fb;
    --ui-accent-soft: #302b43;
  }
}

:root[data-theme="dark"] :is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) {
  --ui-surface: #161719;
  --ui-soft: #1d1e21;
  --ui-pill: #27272c;
  --ui-ink: #f3f1f0;
  --ui-secondary: #c1bdc7;
  --ui-muted: #aaa8af;
  --ui-line: #323236;
  --ui-control-line: #89828f;
  --ui-accent: #d4c5fb;
  --ui-accent-soft: #302b43;
}

/* Cards retain their existing dimensions, body padding and nested layouts.
 * Do not clip their contents: table scrolling and focus rings need room. */
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) :is(.panel, .tiles) {
  background: var(--ui-surface);
  border-color: var(--ui-line);
  border-radius: var(--ui-card-radius);
  box-shadow: none;
  color: var(--ui-ink);
  font-family: var(--ui-font);
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) .panel > header {
  border-color: var(--ui-line);
  border-radius: var(--ui-card-radius) var(--ui-card-radius) 0 0;
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) .panel > header h2 {
  font-family: var(--ui-font);
  font-weight: 500;
  letter-spacing: -.02em;
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) .tile {
  background: var(--ui-surface);
  color: var(--ui-ink);
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) :is(.muted, .panel > header .hint, .tile .k, .tile .d, .kv dt) {
  color: var(--ui-muted);
}

/* Preserve native table semantics, numeric typography, hidden-column rules,
 * nowrap decisions and the existing 861–1199px administrative corrections. */
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) .tw > table {
  background: var(--ui-surface);
  color: var(--ui-ink);
  font-family: var(--ui-font);
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) .tw > table th {
  background: var(--ui-soft);
  color: var(--ui-muted);
  border-color: var(--ui-line);
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) .tw > table td {
  border-color: var(--ui-line);
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) .tw > table tbody tr:hover {
  background: var(--ui-soft);
}
/* The existing scroll wrapper clips the final table to the card's inner edge. */
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) .panel > .body.tight:last-child > .tw:last-child {
  border-radius: 0 0 calc(var(--ui-card-radius) - 1px) calc(var(--ui-card-radius) - 1px);
}

/* Keep .btn.sm's smaller text and padding. Minimum heights are shared, without
 * forcing widths or reflowing the application's action groups. Variant rules
 * below keep destructive, ghost and disabled controls distinct. */
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) .btn {
  border-radius: 999px;
  min-height: 38px;
  font-family: var(--ui-font);
  font-weight: 500;
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) .btn.sm {
  min-height: 34px;
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) .btn:not(.primary):not(.ghost):not(.danger) {
  background: var(--ui-surface);
  color: var(--ui-ink);
  border-color: var(--ui-control-line);
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) .btn:not(.primary):not(.ghost):not(.danger):hover:not(:disabled):not([aria-disabled="true"]) {
  background: var(--ui-soft);
  border-color: var(--ui-accent);
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) .btn.primary:not(.ghost):not(.danger) {
  background: var(--ui-primary);
  border-color: transparent;
  color: var(--ui-on-primary);
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) .btn.primary:not(.ghost):not(.danger):hover:not(:disabled):not([aria-disabled="true"]) {
  filter: brightness(1.07);
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) .btn.ghost:not(.danger) {
  background: transparent;
  color: var(--ui-secondary);
  border-color: transparent;
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) .btn.ghost:not(.danger):hover:not(:disabled):not([aria-disabled="true"]) {
  background: var(--ui-pill);
  color: var(--ui-ink);
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) .btn.danger {
  background: var(--ui-surface);
  color: var(--crit);
  border-color: var(--crit);
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) .btn.danger:hover:not(:disabled):not([aria-disabled="true"]) {
  background: var(--crit-soft);
  color: var(--crit);
  border-color: var(--crit);
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) .btn:is(:disabled, [aria-disabled="true"]) {
  opacity: .45;
  cursor: not-allowed;
  filter: none;
}

/* Internal tab buttons are a separate primitive from .btn. Keep their native
 * spacing, scrolling, order and selection behavior; focus uses the rule below. */
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) .tabs > button {
  color: var(--ui-muted);
  font-family: var(--ui-font);
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) .tabs > button[aria-selected="true"] {
  color: var(--ui-accent);
  border-bottom-color: var(--ui-accent);
}

/* Only standard field primitives receive text-control styling. Native toggles,
 * sliders, pickers, file controls and hidden inputs retain their own shapes.
 * Values, labels, types, validation and keyboard behavior remain native. */
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) .field {
  font-family: var(--ui-font);
  color: var(--ui-ink);
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) .field label {
  color: var(--ui-secondary);
  font-size: 12px;
  font-weight: 500;
  letter-spacing: 0;
  text-transform: none;
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) .field :is(input:not([type="checkbox"], [type="radio"], [type="range"], [type="color"], [type="file"], [type="hidden"], [type="image"], [type="button"], [type="submit"], [type="reset"]), select, textarea) {
  background: var(--ui-soft);
  color: var(--ui-ink);
  border-color: var(--ui-control-line);
  border-radius: var(--ui-field-radius);
  min-height: 44px;
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) .field :is(input, textarea)::placeholder {
  color: var(--ui-muted);
  opacity: 1;
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) .field :is(input, select, textarea):focus {
  border-color: var(--ui-accent);
  box-shadow: 0 0 0 3px var(--ui-accent-soft);
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) .field :is(input, textarea)[readonly] {
  background: var(--ui-surface);
  border-style: dashed;
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) .field :is(input, select, textarea):disabled {
  color: var(--ui-muted);
  cursor: not-allowed;
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) .field :is(input, select, textarea)[aria-invalid="true"] {
  border-color: var(--crit);
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) .field :is(input, select, textarea)[aria-invalid="true"]:focus {
  box-shadow: 0 0 0 3px var(--crit-soft);
}

/* Standalone search, select and table editors share the field palette without
 * inheriting .field geometry. Keep their existing widths, padding, fonts and
 * native control behavior, including compact and monospaced variants. */
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) :is(input:not([type="checkbox"], [type="radio"], [type="range"], [type="color"], [type="file"], [type="hidden"], [type="image"], [type="button"], [type="submit"], [type="reset"]), select, textarea):not(:where(.field *)) {
  background: var(--ui-soft);
  color: var(--ui-ink);
  border-color: var(--ui-control-line);
  border-radius: var(--ui-field-radius);
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) :is(input, textarea):not(:where(.field *))::placeholder {
  color: var(--ui-muted);
  opacity: 1;
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) :is(input:not([type="checkbox"], [type="radio"], [type="range"], [type="color"], [type="file"], [type="hidden"], [type="image"], [type="button"], [type="submit"], [type="reset"]), select, textarea):not(:where(.field *)):focus {
  border-color: var(--ui-accent);
  box-shadow: 0 0 0 3px var(--ui-accent-soft);
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) :is(input:not([type="checkbox"], [type="radio"], [type="range"], [type="color"], [type="file"], [type="hidden"], [type="image"], [type="button"], [type="submit"], [type="reset"]), textarea):not(:where(.field *))[readonly] {
  background: var(--ui-surface);
  border-style: dashed;
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) :is(input:not([type="checkbox"], [type="radio"], [type="range"], [type="color"], [type="file"], [type="hidden"], [type="image"], [type="button"], [type="submit"], [type="reset"]), select, textarea):not(:where(.field *)):disabled {
  color: var(--ui-muted);
  cursor: not-allowed;
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) :is(input:not([type="checkbox"], [type="radio"], [type="range"], [type="color"], [type="file"], [type="hidden"], [type="image"], [type="button"], [type="submit"], [type="reset"]), select, textarea):not(:where(.field *))[aria-invalid="true"] {
  border-color: var(--crit);
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) :is(input:not([type="checkbox"], [type="radio"], [type="range"], [type="color"], [type="file"], [type="hidden"], [type="image"], [type="button"], [type="submit"], [type="reset"]), select, textarea):not(:where(.field *))[aria-invalid="true"]:focus {
  box-shadow: 0 0 0 3px var(--crit-soft);
}

/* Legacy textareas declare these four appearance properties inline. Override
 * only that appearance; preserve rows, resizing, spacing and monospaced text.
 * Matching state rules keep focus, readonly, disabled and invalid visible. */
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) textarea[style] {
  background: var(--ui-soft) !important;
  color: var(--ui-ink) !important;
  border-color: var(--ui-control-line) !important;
  border-radius: var(--ui-field-radius) !important;
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) textarea[style]:focus {
  border-color: var(--ui-accent) !important;
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) textarea[style][readonly] {
  background: var(--ui-surface) !important;
  border-style: dashed !important;
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) textarea[style]:disabled {
  color: var(--ui-muted) !important;
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) textarea[style][aria-invalid="true"] {
  border-color: var(--crit) !important;
}

/* Semantic success/warning/error colors are inherited unchanged. Neutral and
 * informational variants use the new palette; no state is communicated by
 * color alone and none of the existing status text is replaced. */
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) .chip:not(.ok):not(.warn):not(.crit):not(.acc) {
  background: var(--ui-soft);
  color: var(--ui-secondary);
  border-color: var(--ui-line);
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) .chip.acc {
  background: var(--ui-accent-soft);
  color: var(--ui-accent);
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) .note {
  border-radius: var(--ui-field-radius);
  font-family: var(--ui-font);
  line-height: 1.6;
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) .note.info {
  background: var(--ui-accent-soft);
  color: var(--ui-accent);
}
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) .meter > i:not(.ok):not(.warn):not(.crit) { background: var(--ui-accent); }

/* The overlay is a sibling of #app; it has its own private tokens above.
 * Do not change .scrim, positioning, widths, scrolling, focus guards or
 * [hidden]/[inert] behavior. Login has no shell-ui marker and is excluded. */
#app.shell-ui ~ #overlay .modal {
  background: var(--ui-surface);
  color: var(--ui-ink);
  border-color: var(--ui-line);
  border-radius: var(--ui-dialog-radius);
  box-shadow: none;
  font-family: var(--ui-font);
}
#app.shell-ui ~ #overlay .modal > :is(header, footer) {
  border-color: var(--ui-line);
}
#app.shell-ui ~ #overlay .modal > header h3 {
  font-family: var(--ui-font);
  font-weight: 500;
  letter-spacing: -.02em;
}

/* Include table-row-action explicitly through the scoped focus rule, fixing
 * its original undefined --acc token without changing the base stylesheet.
 * Focus sentinels must remain invisible; their behavior belongs to ui.js. */
:is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) :focus-visible:not([data-modal-guard]) {
  outline: 2px solid var(--ui-accent);
  outline-offset: 3px;
}

@media (hover: none), (max-width: 860px) {
  :is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) :is(.btn, .btn.sm, .table-row-action) {
    min-height: 44px;
  }
  /* Preserve native mobile form legibility without altering checkbox sizes. */
  :is(#app.shell-ui > .shell > .main > .canvas, #app.shell-ui ~ #overlay) .field :is(input:not([type="checkbox"], [type="radio"], [type="range"], [type="color"], [type="file"], [type="hidden"], [type="image"], [type="button"], [type="submit"], [type="reset"]), select, textarea) {
    font-size: 16px;
  }
}
