/* CSOS Portal UI, built on static/brand/tokens.css (load that first).
   Only --brand-* tokens vary per tenant; everything here reads tokens. */

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: var(--font-sans);
  font-size: var(--text-sm);
  line-height: 1.5;
  color: var(--ink);
  background: var(--paper);
}

a { color: var(--brand-primary); text-decoration: none; font-weight: var(--weight-semibold); }
a:hover { text-decoration: underline; }

:focus { outline: none; }
:focus-visible { box-shadow: var(--focus-ring); border-radius: 4px; }

h1, h2, h3, h4 { margin: 0; color: var(--ink); }
h1 { font-size: var(--text-2xl); font-weight: var(--weight-heavy); }
h2 { font-size: var(--text-lg); font-weight: var(--weight-bold); }
h3 { font-size: var(--text-md); font-weight: var(--weight-bold); }

.mono, .num, .ref { font-family: var(--font-mono); font-weight: var(--weight-medium); }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.ref { color: var(--brand-primary); }
.muted { color: var(--ink-muted); }
.small { font-size: var(--text-xs); }
.right { text-align: right; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------------------------------------------------------------- shell */

.app-header {
  height: var(--header-height);
  background: var(--brand-primary);
  color: var(--brand-on-primary);
  display: flex;
  align-items: center;
  gap: var(--space-6);
  padding: 0 var(--space-6);
}
.app-header .brand { display: flex; align-items: center; gap: var(--space-3); color: var(--brand-on-primary); text-decoration: none; }
.app-header .brand img { width: 36px; height: 36px; }
.app-header .wordmark { font-size: 22px; line-height: 1; white-space: nowrap; }
.app-header .wordmark b { font-weight: var(--weight-heavy); }
.app-header .wordmark span { font-weight: var(--weight-medium); }

.tenant-pill {
  border: 1px solid rgba(255, 255, 255, 0.35);
  border-radius: var(--radius-pill);
  padding: 6px 14px;
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  white-space: nowrap;
}

.tenant-switcher { margin: 0; }
.tenant-switcher select {
  width: auto;
  appearance: none;
  -webkit-appearance: none;
  background: transparent url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M1 1l4 4 4-4' fill='none' stroke='white' stroke-width='1.5'/%3E%3C/svg%3E") no-repeat right 12px center;
  color: var(--brand-on-primary);
  border: 1px solid rgba(255, 255, 255, 0.35);
  border-radius: var(--radius-pill);
  padding: 6px 32px 6px 14px;
  font: inherit;
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  cursor: pointer;
}
.tenant-switcher select:hover { border-color: rgba(255, 255, 255, 0.7); }
.tenant-switcher select:focus-visible { border-color: var(--brand-accent); }
.tenant-switcher option { color: var(--ink); background: var(--surface); }

.header-search { flex: 1; max-width: 420px; margin-left: auto; }
.header-search input[type=search] {
  width: 100%;
  height: 38px;
  border: 1px solid transparent;
  border-radius: var(--radius-control);
  background: rgba(255, 255, 255, 0.12);
  color: var(--brand-on-primary);
  padding: 0 var(--space-4);
  font: inherit;
}
.header-search input[type=search]::placeholder { color: rgba(255, 255, 255, 0.75); }
.header-search input[type=search]:focus-visible { background: rgba(255, 255, 255, 0.2); border-color: transparent; }

.user-menu { display: flex; align-items: center; gap: var(--space-3); }
.avatar {
  width: 36px; height: 36px;
  border-radius: 50%;
  background: var(--brand-accent);
  color: var(--brand-on-accent);
  display: grid; place-items: center;
  font-weight: var(--weight-bold);
  font-size: var(--text-xs);
  letter-spacing: 0.02em;
}
.user-meta { line-height: 1.2; font-size: var(--text-xs); }
.user-meta strong { display: block; font-size: var(--text-sm); }
.user-meta span { opacity: 0.8; }
.link-button {
  background: none; border: none; padding: 4px 6px; font: inherit;
  color: inherit; cursor: pointer; opacity: 0.85; font-size: var(--text-xs); font-weight: var(--weight-semibold);
  white-space: nowrap;
}
.link-button:hover { opacity: 1; text-decoration: underline; }

.header-rule { height: var(--header-rule); background: var(--brand-accent); }

.layout { display: flex; min-height: calc(100vh - var(--header-height) - var(--header-rule)); }

.sidebar {
  width: var(--sidebar-width);
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: var(--space-6) var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}
.nav-label {
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--ink-muted);
  padding: var(--space-4) var(--space-3) var(--space-2);
}
.nav-label:first-child { padding-top: 0; }
.nav-link {
  display: flex; align-items: center; gap: var(--space-3);
  padding: 10px var(--space-3);
  border-radius: 8px;
  color: var(--ink);
  font-weight: var(--weight-medium);
  text-decoration: none;
}
.nav-link svg { width: 18px; height: 18px; flex-shrink: 0; color: var(--ink-muted); }
.nav-link:hover { background: var(--paper); text-decoration: none; }
.nav-link.active { background: var(--brand-primary-tint); color: var(--brand-primary); font-weight: var(--weight-bold); }
.nav-link.active svg { color: var(--brand-primary); }
.sidebar-foot { margin-top: auto; padding: var(--space-6) var(--space-3) 0; }
.sidebar-foot img { max-width: 120px; height: auto; display: block; }
.sidebar-foot .small { margin-top: var(--space-2); }

