/* ==========================================================================
   CometFrog EDMS/QMS — shared stylesheet
   Design tokens + components. Presentation only.
   Body-scoped surfaces:
     body.app  -> tenant application (dark sidebar shell)
     body.auth -> centered auth/login pages
     body.sa   -> superadmin panel (navy topbar)
   ========================================================================== */

/* ---------- Design tokens ---------- */
:root {
  /* Brand */
  --brand:        #1e3a8a;   /* primary navy-blue */
  --brand-strong: #1b3374;
  --brand-tint:   #eef4ff;
  --accent:       #4f86ff;

  /* Sidebar palette */
  --sb-bg:     #0f172a;
  --sb-box:    #0b1224;
  --sb-border: #111827;
  --sb-text:   #e5e7eb;
  --sb-muted:  #9ca3af;
  --sb-active: #1e3a8a;
  --sbw:           260px;   /* expanded width */
  --sbw-collapsed: 68px;    /* collapsed width */

  /* Neutrals / surfaces */
  --bg:      #f6f8fb;
  --surface: #ffffff;
  --ink:     #1a202c;
  --muted:   #667085;
  --line:    #e2e8f0;
  --line-2:  #d9dee6;

  /* Status colours */
  --ok:      #16a34a;
  --ok-bg:   #dcfce7;
  --warn:    #b45309;
  --warn-bg: #fef3c7;
  --bad:     #dc2626;
  --bad-bg:  #fee2e2;
  --info:    #1d4ed8;
  --info-bg: #eff6ff;

  /* Shape */
  --r:    10px;
  --r-sm: 8px;
  --r-pill: 999px;
  --sh:   0 1px 3px rgba(16,24,40,.08);
  --sh-2: 0 4px 14px rgba(16,24,40,.10);

  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

/* ---------- Reset / base ---------- */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  font-family: var(--font);
  color: var(--ink);
  background: var(--bg);
  margin: 0;
  line-height: 1.5;
  font-size: 15px;
}
h1 { font-size: 1.45rem; margin: .2rem 0 .1rem; letter-spacing: -.01em; }
h2 { font-size: 1.15rem; margin: 1.3rem 0 .5rem; }
h3 { font-size: 1rem; margin: .2rem 0 .4rem; }
a { color: var(--brand); }
p { margin: .5rem 0; }
img { max-width: 100%; }

.muted { color: var(--muted); }
.small { font-size: .875rem; color: #55607a; }
.nowrap { white-space: nowrap; }
.text-right { text-align: right; }
.spacer { flex: 1; }

/* Accessible focus for keyboard users */
a:focus-visible,
button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
summary:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ==========================================================================
   Tenant application shell (body.app)
   ========================================================================== */
body.app { margin-left: var(--sbw-collapsed); transition: margin-left .2s ease; }
body.app.sidebar-expanded { margin-left: var(--sbw); }
@media (max-width: 900px) {
  body.app, body.app.sidebar-expanded { margin-left: 0; }
}

.content {
  padding: 1.4rem 1.75rem 3rem;
  max-width: 1180px;
}
@media (max-width: 900px) {
  .content { padding: 3.2rem 1rem 2.5rem; }  /* room for mobile menu button */
}

/* ---------- Sidebar (rendered by _nav.html) ---------- */
#sidebar {
  position: fixed; top: 0; left: 0; bottom: 0;
  width: var(--sbw-collapsed); transition: width .2s ease, transform .2s ease;
  background: var(--sb-bg); color: var(--sb-text); box-sizing: border-box; z-index: 1000;
  display: flex; flex-direction: column; padding: 12px 10px;
  overflow: hidden;
}
body.sidebar-expanded #sidebar { width: var(--sbw); }
#sb-scroll { flex: 1 1 auto; overflow-y: auto; min-height: 0; }

#toggle {
  display: none; position: fixed; top: 10px; left: 10px; z-index: 1100;
  background: var(--sb-bg); color: var(--sb-text); border: 1px solid #1f2937;
  border-radius: 8px; padding: 6px 10px; cursor: pointer;
}
@media (max-width: 900px) {
  #toggle { display: block; }
  #sidebar { transform: translateX(-100%); }
  body.sidebar-open #sidebar { transform: translateX(0); }
}

#topbar { display: flex; flex-direction: column; align-items: flex-start; gap: 6px; margin-bottom: 8px; }
#brandtext { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#brand { font-size: 13px; letter-spacing: .08em; text-transform: uppercase; color: var(--sb-muted); margin: 0; }
#app-title { font-size: 18px; font-weight: 700; margin: 0; }
body:not(.sidebar-expanded) #brandtext { display: none; }

