/* contra — design tokens.
   Light is defined on bare :root. Dark is redefined in two places: the
   system-preference media query (guarded so an explicit light choice wins)
   and the explicit [data-theme="dark"] attribute. A colour whose ONLY
   definition lives inside a media query breaks the manual toggle. */

:root {
  --canvas:        #f5f6f8;
  --surface:       #ffffff;
  --surface-2:     #f0f2f5;
  --border:        #dfe3e8;
  --ink:           #1b2330;
  --ink-2:         #5b6675;
  --ink-3:         #8a94a3;
  --brand:         #1f5f8b;
  --brand-ink:     #ffffff;
  --brand-soft:    #e8f1f8;
  --urgent:        #b3261e;
  --urgent-soft:   #fbeae9;
  --warn:          #8a5a00;
  --warn-soft:     #fdf3e0;
  --ok:            #1e6b45;
  --ok-soft:       #e6f4ec;
  /* chart series — validated for CVD + contrast against --surface.
     Priority is an ordered state, so it wears the same semantics as the
     badges: neutral, informational, warning, critical. --series-flat is the
     single sequential hue for magnitude bars (length already encodes the
     value), --series-mute the de-emphasis gray for "everything else". */
  --series-normal: #8a94a3;
  --series-medium: #15639f;
  --series-high:   #c98500;
  --series-urgent: #b3213c;
  --series-flat:   #1f5f8b;
  --series-mute:   #c3cad3;
  --grid-line:     #e7ebef;

  --shadow:        0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --radius:        10px;

  /* Layering — REQ-UI-10. A confirmation must never sit under the modal that
     opened it, and Bootstrap's own modal is 1055. */
  --z-modal:       1055;
  --z-confirm:     1080;
  --z-toast:       1090;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --canvas:      #11151c;
    --surface:     #171d26;
    --surface-2:   #1f2733;
    --border:      #2b3543;
    --ink:         #e7ecf3;
    --ink-2:       #a8b3c2;
    --ink-3:       #737f8f;
    --brand:       #4a9fd5;
    --brand-ink:   #0b1017;
    --brand-soft:  #16293a;
    --urgent:      #f2837b;
    --urgent-soft: #35191a;
    --warn:        #e0ae5c;
    --warn-soft:   #322616;
    --ok:          #6cc79a;
    --ok-soft:     #142b20;
    --series-normal: #737f8f;
    --series-medium: #3987e5;
    --series-high:   #c98500;
    --series-urgent: #e33b56;
    --series-flat:   #4a9fd5;
    --series-mute:   #3a4553;
    --grid-line:     #242e3b;
    --shadow:      0 1px 2px rgba(0, 0, 0, .4), 0 1px 3px rgba(0, 0, 0, .5);
  }
}

:root[data-theme="dark"] {
  --canvas:      #11151c;
  --surface:     #171d26;
  --surface-2:   #1f2733;
  --border:      #2b3543;
  --ink:         #e7ecf3;
  --ink-2:       #a8b3c2;
  --ink-3:       #737f8f;
  --brand:       #4a9fd5;
  --brand-ink:   #0b1017;
  --brand-soft:  #16293a;
  --urgent:      #f2837b;
  --urgent-soft: #35191a;
  --warn:        #e0ae5c;
  --warn-soft:   #322616;
  --ok:          #6cc79a;
  --ok-soft:     #142b20;
  --series-normal: #737f8f;
  --series-medium: #3987e5;
  --series-high:   #c98500;
  --series-urgent: #e33b56;
  --series-flat:   #4a9fd5;
  --series-mute:   #3a4553;
  --grid-line:     #242e3b;
  --shadow:      0 1px 2px rgba(0, 0, 0, .4), 0 1px 3px rgba(0, 0, 0, .5);
}

* { box-sizing: border-box; }

body {
  background: var(--canvas);
  color: var(--ink);
  font-family: "Segoe UI", Tahoma, "Noto Sans Arabic", system-ui, -apple-system, sans-serif;
  min-height: 100vh;
  margin: 0;
}

/* ---- shell ---- */
.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: .6rem 1rem;
  display: flex; align-items: center; gap: .75rem;
  position: sticky; top: 0; z-index: 1030;
}
.topbar .brand {
  font-weight: 700; color: var(--brand); text-decoration: none;
  font-size: 1.05rem; white-space: nowrap;
}
.topbar .spacer { flex: 1 1 auto; }

.nav-links { display: flex; gap: .25rem; flex-wrap: wrap; }
.nav-links a {
  color: var(--ink-2); text-decoration: none; padding: .4rem .7rem;
  border-radius: var(--radius); font-size: .92rem; white-space: nowrap;
}
.nav-links a:hover { background: var(--surface-2); color: var(--ink); }
.nav-links a.active { background: var(--brand-soft); color: var(--brand); font-weight: 600; }

