:root {
  --navy: #1f3b57;
  --navy-2: #2a4d70;
  --bg: #eef1f5;
  --panel: #ffffff;
  --ink: #1d2733;
  --muted: #6b7a8d;
  --line: #d9e0e8;
  --blue: #2f6fb0;
  --teal: #2a9d8f;
  --night: #5b4b8a;
  --danger: #d1495b;
  --danger-bg: #fdecef;
  --warn: #e09f3e;
  --shadow: 0 1px 3px rgba(20,40,60,.12);
}

* { box-sizing: border-box; }
/* The `hidden` attribute must win over our id/class display rules below. */
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
}
h1, h2 { margin: 0; font-weight: 600; }
.muted { color: var(--muted); }
button { font: inherit; cursor: pointer; }

/* Top bar */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  background: var(--navy); color: #fff; padding: 10px 18px;
  position: sticky; top: 0; z-index: 30; box-shadow: var(--shadow);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand h1 { font-size: 17px; letter-spacing: .2px; }
.brand .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--teal); box-shadow: 0 0 0 3px rgba(42,157,143,.3); }
.topbar-meta { display: flex; align-items: center; gap: 14px; }
.topbar-meta .muted { color: #b9c6d6; }
/* All top-bar action buttons share one consistent ghost style. */
#btn-users, #btn-manage, #btn-refresh, #btn-logout {
  background: transparent; color: #fff; border: 1px solid #3a5d82;
  padding: 6px 12px; border-radius: 6px;
}
#btn-users:hover, #btn-manage:hover, #btn-refresh:hover, #btn-logout:hover { background: var(--navy-2); }

