/* Tier 4 Legal — Client Portal */
/* Palette: cream #FAFAF8, blue #1E40AF, green #166534, text #1a1a1a */

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

:root {
  --bg: #FAFAF8;
  --bg-card: #fff;
  --brand: #1E40AF;
  --brand-light: #3B82F6;
  --brand-bg: #EFF6FF;
  --green: #166534;
  --green-light: #22C55E;
  --green-bg: #F0FDF4;
  --red: #DC2626;
  --red-bg: #FEF2F2;
  --orange: #EA580C;
  --orange-bg: #FFF7ED;
  --text: #1a1a1a;
  --text-secondary: #6b7280;
  --text-muted: #9ca3af;
  --border: #e5e7eb;
  --border-light: #f3f4f6;
  --radius: 8px;
  --radius-lg: 12px;
  --shadow: 0 1px 3px rgba(0,0,0,0.08);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.1);
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-mono: 'SF Mono', 'Cascadia Code', 'Consolas', monospace;
}

body { font-family: var(--font); background: var(--bg); color: var(--text); line-height: 1.6; min-height: 100vh; display: flex; flex-direction: column; }

/* ═══ SR Only ═══ */
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); border: 0; }

/* ═══ Nav ═══ */
.portal-nav { background: var(--brand); color: #fff; padding: 0 1.5rem; position: sticky; top: 0; z-index: 100; box-shadow: var(--shadow-md); }
.nav-inner { max-width: 1200px; margin: 0 auto; display: flex; align-items: center; height: 60px; gap: 2rem; }
.nav-brand { display: flex; align-items: center; gap: 0.5rem; color: #fff; text-decoration: none; font-weight: 700; font-size: 1.1rem; }
.brand-icon { font-size: 1.4rem; }
.nav-links { display: flex; gap: 0.25rem; flex: 1; }
.nav-link { color: rgba(255,255,255,0.8); text-decoration: none; padding: 0.5rem 0.75rem; border-radius: 6px; font-size: 0.9rem; transition: all 0.15s; }
.nav-link:hover, .nav-link:focus { color: #fff; background: rgba(255,255,255,0.15); }
.nav-link.active { color: #fff; background: rgba(255,255,255,0.2); font-weight: 600; }
.nav-right { display: flex; align-items: center; gap: 1rem; }
.nav-user { color: rgba(255,255,255,0.7); font-size: 0.85rem; }
.nav-logout { font-size: 0.85rem !important; opacity: 0.7; }
.badge { background: var(--red); color: #fff; font-size: 0.7rem; padding: 1px 6px; border-radius: 10px; margin-left: 4px; font-weight: 700; }

/* ═══ Main ═══ */
.portal-main { max-width: 1200px; margin: 0 auto; padding: 2rem 1.5rem; flex: 1; width: 100%; }
.portal-footer { text-align: center; padding: 1.5rem; color: var(--text-muted); font-size: 0.8rem; border-top: 1px solid var(--border); margin-top: 2rem; }

/* ═══ Page Header ═══ */
.page-header { margin-bottom: 1.5rem; }
.page-header h1 { font-size: 1.75rem; font-weight: 700; color: var(--text); }
.page-header .subtitle { color: var(--text-secondary); margin-top: 0.25rem; }
.back-link { color: var(--brand); text-decoration: none; font-size: 0.85rem; display: inline-block; margin-bottom: 0.5rem; }
.back-link:hover { text-decoration: underline; }

/* ═══ Stats ═══ */
.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1rem; margin-bottom: 2rem; }
.stat-card { background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: 1.25rem; box-shadow: var(--shadow); }
.stat-card.stat-sm { padding: 1rem; }
.stat-label { display: block; font-size: 0.8rem; color: var(--text-secondary); text-transform: uppercase; letter-spacing: 0.05em; margin-bottom: 0.25rem; }
.stat-value { display: block; font-size: 1.75rem; font-weight: 700; font-family: var(--font-mono); }
.stat-money { color: var(--brand); }
.stat-trust { color: var(--green); }
.invoice-summary { display: flex; gap: 1rem; margin-bottom: 1.5rem; }
.invoice-summary .stat-card { flex: 1; }

/* ═══ Cards ═══ */
.card { background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: 1.5rem; box-shadow: var(--shadow); margin-bottom: 1.5rem; }
.card h2 { font-size: 1.1rem; font-weight: 600; margin-bottom: 1rem; color: var(--text); }
.card-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 1rem; }
.card-header h2 { margin-bottom: 0; }
.link-all { color: var(--brand); text-decoration: none; font-size: 0.85rem; }
.link-all:hover { text-decoration: underline; }
.empty { color: var(--text-muted); text-align: center; padding: 2rem; }
.empty-card { text-align: center; }
.dashboard-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1.5rem; }

/* ═══ Tables ═══ */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
thead th { text-align: left; font-weight: 600; color: var(--text-secondary); font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.03em; padding: 0.75rem 0.5rem; border-bottom: 2px solid var(--border); }
tbody td { padding: 0.75rem 0.5rem; border-bottom: 1px solid var(--border-light); vertical-align: middle; }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--border-light); }
.td-primary { font-weight: 600; }
.td-link { color: var(--brand); text-decoration: none; font-weight: 500; }
.td-link:hover { text-decoration: underline; }
.td-money { font-family: var(--font-mono); text-align: right; white-space: nowrap; }
.td-num { font-family: var(--font-mono); text-align: center; }
.td-bold { font-weight: 700; }
.td-date { white-space: nowrap; color: var(--text-secondary); }
.td-desc { max-width: 300px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.td-ref { font-family: var(--font-mono); font-size: 0.8rem; color: var(--text-muted); }
.row-overdue { background: var(--red-bg); }

/* ═══ Status Badges ═══ */
.status { display: inline-block; padding: 2px 8px; border-radius: 4px; font-size: 0.75rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.03em; }
.status-lg { font-size: 0.85rem; padding: 4px 12px; }
.status-active, .status-paid { background: var(--green-bg); color: var(--green); }
.status-pending, .status-draft { background: var(--border-light); color: var(--text-secondary); }
.status-sent, .status-partially_paid { background: var(--brand-bg); color: var(--brand); }
.status-overdue, .status-written_off { background: var(--red-bg); color: var(--red); }
.status-closed, .status-archived { background: var(--border-light); color: var(--text-muted); }
.badge-type { display: inline-block; padding: 2px 6px; border-radius: 4px; font-size: 0.75rem; background: var(--border-light); color: var(--text-secondary); text-transform: capitalize; }

/* ═══ Alerts ═══ */
.alert { padding: 1rem 1.25rem; border-radius: var(--radius); margin-bottom: 1.5rem; font-size: 0.9rem; }
.alert a { color: inherit; font-weight: 600; }
.alert-error { background: var(--red-bg); color: var(--red); border: 1px solid #FECACA; }
.alert-success { background: var(--green-bg); color: var(--green); border: 1px solid #BBF7D0; }
.alert-info { background: var(--brand-bg); color: var(--brand); border: 1px solid #BFDBFE; }

/* ═══ Invoice Detail ═══ */
.invoice-meta .meta-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 1rem; }
.meta-label { display: block; font-size: 0.75rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.05em; }
.meta-value { font-weight: 600; margin-top: 0.25rem; display: block; }
.invoice-narrative { color: var(--text-secondary); line-height: 1.7; white-space: pre-wrap; }
.totals-grid { max-width: 400px; margin-left: auto; }
.total-row { display: flex; justify-content: space-between; padding: 0.5rem 0; border-bottom: 1px solid var(--border-light); }
.total-sub { border-top: 2px solid var(--border); padding-top: 0.75rem; }
.total-trust .td-money { color: var(--green); }
.total-paid .td-money { color: var(--green); }
.total-balance { border-top: 2px solid var(--text); padding-top: 0.75rem; font-size: 1.1rem; }
.total-balance span { font-weight: 700; }
.pay-section { text-align: center; margin: 2rem 0; }
.pay-note { color: var(--text-muted); font-size: 0.85rem; margin-top: 0.75rem; }

/* ═══ Buttons ═══ */
.btn { display: inline-flex; align-items: center; justify-content: center; padding: 0.6rem 1.25rem; border-radius: 6px; font-size: 0.9rem; font-weight: 600; text-decoration: none; border: none; cursor: pointer; transition: all 0.15s; }
.btn-sm { padding: 0.35rem 0.75rem; font-size: 0.8rem; }
.btn-lg { padding: 0.85rem 2rem; font-size: 1.1rem; }
.btn-primary { background: var(--brand); color: #fff; }
.btn-primary:hover { background: var(--brand-light); }
.btn-primary:focus { outline: 2px solid var(--brand); outline-offset: 2px; }

/* ═══ Messages ═══ */
.messages-container { padding: 0; overflow: hidden; }
.messages-thread { padding: 1.5rem; max-height: 500px; overflow-y: auto; display: flex; flex-direction: column; gap: 1rem; }
.message { max-width: 80%; padding: 0.75rem 1rem; border-radius: var(--radius-lg); }
.message-client { align-self: flex-end; background: var(--brand-bg); border: 1px solid #BFDBFE; }
.message-firm { align-self: flex-start; background: var(--border-light); border: 1px solid var(--border); }
.message-meta { display: flex; justify-content: space-between; gap: 1rem; margin-bottom: 0.25rem; }
.message-sender { font-weight: 600; font-size: 0.8rem; }
.message-time { font-size: 0.75rem; color: var(--text-muted); }
.message-body { font-size: 0.9rem; line-height: 1.5; white-space: pre-wrap; }
.message-form { padding: 1rem 1.5rem; border-top: 1px solid var(--border); background: var(--bg); }
.message-form textarea { width: 100%; padding: 0.75rem; border: 1px solid var(--border); border-radius: var(--radius); font-family: var(--font); font-size: 0.9rem; resize: vertical; min-height: 80px; }
.message-form textarea:focus { outline: 2px solid var(--brand); border-color: var(--brand); }
.message-form-actions { display: flex; justify-content: flex-end; margin-top: 0.5rem; }

/* ═══ Login ═══ */
.login-container { display: flex; justify-content: center; align-items: center; min-height: 70vh; }
.login-card { max-width: 480px; width: 100%; background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: 2.5rem; box-shadow: var(--shadow-md); text-align: center; }
.login-brand { margin-bottom: 2rem; }
.brand-icon-lg { font-size: 3rem; display: block; margin-bottom: 0.5rem; }
.login-brand h1 { font-size: 1.5rem; font-weight: 700; margin-bottom: 0.25rem; }
.login-brand p { color: var(--text-secondary); }
.login-info { text-align: left; margin-top: 1.5rem; }
.login-info h2 { font-size: 1rem; margin-bottom: 0.75rem; }
.login-info p { color: var(--text-secondary); font-size: 0.9rem; margin-bottom: 0.5rem; }

/* ═══ Responsive ═══ */
@media (max-width: 768px) {
  .nav-inner { gap: 0.5rem; }
  .brand-text { display: none; }
  .nav-links { gap: 0; }
  .nav-link { padding: 0.4rem 0.5rem; font-size: 0.8rem; }
  .nav-user { display: none; }
  .portal-main { padding: 1rem; }
  .page-header h1 { font-size: 1.35rem; }
  .stats-grid { grid-template-columns: repeat(2, 1fr); gap: 0.5rem; }
  .stat-value { font-size: 1.25rem; }
  .dashboard-grid { grid-template-columns: 1fr; }
  .invoice-summary { flex-direction: column; }
  .message { max-width: 90%; }
  .invoice-meta .meta-grid { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 480px) {
  .stats-grid { grid-template-columns: 1fr; }
  .nav-inner { flex-wrap: wrap; height: auto; padding: 0.5rem 0; }
  .nav-links { order: 3; width: 100%; justify-content: center; }
}

/* ═══ Focus Visible ═══ */
:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }
a:focus:not(:focus-visible), button:focus:not(:focus-visible) { outline: none; }