.content { flex: 1; min-width: 0; padding: var(--space-8) var(--space-10); }

.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: var(--space-4); margin-bottom: var(--space-6); flex-wrap: wrap; }
.page-head .eyebrow { font-size: var(--text-xs); font-weight: var(--weight-semibold); color: var(--ink-muted); text-transform: uppercase; letter-spacing: 0.06em; margin-bottom: var(--space-1); }
.page-head p { margin: var(--space-1) 0 0; color: var(--ink-muted); }
.actions { display: flex; gap: var(--space-2); flex-wrap: wrap; align-items: center; }

/* ---------------------------------------------------------------- surfaces */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  padding: var(--space-6);
  margin-bottom: var(--space-6);
}
.card > h2, .card > h3, .card-head { margin-bottom: var(--space-4); }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: var(--space-3); flex-wrap: wrap; }
.card-head h2, .card-head h3 { margin: 0; }
.card.flush { padding: 0; overflow: hidden; }
.card.flush > .card-head { padding: var(--space-5) var(--space-6) 0; }

.grid { display: grid; gap: var(--space-6); grid-template-columns: repeat(auto-fit, minmax(min(280px, 100%), 1fr)); }
.grid > * { min-width: 0; }
.card:not(.flush) { overflow-x: auto; }
.grid > .card { margin-bottom: 0; }
.stack { display: grid; gap: var(--space-6); margin-bottom: var(--space-6); }

/* Stat tiles: white; at most one .urgent (maroon) per row. */
.tiles { display: grid; gap: var(--space-4); grid-template-columns: repeat(auto-fit, minmax(min(180px, 100%), 1fr)); margin-bottom: var(--space-6); }
.tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-card);
  padding: var(--space-5);
  display: block;
  color: var(--ink);
  text-decoration: none;
}
.tile:hover { text-decoration: none; }
.tile .label { font-size: var(--text-xs); font-weight: var(--weight-semibold); color: var(--ink-muted); text-transform: uppercase; letter-spacing: 0.05em; }
.tile .value { font-family: var(--font-mono); font-weight: var(--weight-medium); font-size: var(--text-xl); margin-top: var(--space-2); }
.tile .hint { font-size: var(--text-xs); color: var(--ink-muted); margin-top: var(--space-1); }
.tile.urgent { background: var(--brand-primary); border-color: var(--brand-primary); color: var(--brand-on-primary); }
.tile.urgent .label, .tile.urgent .hint { color: var(--brand-on-primary); opacity: 0.85; }

/* Key-value facts on detail pages */
.facts { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: var(--space-5) var(--space-6); }
.fact dt { font-size: var(--text-xs); font-weight: var(--weight-semibold); color: var(--ink-muted); text-transform: uppercase; letter-spacing: 0.05em; }
.fact dd { margin: var(--space-1) 0 0; font-size: var(--text-sm); }
dl { margin: 0; }

/* ---------------------------------------------------------------- tables */

.table-wrap { overflow-x: auto; }
.table-wrap > table { min-width: 880px; }
table { width: 100%; border-collapse: collapse; background: var(--surface); }
th {
  background: var(--paper);
  text-align: left;
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--ink-muted);
  padding: 10px var(--space-4);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
td { padding: 12px var(--space-4); border-bottom: 1px solid var(--border-subtle); vertical-align: top; }
tr:last-child td { border-bottom: none; }
tbody tr:hover td { background: var(--brand-primary-wash); }
td.empty { color: var(--ink-muted); text-align: center; padding: var(--space-8); }
.card:not(.flush) table { border: 1px solid var(--border-subtle); border-radius: 10px; overflow: hidden; }

/* ---------------------------------------------------------------- badges */

