/* ============================================================================
   SAT — Material-style, mobile-friendly styling (pure CSS, no framework).
   Fluent from 320px up. Uses dynamic viewport (dvh) + iOS safe areas, ≥44px
   touch targets, and non-janky opacity/transform transitions.
   ========================================================================== */

:root {
  --bg: #f0f1f5;
  --panel: #ffffff;
  --border: #e0e3e8;
  --text: #1b2430;
  --muted: #687386;
  --primary: #2b6cb0;
  --primary-hover: #245a94;
  --primary-press: #1d4d7c;
  --outline: #6b7687;
  --danger: #c0392b;
  --blue-fill: #ddebf7;
  --gray-fill: #e7e6e6;
  --orange-fill: #fce4d6;
  --peach-fill: #fbe5d6;
  --green-fill: #e2efda;
  --ring: rgba(43, 108, 176, 0.28);
  /* Material elevation (1dp card + 3dp raised) */
  --elev-1: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.16);
  --elev-2: 0 3px 6px rgba(0, 0, 0, 0.16), 0 3px 6px rgba(0, 0, 0, 0.2);
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: Roboto, -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

.app {
  display: flex;
  min-height: 100dvh;
}

/* ----------------------- Sidebar ----------------------- */
.sidebar {
  width: 320px;
  flex: 0 0 320px;
  background: var(--panel);
  border-right: 1px solid var(--border);
  padding: 20px 16px calc(20px + env(safe-area-inset-bottom, 0px));
  overflow-y: auto;
  max-height: 100dvh;
  position: sticky;
  top: 0;
  overscroll-behavior: contain;
}

.sidebar h2 { font-size: 18px; margin: 0 0 14px; color: var(--text); font-weight: 600; }

fieldset {
  border: 1px solid var(--border);
  background: var(--panel);
  border-radius: 8px;
  padding: 14px 12px 12px;
  margin: 0 0 14px;
  box-shadow: var(--elev-1);
}
legend { font-weight: 600; font-size: 13px; padding: 0 6px; color: #33404f; letter-spacing: 0.2px; }

label { display: block; font-size: 13px; font-weight: 500; margin: 12px 0 4px; color: #374151; }

input[type="date"],
input[type="text"],
select,
textarea {
  width: 100%;
  padding: 11px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 16px; /* ≥16px prevents iOS auto-zoom on focus */
  background: #fff;
  color: var(--text);
  font-family: inherit;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
textarea { resize: vertical; font-family: inherit; }
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--ring);
}
input::placeholder { color: #9aa3b2; }

.hint { font-size: 12px; color: var(--muted); margin: 6px 0 0; }
.error { color: var(--danger); font-size: 13px; margin: 6px 0 0; min-height: 1em; }

/* ----------------------- Buttons (Material) ----------------------- */
button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  border-radius: 8px;
  font-weight: 600;
  /* ≥44px hit area for touch */
  min-height: 44px;
  min-width: 44px;
  padding: 10px 16px;
  font-size: 15px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: background-color 0.15s ease, box-shadow 0.15s ease, transform 0.06s ease, opacity 0.15s ease;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}
button:active { transform: translateY(1px) scale(0.99); }

button.primary {
  background: var(--primary);
  color: #fff;
  box-shadow: var(--elev-2);
}
button.primary:hover { background: var(--primary-hover); }
button.primary:active { background: var(--primary-press); box-shadow: var(--elev-1); }

button.outline {
  background: transparent;
  color: var(--primary);
  border: 2px solid var(--primary);
}
button.outline:hover { background: rgba(43, 108, 176, 0.08); }
button.outline:active { background: rgba(43, 108, 176, 0.16); }

button.icon-btn {
  min-width: 44px;
  min-height: 44px;
  padding: 0;
  background: transparent;
  border-radius: 50%;
  color: var(--muted);
  font-size: 18px;
}
button.icon-btn:hover { background: rgba(0, 0, 0, 0.06); color: var(--text); }

button:disabled { box-shadow: none; cursor: not-allowed; opacity: 0.6; transform: none; }
button.primary:disabled { background: #c4cbd4; }
button.outline:disabled { background: transparent; color: #aab2bf; border-color: #e0e3e8; }

button.primary.big { padding: 13px 18px; font-size: 16px; }
button.outline.big { padding: 11px 18px; font-size: 16px; }

/* Compact bulk Lock all / Unlock all toggle above the Shift Counts table. */
.counts-bulk { justify-content: flex-end; margin: 6px 0 4px; }
.counts-bulk button.small { flex: 0 0 auto; padding: 5px 12px; font-size: 13px; }

.btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}
.btn-row button { flex: 1 1 auto; }

button.del {
  background: transparent;
  color: var(--muted);
  font-size: 16px;
  padding: 0;
  border-radius: 50%;
}
button.del:hover { background: rgba(192, 57, 43, 0.1); color: var(--danger); }

/* ----------------------- Records list ----------------------- */
.record {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 8px 8px 12px;
  margin-top: 8px;
  font-size: 13px;
}
.record .muted { margin: 0; }

/* ----------------------- Main ----------------------- */
.main {
  flex: 1;
  padding: 28px 36px calc(28px + env(safe-area-inset-bottom, 0px));
  max-width: 1280px;
  min-width: 0; /* allow the matrix to scroll instead of blowing the layout */
}
.main header { margin-bottom: 22px; }
.main h1 { font-size: 24px; margin: 0 0 4px; font-weight: 600; }
.subtitle { color: var(--muted); margin: 0; font-size: 14px; }

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--elev-1);
  padding: 18px 20px;
  margin-bottom: 20px;
}
.panel h3 { margin: 0 0 12px; font-size: 16px; font-weight: 600; }

