:root {
  --bg: #f4f6f8;
  --card: #ffffff;
  --ink: #1d2733;
  --muted: #66758a;
  --accent: #c8102e;          /* Bureau Veritas red */
  --accent-dark: #9e0c24;
  --line: #dde3ea;
  --ok: #1d7a3a;
}
* { box-sizing: border-box; }
body {
  margin: 0; font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg); color: var(--ink); font-size: 14px;
}
.hidden { display: none !important; }

.topbar {
  display: flex; align-items: center; gap: 24px;
  background: #fff; border-bottom: 2px solid var(--accent);
  padding: 10px 20px; position: sticky; top: 0; z-index: 10;
}
.topbar h1 { font-size: 18px; margin: 0; color: var(--accent); }
.navbtn {
  background: none; border: none; font-size: 14px; padding: 8px 12px;
  cursor: pointer; color: var(--muted); border-bottom: 2px solid transparent;
}
.navbtn.active { color: var(--ink); border-bottom-color: var(--accent); font-weight: 600; }
.save-status { margin-left: auto; color: var(--muted); font-size: 12px; }
.save-status.saved { color: var(--ok); }

.view { padding: 20px; max-width: 1500px; margin: 0 auto; }
.toolbar { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; }
.toolbar .spacer { flex: 1; }
.editor-title { font-weight: 600; font-size: 16px; }