.badge {
  display: inline-block;
  border-radius: var(--radius-pill);
  padding: 3px 10px;
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  line-height: 1.4;
  white-space: nowrap;
  background: var(--neutral-tint);
  color: var(--ink-muted);
}
.stage-draft { background: var(--stage-draft-bg); color: var(--stage-draft-fg); }
.stage-lodged { background: var(--stage-lodged-bg); color: var(--stage-lodged-fg); }
.stage-conciliation { background: var(--stage-conciliation-bg); color: var(--stage-conciliation-fg); }
.stage-adjudication { background: var(--stage-adjudication-bg); color: var(--stage-adjudication-fg); }
.stage-order { background: var(--stage-order-bg); color: var(--stage-order-fg); }
.stage-closed { background: var(--stage-closed-bg); color: var(--stage-closed-fg); }
.badge.ok { background: var(--brand-accent-tint); color: var(--success); }
.badge.bad { background: #FCE8E6; color: var(--danger); }
.badge.warn { background: var(--stage-conciliation-bg); color: var(--warning); }

/* ---------------------------------------------------------------- buttons */

.btn, button.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2);
  border-radius: var(--radius-control);
  padding: 12px 20px;
  font: inherit;
  font-weight: var(--weight-semibold);
  line-height: 1;
  cursor: pointer;
  text-decoration: none;
  border: 1.5px solid var(--brand-primary);
  background: var(--brand-primary);
  color: var(--brand-on-primary);
  white-space: nowrap;
}
.btn:hover { background: var(--brand-primary-hover); border-color: var(--brand-primary-hover); text-decoration: none; }
.btn.secondary { background: var(--surface); color: var(--brand-primary); }
.btn.secondary:hover { background: var(--brand-primary-wash); }
.btn.tertiary { background: none; border-color: transparent; color: var(--ink-muted); padding-left: var(--space-2); padding-right: var(--space-2); }
.btn.tertiary:hover { color: var(--ink); }
.btn.small { padding: 8px 14px; font-size: var(--text-xs); }
form.inline { display: inline; margin: 0; }

/* ---------------------------------------------------------------- forms */

label, .label { display: block; font-size: 13px; font-weight: var(--weight-semibold); color: var(--ink); margin-bottom: 6px; }
input[type=text], input[type=password], input[type=email], input[type=number], input[type=date],
input[type=search], input[type=file], select, textarea {
  width: 100%;
  font: inherit;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-control);
  padding: 10px 12px;
}
input[type=file] { padding: 8px; }
select[multiple] { min-height: 120px; }
input:focus-visible, select:focus-visible, textarea:focus-visible { border-color: var(--brand-primary); box-shadow: var(--focus-ring); }
input[type=checkbox] { width: 18px; height: 18px; accent-color: var(--brand-primary); margin: 0; }
.field { margin-bottom: var(--space-4); }
.field .help { font-size: var(--text-xs); color: var(--ink-muted); margin-top: 4px; }
.field .error { font-size: var(--text-xs); color: var(--danger); margin-top: 4px; font-weight: var(--weight-semibold); }
.field.check { display: flex; align-items: flex-start; gap: var(--space-3); }
.field.check label { margin: 0; font-weight: var(--weight-medium); }
.form-grid { display: grid; gap: 0 var(--space-6); grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }
.form-section { font-size: var(--text-xs); font-weight: var(--weight-semibold); text-transform: uppercase; letter-spacing: 0.06em; color: var(--ink-muted); margin: var(--space-6) 0 var(--space-3); padding-top: var(--space-5); border-top: 1px solid var(--border-subtle); }
.form-section:first-child { margin-top: 0; padding-top: 0; border-top: none; }

.filter-bar { display: flex; gap: var(--space-3); flex-wrap: wrap; align-items: flex-end; padding: var(--space-5) var(--space-6); border-bottom: 1px solid var(--border); }
.filter-bar .field { margin: 0; flex: 0 1 200px; min-width: 0; }
.filter-bar .field.grow { flex: 1 1 240px; }
.chips { display: flex; gap: var(--space-2); flex-wrap: wrap; padding: var(--space-4) var(--space-6); border-bottom: 1px solid var(--border); }
.chip {
  border: 1px solid var(--border);
  border-radius: var(--radius-pill);
  padding: 6px 12px;
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  color: var(--ink-muted);
  text-decoration: none;
  background: var(--surface);
}
.chip:hover { text-decoration: none; border-color: var(--brand-primary); color: var(--brand-primary); }
.chip.active { background: var(--brand-primary-tint); border-color: var(--brand-primary-tint); color: var(--brand-primary); }
.chip .count { font-family: var(--font-mono); margin-left: 4px; }