#userchip {
  font-size: 12px; color: var(--sb-muted); white-space: nowrap; overflow: hidden;
  text-overflow: ellipsis; max-width: 100%;
}
body:not(.sidebar-expanded) #userchip { display: none; }

.sec { margin-top: 12px; }
.sec h3 {
  font-size: 12px; letter-spacing: .12em; text-transform: uppercase;
  color: var(--sb-muted); margin: 10px 8px 6px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
body:not(.sidebar-expanded) .sec h3 { display: none; }

.box { background: var(--sb-box); border: 1px solid var(--sb-border); border-radius: 10px; padding: 6px; }

#sidebar details { border-radius: 8px; }
#sidebar details > summary {
  list-style: none; cursor: pointer; padding: 8px 10px; border-radius: 8px;
  color: var(--sb-text); font-weight: 600;
}
#sidebar details[open] > summary { background: #0a122d; }
#sidebar details > summary::-webkit-details-marker { display: none; }
body:not(.sidebar-expanded) #sidebar details > summary { padding: 4px 6px; font-size: 0; line-height: 0; }
body:not(.sidebar-expanded) #sidebar details[open] > summary { background: transparent; }
#sidebar details.no-collapse > summary { pointer-events: none; cursor: default; }

.nav { display: flex; flex-direction: column; gap: 2px; padding: 4px; }
.nav a {
  display: flex; align-items: center; gap: 10px;
  padding: 7px 10px; border-radius: 8px; color: var(--sb-text); text-decoration: none;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.nav a:hover { background: #111b3a; }
.nav a.active { background: var(--sb-active); color: #fff; }

.nav .dot { width: 8px; height: 8px; border-radius: 999px; background: #4b5563; flex: 0 0 8px; }
.nav a.active .dot { background: #93c5fd; }
.label { flex: 1 1 auto; overflow: hidden; text-overflow: ellipsis; }
body:not(.sidebar-expanded) .label { display: none; }

/* KPIs inside sidebar */
.kpi-box { background: var(--sb-box); border: 1px solid var(--sb-border); border-radius: 10px; padding: 8px; margin-top: 6px; }
body:not(.sidebar-expanded) .kpi-box { display: none; }
.kpi-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; }
.kpi-title { font-size: 12px; color: var(--sb-muted); letter-spacing: .06em; text-transform: uppercase; }
.kpi-val { font-size: 12px; color: var(--sb-text); }
.meter { height: 8px; border-radius: 999px; background: #111827; overflow: hidden; }
.meter-bar { height: 100%; width: 0; border-radius: 999px; transition: width .25s ease; background: var(--sb-active); }
.meter-bar.ok { background: #16a34a; }
.meter-bar.warn { background: #f59e0b; }
.meter-bar.bad { background: #ef4444; }
.meter-bar.blue { background: var(--accent); }
.kpi-sub { margin-top: 6px; font-size: 12px; color: var(--sb-muted); }
.kpi-row { display: flex; align-items: center; gap: 8px; margin-top: 8px; }
.kpi-ct { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.kpi-break { flex-basis: 100%; height: 0; }
.kpi-pill {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; padding: 6px 10px; border-radius: 999px;
  background: var(--sb-active); color: #fff; text-decoration: none;
}
.kpi-pill.pw { width: 105px; justify-content: space-between; }
.kpi-pill.alert { background: #b91c1c; }
.kpi-pill.warn  { background: #f59e0b; color: #111; }
.pill-badge {
  min-width: 18px; height: 18px; border-radius: 999px; padding: 0 6px;
  display: inline-flex; align-items: center; justify-content: center;
  background: rgba(255,255,255,.15); color: #fff; font-weight: 700; font-size: 12px;
}

#sb-footer { padding-top: 10px; }
.logo-bottom {
  width: 60px; height: 60px; border-radius: 12px;
  background: var(--sb-box); border: 1px solid var(--sb-border);
  display: flex; align-items: center; justify-content: center;
  margin: 6px auto 2px;
}
.logo-bottom img { width: 48px; height: 48px; object-fit: contain; }

/* ==========================================================================
   Shared components (tenant content area)
   ========================================================================== */

/* Buttons */
.btn {
  display: inline-flex; align-items: center; gap: .4rem;
  border: 1px solid var(--line-2); border-radius: var(--r-sm);
  background: var(--surface); padding: .4rem .7rem; cursor: pointer;
  text-decoration: none; color: var(--ink); font-size: .9rem; line-height: 1.2;
  transition: background .12s, border-color .12s, box-shadow .12s;
}
.btn:hover { background: var(--brand-tint); border-color: #b9d1ff; }
.btn.active { border-color: var(--accent); background: var(--brand-tint); }
.btn.primary { background: var(--brand); color: #fff; border-color: var(--brand); }
.btn.primary:hover { background: var(--brand-strong); }
.btn.danger { background: var(--bad-bg); color: var(--bad); border-color: #fca5a5; }
.btn.danger:hover { background: #fdd; }
.btn-link { background: none; border: none; padding: 0; margin: 0; color: #0645ad; cursor: pointer; }
.btn-link:hover { text-decoration: underline; }

/* Cards */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); padding: 1rem; box-shadow: var(--sh); }

/* Tables */
table { border-collapse: collapse; width: 100%; margin-top: .6rem; background: var(--surface); }
th, td { border: 1px solid var(--line); padding: 8px 10px; vertical-align: top; text-align: left; }
th { background: #f2f5f9; font-weight: 600; }
tbody tr:hover td { background: #fafbfd; }
.table-wrap { width: 100%; overflow-x: auto; }

/* Form controls */
input[type="text"], input[type="email"], input[type="password"],
input[type="date"], input[type="datetime-local"], input[type="number"],
input[type="search"], select, textarea {
  font: inherit; color: var(--ink); background: var(--surface);
  padding: .5rem .6rem; border: 1px solid var(--line-2); border-radius: var(--r-sm);
  box-sizing: border-box; max-width: 100%;
}
input:focus, select:focus, textarea:focus {
  outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(79,134,255,.15);
}
textarea { min-height: 96px; }
label { display: inline-block; }

.field { margin: .5rem 0; }
.field label { display: block; font-size: .9rem; color: #3a4457; margin-bottom: .25rem; font-weight: 600; }
.field input[type="text"], .field select, .field textarea,
.field input[type="date"], .field input[type="datetime-local"] { width: 100%; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
@media (max-width: 700px) { .grid-2 { grid-template-columns: 1fr; } }

/* Form-as-grid layouts (shared) */
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; max-width: 900px; }
.form-grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; max-width: 900px; }
.form-row { display: grid; grid-template-columns: 1fr 2fr; gap: .6rem 1rem; margin: .5rem 0; }
.span-all { grid-column: 1 / -1; }
@media (max-width: 700px) {
  .form-grid, .form-grid-3, .form-row { grid-template-columns: 1fr; }
}

/* Form pages: consistent card width, labels and full-width fields */
.form-card { max-width: 900px; }
.form-card--wide { max-width: 1100px; }
.form-card input:not([type="radio"]):not([type="checkbox"]):not([type="hidden"]):not([type="submit"]):not([type="button"]):not([type="file"]),
.form-card select, .form-card textarea { width: 100%; }
.form-card textarea { min-height: 120px; }
.form-card label { display: block; font-weight: 600; margin: .8rem 0 .2rem; }
.form-card .form-row label { margin: 0; }
.form-card label.inline { display: inline-flex; font-weight: 400; margin: 0; }

/* Pills / badges */
.pill { display: inline-block; padding: .15rem .55rem; border: 1px solid var(--line-2); border-radius: var(--r-pill); font-size: .8rem; }
.badge { display: inline-block; padding: .1rem .45rem; border-radius: var(--r-sm); background: var(--brand-tint); border: 1px solid #ccd6f6; font-size: .78rem; }

/* Unified status pills */
.status-pill {
  display: inline-block; padding: .12rem .55rem; border-radius: var(--r-pill);
  border: 1px solid var(--line-2); font-size: .78rem; font-weight: 600; line-height: 1.5;
  background: #f1f5f9; color: #475569;
}
.status-pill.st-draft    { background: #f1f5f9; color: #475569; border-color: #cbd5e1; }
.status-pill.st-review   { background: var(--warn-bg); color: var(--warn); border-color: #fcd34d; }
.status-pill.st-approved { background: #dbeafe; color: #1e40af; border-color: #93c5fd; }
.status-pill.st-effective{ background: var(--ok-bg); color: #166534; border-color: #86efac; }
.status-pill.st-obsolete { background: #f3f4f6; color: #9ca3af; border-color: #e5e7eb; }
.status-pill.st-open, .status-pill.st-inprogress { background: var(--info-bg); color: var(--info); border-color: #bfdbfe; }
.status-pill.st-closed { background: var(--ok-bg); color: #166534; border-color: #86efac; }

/* Status dots (view_doc) */
.dot-status { display: inline-block; width: 10px; height: 10px; border-radius: 50%; }
.dot-status.red { background: #e74c3c; }
.dot-status.amber { background: #f39c12; }
.dot-status.green { background: #2ecc71; }

/* Risk chips (NCR/CAPA) */
.risk-chip { display: inline-block; border-radius: var(--r-pill); border: 1px solid var(--line-2); padding: .15rem .5rem; margin-left: .35rem; }
.risk-red { background: var(--bad-bg); }
.risk-amber { background: var(--warn-bg); }
.risk-green { background: var(--ok-bg); }

/* Date highlighting in registers */
.dueSoon { background-color: var(--warn-bg); }
.overdue { background-color: var(--bad-bg); }

/* Progress (home training bar) */
.progress { width: 260px; max-width: 60vw; height: 10px; border: 1px solid #ccd; border-radius: 999px; background: #f3f6ff; overflow: hidden; }
.progress > div { height: 100%; background: var(--accent); }
.progress.overdue > div { background: #ef4444; }

/* Flash messages */
.flash-list { list-style: none; padding: 0; margin: 0 0 1rem; }
.flash-list li, .flash {
  padding: .6rem .85rem; margin-bottom: .5rem; border-radius: var(--r-sm);
  border: 1px solid; font-size: .9rem;
}
.flash-list li { background: var(--warn-bg); border-color: #fcd34d; color: #7c4a03; }
.flash-list li.danger, .flash-err { background: var(--bad-bg); border-color: #fca5a5; color: #991b1b; }
.flash-list li.success, .flash-ok { background: var(--ok-bg); border-color: #86efac; color: #166534; }
.flash-info { background: var(--info-bg); border-color: #bfdbfe; color: var(--info); }

/* Layout helpers used across pages */
.row { display: flex; gap: 1.5rem; align-items: flex-start; flex-wrap: wrap; }
.col { flex: 1; min-width: 260px; }
.top-actions { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; margin-bottom: 1rem; }
/* Shared action-button row for form pages */
.form-actions { display: flex; flex-wrap: wrap; gap: .5rem; margin-top: 1rem; }
.inline-form { display: inline; margin-left: .5rem; }
/* Buttons/forms rendered inline in table cells & rows */
form.inline { display: inline; }

/* Monospace text (IDs, SQL, serials) */
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; }

/* Search / filter controls bar (register list pages) */
.controls { display: flex; gap: .5rem; align-items: center; margin: .6rem 0 1rem; }
.controls input[type="search"] { border: 1px solid #d9d9d9; border-radius: 10px; padding: .4rem .6rem; min-width: 240px; }

/* Training-compliance meta line (home + my_training) */
.train-wrap { display: flex; align-items: center; gap: .6rem; flex-wrap: wrap; }
.train-meta { font-size: .85rem; color: #333; white-space: nowrap; }

/* Traffic-light status badges (training compliance) */
.traffic { display: inline-block; padding: .15rem .5rem; border-radius: .5rem; color: #fff; font-size: .85rem; }
.traffic.red, .traffic.Red { background: #e74c3c; }
.traffic.amber, .traffic.Amber { background: #f39c12; }
.traffic.green, .traffic.Green { background: #2ecc71; }

/* Due-date cell highlighting (equipment / CAPA registers) */
.due-red { background: #ffecec; }
.due-amber { background: #fff9e6; }

/* Register row status colouring */
tr.row-closed { color: #666; background: #f5f5f5; }
tr.row-red { background: #ffefef; }
tr.row-amber { background: #fff7e0; }

/* ==========================================================================
   Auth / login pages (body.auth)
   ========================================================================== */
body.auth {
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh; padding: 1.5rem; background: var(--bg);
}
.auth-card {
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px;
  padding: 2rem 2.25rem; box-shadow: var(--sh-2); width: 100%; max-width: 380px;
}
.auth-logo { display: flex; align-items: center; gap: .6rem; margin-bottom: .35rem; }
.auth-logo img { width: 40px; height: 40px; object-fit: contain; }
.auth-brand { font-weight: 800; font-size: 1.15rem; color: var(--brand); letter-spacing: -.01em; }
.auth-sub { font-size: .82rem; color: var(--muted); margin-bottom: 1.5rem; }
.auth-card form { max-width: none; }
.auth-card label { display: block; font-size: .84rem; font-weight: 600; margin: .8rem 0 .28rem; }
.auth-card input { display: block; width: 100%; }
.auth-card .btn.primary, .auth-card button[type="submit"] { width: 100%; justify-content: center; margin-top: 1.25rem; padding: .6rem; }
.auth-card .hint { font-size: .8rem; color: var(--muted); margin-top: .3rem; }

/* ==========================================================================
   Superadmin panel (body.sa) — faithful port, scoped
   ========================================================================== */
body.sa { background: var(--bg); }
body.sa .topbar { background: #1e2d40; color: #fff; padding: .75rem 2rem; display: flex; align-items: center; gap: 1.25rem; flex-wrap: wrap; }
body.sa .topbar .brand { font-weight: 700; font-size: 1.05rem; letter-spacing: -.01em; margin-right: auto; }
body.sa .topbar a, body.sa .topbar button { color: rgba(255,255,255,.75); text-decoration: none; font-size: .85rem; background: none; border: none; cursor: pointer; padding: 0; }
body.sa .topbar a:hover, body.sa .topbar button:hover { color: #fff; }
body.sa .wrap { max-width: 1200px; margin: 0 auto; padding: 1.5rem 2rem; }
body.sa .ph { display: flex; align-items: center; gap: .75rem; margin-bottom: 1.25rem; flex-wrap: wrap; }
body.sa .ph h1 { margin: 0; font-size: 1.35rem; font-weight: 700; }
body.sa .cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(195px, 1fr)); gap: .85rem; margin-bottom: 1.5rem; }
body.sa .card { background: #fff; border: 1px solid var(--line); border-radius: var(--r-sm); padding: 1rem 1.1rem; box-shadow: var(--sh); text-decoration: none; color: inherit; display: block; }
body.sa .card:hover { box-shadow: var(--sh-2); }
body.sa .card h3 { margin: 0 0 .25rem; font-size: .9rem; font-weight: 600; }
body.sa .card p { margin: 0; font-size: .78rem; color: #64748b; }
body.sa .sec { background: #fff; border: 1px solid var(--line); border-radius: var(--r-sm); padding: 1.1rem 1.25rem; box-shadow: var(--sh); margin-bottom: 1.1rem; margin-top: 0; }
body.sa .sec-hd { margin: 0 0 .85rem; font-size: .95rem; font-weight: 600; padding-bottom: .6rem; border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: .5rem; }
body.sa .sec-hd .sp { flex: 1; }
body.sa table { border-collapse: collapse; width: 100%; }
body.sa th, body.sa td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; font-size: .8rem; vertical-align: middle; }
body.sa th { background: #f8fafc; font-weight: 600; }
body.sa tr:hover td { background: #fafbfc; }
body.sa .btn { display: inline-flex; align-items: center; gap: .3rem; border: 1px solid var(--line); border-radius: var(--r-sm); background: #fff; padding: .28rem .6rem; cursor: pointer; text-decoration: none; color: #374151; font-size: .78rem; white-space: nowrap; }
body.sa .btn:hover { background: #f1f5f9; }
body.sa .btn-p { background: #1e2d40; color: #fff; border-color: #1e2d40; }
body.sa .btn-p:hover { background: #2c4460; }
body.sa .btn-d { background: #fef2f2; color: #dc2626; border-color: #fca5a5; }
body.sa .btn-d:hover { background: #fee2e2; }
body.sa .btn-g { background: #f0fdf4; color: #16a34a; border-color: #86efac; }
body.sa .btn-g:hover { background: #dcfce7; }
body.sa .ctx { background: #eff6ff; border: 1px solid #bfdbfe; border-radius: var(--r-sm); padding: .6rem 1rem; font-size: .85rem; margin-bottom: 1.25rem; display: flex; align-items: center; gap: .75rem; flex-wrap: wrap; }
body.sa .ctx strong { color: #1d4ed8; }
body.sa .flash { padding: .6rem 1rem; border-radius: var(--r-sm); margin-bottom: 1rem; font-size: .85rem; }
body.sa .fi { margin-bottom: .6rem; }
body.sa .fi label { display: block; font-size: .78rem; font-weight: 500; margin-bottom: .2rem; color: #374151; }
body.sa .fi input, body.sa .fi select { padding: .35rem .55rem; border: 1px solid var(--line); border-radius: var(--r-sm); font-size: .8rem; max-width: 280px; width: 100%; }
body.sa .badge { display: inline-block; padding: .1rem .4rem; border-radius: 99px; font-size: .7rem; font-weight: 600; background: transparent; border: none; }
body.sa .badge-ok { background: #dcfce7; color: #166534; }
body.sa .badge-off { background: #f1f5f9; color: #64748b; }
body.sa .ifrow { display: inline-flex; align-items: center; gap: .4rem; flex-wrap: wrap; }
body.sa details summary { cursor: pointer; font-size: .85rem; color: #64748b; padding: .4rem 0; }
body.sa details summary:hover { color: #1a202c; }
body.sa details[open] summary { color: #1a202c; font-weight: 500; }