.add-user { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.add-user input, .add-user select { padding: 7px 9px; border: 1px solid var(--line); border-radius: 7px; font: inherit; }
.add-user input[name=email] { flex: 1 1 160px; }
.add-user input[name=password] { flex: 1 1 140px; }
.add-user input[name=pilot_code] { width: 90px; }
.file-row select.assign-sel { margin-left: auto; }

/* Manage-CSVs modal */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 180; display: flex; align-items: center; justify-content: center;
  background: rgba(15,32,50,.55);
}
.modal { background: #fff; border-radius: 12px; width: 520px; max-width: 92vw; max-height: 86vh; overflow: auto; box-shadow: 0 20px 60px rgba(0,0,0,.4); }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 18px; border-bottom: 1px solid var(--line); background: var(--navy); color: #fff; border-radius: 12px 12px 0 0; }
.modal-head h2 { font-size: 15px; }
.modal-x { background: none; border: none; color: #fff; font-size: 22px; line-height: 1; }
.modal-nav { display: flex; align-items: center; gap: 8px; }
.modal-nav #job-prev, .modal-nav #job-next {
  background: var(--navy-2); color: #fff; border: 1px solid #3a5d82; border-radius: 6px;
  width: 30px; height: 28px; font-size: 15px; line-height: 1;
}
.modal-nav #job-prev:hover:not(:disabled), .modal-nav #job-next:hover:not(:disabled) { background: #34577c; }
.modal-nav #job-prev:disabled, .modal-nav #job-next:disabled { opacity: .35; cursor: default; }
.modal-nav #job-pos { color: #b9c6d6; font-size: 12px; min-width: 56px; text-align: center; }
.modal-body { padding: 18px; }
.modal-body h3 { font-size: 13px; color: var(--navy); margin: 18px 0 8px; }
.uploader { display: block; border: 2px dashed var(--line); border-radius: 8px; padding: 16px; text-align: center; cursor: pointer; margin-bottom: 10px; }
.uploader:hover { border-color: var(--blue); background: #f6f9fc; }
.uploader span { display: block; margin-top: 6px; color: var(--muted); font-size: 13px; }
.btn-primary { background: var(--navy); color: #fff; border: none; padding: 9px 16px; border-radius: 8px; font-weight: 600; }
.btn-primary:hover { background: var(--navy-2); }
.btn-primary:disabled { opacity: .6; cursor: default; }
.file-list { border: 1px solid var(--line); border-radius: 8px; max-height: 260px; overflow: auto; }
.file-row { display: flex; align-items: center; gap: 12px; padding: 7px 12px; border-bottom: 1px solid #eef2f6; font-size: 13px; }
.file-row:last-child { border-bottom: none; }
.file-row .fname { flex: 1; font-variant-numeric: tabular-nums; }
.link.danger { color: var(--danger); text-decoration: underline; background: none; border: none; font-size: 12px; }

/* Login screen */
#login-overlay {
  position: fixed; inset: 0; z-index: 200; display: flex; align-items: center; justify-content: center;
  background: linear-gradient(160deg, #1f3b57, #0f2032);
}
.login-card {
  background: #fff; border-radius: 14px; padding: 28px 30px; width: 340px; max-width: 90vw;
  box-shadow: 0 18px 50px rgba(0,0,0,.35); display: flex; flex-direction: column; gap: 10px;
}
.login-brand { display: flex; align-items: center; gap: 10px; }
.login-brand h1 { font-size: 18px; color: var(--navy); }
.login-brand .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--teal); box-shadow: 0 0 0 3px rgba(42,157,143,.3); }
.login-sub { margin: 0 0 6px; color: var(--muted); font-size: 13px; }
.login-card label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.login-card input { padding: 9px 10px; border: 1px solid var(--line); border-radius: 8px; font-size: 14px; }
.login-card input:focus { outline: 2px solid var(--blue); border-color: var(--blue); }
.login-card button {
  margin-top: 6px; padding: 10px; border: none; border-radius: 8px; background: var(--navy);
  color: #fff; font-weight: 600; font-size: 14px;
}
.login-card button:hover { background: var(--navy-2); }
.login-card button:disabled { opacity: .6; cursor: default; }
.login-error { color: var(--danger); font-size: 13px; min-height: 16px; margin: 2px 0 0; }
.login-foot { color: var(--muted); font-size: 11px; margin: 4px 0 0; text-align: center; }

main { max-width: 1500px; margin: 0 auto; padding: 16px; }

/* Panels */
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow); }
.controls { display: grid; grid-template-columns: 1.6fr 1fr; gap: 0; }
.controls-block { padding: 14px 16px; }
.controls-block + .controls-block { border-left: 1px solid var(--line); }
.controls-head { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 10px; }
.controls-head h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .6px; color: var(--navy); }
.link { background: none; border: none; color: var(--blue); font-size: 12px; text-decoration: underline; padding: 0; }
.hint { font-size: 12px; color: var(--muted); margin: 10px 0 0; }

/* Assumptions */
.assump-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px 18px; }
.assump { display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 4px 8px; }
.assump label { font-size: 12px; color: var(--ink); }
.assump .val { font-variant-numeric: tabular-nums; font-weight: 600; color: var(--navy); min-width: 36px; text-align: right; }
.assump input[type=range] { grid-column: 1 / -1; width: 100%; accent-color: var(--blue); }
.assump input[type=range]:disabled { accent-color: var(--muted); opacity: .65; cursor: not-allowed; }

/* Transient toast message */
.toast-msg {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 150;
  background: #11202e; color: #fff; padding: 10px 16px; border-radius: 8px; font-size: 13px;
  box-shadow: 0 6px 20px rgba(0,0,0,.3);
}