.doc-actions { display: grid; gap: var(--space-4); grid-template-columns: repeat(auto-fit, minmax(min(220px, 100%), 1fr)); margin-bottom: var(--space-5); }
.doc-actions .label { font-size: var(--text-xs); text-transform: uppercase; letter-spacing: 0.05em; color: var(--ink-muted); margin-bottom: var(--space-2); }
.btn[disabled] { opacity: 0.45; cursor: not-allowed; }

/* ---------------------------------------------------------------- checklist, messages, pager */

.checklist { list-style: none; margin: 0; padding: 0; }
.checklist li { display: flex; justify-content: space-between; gap: var(--space-4); padding: 10px 0; border-bottom: 1px solid var(--border-subtle); align-items: center; }
.checklist li:last-child { border-bottom: none; }
.checklist .detail { display: block; font-size: var(--text-xs); color: var(--ink-muted); }

.messages { margin-bottom: var(--space-6); display: grid; gap: var(--space-2); }
.message {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--info);
  border-radius: 10px;
  padding: var(--space-3) var(--space-4);
}
.message.success { border-left-color: var(--brand-accent); }
.message.warning { border-left-color: var(--warning); }
.message.error { border-left-color: var(--danger); }

.pager { display: flex; justify-content: space-between; align-items: center; padding: var(--space-4) var(--space-6); border-top: 1px solid var(--border); }

.bar { height: 8px; background: var(--neutral-tint); border-radius: var(--radius-pill); overflow: hidden; min-width: 80px; }
.bar > span { display: block; height: 100%; background: var(--brand-primary); }

/* ---------------------------------------------------------------- login */

.auth { min-height: 100vh; display: flex; flex-direction: column; }
.auth .header-rule { height: calc(var(--header-rule) + 2px); }
.auth-body { flex: 1; display: grid; place-items: center; padding: var(--space-8) var(--space-4); }
.auth-card { width: 100%; max-width: 400px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-card); padding: var(--space-8); }
.auth-card .lockup { display: flex; align-items: center; gap: var(--space-3); margin-bottom: var(--space-8); color: var(--brand-primary); }
.auth-card .lockup img { width: 52px; height: 52px; }
.auth-card .lockup .wordmark { font-size: 28px; line-height: 1; }
.auth-card .lockup .wordmark b { font-weight: var(--weight-heavy); }
.auth-card .lockup .wordmark span { font-weight: var(--weight-medium); }
.auth-card .lockup .by { font-size: var(--text-xs); font-weight: var(--weight-medium); color: var(--ink-muted); margin-top: 4px; }
.auth-card h1 { font-size: var(--text-xl); margin-bottom: var(--space-1); }
.auth-card .btn { width: 100%; margin-top: var(--space-2); }
.auth-foot { display: flex; align-items: center; justify-content: center; gap: var(--space-3); padding: var(--space-6); color: var(--ink-muted); font-size: var(--text-xs); }
.auth-foot img { height: 22px; width: auto; }

/* ---------------------------------------------------------------- responsive */

@media (max-width: 960px) {
  .app-header { gap: var(--space-3); padding: 0 var(--space-4); }
  .header-search, .user-meta, .tenant-pill { display: none; }
  .user-menu { margin-left: auto; }
  .layout { flex-direction: column; }
  .sidebar {
    width: auto; flex-direction: row; overflow-x: auto; padding: var(--space-2) var(--space-4);
    border-right: none; border-bottom: 1px solid var(--border); gap: var(--space-1);
  }
  .nav-label, .sidebar-foot { display: none; }
  .nav-link { white-space: nowrap; padding: 8px 12px; }
  .content { padding: var(--space-6) var(--space-4); }
  .card { padding: var(--space-4); }
  .card.flush > .card-head { padding: var(--space-4) var(--space-4) 0; }
  .filter-bar, .chips { padding-left: var(--space-4); padding-right: var(--space-4); }
}

@media (max-width: 420px) {
  .hide-narrow { display: none; }
  .app-header { gap: var(--space-2); padding: 0 var(--space-3); }
  .user-menu { gap: var(--space-1); }
  .avatar { width: 30px; height: 30px; }
  .app-header .brand { gap: var(--space-2); }
  .app-header .wordmark { font-size: 18px; }
  .tenant-switcher select { max-width: 96px; text-overflow: ellipsis; padding-right: 24px; background-position: right 8px center; }
  .app-header .brand img { width: 30px; height: 30px; }
  h1 { font-size: var(--text-xl); }
}

@media print {
  .app-header, .header-rule, .sidebar, .actions, form { display: none !important; }
  .content { padding: 0; }
  .card { border: none; padding: 0; }
}