.hidden { display: none !important; }

.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }

table.preview, table.counts, table.violation {
  border-collapse: collapse;
  width: 100%;
  font-size: 13px;
}
table.violation td:nth-child(3) {
  /* Issue text is prose — left-align so it reads like a log entry. */
  text-align: left;
  white-space: normal;
  min-width: 18em;
}
table.violation td.muted { color: var(--muted); text-align: center; }
th {
  background: var(--gray-fill);
  border: 1px solid var(--border);
  padding: 8px 10px;
  text-align: center;
  font-weight: 600;
  position: sticky;
  top: 0;
  z-index: 1;
}
td {
  border: 1px solid var(--border);
  padding: 7px 10px;
  text-align: center;
  white-space: nowrap;
}
td.date-cell { font-weight: 600; text-align: left; }
tr.weekend-row td.date-cell { background: var(--peach-fill); }

/* Shift Counts footer: per-column Total and Average */
tr.counts-total td {
  border-top: 2px solid var(--border);
  font-weight: 700;
  background: var(--gray-fill);
}
tr.counts-avg td {
  color: var(--muted);
}
tr.counts-total td:first-child,
tr.counts-avg td:first-child {
  font-weight: 600;
  text-align: left;
  color: inherit;
}
td.avail-cell { background: var(--orange-fill); }

/* ----------------------- Month strip / workspace --------------------- */
.month-strip {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
}
.month-strip.no-pad { padding: 10px 14px; }
.month-chip {
  min-height: 36px;
  padding: 6px 14px;
  font-size: 13px;
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text);
  border-radius: 18px;
  font-weight: 600;
}
.month-chip:hover { background: var(--blue-fill); }
.month-chip.active {
  background: var(--primary);
  color: #fff;
  box-shadow: var(--elev-1);
}

/* ----------------------- Editable count-target cells ------------------ */
table.counts td.counts-name { font-weight: 600; text-align: left; min-width: 6em; }
td.counts-cell { padding: 2px 3px; }
span.counts-edit {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 2px;
  position: relative;
  min-width: 68px;
}
input.counts-input {
  width: 44px;
  padding: 5px 6px;
  font-size: 14px;
  text-align: center;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  min-height: 30px;
}
input.counts-input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 2px var(--ring);
}
span.counts-edit.locked {
  background: var(--gray-fill);
  border-radius: 6px;
  padding: 1px 2px;
}
span.counts-edit.locked input.counts-input {
  background: var(--gray-fill);
  color: #555;
  font-weight: 600;
}
button.counts-lock {
  min-width: 24px;
  min-height: 24px;
  width: 24px;
  height: 24px;
  padding: 0;
  font-size: 12px;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
}
button.counts-lock:hover { background: rgba(0, 0, 0, 0.08); color: var(--text); }

/* Inline feedback under the counts table */
p.counts-note { min-height: 1em; font-size: 13px; margin: 4px 0 6px; }
p.counts-note.ok { color: var(--primary); }
p.counts-note.error { color: var(--danger); font-weight: 600; }

