/* Thimsa Demo Portal — single stylesheet.
   Palette: cream #FCFAF8 background, near-black #0A0A0F text, violet #7C5CC4
   accent (per build spec §9). Tranche colors come from the engine config.
   RTL is supported via [dir=rtl] selectors at the bottom of the file —
   the markup is the same in both languages. */

:root {
  --bg: #FCFAF8;
  --ink: #0A0A0F;
  --ink-soft: #4a4a55;
  --muted: #6b6b75;
  --line: #e7e3dc;
  --line-soft: #f0ece5;
  --accent: #7C5CC4;
  --accent-ink: #ffffff;
  --warn-bg: #FFF7E6;
  --warn-line: #F0D58A;
  --warn-ink: #6B5413;
  --hero-from: #0E3B2E;
  --hero-to: #155C45;
  --tranche-platinum: #1B7F5C;
  --tranche-gold: #C8860D;
  --tranche-growth: #C0392B;
  --radius: 12px;
  --radius-lg: 16px;
}

/* ============ Al-Awwal (Saudi Ministry of Culture) — Arabic typeface ============ */
@font-face {
  font-family: 'Al-Awwal';
  src: url('/static/fonts/al-awwal-regular.ttf') format('truetype');
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: 'Al-Awwal';
  src: url('/static/fonts/al-awwal-bold.ttf') format('truetype');
  font-weight: 700;
  font-style: normal;
  font-display: swap;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Segoe UI Arabic", "Tahoma", sans-serif;
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { letter-spacing: -0.01em; }
h1 { font-size: 1.7rem; margin: 0 0 .4rem; font-weight: 700; }
h2 { font-size: 1.15rem; margin: 1.8rem 0 .8rem; font-weight: 700; }
h3 { font-size: .95rem; margin: 0 0 .6rem; font-weight: 600; }

p { margin: .35rem 0; }
.muted { color: var(--muted); }
.small { font-size: .8rem; }
.mono { font-family: ui-monospace, Menlo, Consolas, monospace; }
.hidden { display: none !important; }

/* Western numerals everywhere, even when html[lang=ar]. */
[lang="ar"] { font-variant-numeric: lining-nums; }

/* Al-Awwal for all Arabic text; keep Latin/monospace stacks as fallbacks.
   Language-based targeting (the document is <html lang="ar"> in Arabic mode),
   so the font follows the language rather than RTL layout direction. */
[lang="ar"] {
  font-family: 'Al-Awwal', -apple-system, BlinkMacSystemFont, "Segoe UI Arabic", "Tahoma", sans-serif;
}
.num { font-variant-numeric: tabular-nums; font-feature-settings: "lnum" 1; }

/* ============ Lang toggle ============ */

.lang-toggle {
  font-size: .82rem;
  font-weight: 600;
  color: var(--accent);
  text-decoration: none;
  padding: 4px 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
}
.lang-toggle:hover { border-color: var(--accent); }
.floating-lang {
  position: absolute;
  top: 16px; right: 18px;
  z-index: 20;
}
[dir="rtl"] .floating-lang { right: auto; left: 18px; }

/* ============ Sign-in ============ */

.signin-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.signin-card {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 36px 32px;
  max-width: 440px;
  width: 100%;
  text-align: center;
  box-shadow: 0 1px 2px rgba(10, 10, 15, 0.04), 0 12px 32px rgba(10, 10, 15, 0.06);
}
.wordmark {
  font-weight: 800;
  letter-spacing: 4px;
  font-size: .85rem;
  color: var(--accent);
}
[dir="rtl"] .wordmark { letter-spacing: 2px; }
.wordmark-sm { letter-spacing: 3px; }
.signin-card h1 { margin: 14px 0 12px; }
.signin-card p { font-size: .92rem; }
.signin-fineprint { margin-top: 18px; font-size: .78rem; color: var(--muted); }

.btn-google {
  display: inline-flex; align-items: center; gap: 10px;
  margin-top: 18px;
  padding: 11px 22px;
  background: var(--ink);
  color: #fff;
  border-radius: 999px;
  text-decoration: none;
  font-weight: 600;
  font-size: .95rem;
  transition: background .15s ease;
}
.btn-google:hover { background: #1a1a23; }
.g-glyph {
  display: inline-grid; place-items: center;
  width: 22px; height: 22px;
  background: #fff; color: var(--ink);
  border-radius: 999px; font-weight: 800; font-size: .8rem;
}

/* ============ Topbar ============ */

.topbar {
  display: flex; justify-content: space-between; align-items: center;
  padding: 14px 32px;
  border-bottom: 1px solid var(--line);
  background: rgba(252, 250, 248, 0.92);
  backdrop-filter: saturate(180%) blur(8px);
  position: sticky; top: 0; z-index: 10;
}
.topbar-left { display: flex; align-items: baseline; gap: 14px; }
.topbar-eyebrow {
  font-size: .72rem; letter-spacing: 2px; text-transform: uppercase;
  color: var(--muted);
}
[dir="rtl"] .topbar-eyebrow { letter-spacing: 0; }
.topbar-right { display: flex; align-items: center; gap: 14px; }
.link-btn {
  background: none; border: none; color: var(--accent);
  font: inherit; cursor: pointer; text-decoration: underline;
  padding: 0;
}

/* ============ App shell ============ */

.app-shell {
  max-width: 1100px;
  margin: 0 auto;
  padding: 28px 32px 64px;
}

.hero {
  border-radius: var(--radius-lg);
  padding: 22px 26px;
  margin: 0 0 22px;
  color: #fff;
  background: linear-gradient(120deg, var(--hero-from) 0%, var(--hero-to) 100%);
}
.hero h1 { color: #fff; margin: .4rem 0 .3rem; }
.hero p { margin: 0; opacity: .88; font-size: .95rem; }
.eyebrow {
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: .7rem; letter-spacing: 2px;
  text-transform: uppercase; opacity: .8;
}
[dir="rtl"] .eyebrow {
  font-family: inherit;
  letter-spacing: 0;
  text-transform: none;
}

/* ============ Form ============ */

form { margin-top: 4px; }
.form-row { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.form-row.inline { flex-direction: row; align-items: center; gap: 12px; }
.form-row.inline label { margin: 0; }
label { font-weight: 600; font-size: .85rem; color: var(--ink-soft); }
input[type="text"], input[type="number"], input[type="file"] {
  font: inherit;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  width: 100%;
  max-width: 520px;
}
input[type="text"]:focus, input[type="number"]:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
  border-color: var(--accent);
}
.form-actions { display: flex; align-items: center; gap: 14px; margin-top: 8px; }
.btn-primary {
  background: var(--ink);
  color: #fff;
  border: 0;
  padding: 11px 22px;
  border-radius: 999px;
  font: inherit; font-weight: 600;
  cursor: pointer;
}
.btn-primary:hover { background: #1a1a23; }
.btn-primary:disabled { opacity: .55; cursor: progress; }
.status-msg { color: var(--muted); font-size: .9rem; }
.status-msg.error { color: var(--tranche-growth); }

/* ============ Result sections ============ */

.result { margin-top: 24px; }
.result-section { margin: 28px 0; }
.result-section h2 { margin-top: 0; }

.completeness-banner {
  background: var(--warn-bg);
  border: 1px solid var(--warn-line);
  color: var(--warn-ink);
  border-radius: 10px;
  padding: 12px 14px;
  font-size: .88rem;
  margin-bottom: 16px;
}

/* Section A — score */
.score-card {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 22px 26px;
}
.score-headline {
  display: flex; align-items: baseline; gap: 14px;
  flex-wrap: wrap;
}
.score-number {
  font-size: 2.6rem; font-weight: 800;
  font-variant-numeric: tabular-nums;
  line-height: 1;
}
.score-tier {
  font-size: 1.15rem; font-weight: 700;
  padding: 4px 12px;
  border-radius: 999px;
  background: var(--line-soft);
  color: var(--ink);
}
.score-tier[data-tier="Platinum"] { background: rgba(27,127,92,.12); color: var(--tranche-platinum); }
.score-tier[data-tier="Gold"]     { background: rgba(200,134,13,.14); color: var(--tranche-gold); }
.score-tier[data-tier="Growth"]   { background: rgba(192,57,43,.12); color: var(--tranche-growth); }
.score-explain {
  margin-top: 14px;
  font-size: 1.02rem;
  color: var(--ink-soft);
  line-height: 1.55;
}

/* Section B — fund lead */
.fund-card {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 22px 26px;
  border-left: 4px solid var(--accent);
}
[dir="rtl"] .fund-card {
  border-left: 1px solid var(--line);
  border-right: 4px solid var(--accent);
}
.fund-big {
  display: flex; flex-direction: column; gap: 4px;
  margin-bottom: 10px;
}
.fund-big-num {
  font-size: 2.4rem; font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
  line-height: 1;
}
.fund-big-label {
  font-size: .78rem; text-transform: uppercase;
  letter-spacing: 1.5px; color: var(--muted);
}
[dir="rtl"] .fund-big-label { text-transform: none; letter-spacing: 0; }
.fund-intro {
  font-size: .98rem;
  color: var(--ink-soft);
  line-height: 1.55;
}

/* Section C — performance metrics */
.metric-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
}
@media (max-width: 900px) { .metric-grid { grid-template-columns: repeat(2, 1fr); } }
.metric-card {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px 14px;
}
.metric-label {
  font-size: .82rem; font-weight: 600;
  color: var(--ink-soft);
}
.metric-value {
  font-size: 1.4rem; font-weight: 700; margin-top: 4px;
  font-variant-numeric: tabular-nums;
}
.metric-interp {
  font-size: .78rem;
  margin-top: 6px;
  padding: 2px 0;
  color: var(--muted);
}
.metric-interp.is-strong { color: var(--tranche-platinum); font-weight: 600; }
.metric-interp.is-watch  { color: var(--tranche-gold);     font-weight: 600; }
.metric-interp.is-risk   { color: var(--tranche-growth);   font-weight: 600; }
.metric-missing { color: var(--tranche-growth); font-weight: 600; font-size: .82rem; }

/* Section D — funding profile */
.funding-card {
  border-radius: var(--radius-lg);
  padding: 22px 24px;
  color: #fff;
  margin-top: 12px;
}
.funding-card .eyebrow { color: #fff; }
.terms { display: flex; gap: 28px; flex-wrap: wrap; margin-top: 10px; }
.terms div span {
  display: block;
  font-size: .72rem; text-transform: uppercase; letter-spacing: 1px; opacity: .85;
}
[dir="rtl"] .terms div span { text-transform: none; letter-spacing: 0; }
.terms div b { font-size: 1.4rem; font-variant-numeric: tabular-nums; }

.funding-grid {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-top: 14px;
}
@media (max-width: 900px) { .funding-grid { grid-template-columns: repeat(2, 1fr); } }
.funding-grid .metric-card { background: #fff; }

.disclaimer {
  background: var(--warn-bg);
  border: 1px solid var(--warn-line);
  color: var(--warn-ink);
  border-radius: 10px;
  padding: 14px 16px;
  font-size: .82rem;
  margin-top: 18px;
}

/* ============ Audit ============ */

.audit-table {
  width: 100%;
  border-collapse: collapse;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
  margin-top: 16px;
  font-size: .88rem;
}
.audit-table th, .audit-table td {
  text-align: left;
  padding: 8px 10px;
  border-bottom: 1px solid var(--line-soft);
}
.audit-table th { background: var(--line-soft); font-weight: 600; }
.audit-table tr:last-child td { border-bottom: 0; }

/* ============ "How the score is calculated" view ============
   A model-transparency panel toggled by the result tabs. Gauge + bars are
   inline SVG / CSS (no chart library — keeps CSP `script-src 'self'`). */

.result-tabs {
  display: flex;
  gap: 6px;
  border-bottom: 1px solid var(--line);
  margin-bottom: 22px;
}
.result-tab {
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  font: inherit;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
  padding: 10px 14px;
  margin-bottom: -1px;
}
.result-tab:hover { color: var(--ink-soft); }
.result-tab.is-active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

/* "See how this was calculated" CTA under the score. */
.calc-cta {
  display: inline-block;
  margin-top: 12px;
  font-size: .92rem;
}

.calc-panel .result-section:first-child { margin-top: 0; }
.calc-intro h2 { margin-bottom: 6px; }

/* Risk gauge */
.calc-gauge {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px 20px 10px;
}
.calc-gauge-svg {
  width: 100%;
  height: auto;
  display: block;
  /* The 0–100 axis is conceptually LTR; keep it stable in Arabic too. */
  direction: ltr;
}
.calc-gauge-scale {
  display: flex;
  justify-content: space-between;
  margin-top: 8px;
  /* The 0→100 axis is fixed LTR, so the "lower=better" / "higher=risk"
     labels must keep their left/right ends even in Arabic. */
  direction: ltr;
}
.calc-scale-good { color: var(--tranche-platinum); font-weight: 600; }
.calc-scale-bad  { color: var(--tranche-growth);   font-weight: 600; }
.calc-gauge-caption {
  margin-top: 12px;
  font-size: .98rem;
  color: var(--ink-soft);
  line-height: 1.55;
}

/* Pillars */
.calc-pillar {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px 18px;
  margin-bottom: 12px;
}
.calc-pillar-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
}
.calc-pillar-name { font-weight: 700; font-size: 1.05rem; }
.calc-pillar-desc { margin-top: 2px; max-width: 46ch; }
.calc-weight-badge {
  text-align: center;
  flex: none;
  background: var(--line-soft);
  border-radius: 10px;
  padding: 6px 12px;
}
.calc-weight-num {
  display: block;
  font-weight: 800;
  font-size: 1.25rem;
  font-variant-numeric: tabular-nums;
  color: var(--accent);
}
.calc-weight-label {
  display: block;
  font-size: .68rem;
  text-transform: uppercase;
  letter-spacing: 1px;
}
.calc-bar-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 14px 0 4px;
}
.calc-bar-track {
  flex: 1;
  height: 10px;
  background: var(--line-soft);
  border-radius: 999px;
  overflow: hidden;
  direction: ltr;
}
.calc-bar-fill {
  height: 100%;
  border-radius: 999px;
  transition: width .3s ease;
}
.calc-bar-value {
  flex: none;
  font-variant-numeric: tabular-nums;
  min-width: 64px;
  text-align: right;
}
.calc-inputs-label {
  margin-top: 10px;
  text-transform: uppercase;
  letter-spacing: 1px;
  font-size: .68rem;
}
.calc-inputs {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 6px;
}
.calc-input-chip {
  display: inline-flex;
  align-items: baseline;
  gap: 6px;
  background: var(--line-soft);
  border-radius: 8px;
  padding: 5px 10px;
  font-size: .85rem;
}
.calc-input-name { color: var(--ink-soft); }
.calc-input-val {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.calc-pillar-na { margin: 10px 0 0; }

/* Contribution chart */
.calc-stack {
  display: flex;
  height: 26px;
  width: 100%;
  border-radius: 8px;
  overflow: hidden;
  background: var(--line-soft);
  direction: ltr;
}
.calc-stack-seg { height: 100%; }
.calc-stack-seg + .calc-stack-seg {
  border-left: 1.5px solid #fff;
}
.calc-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 20px;
  margin-top: 14px;
}
.calc-legend-item {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: .9rem;
}
.calc-legend-swatch {
  width: 12px;
  height: 12px;
  border-radius: 3px;
  flex: none;
}
.calc-legend-name { color: var(--ink-soft); }
.calc-legend-pts {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.calc-legend-total {
  margin-left: auto;
  padding-left: 18px;
  border-left: 1px solid var(--line);
}
.calc-legend-total .calc-legend-name { color: var(--ink); font-weight: 600; }

/* ============ Score distribution (ranked payer lollipop) ============ */
.dist-lolli {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px 20px 12px;
}
.dist-lolli-svg {
  width: 100%;
  height: auto;
  display: block;
  /* The 0–100 risk axis is always LTR; keep it stable in Arabic too. */
  direction: ltr;
}
.dist-zone { opacity: 0.14; }
.dist-zone-label { font-size: 2.5px; font-weight: 700; opacity: 0.85; }
.dist-bound { stroke: #0A0A0F; stroke-width: 0.3; stroke-dasharray: 1 1; }
.dist-axis-line { stroke: #d9d2c7; stroke-width: 0.3; }
.dist-tick { font-size: 2.2px; fill: #6b6b75; }
.dist-conn { stroke: #c9c2b6; stroke-width: 0.5; }
.dist-dot { stroke: #fff; stroke-width: 0.5; }
.dist-score {
  font-size: 2.4px;
  font-weight: 700;
  fill: #0A0A0F;
  font-variant-numeric: tabular-nums;
}
.dist-name { font-size: 2.6px; fill: #3a3a42; }
/* Show the LTR name gutter by default; flip to the RTL gutter in Arabic. */
.dist-name-rtl { display: none; }
[dir="rtl"] .dist-name-ltr { display: none; }
[dir="rtl"] .dist-name-rtl { display: inline; }
.dist-stats {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 16px;
}
.dist-stat {
  flex: 1 1 120px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px 14px;
}
.dist-stat-value {
  font-size: 1.5rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.dist-stat-label { margin-top: 2px; }
.dist-tiers {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.dist-tier-row {
  display: flex;
  align-items: center;
  gap: 12px;
}
.dist-tier-name {
  flex: 0 0 7.5em;
  font-weight: 600;
}
.dist-tier-track {
  flex: 1 1 auto;
  height: 14px;
  background: var(--line-soft);
  border-radius: 7px;
  overflow: hidden;
}
.dist-tier-fill {
  display: block;
  height: 100%;
  border-radius: 7px;
}
.dist-tier-count {
  flex: 0 0 auto;
  font-variant-numeric: tabular-nums;
  color: var(--ink-soft);
  min-width: 5.5em;
  text-align: right;
}

/* ============ RTL adjustments ============
   The markup is the same; these rules flip alignments + spatial directions
   without forking the entire stylesheet. */

[dir="rtl"] body { text-align: right; }
[dir="rtl"] .calc-pillar-head { flex-direction: row-reverse; }
[dir="rtl"] .calc-bar-value { text-align: left; }
[dir="rtl"] .calc-weight-label,
[dir="rtl"] .calc-inputs-label { text-transform: none; letter-spacing: 0; }
[dir="rtl"] .calc-legend-total {
  margin-left: 0;
  margin-right: auto;
  padding-left: 0;
  padding-right: 18px;
  border-left: none;
  border-right: 1px solid var(--line);
}
[dir="rtl"] .calc-stack-seg + .calc-stack-seg {
  border-left: none;
  border-right: 1.5px solid #fff;
}
[dir="rtl"] .calc-weight-num,
[dir="rtl"] .calc-bar-value,
[dir="rtl"] .calc-input-val,
[dir="rtl"] .calc-legend-pts {
  direction: ltr;
  unicode-bidi: isolate;
  display: inline-block;
}
[dir="rtl"] .audit-table th,
[dir="rtl"] .audit-table td { text-align: right; }
[dir="rtl"] .dist-tier-count { text-align: left; }
[dir="rtl"] .form-row.inline { flex-direction: row-reverse; justify-content: flex-end; }
[dir="rtl"] .score-headline { flex-direction: row-reverse; justify-content: flex-end; }

/* Numbers (SAR amounts, scores) inside RTL flow should remain LTR so
   "SAR 1,234" reads correctly. */
[dir="rtl"] .num,
[dir="rtl"] .score-number,
[dir="rtl"] .metric-value,
[dir="rtl"] .fund-big-num,
[dir="rtl"] .terms div b {
  direction: ltr;
  unicode-bidi: isolate;
  display: inline-block;
}

/* ============ Bundle face value validation ============
   Applied when the user enters a face value above the total approved
   receivables cap. The renderer (public/app.js) toggles
   `.face-input-invalid` on the input and reveals `.face-warning`. */
.face-hint {
  margin: 4px 0 6px;
  max-width: 520px;
}
.face-warning {
  color: var(--tranche-growth);
  font-weight: 600;
  font-size: .88rem;
  margin: 4px 0 10px;
  max-width: 520px;
}
.face-input-invalid {
  border-color: var(--tranche-growth) !important;
  background: rgba(192, 57, 43, 0.04);
}
.face-input-invalid:focus {
  outline-color: var(--tranche-growth) !important;
  border-color: var(--tranche-growth) !important;
}