.content { max-width: 1400px; margin: 0 auto; padding: 1.25rem; }

/* ---- cards ---- */
.card-c {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 1rem;
}
.section-title { font-size: 1.05rem; font-weight: 600; margin: 0 0 .75rem; }
.muted { color: var(--ink-2); }
.tiny { font-size: .82rem; }

/* REQ-UI-02 — the contract card stays minimal on purpose. */
.contract-card { display: flex; flex-direction: column; gap: .6rem; margin-bottom: .75rem; }
.contract-card h3 { font-size: 1rem; margin: 0; font-weight: 600; }
.contract-meta { display: flex; gap: .75rem; flex-wrap: wrap; font-size: .85rem; color: var(--ink-2); }

/* ---- stage bar ---- */
.stage-bar { display: flex; gap: 3px; flex-wrap: wrap; }
.stage-step {
  flex: 1 1 0; min-width: 72px; text-align: center; font-size: .74rem;
  padding: .35rem .3rem; border-radius: 6px; cursor: pointer;
  background: var(--surface-2); color: var(--ink-3);
  border: 1px solid transparent; transition: background .12s, color .12s;
}
.stage-step:hover { border-color: var(--brand); color: var(--ink); }
.stage-step.done { background: var(--ok-soft); color: var(--ok); }
.stage-step.current { background: var(--brand); color: var(--brand-ink); font-weight: 600; }
.stage-step[aria-disabled="true"] { cursor: default; }

/* ---- badges ---- */
.badge-c {
  display: inline-block; padding: .18rem .5rem; border-radius: 999px;
  font-size: .75rem; font-weight: 600; line-height: 1.4;
}
.badge-overdue  { background: var(--urgent-soft); color: var(--urgent); }
.badge-expiring { background: var(--warn-soft);   color: var(--warn); }
.badge-unread   { background: var(--brand-soft);  color: var(--brand); }
.badge-urgent   { background: var(--urgent); color: #fff; }
.badge-high     { background: var(--warn-soft); color: var(--warn); }
.badge-medium   { background: var(--surface-2); color: var(--ink-2); }
.badge-normal   { background: var(--surface-2); color: var(--ink-3); }

/* ---- forms / buttons ---- */
.form-label { font-size: .85rem; color: var(--ink-2); margin-bottom: .2rem; font-weight: 500; }
.form-control, .form-select, textarea {
  background: var(--surface); color: var(--ink);
  border: 1px solid var(--border); border-radius: 8px;
  padding: .45rem .6rem; width: 100%; font-size: .92rem;
}
.form-control:focus, .form-select:focus, textarea:focus {
  outline: 2px solid var(--brand); outline-offset: -1px; border-color: var(--brand);
}
.btn-c {
  border: 1px solid var(--border); background: var(--surface); color: var(--ink);
  border-radius: 8px; padding: .42rem .85rem; font-size: .9rem; cursor: pointer;
  text-decoration: none; display: inline-flex; align-items: center; gap: .35rem;
}
.btn-c:hover { background: var(--surface-2); }
.btn-c.primary { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.btn-c.danger  { background: var(--urgent); color: #fff; border-color: var(--urgent); }
.btn-c:disabled { opacity: .55; cursor: not-allowed; }

/* ---- tables: never break the page on small screens (NFR-04) ---- */
.table-wrap { overflow-x: auto; }
table.tbl { width: 100%; border-collapse: collapse; font-size: .88rem; }
table.tbl th, table.tbl td {
  padding: .5rem .6rem; border-bottom: 1px solid var(--border);
  text-align: start; vertical-align: top;
}
table.tbl th { color: var(--ink-2); font-weight: 600; background: var(--surface-2); }

/* ---- toasts (REQ-UI-06) ---- */
#toast-host {
  position: fixed; top: 1rem; inset-inline: 0; margin-inline: auto;
  width: min(92vw, 420px); z-index: var(--z-toast);
  display: flex; flex-direction: column; gap: .5rem; pointer-events: none;
}
.toast-c {
  background: var(--surface); color: var(--ink);
  border: 1px solid var(--border); border-inline-start: 4px solid var(--brand);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: .65rem .85rem; font-size: .9rem; pointer-events: auto;
  animation: toast-in .16s ease-out;
}
.toast-c.success { border-inline-start-color: var(--ok); }
.toast-c.error   { border-inline-start-color: var(--urgent); }
.toast-c.warning { border-inline-start-color: var(--warn); }
.toast-c.leaving { opacity: 0; transition: opacity .2s; }
@keyframes toast-in { from { opacity: 0; transform: translateY(-6px); } to { opacity: 1; } }

/* ---- confirm dialog (REQ-UI-07 / REQ-UI-10) ---- */
#confirm-host {
  position: fixed; inset: 0; z-index: var(--z-confirm);
  background: rgba(8, 12, 18, .5);
  display: flex; align-items: center; justify-content: center; padding: 1rem;
}
#confirm-host[hidden] { display: none; }
.confirm-box {
  background: var(--surface); color: var(--ink); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  width: min(94vw, 420px); padding: 1.1rem;
}
.confirm-box h4 { margin: 0 0 .5rem; font-size: 1.02rem; }
.confirm-actions { display: flex; gap: .5rem; justify-content: flex-end; margin-top: 1rem; }

/* ---- drawer (contract details) ---- */
#drawer-host {
  position: fixed; inset: 0; z-index: var(--z-modal);
  background: rgba(8, 12, 18, .5); display: flex; justify-content: center;
  align-items: flex-start; padding: 1rem; overflow-y: auto;
}
#drawer-host[hidden] { display: none; }
.drawer-box {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  width: min(96vw, 860px); margin: auto 0; padding: 1.1rem;
}