button, .btn-link {
  font-size: 14px; padding: 8px 14px; border-radius: 6px; cursor: pointer;
  border: 1px solid var(--line); background: #fff; color: var(--ink);
  text-decoration: none; display: inline-block;
}
button:hover, .btn-link:hover { border-color: var(--muted); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover { background: var(--accent-dark); }
button.danger { color: var(--accent); }
button:disabled { opacity: .5; cursor: default; }
button.small { padding: 3px 8px; font-size: 12px; }

.card {
  background: var(--card); border: 1px solid var(--line); border-radius: 10px;
  padding: 16px; margin-bottom: 12px;
}

.searchbar {
  flex: 1 1 280px; max-width: 480px; padding: 9px 12px;
  border: 1px solid var(--line); border-radius: 7px; font-size: 14px;
}
.toolbar select { padding: 8px 10px; border: 1px solid var(--line); border-radius: 7px; background: #fff; }
.hint { color: var(--muted); font-size: 12px; margin: -6px 0 12px; }

/* Template cards */
.tpl-card h3 { margin: 0 0 2px; font-size: 15px; }
.tpl-card .client-line { color: var(--muted); font-size: 12px; margin-bottom: 8px; }
.tpl-card ul.pvis { margin: 6px 0; padding-left: 18px; }
.tpl-card ul.pvis li { font-size: 12.5px; line-height: 1.8; }
.tpl-card ul.pvis li b { font-weight: 600; }
.tpl-card .row { display: flex; justify-content: flex-end; gap: 8px; margin-top: 10px; }

/* Reports table */
.report-table { display: flex; flex-direction: column; gap: 8px; }
.report-row {
  display: grid; grid-template-columns: 1.1fr 1.6fr 1.6fr 1fr auto;
  gap: 12px; align-items: center; background: var(--card);
  border: 1px solid var(--line); border-radius: 8px; padding: 10px 14px;
}
@media (max-width: 900px) { .report-row { grid-template-columns: 1fr 1fr; } }
.report-row .rn { font-weight: 600; }
.report-row .sub { color: var(--muted); font-size: 12px; }
.report-row .actions { display: flex; gap: 6px; }

/* Jobs list */
.job-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 14px; }
.job-card { cursor: pointer; transition: box-shadow .15s; }
.job-card:hover { box-shadow: 0 3px 12px rgba(0,0,0,.10); }
.job-card h3 { margin: 0 0 6px; font-size: 15px; }
.job-card .meta { color: var(--muted); font-size: 12px; line-height: 1.7; }
.job-card .row { display: flex; justify-content: space-between; align-items: center; margin-top: 8px; }
.badge {
  background: var(--bg); border: 1px solid var(--line); border-radius: 12px;
  font-size: 11px; padding: 2px 9px; color: var(--muted); margin-right: 5px;
}

/* Editor layout */
.editor-grid { display: grid; grid-template-columns: minmax(0, 1fr) 360px; gap: 18px; }
@media (max-width: 1000px) { .editor-grid { grid-template-columns: 1fr; } }
.editor-right h3, .editor-right h4 { margin: 14px 0 6px; }

.tabs { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.tab {
  padding: 7px 14px; border: 1px solid var(--line); border-radius: 6px 6px 0 0;
  background: #eceff3; cursor: pointer; font-size: 13px;
}
.tab.active { background: #fff; border-bottom-color: #fff; font-weight: 600; color: var(--accent); }
.tab.add { background: none; border-style: dashed; }

/* Forms */
.section { margin-bottom: 10px; }
.section > summary {
  cursor: pointer; font-weight: 600; padding: 10px 14px;
  background: var(--card); border: 1px solid var(--line); border-radius: 8px;
  list-style: none;
}
.section[open] > summary { border-radius: 8px 8px 0 0; border-bottom: none; color: var(--accent); }
.section .body {
  background: var(--card); border: 1px solid var(--line); border-top: none;
  border-radius: 0 0 8px 8px; padding: 14px;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 10px 14px;
}
.field { display: flex; flex-direction: column; gap: 3px; }
.field.wide { grid-column: 1 / -1; }
.field label { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
.field input, .field textarea, .field select {
  font-size: 14px; padding: 7px 9px; border: 1px solid var(--line); border-radius: 6px;
  font-family: inherit; background: #fff; color: var(--ink);
}
.field input:focus, .field textarea:focus { outline: 2px solid #f4b8c2; border-color: var(--accent); }
.field textarea { resize: vertical; min-height: 56px; }

.nc-row { display: grid; grid-template-columns: 1fr 1fr auto; gap: 10px; margin-bottom: 8px; grid-column: 1 / -1; }
.list-actions { grid-column: 1 / -1; }

.vessel-actions { display: flex; gap: 8px; margin: 4px 0 12px; }

/* Photos */
.upload-drop {
  display: block; border: 2px dashed var(--line); border-radius: 8px;
  text-align: center; padding: 18px 10px; color: var(--muted); cursor: pointer;
  background: var(--card);
}
.upload-drop.drag { border-color: var(--accent); color: var(--accent); }
.photo-tray {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  gap: 8px; min-height: 40px; padding: 8px; background: var(--card);
  border: 1px solid var(--line); border-radius: 8px;
}
.photo-cell { position: relative; }
.photo-cell img {
  width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: 6px; display: block;
  cursor: pointer; border: 2px solid transparent;
}
.photo-cell.selected img { border-color: var(--accent); }
.photo-cell .cap {
  font-size: 10px; color: var(--muted); white-space: nowrap; overflow: hidden;
  text-overflow: ellipsis; margin-top: 2px;
}
.photo-cell .np-star {
  position: absolute; top: 3px; right: 3px; background: rgba(255,255,255,.9);
  border-radius: 4px; font-size: 11px; padding: 1px 4px;
}
.photo-actionbar {
  grid-column: 1 / -1; display: flex; flex-wrap: wrap; gap: 6px; align-items: center;
  padding: 6px; background: var(--bg); border-radius: 6px;
}
.photo-actionbar input[type=text] { flex: 1 1 140px; padding: 5px 8px; border: 1px solid var(--line); border-radius: 5px; }
.photo-actionbar select { padding: 5px; border: 1px solid var(--line); border-radius: 5px; }

.np-slot { background: var(--card); border: 1px solid var(--line); border-radius: 8px; padding: 10px; margin-bottom: 8px; }
.np-slot-head { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; margin-bottom: 6px; }
.np-slot-body { display: flex; align-items: center; gap: 10px; }
.np-slot-body img { width: 64px; height: 64px; object-fit: cover; border-radius: 6px; border: 2px solid var(--accent); }
.np-slot-body select { flex: 1; padding: 6px 8px; border: 1px solid var(--line); border-radius: 6px; min-width: 0; }
#np-slot-none { color: var(--muted); font-size: 12px; }

.output-list { background: var(--card); border: 1px solid var(--line); border-radius: 8px; padding: 8px; }
.output-list a { display: block; padding: 4px 6px; color: var(--ink); text-decoration: none; font-size: 13px; border-radius: 4px; }
.output-list a:hover { background: var(--bg); color: var(--accent); }
.output-list .empty, .photo-tray .empty { color: var(--muted); font-size: 12px; padding: 6px; }

/* Legacy */
.legacy-card { max-width: 640px; margin: 30px auto; }
.legacy-opts { display: flex; gap: 24px; margin: 14px 0; }
.legacy-log { background: #10161d; color: #c7d3e0; padding: 12px; border-radius: 8px; font-size: 12px; max-height: 320px; overflow: auto; white-space: pre-wrap; }

.extract-filelist { font-size: 12px; color: var(--muted); margin: 10px 0; line-height: 1.7; }
.extract-actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin: 12px 0; }
.extract-or { color: var(--muted); font-size: 12px; }
#extract-name { flex: 1 1 180px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 6px; }

/* Modal */
.modal-back {
  position: fixed; inset: 0; background: rgba(15,23,32,.45);
  display: flex; align-items: center; justify-content: center; z-index: 50;
}
.modal {
  background: #fff; border-radius: 12px; padding: 22px; width: min(480px, 92vw);
  max-height: 86vh; overflow: auto;
}
.modal h2 { margin: 0 0 14px; font-size: 17px; }
.modal .field { margin-bottom: 12px; }
.modal .actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 16px; }
.tmpl-option { padding: 9px 12px; border: 1px solid var(--line); border-radius: 7px; cursor: pointer; margin-bottom: 7px; }
.tmpl-option:hover { border-color: var(--accent); }
.tmpl-option.selected { border-color: var(--accent); background: #fdf2f4; }
.tmpl-option .sub { color: var(--muted); font-size: 12px; }
.tmpl-option { position: relative; }
.tmpl-xlsx {
  position: absolute; top: 8px; right: 10px; font-size: 11px;
  color: var(--muted); text-decoration: none; border: 1px solid var(--line);
  border-radius: 5px; padding: 2px 7px;
}
.tmpl-xlsx:hover { color: var(--accent); border-color: var(--accent); }
.tmpl-serial {
  display: inline-block; margin-left: 8px; font-size: 11px; font-weight: 600;
  color: var(--accent); background: #fdf2f4; border-radius: 4px; padding: 1px 7px;
}
.tmpl-vessels { margin-top: 5px; }
.tmpl-vessels summary {
  cursor: pointer; font-size: 12px; color: var(--muted); list-style: none;
  user-select: none;
}
.tmpl-vessels summary::before { content: "▸ "; }
.tmpl-vessels[open] summary::before { content: "▾ "; }
.tmpl-vessels ul { margin: 5px 0 2px; padding-left: 16px; }
.tmpl-vessels li { font-size: 12px; color: var(--ink); line-height: 1.7; }
#nj-search { width: 100%; padding: 8px 10px; border: 1px solid var(--line); border-radius: 6px; }
#nj-templates { max-height: 46vh; overflow: auto; margin: 4px 0 6px; padding-right: 2px; }
#nj-templates .empty { color: var(--muted); font-size: 13px; padding: 10px; text-align: center; }

.preset-btn { padding: 0 6px; font-size: 12px; line-height: 1.5; vertical-align: middle; }
.preset-modal { width: min(720px, 94vw); }
#preset-search { width: 100%; padding: 8px 10px; border: 1px solid var(--line); border-radius: 6px; margin-bottom: 10px; }
#preset-list { max-height: 55vh; overflow: auto; }
.preset-item {
  padding: 9px 12px; border: 1px solid var(--line); border-radius: 7px;
  margin-bottom: 7px; cursor: pointer; font-size: 13px; white-space: pre-wrap;
}
.preset-item:hover { border-color: var(--accent); background: #fdf2f4; }

.spinner {
  display: inline-block; width: 15px; height: 15px; border: 2px solid #fff;
  border-top-color: transparent; border-radius: 50%; animation: spin .8s linear infinite;
  vertical-align: -3px; margin-right: 7px;
}
@keyframes spin { to { transform: rotate(360deg); } }

.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%);
  background: #1d2733; color: #fff; padding: 11px 20px; border-radius: 8px;
  font-size: 13px; z-index: 99; box-shadow: 0 4px 14px rgba(0,0,0,.3);
}
.toast.error { background: var(--accent-dark); }
