:root {
  --bg: #f4f1ec;
  --card: #fff;
  --ink: #1d1d1f;
  --muted: #6b6b70;
  --accent: #e4572e;
  --line: #e3ded6;
  --ok: #2a9d4f;
  --warn: #c77700;
  --bad: #d62828;
}
* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
}
header { padding: 20px 16px 4px; max-width: 1100px; margin: 0 auto; }
h1 { margin: 0; font-size: 1.6rem; }
.tag { margin: 4px 0 0; color: var(--muted); }
main { max-width: 1100px; margin: 0 auto; padding: 8px 12px 40px; }
.card {
  background: var(--card);
  border-radius: 14px;
  padding: 16px;
  margin: 12px 0;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
}
h2 { margin: 0 0 12px; font-size: 1.15rem; display: flex; align-items: center; gap: 8px; }
.step {
  display: inline-grid; place-items: center;
  width: 26px; height: 26px; border-radius: 50%;
  background: var(--accent); color: #fff; font-size: 0.9rem;
}
.row { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; }
label { display: flex; flex-direction: column; font-size: 0.85rem; color: var(--muted); gap: 4px; }
label.check { flex-direction: row; align-items: center; color: var(--ink); font-size: 0.95rem; }
input, select, textarea {
  font: inherit; font-size: 16px;
  padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: #fff; color: var(--ink);
}
label input[type="number"] { width: 90px; }
label.grow { flex: 1; min-width: 200px; }
details { margin-top: 12px; }
summary { cursor: pointer; color: var(--muted); margin-bottom: 10px; }
textarea { width: 100%; margin: 10px 0 6px; }
button {
  font: inherit; cursor: pointer; border: none; border-radius: 8px;
  padding: 9px 14px; background: var(--accent); color: #fff;
}
button.secondary { background: #f0ebe4; color: var(--ink); }
button.ghost { background: transparent; color: var(--muted); }
button.big { width: 100%; padding: 14px; font-size: 1.1rem; font-weight: 600; margin-top: 12px; }
.buttons { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
.hint { color: var(--muted); font-size: 0.9rem; margin: 0 0 10px; }

table.roster { width: 100%; border-collapse: collapse; }
table.roster th { text-align: left; font-size: 0.8rem; color: var(--muted); font-weight: 500; padding: 0 4px 4px; }
table.roster td { padding: 3px 4px; }
table.roster input { width: 100%; }
table.roster .num { width: 52px; text-align: center; }
table.roster .min { width: 70px; text-align: center; }
table.roster .pos { width: 64px; padding: 8px 4px; }
table.roster td:nth-child(3) { width: 100%; }
.inout { width: 34px; height: 34px; padding: 0; border-radius: 8px; background: #f0ebe4; color: #fff; font-weight: 700; border: 2px solid #d9d3c9; }
.inout.on { background: var(--ok); border-color: var(--ok); }
table.roster tr.is-out td:not(.c-in) { opacity: 0.4; }
table.roster tr.is-out .name { text-decoration: line-through; }
.teambar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin: 0 0 10px; }
.teambar label { font-size: 0.85rem; color: var(--muted); }
.teambar select { flex: 1; min-width: 140px; max-width: 280px; }
.teambar button { margin: 0; }
.star { background: #f0ebe4; color: #c8c2b8; font-size: 1.1rem; padding: 6px 10px; }
.star.on { background: #fff3c4; color: #e0a800; }
.del { background: transparent; color: #b5b0a8; padding: 6px 8px; }

.status { margin-top: 12px; padding: 10px 12px; border-radius: 8px; font-size: 0.92rem; }
.status.ok { background: #eaf6ee; color: var(--ok); }
.status.warn { background: #fff4e0; color: var(--warn); }
.error { margin-top: 10px; background: #fdeaea; color: var(--bad); padding: 10px; border-radius: 8px; }

.result-head { display: flex; justify-content: space-between; flex-wrap: wrap; align-items: center; gap: 8px; }
.result-head h2 { margin: 0; }
.result-head .buttons { margin: 0; }
.chart-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; margin-top: 8px; }
table.chart { border-collapse: separate; border-spacing: 0; font-size: 0.8rem; }
.chart th, .chart td { padding: 0; text-align: center; }
.chart th.pname {
  position: sticky; left: 0; z-index: 1; background: var(--card);
  text-align: left; padding: 0 10px 0 0; white-space: nowrap; font-weight: 500; max-width: 140px; overflow: hidden; text-overflow: ellipsis;
}
.chart th.per { font-weight: 700; padding: 2px 0; border-left: 2px solid var(--ink); }
.chart th.clk { color: var(--muted); font-weight: 400; font-size: 0.7rem; min-width: 20px; }
.chart td.cell {
  height: 26px; min-width: 20px; cursor: pointer;
  background: #f5f2ed; border-right: 1px solid #fff; border-bottom: 2px solid #fff;
}
.chart td.cell.on { border-right-color: rgba(255, 255, 255, 0.35); }
.chart .pstart { border-left: 2px solid var(--ink) !important; }
.chart td.tot, .chart th.tot { padding: 0 6px; font-weight: 700; min-width: 44px; }
.chart td.tot small { font-weight: 400; color: var(--muted); }
.chart tr.count td { color: var(--muted); font-size: 0.72rem; padding-top: 3px; }
.chart .bad { color: var(--bad) !important; font-weight: 700; }

.subs-title { margin-top: 22px; }
.subs { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 12px; }
.period { border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; break-inside: avoid; }
.period h3 { margin: 0 0 6px; }
.period .lineup { font-size: 0.9rem; margin-bottom: 6px; }
.period ul { list-style: none; padding: 0; margin: 0; }
.period li { padding: 6px 0; border-top: 1px dashed var(--line); font-size: 0.9rem; display: grid; grid-template-columns: 46px 1fr; gap: 2px 8px; }
.period .time { font-weight: 700; grid-row: span 2; font-variant-numeric: tabular-nums; }
.period .in { color: var(--ok); }
.period .out { color: var(--bad); }

footer { text-align: center; color: var(--muted); font-size: 0.8rem; padding: 0 0 24px; }
.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 20px; font-size: 0.9rem;
}

/* Lineup view */
.lu-cards { display: flex; flex-direction: column; gap: 14px; margin-top: 8px; }
.lu-card {
  display: grid; grid-template-columns: 1fr 1fr;
  border: 1px solid var(--line); border-radius: 14px; padding: 16px 18px; background: #fff;
}
.lu-card:has(.lu-period:only-child) { grid-template-columns: 1fr; }
.lu-period { padding-right: 20px; min-width: 0; }
.lu-period + .lu-period { padding: 0 0 0 20px; border-left: 1px solid var(--line); }
.lu-period h3 { margin: 0 0 8px; font-size: 0.95rem; }
.lu-axis { position: relative; height: 20px; margin: 0 6px 4px 34px; font-size: 0.65rem; color: var(--muted); }
.lu-axis span { position: absolute; top: 0; transform: translateX(-50%); white-space: nowrap; }
.lu-axis span::after { content: ""; position: absolute; left: 50%; top: 14px; width: 1px; height: 5px; background: #c9c4bb; }
.lu-row { display: flex; align-items: center; height: 32px; margin-bottom: 9px; }
.lu-row:last-child { margin-bottom: 0; }
.lu-pos { width: 34px; flex: none; font-size: 0.8rem; color: var(--muted); }
.lu-bar { position: relative; flex: 1; display: flex; height: 100%; border-radius: 6px; overflow: hidden; min-width: 0; touch-action: pan-y; }
.lu-handle {
  position: absolute; top: 0; bottom: 0; width: 22px; margin-left: -11px; cursor: ew-resize; touch-action: none; z-index: 2;
}
.lu-handle::after {
  content: ""; position: absolute; left: 50%; top: 4px; bottom: 4px; width: 4px; margin-left: -2px;
  border-radius: 2px; background: rgba(255, 255, 255, 0.95); box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.18);
}
.lu-handle:hover::after, .lu-handle:active::after { background: #fff; box-shadow: 0 0 0 2px var(--ink); }
.lu-seg {
  border: 0; border-radius: 0; padding: 0 5px; min-width: 0; height: 100%;
  display: flex; align-items: center; box-shadow: inset 1px 0 rgba(0, 0, 0, 0.12);
  -webkit-print-color-adjust: exact; print-color-adjust: exact;
}
.lu-seg:first-child { box-shadow: none; }
.lu-seg:hover { filter: brightness(1.07); }
.lu-seg .chip {
  background: rgba(255, 255, 255, 0.88); color: var(--ink); font-size: 0.78rem; font-weight: 600;
  padding: 2px 6px; border-radius: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%;
}
@media (max-width: 760px) {
  .lu-card { grid-template-columns: 1fr; padding: 14px 12px; }
  .lu-period { padding: 0; }
  .lu-period + .lu-period { padding: 14px 0 0; margin-top: 14px; border-left: 0; border-top: 1px solid var(--line); }
  .lu-axis span.odd { visibility: hidden; }
  .lu-seg .chip { font-size: 0.72rem; padding: 2px 4px; }
}

.minutes { display: grid; grid-template-columns: repeat(auto-fill, minmax(255px, 1fr)); gap: 6px 12px; margin-top: 14px; }
.mrow { display: flex; align-items: center; gap: 6px; background: #f6f4ef; border-radius: 10px; padding: 4px 6px 4px 10px; border: 2px solid transparent; }
.mrow i { width: 12px; height: 12px; border-radius: 50%; flex: none; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
.mrow .nm { flex: 1; font-weight: 600; min-width: 3.5em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mrow .pin { background: transparent; padding: 4px; opacity: 0.25; filter: grayscale(1); margin: 0; }
.mrow.pinned { border-color: #e0a800; background: #fffaf0; }
.mrow.pinned .pin { opacity: 1; filter: none; }
.mrow .mstep { width: 36px; height: 36px; padding: 0; font-size: 1.2rem; font-weight: 700; margin: 0; border-radius: 8px; }
.mrow .mval { width: 52px; text-align: center; font-weight: 700; font-size: 1rem; padding: 6px 2px; }
.mchip { display: inline-flex; align-items: center; gap: 6px; background: #f1efe9; border-radius: 14px; padding: 4px 10px; font-size: 0.85rem; }
.mchip i { width: 10px; height: 10px; border-radius: 50%; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
.mchip.off { background: #fdeaea; color: var(--bad); }
.timeline { margin-top: 18px; }
.period li > div div + div { margin-top: 2px; }
.period li small, .period .lineup small { color: var(--muted); }

/* Edit dialog */
dialog { border: 0; border-radius: 16px; padding: 0; width: min(440px, 94vw); max-height: 88vh; box-shadow: 0 10px 40px rgba(0, 0, 0, 0.25); }
dialog::backdrop { background: rgba(20, 20, 25, 0.45); }
.dlg { padding: 16px; }
.dlg-head { display: flex; justify-content: space-between; align-items: center; }
.dlg-head h3 { margin: 0; }
.dlg-now { margin: 4px 0 12px; color: var(--muted); }
.dlg-players { display: flex; flex-direction: column; gap: 6px; max-height: 50vh; overflow-y: auto; }
.dlg-player {
  display: flex; align-items: center; gap: 10px; text-align: left;
  background: #f6f3ee; color: var(--ink); padding: 10px 12px;
}
.dlg-player:hover { background: #ece7df; }
.dlg-player i { width: 14px; height: 14px; border-radius: 50%; flex: none; }
.dlg-player .nm { flex: 1; font-weight: 600; }
.dlg-player .nm small { color: var(--muted); font-weight: 400; }
.dlg-player .meta { font-size: 0.82rem; color: var(--muted); white-space: nowrap; }
.dlg-player em { color: var(--warn); font-style: normal; }
.need { color: var(--ok); }
.over { color: var(--bad); }

@media print {
  body { background: #fff; }
  header, #setup, #roster, footer, .noprint, .result-head .buttons, .toast, dialog { display: none !important; }
  .lu-card { break-inside: avoid; }
  .card { box-shadow: none; padding: 0; }
  .chart-wrap { overflow: visible; }
  .chart td.cell { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  @page { size: landscape; margin: 10mm; }
}

@media (max-width: 600px) {
  table.roster th:nth-child(2), table.roster td:nth-child(2) { display: none; }
  table.roster td { padding: 3px 2px; }
  table.roster .min { width: 46px; padding-left: 2px; padding-right: 2px; }
  table.roster .pos { width: 54px; }
  .inout { width: 30px; height: 30px; }
  .star { padding: 6px 6px; }
  .del { padding: 6px 2px; }
}