/* Filters */
.field { display: flex; flex-direction: column; gap: 5px; }
.field > span { font-size: 12px; color: var(--muted); }
.field-row { display: flex; gap: 12px; margin-top: 10px; }
.field-row .field { flex: 1; }
input[type=date] { padding: 5px 7px; border: 1px solid var(--line); border-radius: 6px; font: inherit; }
.chips { display: flex; flex-direction: column; align-items: flex-start; gap: 7px; }
.chip-group { display: flex; align-items: center; flex-wrap: wrap; gap: 5px; }
.chip-grouplabel {
  font-size: 11px; font-weight: 700; color: var(--navy); text-transform: uppercase;
  letter-spacing: .5px; cursor: pointer; padding: 3px 8px; border-radius: 5px; background: #eef2f7;
  margin-right: 3px;
}
.chip-grouplabel:hover { background: #e1e8f0; }
.chip {
  font-size: 12px; padding: 3px 9px; border-radius: 999px; cursor: pointer; user-select: none;
  border: 1px solid var(--line); background: #f4f7fa; color: var(--muted);
}
.chip.on { background: var(--blue); border-color: var(--blue); color: #fff; }
.toggles { display: flex; flex-direction: column; gap: 6px; margin-top: 12px; }
.chk { font-size: 13px; display: flex; align-items: center; gap: 7px; cursor: pointer; }
.history-load { display: flex; align-items: center; gap: 10px; margin-top: 12px; flex-wrap: wrap; }
.history-load #btn-earlier {
  background: var(--navy); color: #fff; border: none; padding: 6px 12px; border-radius: 7px; font-weight: 600; font-size: 13px;
}
.history-load #btn-earlier:hover { background: var(--navy-2); }
.history-load #btn-earlier:disabled { opacity: .5; cursor: default; }
.history-load #btn-all { font-size: 12px; }
#history-range { font-size: 12px; }

/* Tabs */
.tabs { display: flex; gap: 4px; margin: 16px 0 12px; }
.tab {
  background: transparent; border: 1px solid transparent; border-bottom: none;
  padding: 8px 16px; border-radius: 8px 8px 0 0; color: var(--muted); font-weight: 500;
}
.tab.active { background: var(--panel); color: var(--navy); border-color: var(--line); box-shadow: 0 -1px 2px rgba(0,0,0,.03); }

.view { background: var(--panel); border: 1px solid var(--line); border-radius: 0 10px 10px 10px; box-shadow: var(--shadow); padding: 0; min-height: 360px; }

/* Summary cards */
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; padding: 16px; }
.card { border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; background: #fff; }
.card.alert { border-color: var(--danger); background: var(--danger-bg); }
.card .pilot { font-size: 16px; font-weight: 700; color: var(--navy); }
.card .pilot .tag { font-size: 11px; font-weight: 600; padding: 2px 7px; border-radius: 999px; margin-left: 8px; vertical-align: middle; }
.tag.red { background: var(--danger); color: #fff; }
.tag.night { background: var(--night); color: #fff; }
.card .stat-row { display: flex; justify-content: space-between; margin-top: 8px; font-size: 13px; }
.card .stat-row .k { color: var(--muted); }
.card .stat-row .v { font-variant-numeric: tabular-nums; font-weight: 600; }
.card .v.bad { color: var(--danger); }

/* Tables */
.tbl-wrap { overflow: auto; max-height: 70vh; }
table.grid { border-collapse: collapse; width: 100%; font-size: 13px; }
table.grid th, table.grid td { padding: 7px 10px; border-bottom: 1px solid var(--line); text-align: left; white-space: nowrap; }
table.grid thead th {
  position: sticky; top: 0; background: var(--navy); color: #fff; z-index: 2;
  font-weight: 600; cursor: pointer; user-select: none;
}
table.grid thead th .arrow { opacity: .55; font-size: 10px; margin-left: 4px; }
table.grid tbody tr:hover { background: #f3f7fb; }
table.grid td.num { text-align: right; font-variant-numeric: tabular-nums; }
tr.sec-row td {
  background: var(--navy-2); color: #fff; font-weight: 700; text-transform: uppercase;
  letter-spacing: .6px; font-size: 12px; position: sticky; top: 33px; z-index: 1;
}
tr.short td { background: var(--danger-bg); }
tr.short:hover td { background: #fbe1e6; }
td.short-val { color: var(--danger); font-weight: 700; }
.pill { font-size: 11px; font-weight: 600; padding: 2px 7px; border-radius: 5px; }
.pill.ARR { background: #e3f0fb; color: #1d5b94; }
.pill.DEP { background: #e7f6f2; color: #1c7a6e; }
.pill.SHF { background: #fbf0e1; color: #9a6a1c; }
.pill.night { background: #ece7f6; color: var(--night); }

/* Timeline */
.timeline { padding: 12px 0 18px; }
.tl-toolbar { display: flex; align-items: center; gap: 16px; padding: 0 16px 12px; flex-wrap: wrap; }
.tl-toolbar .zoom { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--muted); flex-wrap: wrap; }
.legend i { display: inline-block; width: 12px; height: 12px; border-radius: 3px; margin-right: 5px; vertical-align: -2px; }
.tl-scroll { overflow-x: auto; overflow-y: hidden; border-top: 1px solid var(--line); }
.tl-inner { position: relative; }
.tl-axis { position: relative; height: 28px; border-bottom: 1px solid var(--line); background: #fafbfd; }
.tl-day { position: absolute; top: 0; height: 100%; border-left: 1px solid var(--line); padding-left: 5px; font-size: 11px; color: var(--muted); }
.tl-day.weekend { background: #f0f3f7; }
.tl-row { position: relative; border-bottom: 1px solid #eef2f6; }
.tl-row .lane { position: relative; }
.tl-row .rowlabel {
  position: sticky; left: 0; z-index: 5; width: 54px; display: flex; align-items: center;
  background: #fff; border-right: 1px solid var(--line); font-weight: 700; color: var(--navy);
  justify-content: center;
}
.tl-corner {
  position: sticky; left: 0; z-index: 6; height: 100%;
  background: #fafbfd; border-right: 1px solid var(--line);
}
.tl-grid { position: absolute; top: 0; bottom: 0; border-left: 1px solid #eef2f6; }
.tl-grid.weekend { background: #f6f8fb; }
.tl-band { position: relative; height: 22px; background: #e7edf4; border-bottom: 1px solid var(--line); border-top: 1px solid var(--line); }
.tl-band span {
  position: sticky; left: 0; display: inline-block; padding: 3px 10px;
  font-size: 11px; font-weight: 700; color: var(--navy); text-transform: uppercase; letter-spacing: .6px;
  background: #e7edf4;
}

.block {
  position: absolute; border-radius: 4px; background: var(--blue); color: #fff;
  font-size: 10px; line-height: 1; overflow: hidden; cursor: pointer;
  display: flex; align-items: center; padding: 0 4px; box-shadow: inset 0 0 0 1px rgba(255,255,255,.15);
}
.block.night { background: var(--night); }
.block.short { box-shadow: 0 0 0 2px var(--danger); z-index: 3; }
.restgap { position: absolute; height: 2px; background: repeating-linear-gradient(90deg, var(--danger) 0 4px, transparent 4px 8px); }

/* Tooltip */
.tooltip {
  position: fixed; z-index: 100; background: #11202e; color: #fff; padding: 8px 10px;
  border-radius: 7px; font-size: 12px; pointer-events: none; max-width: 280px;
  box-shadow: 0 4px 14px rgba(0,0,0,.3); line-height: 1.5;
}
.tooltip b { color: #8fd0c6; }
.tooltip .r { color: var(--warn); }

/* 2nd-pilot */
.tab-badge {
  display: inline-block; min-width: 17px; padding: 1px 5px; border-radius: 999px;
  background: var(--danger); color: #fff; font-size: 11px; font-weight: 700; text-align: center; vertical-align: 1px;
}
tr.need td { background: #fff7e6; }
tr.need:hover td { background: #fdeecb; }
.need-dot { color: var(--warn); font-size: 11px; }
.assign-sel { font: inherit; padding: 3px 6px; border: 1px solid var(--line); border-radius: 6px; background: #fff; }
.assign-cell { display: flex; align-items: center; gap: 10px; }
.assign-cell .link { font-size: 12px; white-space: nowrap; }
tr.dismissed-row td { color: var(--muted); opacity: .7; }
tr.dismissed-row td:nth-child(2) b { text-decoration: line-through; }
.pill.second { background: #e9e2f7; color: var(--night); }
tr.secondleg td:first-child { box-shadow: inset 3px 0 0 var(--night); }
.block.second { background: repeating-linear-gradient(45deg, var(--teal) 0 6px, #248a7e 6px 12px); }
.block.second.night { background: repeating-linear-gradient(45deg, var(--night) 0 6px, #4a3d75 6px 12px); }

/* Loading overlay */
.loading {
  position: fixed; inset: 0; z-index: 170; display: flex; align-items: center; justify-content: center;
  background: rgba(238,241,245,.72);
}
.loading-card {
  background: #fff; border: 1px solid var(--line); border-radius: 12px; padding: 24px 30px;
  box-shadow: var(--shadow); display: flex; flex-direction: column; align-items: center; gap: 12px;
}
.loading-card p { margin: 0; color: var(--navy); font-weight: 600; font-size: 14px; }
.spinner {
  width: 34px; height: 34px; border-radius: 50%;
  border: 4px solid #d9e0e8; border-top-color: var(--blue);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Job detail modal */
.modal-lg { width: 1360px; max-width: 96vw; }
/* Anchor to the top (with padding) so the header/controls stay put as the tide
 * CSV loads and the popup grows downward, instead of re-centering vertically. */
#job-modal { align-items: flex-start; padding-top: 60px; }
#job-modal .modal { max-height: calc(100vh - 120px); }
.spinner.sm { width: 18px; height: 18px; border-width: 3px; }
.jm-loading { display: flex; align-items: center; gap: 10px; color: var(--muted); padding: 14px 2px; font-size: 13px; }
tr.clickable { cursor: pointer; }
.jm-summary { display: flex; gap: 28px; flex-wrap: wrap; padding: 2px 0 12px; border-bottom: 1px solid var(--line); margin-bottom: 14px; }
.jm-summary > div { display: flex; flex-direction: column; gap: 2px; }
.jm-summary .k { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .4px; }
.jm-summary .v { font-weight: 600; font-variant-numeric: tabular-nums; color: var(--navy); }
.jm-summary .v.bad { color: var(--danger); }
.jm-leg { border: 1px solid var(--line); border-radius: 9px; padding: 12px 14px; margin-bottom: 12px; }
.jm-leg > h3 { margin: 0 0 8px; font-size: 14px; color: var(--navy); }
table.jm-breakdown { width: 100%; border-collapse: collapse; font-size: 13px; }
.jm-breakdown td { padding: 4px 6px; border-bottom: 1px dotted var(--line); }
.jm-breakdown td.num { text-align: right; font-variant-numeric: tabular-nums; }
.jm-breakdown tr.strong td { font-weight: 600; }
.jm-breakdown tr.total td { font-weight: 700; color: var(--navy); border-top: 2px solid var(--line); border-bottom: none; }
.jm-flat { margin: 10px 0; padding: 8px 10px; background: #f3f7fb; border: 1px solid var(--line); border-radius: 7px; }
.jm-flat .chk { font-size: 13px; font-weight: 500; }
.jm-flat-note { font-size: 11px; margin: 3px 0 0 24px; }
.jm-sp { margin-top: 10px; }
.jm-sp-label { font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.sp-info { font-size: 12px; margin-bottom: 5px; }
.sp-row { display: flex; gap: 12px; align-items: center; }
.jm-tide > h3 { font-size: 14px; color: var(--navy); margin: 16px 0 6px; }
.jm-tide-file { font-family: ui-monospace, monospace; font-size: 11px; color: var(--muted); margin: 8px 0 4px; }
.jm-tide-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 7px; max-height: 320px; }
table.tide-grid { font-size: 11px; white-space: nowrap; }
table.tide-grid th, table.tide-grid td { padding: 4px 8px; }
table.tide-grid tr.hl td { background: #fff6d6; font-weight: 600; }
table.tide-grid tr.hl:hover td { background: #ffeeb0; }

.empty { padding: 48px; text-align: center; color: var(--muted); }
.toolbar-search { margin-left: auto; }
.toolbar-search input { padding: 6px 10px; border: 1px solid var(--line); border-radius: 6px; font: inherit; width: 200px; }
.section-head { padding: 12px 16px; font-size: 13px; color: var(--muted); border-bottom: 1px solid var(--line); }