/* ----------------------- Balance / Carried rows --------------------- */
tr.counts-balance td, tr.counts-carried td {
  font-size: 12px;
  color: var(--muted);
  background: #fbfcfe;
}
td.counts-balance-label, td.counts-carried-label {
  text-align: left;
  font-weight: 600;
  white-space: nowrap;
}
tr.counts-balance td.pos { color: #b7791f; font-weight: 600; }
tr.counts-balance td.neg { color: #2f855a; font-weight: 600; }
tr.counts-carried td { color: var(--text); background: #f1f5f9; }
td.counts-carried-label { font-weight: 700; }

/* Manual deployment override: bold text on the usual deployment cell fill. */
td.deploy-manual { font-weight: 700; }

/* Manually editable shift cells in the Preview table. Hover hints that the
   cell can be clicked to open an inline dropdown. */
td.cell-edit { cursor: pointer; }
td.cell-edit:hover { background: var(--gray-fill); }
td.cell-edit select.cell-edit-select {
  width: 100%;
  min-width: 7em;
  padding: 2px 4px;
  font: inherit;
  border: 1px solid var(--border);
  border-radius: 3px;
  background: #fff;
}

/* Forced-tiring day: the counts-fairness rule left no rest-compliant alternative,
   so this staff must bear a consecutive / double / overload pattern. Red. */
td.flag-cell { background: #f8d7da; color: #842029; font-weight: 700; }
td.flag-date { background: #f8d7da; color: #842029; }
tr.weekend-row td.flag-date { background: #f8d7da; color: #842029; }
table.matrix td.matrix-flag { background: #f8d7da; color: #842029; font-weight: 700; }
.sw-flag { background: #f8d7da; border-color: #842029; }

/* Rule-violation (warn, don't forbid): a cell the user edited (or a hard lock)
   that breaks a scheduling rule. Amber, distinct from the red forced-tiring
   flag; the tooltip lists exactly which rule(s). The edit is honored anyway. */
td.rule-viol { background: #fff3cd; color: #664d03; font-weight: 600; }
tr.weekend-row td.rule-viol { background: #fff3cd; color: #664d03; }
table.matrix td.matrix-rule-viol { background: #fff3cd; color: #664d03; font-weight: 600; }

/* ----------------------- Availability matrix ----------------------- */
.matrix-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 16px;
  font-size: 12px;
  color: var(--text);
  margin: 0 0 12px;
}
.matrix-legend span { display: inline-flex; align-items: center; gap: 6px; }
.matrix-legend .sw {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 1px solid var(--border);
  border-radius: 4px;
  flex: none;
}
.sw-unavail { background: var(--orange-fill); }
.sw-morning { background: var(--blue-fill); }
.sw-deploy { background: var(--gray-fill); }
.sw-weekend { background: var(--peach-fill); }
.sw-avail { background: var(--green-fill); }
.sw-manual { font-weight: 700; }

table.matrix {
  border-collapse: collapse;
  width: max-content;
  min-width: 100%;
  font-size: 11px;
}
table.matrix th, table.matrix td {
  border: 1px solid var(--border);
  padding: 4px 6px;
  text-align: center;
  white-space: nowrap;
  min-width: 44px;
}
table.matrix .matrix-corner {
  position: sticky;
  left: 0;
  z-index: 3;
  background: var(--gray-fill);
  text-align: left;
  min-width: 112px;
  padding: 8px;
}
table.matrix th.matrix-date {
  position: sticky;
  top: 0;
  z-index: 2;
  font-weight: 600;
  line-height: 1.25;
  white-space: pre-line;
  vertical-align: bottom;
}
table.matrix th.matrix-weekend { background: var(--peach-fill); }
table.matrix th.matrix-date.matrix-weekend,
table.matrix .matrix-weekend { color: #8a3b12; }
table.matrix th.matrix-name {
  position: sticky;
  left: 0;
  z-index: 2;
  background: var(--gray-fill);
  text-align: left;
  min-width: 112px;
  font-weight: 600;
  padding: 8px;
}
table.matrix .matrix-cell { background: var(--green-fill); }
table.matrix .matrix-morning { background: var(--blue-fill); }
table.matrix .matrix-morning-manual { background: var(--blue-fill); font-weight: 700; }
table.matrix .matrix-deploy { background: var(--gray-fill); }
table.matrix .matrix-deploy-manual { background: var(--gray-fill); font-weight: 700; }
table.matrix .matrix-weekend-duty { background: var(--peach-fill); }
table.matrix .matrix-weekend-manual { background: var(--peach-fill); font-weight: 700; }
table.matrix .matrix-unavail { background: var(--orange-fill); }

/* ----------------------- Excel preview ----------------------- */
.xls-preview-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 10px;
}
.xls-preview-head h3 { margin: 0; }

.xls-preview-wrap {
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  background: #fff;
  height: min(62vh, 560px);
}
.xls-preview-wrap iframe {
  display: block;
  width: 100%;
  height: 100%;
  border: none;
  background: #fff;
}

.export-panel .hint { margin-top: 10px; }

/* ----------------------- Responsive ----------------------- */
@media (max-width: 820px) {
  .app { flex-direction: column; }
  .sidebar {
    width: 100%;
    flex: none;
    max-height: none;
    position: static;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }
  .main { padding: 20px 12px calc(20px + env(safe-area-inset-bottom, 0px)); }
  .btn-row { flex-direction: column; }
  .btn-row button { width: 100%; }
  .xls-preview-wrap { height: 60dvh; }
}

/* Extra-tight phones (320px): keep the sticky staff column + horizontal scroll
   intact so nothing overlaps. */
@media (max-width: 380px) {
  .sidebar h2 { font-size: 16px; }
  h1 { font-size: 20px; }
  fieldset { padding: 12px 10px 10px; }
  .panel { padding: 14px 12px; }
  table.matrix th, table.matrix td { min-width: 40px; padding: 3px 5px; }
}