:root {
  --red: #8B0101;
  --red-soft: rgba(139, 1, 1, 0.12);
  --red-ink: #a50101;
  --gray: #2a2a2a;
  --gray-soft: #333333;
  --bg: #0f0f0f;
  --bg-gradient: linear-gradient(135deg, #0f0f0f 0%, #1a1a1a 50%, #0f0f0f 100%);
  --surface: #1a1a1a;
  --surface-gradient: linear-gradient(145deg, #222222 0%, #1a1a1a 100%);
  --ink: #ffffff;
  --ink-1: #ffffff;
  --ink-2: #e0e0e0;
  --ink-3: #bfbfbf;
  --line: rgba(255,255,255,0.08);
  --line-strong: rgba(255,255,255,0.12);
  --focus: #8B0101;
  --radius: 12px;
  --pad: 16px;
  --row-pad-y: 10px;
  --shadow: 0 8px 32px rgba(0,0,0,0.5);
  --shadow-glow: 0 0 15px rgba(139,1,1,0.25);
  --transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

@keyframes slideUp {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 14px;
  line-height: 1.45;
  color: var(--ink);
  background: var(--bg-gradient);
  font-feature-settings: "cv11", "ss01";
  -webkit-font-smoothing: antialiased;
}
:lang(ar), .ar { font-family: "Noto Sans Arabic", "Inter", sans-serif; direction: rtl; }

/* ---- Top nav ---- */
.topbar {
  position: sticky; top: 0; z-index: 40;
  background: rgba(26,26,26,0.92);
  backdrop-filter: saturate(180%) blur(10px);
  -webkit-backdrop-filter: saturate(180%) blur(10px);
  border-bottom: 1px solid var(--line);
}
.topbar-inner {
  max-width: 1280px; margin: 0 auto;
  display: flex; align-items: center; gap: 16px;
  padding: 10px 24px;
}
.brand {
  display: flex; align-items: baseline; gap: 10px;
  font-weight: 600; letter-spacing: -0.01em;
}
.brand .mark {
  width: 10px; height: 10px; border-radius: 2px; background: var(--red);
  display: inline-block; transform: translateY(1px);
}
.brand .ar { color: var(--ink-3); font-size: 12px; }
.topbar-nav {
  flex: 1; display: flex; gap: 2px; overflow-x: auto;
  scrollbar-width: thin;
}
.topbar-nav::-webkit-scrollbar { height: 4px; }
.topbar-nav::-webkit-scrollbar-thumb { background: var(--line-strong); border-radius: 4px; }
.topbar-nav a {
  flex-shrink: 0;
  padding: 6px 10px;
  border-radius: 4px;
  color: var(--ink-2);
  text-decoration: none;
  font-size: 12.5px;
  white-space: nowrap;
  transition: background .15s, color .15s;
}
.topbar-nav a:hover { background: var(--gray-soft); color: var(--ink); }
.topbar-nav a.active { color: var(--red); background: var(--red-soft); font-weight: 500; }

.topbar-actions { display: flex; gap: 8px; align-items: center; }
.save-indicator {
  font-size: 12px; color: var(--ink-3); display: flex; align-items: center; gap: 6px;
}
.save-indicator .dot {
  width: 6px; height: 6px; border-radius: 50%; background: #4CAF50;
}
.save-indicator.saving .dot { background: #F5A623; animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: 0.3; } }

button, .btn {
  font-family: inherit; font-size: 13px;
  border: 1px solid var(--line-strong);
  background: var(--surface);
  color: var(--ink);
  padding: 6px 12px;
  border-radius: var(--radius);
  cursor: pointer;
  transition: all .15s;
  display: inline-flex; align-items: center; gap: 6px;
}
button:hover, .btn:hover { border-color: var(--ink-3); }
button.primary, .btn.primary {
  background: var(--red); color: white; border-color: var(--red);
}
button.primary:hover, .btn.primary:hover { background: var(--red-ink); border-color: var(--red-ink); }
button.ghost { border-color: transparent; background: transparent; color: var(--ink-2); }
button.ghost:hover { background: var(--gray-soft); }
button.small { padding: 3px 8px; font-size: 12px; }
button.danger-ghost { color: var(--red); border-color: transparent; background: transparent; }
button.danger-ghost:hover { background: var(--red-soft); }

/* ---- Document ---- */
.doc {
  max-width: 1280px; margin: 0 auto;
  padding: 32px 24px 120px;
}
.doc-title {
  display: flex; align-items: baseline; justify-content: space-between;
  margin-bottom: 28px;
  padding-bottom: 18px; border-bottom: 1px solid var(--line);
}
.doc-title h1 { font-size: 22px; font-weight: 600; margin: 0; letter-spacing: -0.015em; }
.doc-title .ar-title { font-size: 14px; color: var(--ink-3); direction: rtl; }

/* ---- Sections ---- */
section.sec { margin-bottom: 40px; scroll-margin-top: 76px; }
.sec-head {
  background: linear-gradient(90deg, #8B0101, #a50101);
  color: white;
  padding: 12px 18px;
  border-radius: var(--radius) var(--radius) 0 0;
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
}
.sec-head .num {
  font-variant-numeric: tabular-nums;
  opacity: 0.7;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.08em;
}
.sec-head .title-en { font-weight: 700; font-size: 15px; letter-spacing: -0.01em; }
.sec-head .title-ar { font-size: 14px; opacity: 0.85; direction: rtl; }
.sec-head-left { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }

.sec-sub {
  background: #2a2a2a;
  color: var(--ink);
  padding: 8px 18px;
  font-size: 13px;
  font-weight: 600;
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
}
.sec-sub .ar { font-size: 12px; color: var(--ink-3); }

.sec-body {
  background: var(--surface-gradient);
  border: 1px solid var(--line);
  border-top: none;
  border-radius: 0 0 var(--radius) var(--radius);
  padding: var(--pad);
  box-shadow: 0 8px 30px rgba(0,0,0,0.2);
}

/* ---- Fields ---- */
.field-grid { display: grid; gap: 14px 18px; grid-template-columns: repeat(4, 1fr); }
.field-grid.two { grid-template-columns: repeat(2, 1fr); }
.field-grid.three { grid-template-columns: repeat(3, 1fr); }
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field label {
  font-size: 11.5px;
  color: var(--ink-2);
  font-weight: 500;
  display: flex; gap: 6px; align-items: baseline;
}
.field label .ar {
  color: var(--ink-3); font-size: 11px; font-weight: 400;
}
.field input[type="text"], input[type="date"], input[type="number"], select, textarea {
  width: 100%; border: 1px solid var(--line); background: rgba(0,0,0,0.2); color: var(--ink);
  padding: 10px 12px; border-radius: 8px; font-family: inherit; font-size: 13.5px;
  transition: all .2s; outline: none;
}
input:hover, select:hover, textarea:hover { border-color: var(--line-strong); background: rgba(0,0,0,0.25); }
input:focus, select:focus, textarea:focus { border-color: var(--red); background: rgba(0,0,0,0.3); box-shadow: 0 0 0 3px rgba(139,1,1,0.15); }
.field textarea { resize: vertical; min-height: 36px; line-height: 1.4; }

/* ---- Checkbox groups ---- */
.kpi-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; }
.kpi-card {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px;
  background: var(--bg);
}
.kpi-title { font-size: 12px; font-weight: 600; color: var(--ink); margin-bottom: 8px; }
.kpi-title .ar { font-weight: 400; color: var(--ink-3); font-size: 11px; }
.kpi-opt {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 8px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12.5px;
  transition: background .1s;
}
.kpi-opt:hover { background: var(--gray-soft); }
.kpi-opt input { margin: 0; cursor: pointer; accent-color: var(--red); }
.kpi-opt .status-good { color: #2d6a2d; font-weight: 500; }
.kpi-opt .status-bad { color: var(--red); font-weight: 500; }
.kpi-opt .ar { color: var(--ink-3); font-size: 11px; margin-left: auto; }
.kpi-opt.selected { background: var(--red-soft); }

/* ---- Tables ---- */
.tbl-wrap { overflow-x: auto; }
table.entry {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
table.entry th, table.entry td {
  border-bottom: 1px solid var(--line);
  padding: var(--row-pad-y) 8px;
  text-align: left;
  vertical-align: top;
  color: var(--ink-2);
}
table.entry th {
  background: var(--gray);
  font-weight: 600;
  font-size: 11.5px;
  color: var(--ink);
  letter-spacing: 0.01em;
  border-bottom: 1px solid var(--line-strong);
}
table.entry th .ar {
  display: block; font-weight: 400; color: var(--ink-2); font-size: 10.5px; direction: rtl;
}
table.entry td.num-col {
  color: var(--ink-3);
  font-variant-numeric: tabular-nums;
  width: 28px;
  text-align: center;
  font-size: 12px;
}
table.entry td.action-col { width: 40px; text-align: center; }
table.entry input, table.entry select, table.entry textarea {
  width: 100%;
  border: 1px solid transparent;
  background: transparent;
  padding: 4px 6px;
  font-family: inherit; font-size: 13px;
  border-radius: 3px;
  color: var(--ink);
  min-width: 0;
}
table.entry input:hover, table.entry select:hover, table.entry textarea:hover {
  background: var(--gray-soft);
}
table.entry input:focus, table.entry select:focus, table.entry textarea:focus {
  outline: none;
  background: var(--surface);
  border-color: var(--red);
  box-shadow: 0 0 0 2px var(--red-soft);
}
table.entry textarea { resize: vertical; min-height: 28px; line-height: 1.35; }
table.entry tr:hover td { background: rgba(0,0,0,0.01); }

.add-row {
  margin-top: 12px;
  display: flex; justify-content: space-between; align-items: center;
  gap: 8px;
}
.add-row .count { font-size: 12px; color: var(--ink-3); }

/* ---- Split (two-column subsections) ---- */
.split { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.split .col-head {
  font-size: 12px;
  font-weight: 600;
  color: var(--ink-2);
  margin-bottom: 10px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--line);
  display: flex; justify-content: space-between; align-items: baseline; gap: 10px;
}
.split .col-head .ar { color: var(--ink-3); font-weight: 400; font-size: 11px; }

/* ---- Photos ---- */
.photo-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 14px; }
.photo-card {
  border: 1px dashed var(--line-strong);
  border-radius: var(--radius);
  padding: 10px;
  background: var(--bg);
}
.photo-drop {
  aspect-ratio: 4/3;
  background: repeating-linear-gradient(45deg, transparent, transparent 8px, rgba(0,0,0,0.025) 8px, rgba(0,0,0,0.025) 9px);
  border-radius: 4px;
  display: flex; align-items: center; justify-content: center;
  color: var(--ink-3); font-family: "JetBrains Mono", monospace; font-size: 11px;
  position: relative;
  overflow: hidden;
}
.photo-drop img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.photo-drop input[type=file] {
  position: absolute; inset: 0; opacity: 0; cursor: pointer;
}
.photo-caption {
  margin-top: 8px; width: 100%;
  border: 1px solid var(--line);
  padding: 6px 8px; border-radius: 4px; font-size: 12.5px;
  font-family: inherit;
  color: var(--ink);
  background: rgba(0,0,0,0.2);
}
.photo-caption:focus { outline: none; border: 1px solid var(--red); background: rgba(0,0,0,0.3); }

/* ---- Signatures ---- */
.sig-grid { display: grid; grid-template-columns: 160px 1fr 1fr 1.2fr 100px; gap: 10px; align-items: end; padding: 10px 0; border-bottom: 1px solid var(--line); }
.sig-grid:last-child { border-bottom: none; }
.sig-role { font-weight: 500; font-size: 13px; }
.sig-role .ar { color: var(--ink-3); font-size: 11px; display: block; }
.sig-pad {
  border: 1px solid var(--line);
  background: rgba(0,0,0,0.2);
  border-radius: 4px;
  height: 44px;
  position: relative;
  cursor: crosshair;
}
.sig-pad canvas { width: 100%; height: 100%; display: block; border-radius: 4px; }
.sig-pad .clear {
  position: absolute; top: 2px; right: 4px;
  background: transparent; border: none; color: var(--ink-3);
  font-size: 10px; cursor: pointer; padding: 2px 4px;
}
.sig-pad .clear:hover { color: var(--red); }

.sig-grid input {
  width: 100%;
  border: 1px solid var(--line);
  background: rgba(0,0,0,0.2);
  color: var(--ink);
  padding: 8px 10px;
  border-radius: 4px;
  font-family: inherit;
  font-size: 13.5px;
  transition: all .2s;
  outline: none;
}
.sig-grid input:hover { border-color: var(--line-strong); background: rgba(0,0,0,0.25); }
.sig-grid input:focus { border-color: var(--red); background: rgba(0,0,0,0.3); box-shadow: 0 0 0 3px rgba(139,1,1,0.15); }

/* ---- Weather row ---- */
.weather-row { display: grid; grid-template-columns: 120px 1fr 80px 120px 1.3fr 90px 90px; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--line); align-items: center; }
.weather-row.head { font-weight: 600; font-size: 11.5px; color: var(--ink-2); background: var(--gray); border-bottom: 1px solid var(--line-strong); padding: 8px; border-radius: 4px; margin-bottom: 4px; }
.weather-row.total { font-weight: 600; background: var(--gray-soft); padding: 10px 8px; border-radius: 4px; margin-top: 6px; border-bottom: none; }
.weather-day { font-weight: 500; font-size: 13px; }
.weather-day .ar { color: var(--ink-3); font-size: 11px; display: block; }

.weather-row input {
  width: 100%;
  border: 1px solid transparent;
  background: transparent;
  color: var(--ink);
  padding: 6px 8px;
  border-radius: 4px;
  font-family: inherit;
  font-size: 13.5px;
  transition: all .2s;
}
.weather-row input:hover { background: var(--gray-soft); }
.weather-row input:focus { outline: none; background: var(--surface); border-color: var(--red); box-shadow: 0 0 0 3px rgba(139,1,1,0.15); }

/* ---- Workforce / Equipment ---- */
.wf-row { display: grid; grid-template-columns: 1.4fr repeat(6, 1fr); gap: 6px; padding: 6px 0; border-bottom: 1px solid var(--line); align-items: center; font-size: 13px; }
.wf-row.head { font-weight: 600; font-size: 11.5px; background: var(--gray); padding: 8px 6px; border-radius: 4px; color: var(--ink); border-bottom: 1px solid var(--line-strong); }
.wf-row.total { font-weight: 600; background: var(--gray-soft); padding: 10px 6px; border-radius: 4px; margin-top: 4px; border-bottom: none; }
.wf-row input { border: 1px solid transparent; background: transparent; padding: 5px; font-family: inherit; font-size: 13px; border-radius: 3px; width: 100%; text-align: right; font-variant-numeric: tabular-nums; color: var(--ink); }
.wf-row input:hover { background: var(--gray-soft); }
.wf-row input:focus { outline: none; background: var(--surface); border-color: var(--red); box-shadow: 0 0 0 2px var(--red-soft); }
.wf-label .ar { color: var(--ink-3); font-size: 11px; display: block; }

.eq-row { display: grid; grid-template-columns: 1.4fr 60px 1fr 1fr 1fr 80px; gap: 6px; padding: 6px 0; border-bottom: 1px solid var(--line); align-items: center; font-size: 13px; }
.eq-row.head { font-weight: 600; font-size: 11.5px; background: var(--gray); padding: 8px 6px; border-radius: 4px; color: var(--ink); border-bottom: 1px solid var(--line-strong); }
.eq-row.total { font-weight: 600; background: var(--gray-soft); padding: 10px 6px; border-radius: 4px; margin-top: 4px; border-bottom: none; }
.eq-row input { border: 1px solid transparent; background: transparent; padding: 5px; font-family: inherit; font-size: 13px; border-radius: 3px; width: 100%; text-align: right; font-variant-numeric: tabular-nums; color: var(--ink); }
.eq-row input:hover { background: var(--gray-soft); }
.eq-row input:focus { outline: none; background: var(--surface); border-color: var(--red); box-shadow: 0 0 0 2px var(--red-soft); }

/* ---- HSE split ---- */
.hse-split { display: grid; grid-template-columns: 1fr 1.2fr; gap: 16px; }
.hse-kpi { display: grid; grid-template-columns: 1.5fr 1fr 1fr; gap: 4px 8px; align-items: center; font-size: 13px; padding: 6px 0; border-bottom: 1px solid var(--line); }
.hse-kpi.head { font-weight: 600; font-size: 11.5px; color: var(--ink); background: var(--gray); padding: 8px; border-radius: 4px; border-bottom: 1px solid var(--line-strong); }
.hse-kpi input { border: 1px solid transparent; background: transparent; padding: 5px; font-family: inherit; font-size: 13px; text-align: right; border-radius: 3px; width: 100%; font-variant-numeric: tabular-nums; color: var(--ink); }
.hse-kpi input:hover { background: var(--gray-soft); }
.hse-kpi input:focus { outline: none; background: var(--surface); border-color: var(--red); box-shadow: 0 0 0 2px var(--red-soft); }

/* ---- Floating Tweaks panel ---- */
.tweaks-panel {
  position: fixed;
  bottom: 20px; right: 20px;
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  padding: 14px;
  width: 260px;
  box-shadow: 0 10px 40px rgba(0,0,0,0.12);
  z-index: 50;
  display: none;
}
.tweaks-panel.on { display: block; }
.tweaks-panel h3 { margin: 0 0 12px; font-size: 13px; font-weight: 600; display: flex; justify-content: space-between; align-items: center; }
.tweaks-panel h3 button { padding: 2px 6px; font-size: 11px; }
.tweaks-row { margin-bottom: 10px; }
.tweaks-row label { display: block; font-size: 11.5px; color: var(--ink-2); margin-bottom: 4px; }
.tweaks-row .seg { display: flex; gap: 2px; border: 1px solid var(--line); border-radius: 4px; padding: 2px; }
.tweaks-row .seg button {
  flex: 1; border: none; padding: 5px; font-size: 12px; border-radius: 3px;
}
.tweaks-row .seg button.on { background: var(--red); color: white; }

body.density-compact { --pad: 12px; --row-pad-y: 6px; }
body.density-compact .field-grid { gap: 10px 14px; }
body.density-compact section.sec { margin-bottom: 28px; }

body.lang-en .ar, body.lang-en [data-ar-only] { display: none !important; }
body.lang-ar .en, body.lang-ar [data-en-only] { display: none !important; }
body.lang-ar { direction: rtl; }
body.lang-ar table.entry th, body.lang-ar table.entry td,
body.lang-ar .field { text-align: right; }

/* ---------- Responsive & Fixes ---------- */
@media (max-width: 960px) {
  .field-grid, .field-grid.two, .field-grid.three { grid-template-columns: 1fr 1fr; }
  .kpi-grid { grid-template-columns: 1fr 1fr; }
  .split, .hse-split, .photo-grid { grid-template-columns: 1fr; }
  .weather-row { grid-template-columns: 1fr 1fr; gap: 6px; }
  .weather-row.head { display: none; }
  .wf-row { grid-template-columns: 1.3fr repeat(3, 1fr); }
  .eq-row { grid-template-columns: 1.3fr 1fr 1fr; }
  .eq-row.head, .wf-row.head { font-size: 10px; }
  .sig-grid { grid-template-columns: 1fr; gap: 6px; }
  table.entry { font-size: 12px; }
  .topbar-inner { padding: 8px 14px; }
  .doc { padding: 20px 14px 100px; max-width: 100%; }
  .stat-grid { grid-template-columns: 1fr 1fr; }
  .reports-page { padding: 16px 14px; }
}

@media (max-width: 600px) {
  .field-grid, .field-grid.two, .field-grid.three { grid-template-columns: 1fr; }
  .kpi-grid { grid-template-columns: 1fr; }
  .tweaks-panel { right: 10px; left: 10px; width: auto; }
  .weather-row { grid-template-columns: 1fr; }
  .wf-row, .eq-row { grid-template-columns: 1fr 1fr; }
  .wf-row.head, .eq-row.head { display: none; }
}

/* Fix for Weather Impact / Inputs contrast */
.weather-row input, .field input, .field textarea, table.entry td input {
  color: #ffffff !important;
  background: rgba(255, 255, 255, 0.05);
}
.weather-row input::placeholder, .field input::placeholder {
  color: rgba(255, 255, 255, 0.4);
}

/* Table scroll hint */
.tbl-wrap {
  position: relative;
  border-radius: 8px;
  border: 1px solid var(--line);
}
.tbl-wrap::after {
  content: 'Swipe for more →';
  position: absolute;
  bottom: 4px; right: 8px;
  font-size: 9px;
  color: var(--ink-3);
  pointer-events: none;
  opacity: 0.5;
}
@media (min-width: 769px) { .tbl-wrap::after { display: none; } }

/* Signature pad mobile */
.sig-pad { min-height: 80px; }
.sig-pad canvas { min-height: 80px; }