.tabs { display: flex; gap: .25rem; border-bottom: 1px solid var(--border); margin-bottom: .9rem; }
.tabs button {
  background: none; border: none; border-bottom: 2px solid transparent;
  padding: .5rem .8rem; color: var(--ink-2); cursor: pointer; font-size: .92rem;
}
.tabs button.active { color: var(--brand); border-bottom-color: var(--brand); font-weight: 600; }
.tab-panel[hidden] { display: none; }

.note-item { border: 1px solid var(--border); border-radius: 8px; padding: .6rem .75rem; margin-bottom: .5rem; }
.note-item.unread { border-inline-start: 3px solid var(--brand); background: var(--brand-soft); }

.grid { display: grid; gap: .75rem; }
.grid.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid.cols-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
@media (max-width: 860px) { .grid.cols-3, .grid.cols-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 560px) {
  .grid.cols-2, .grid.cols-3, .grid.cols-4 { grid-template-columns: 1fr; }
  .content { padding: .75rem; }
  .nav-links a { padding: .35rem .5rem; font-size: .85rem; }
}

.stat { background: var(--surface-2); border-radius: var(--radius); padding: .8rem; }
.stat .n { font-size: 1.6rem; font-weight: 700; line-height: 1.1; }
.stat .l { font-size: .82rem; color: var(--ink-2); }

.empty { text-align: center; color: var(--ink-3); padding: 2rem 1rem; }

/* ---- dashboard ---------------------------------------------------------
   Three densities on purpose: a hero band of four big figures, a dense grid
   of secondary KPI chips, then the chart cards. */
.hero-grid { display: grid; gap: .75rem; grid-template-columns: repeat(4, minmax(0, 1fr)); }
.hero {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: .9rem 1rem; display: flex; flex-direction: column; gap: .15rem;
  border-top: 3px solid var(--accent, var(--brand));
}
.hero .n { font-size: 2.1rem; font-weight: 700; line-height: 1.05; letter-spacing: -.02em; }
.hero .n .unit { font-size: .95rem; font-weight: 600; color: var(--ink-2); margin-inline-start: .25rem; }
.hero .l { font-size: .85rem; color: var(--ink-2); font-weight: 500; }
.hero .d { font-size: .76rem; color: var(--ink-3); }

.kpi-grid { display: grid; gap: .5rem; grid-template-columns: repeat(6, minmax(0, 1fr)); }
.kpi {
  background: var(--surface-2); border-radius: 8px; padding: .55rem .65rem;
  display: flex; flex-direction: column; gap: .1rem; min-width: 0;
}
.kpi .n { font-size: 1.15rem; font-weight: 700; line-height: 1.15; }
.kpi .l { font-size: .74rem; color: var(--ink-2); line-height: 1.25; }
.kpi.alert .n { color: var(--urgent); }
.kpi.warn .n  { color: var(--warn); }

.chart-card { display: flex; flex-direction: column; }
.chart-card .section-title { display: flex; align-items: baseline; gap: .5rem; }
.chart-card .section-title .tiny { font-weight: 400; }
/* Chart.js sizes itself to the parent, which therefore needs a real height. */
.chart-box { position: relative; height: 260px; min-width: 0; }
.chart-box.tall { height: 320px; }

.dash-split { display: grid; gap: .75rem; grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); }

@media (max-width: 1100px) { .kpi-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
@media (max-width: 860px) {
  .hero-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .dash-split { grid-template-columns: 1fr; }
}
@media (max-width: 560px) {
  .hero-grid { grid-template-columns: 1fr; }
  .kpi-grid  { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .chart-box { height: 230px; }
}
