:root {
  color-scheme: light dark;
  --bg: #eef1f5;
  --surface: #ffffff;
  --surface-2: #f6f8fa;
  --text: #1c2733;
  --muted: #5d6b7b;
  --border: #d3dae3;
  --accent: #0b63c4;
  --accent-text: #ffffff;
  --header: #0f2d4a;
  --header-text: #f3f7fb;

  --paper: #fffdf6;
  --grid: #c9d0da;
  --grid-strong: #8f9bab;
  --ink: #1c2733;
  --rel: #d9480f;
  --own: #1864ab;
  --tgt: #343a40;
  --cpa: #0b7285;
  --avoid: #a61e75;
  --port: #e03131;
  --stbd: #2b8a3e;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1218;
    --surface: #161d26;
    --surface-2: #1c2530;
    --text: #e6edf3;
    --muted: #9aa7b6;
    --border: #2c3846;
    --accent: #4c9ff0;
    --accent-text: #08131f;
    --header: #0a1a2b;
    --header-text: #e6edf3;

    --paper: #0f161e;
    --grid: #2b3948;
    --grid-strong: #55677b;
    --ink: #e6edf3;
    --rel: #ff922b;
    --own: #4dabf7;
    --tgt: #f1f3f5;
    --cpa: #3bc9db;
    --avoid: #f783c1;
    --port: #ff6b6b;
    --stbd: #51cf66;
  }
}

* {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Hiragino Sans", "Noto Sans JP", "Yu Gothic UI", "Meiryo", sans-serif;
  font-size: 15px;
  line-height: 1.6;
  font-variant-numeric: tabular-nums;
}

h1,
h2 {
  margin: 0;
  line-height: 1.3;
}

button,
input,
select {
  font: inherit;
  color: inherit;
}

button {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 6px;
  padding: 6px 12px;
  cursor: pointer;
}

button:hover:not(:disabled) {
  border-color: var(--accent);
}

button:disabled {
  opacity: 0.45;
  cursor: default;
}

button.quiet {
  background: transparent;
  color: var(--muted);
}

button.icon {
  padding: 2px 8px;
  line-height: 1.4;
  color: var(--muted);
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* ---------------------------------------------------------------- header */

.top {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px 24px;
  padding: 12px 20px;
  background: var(--header);
  color: var(--header-text);
}

.brand h1 {
  font-size: 19px;
  letter-spacing: 0.02em;
}

.brand p {
  margin: 2px 0 0;
  font-size: 13px;
  opacity: 0.82;
}

.top-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.top-actions button,
.top-actions select {
  background: rgb(255 255 255 / 0.1);
  border: 1px solid rgb(255 255 255 / 0.28);
  color: var(--header-text);
  border-radius: 6px;
  padding: 6px 10px;
}

.top-actions select {
  max-width: min(72vw, 330px);
}

.top-actions option {
  color: #1c2733;
  background: #ffffff;
}

.preset {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
}

/* ---------------------------------------------------------------- layout */

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 400px;
  grid-template-areas:
    "board inputs"
    "board results"
    "board detail";
  grid-template-rows: auto auto 1fr;
  gap: 16px;
  max-width: 1320px;
  margin: 0 auto;
  padding: 16px 20px 8px;
  align-items: start;
}

.board {
  grid-area: board;
  position: sticky;
  top: 12px;
  /* keep the sheet, its tools and the steps inside the window height */
  width: min(100%, max(380px, calc(100vh - 262px)));
  margin: 0 auto;
}

.inputs {
  grid-area: inputs;
}

.results {
  grid-area: results;
}

.detail {
  grid-area: detail;
}

.inputs,
.results,
.detail,
.steps {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
}

.results,
.detail {
  padding: 12px 14px 14px;
}

.results h2,
.detail h2 {
  font-size: 15px;
  margin-bottom: 8px;
}

.results-target {
  font-weight: 500;
  color: var(--muted);
  margin-left: 6px;
}

@media (max-width: 980px) {
  .layout {
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas:
      "inputs"
      "board"
      "results"
      "detail";
    padding: 12px 12px 4px;
    gap: 12px;
  }

  .board {
    position: static;
    width: 100%;
  }
}

/* ---------------------------------------------------------------- sheet */

.sheet-frame {
  width: 100%;
  aspect-ratio: 1;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
}

.sheet-tools {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 8px;
}

.sheet-tools .tip {
  color: var(--muted);
  font-size: 12.5px;
}

.detail-frame {
  width: 100%;
  aspect-ratio: 1;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  background: var(--paper);
}

.detail.empty .detail-frame,
.detail.empty .legend {
  display: none;
}

.detail.empty::after {
  content: "手順 4「速力三角形」まで進むと、ここに拡大図が出ます。";
  color: var(--muted);
  font-size: 13px;
}

svg.sheet {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: pan-y;
  user-select: none;
  -webkit-user-select: none;
  cursor: grab;
}

svg.sheet.zoomed {
  touch-action: none;
}

svg.sheet.static {
  cursor: default;
  touch-action: auto;
}

svg.sheet text {
  font-family: inherit;
}

.zoom {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
}

.zoom button {
  padding: 3px 10px;
  font-size: 13px;
  white-space: nowrap;
}

.paper {
  fill: var(--paper);
}

.ring {
  fill: none;
  stroke: var(--grid);
  stroke-width: 1;
}

.ring.minor {
  stroke-dasharray: 1.5 3.5;
}

.ring.outer {
  stroke: var(--grid-strong);
  stroke-width: 1.4;
}

.radial {
  stroke: var(--grid);
  stroke-width: 0.6;
  stroke-dasharray: 1.5 4.5;
}

.radial.strong {
  stroke-dasharray: none;
  stroke-width: 0.7;
}

.radial.axis {
  stroke: var(--grid-strong);
  stroke-dasharray: none;
  stroke-width: 0.9;
}

.tick {
  stroke: var(--grid-strong);
  stroke-width: 1;
}

.tick-label {
  fill: var(--muted);
  font-size: 10.5px;
  text-anchor: middle;
  dominant-baseline: central;
}

.range-label {
  fill: var(--muted);
  font-size: 9.5px;
}

/* the same rings read in knots, for the speed vectors */
.range-label.knots {
  fill: var(--own);
  font-weight: 700;
  paint-order: stroke;
  stroke: var(--paper);
  stroke-width: 3px;
  stroke-linejoin: round;
}

.centre-mark {
  fill: none;
  stroke: var(--grid-strong);
  stroke-width: 1;
}

.ln {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
}

.ln.bold {
  stroke-width: 2.7;
}

.ln.thin {
  stroke-width: 1.1;
}

.ln.dash {
  stroke-dasharray: 7 5;
}

.ln.dots {
  stroke-dasharray: 1.5 4.5;
}

.ln.hollow {
  fill: var(--paper);
  stroke-width: 1.9;
}

.hd {
  fill: currentColor;
  stroke: none;
}

.dt {
  fill: currentColor;
  stroke: var(--paper);
  stroke-width: 1;
}

.lbl {
  fill: currentColor;
  font-size: 12px;
  font-weight: 600;
  paint-order: stroke;
  stroke: var(--paper);
  stroke-width: 3.5px;
  stroke-linejoin: round;
}

.lbl.small {
  font-size: 10.5px;
  font-weight: 500;
}

.lbl.big {
  font-size: 14px;
  font-weight: 700;
}

.lbl.tag {
  font-size: 13px;
}

.ink {
  color: var(--ink);
}

.rel {
  color: var(--rel);
}

.own {
  color: var(--own);
}

.tgt {
  color: var(--tgt);
}

.cpa {
  color: var(--cpa);
}

.avoid {
  color: var(--avoid);
}

.port {
  color: var(--port);
}

.stbd {
  color: var(--stbd);
}

.dim {
  opacity: 0.42;
}

.own-ship polygon {
  fill: var(--own);
  stroke: var(--paper);
  stroke-width: 1;
}

.ship .port-side {
  fill: var(--port);
}

.ship .stbd-side {
  fill: var(--stbd);
}

.ship .hull {
  fill: none;
  stroke: var(--ink);
  stroke-width: 1;
}

/* ---------------------------------------------------------------- steps */

.steps {
  margin-top: 12px;
  padding: 10px 12px 12px;
}

.step-row {
  display: flex;
  align-items: stretch;
  gap: 8px;
}

.step-tabs {
  display: flex;
  flex: 1;
  gap: 4px;
  list-style: none;
  margin: 0;
  padding: 0;
  min-width: 0;
}

.step-tabs li {
  flex: 1;
  min-width: 0;
}

.step-tabs button {
  width: 100%;
  height: 100%;
  padding: 5px 2px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  border-radius: 6px;
  background: var(--surface-2);
}

.step-tabs .num {
  font-weight: 700;
  font-size: 13px;
  line-height: 1.2;
}

.step-tabs .name {
  font-size: 11px;
  line-height: 1.25;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

.step-tabs button.done {
  border-color: color-mix(in srgb, var(--accent) 45%, var(--border));
}

.step-tabs button.on {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}

.step-tabs button.on .name {
  color: inherit;
}

.step-row > button {
  white-space: nowrap;
  padding: 6px 10px;
}

.step-text {
  margin: 10px 2px 0;
  font-size: 14px;
}

@media (max-width: 560px) {
  .sheet-tools .tip {
    display: none;
  }

  .sheet-tools {
    justify-content: flex-end;
  }

  .step-row > button {
    padding: 6px 7px;
    font-size: 13px;
  }

  .step-tabs .name {
    font-size: 9.5px;
  }
}

/* ---------------------------------------------------------------- form */

form {
  padding: 4px 14px 12px;
}

fieldset {
  border: 0;
  border-top: 1px solid var(--border);
  margin: 0;
  padding: 10px 0 12px;
  min-width: 0;
}

fieldset:first-child {
  border-top: 0;
}

legend {
  padding: 0;
  font-weight: 700;
  font-size: 14px;
  float: left;
  width: 100%;
  margin-bottom: 6px;
}

legend + * {
  clear: both;
}

label {
  display: flex;
  flex-direction: column;
  gap: 3px;
  font-size: 13px;
  color: var(--muted);
}

input:not([type="checkbox"]),
select {
  width: 100%;
  min-width: 0;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface-2);
  color: var(--text);
  font-size: 16px;
}

select {
  font-size: 14px;
}

input::placeholder {
  color: color-mix(in srgb, var(--muted) 55%, transparent);
}

.pair {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.unit-field {
  display: flex;
  align-items: center;
  gap: 6px;
}

.unit-field span {
  white-space: nowrap;
  color: var(--muted);
}

label.check {
  flex-direction: row;
  align-items: center;
  gap: 8px;
  color: var(--text);
  font-size: 14px;
  margin-top: 6px;
}

.target-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 8px;
}

.target-tabs button {
  padding: 4px 12px;
  font-size: 13px;
}

.target-tabs button.on {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}

table.obs {
  width: 100%;
  border-collapse: collapse;
}

table.obs th {
  font-size: 12px;
  font-weight: 500;
  color: var(--muted);
  text-align: left;
  padding: 2px 4px 4px 0;
  white-space: nowrap;
}

table.obs td {
  padding: 3px 6px 3px 0;
}

table.obs td:last-child {
  padding-right: 0;
  width: 1%;
}

table.obs tbody th {
  font-weight: 700;
  color: var(--text);
  font-size: 13px;
  padding-right: 8px;
}

.row-actions {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin-top: 8px;
}

.row-actions button {
  font-size: 13px;
  padding: 4px 10px;
}

.avoid-fields {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 10px;
}

.avoid-fields label:nth-child(2) {
  grid-column: 1 / -1;
}

.avoid-fields.off {
  opacity: 0.5;
}

[hidden] {
  display: none !important;
}

/* ---------------------------------------------------------------- results */

table.res {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

table.res th,
table.res td {
  text-align: left;
  vertical-align: top;
  padding: 7px 0;
  border-top: 1px solid var(--border);
}

table.res tr:first-child th,
table.res tr:first-child td {
  border-top: 0;
}

table.res th {
  width: 42%;
  padding-right: 10px;
  font-weight: 500;
  color: var(--muted);
}

table.res b {
  font-size: 15px;
}

table.res small {
  display: block;
  color: var(--muted);
  font-size: 12.5px;
  line-height: 1.5;
}

.res .port {
  color: var(--port);
}

.res .stbd {
  color: var(--stbd);
}

.locked {
  color: var(--muted);
  font-size: 13px;
}

.warn {
  color: var(--rel);
}

.hint {
  margin: 0;
  color: var(--muted);
  font-size: 14px;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 14px;
  list-style: none;
  margin: 8px 0 0;
  padding: 0;
  font-size: 12.5px;
  color: var(--muted);
}

.legend li {
  display: flex;
  align-items: center;
  gap: 6px;
}

.swatch {
  width: 18px;
  height: 3px;
  border-radius: 2px;
  background: currentColor;
}

/* ---------------------------------------------------------------- footer */

.foot {
  max-width: 1320px;
  margin: 0 auto;
  padding: 8px 20px 28px;
  color: var(--muted);
  font-size: 12.5px;
}

.foot p {
  margin: 6px 0 0;
}

/* ---------------------------------------------------------------- print mode */

.print-view {
  display: none;
}

body.print-mode > :not(.print-view) {
  display: none;
}

body.print-mode .print-view {
  display: block;
}

.print-bar {
  position: sticky;
  top: 0;
  z-index: 2;
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 8px 14px;
  padding: 10px 16px;
  background: var(--header);
  color: var(--header-text);
}

.print-bar h2 {
  font-size: 17px;
  align-self: center;
  margin-right: auto;
}

.print-bar h2 small {
  margin-left: 10px;
  font-size: 12.5px;
  font-weight: 500;
  opacity: 0.8;
}

.print-bar label {
  color: inherit;
  font-size: 12px;
}

.print-bar select,
.print-bar button {
  width: auto;
  background: rgb(255 255 255 / 0.1);
  border: 1px solid rgb(255 255 255 / 0.28);
  color: var(--header-text);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 14px;
}

.print-bar option {
  color: #1c2733;
  background: #ffffff;
}

.print-bar button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
  font-weight: 700;
}

.print-note {
  flex-basis: 100%;
  margin: 0;
  font-size: 12px;
  opacity: 0.8;
}

.print-pages {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  padding: 16px 12px 32px;
}

/* One A4 landscape sheet. Sizes are in millimetres so that the preview and the printout match. */
.page {
  /* always printed dark on white, whatever the screen theme is */
  --paper: #ffffff;
  --grid: #b4bac2;
  --grid-strong: #4a525d;
  --ink: #000000;
  --muted: #2f363f;
  --border: #6f7884;
  --rel: #c2410c;
  --own: #1259a8;
  --tgt: #111111;
  --cpa: #0b7285;
  --avoid: #9c1d6e;
  --port: #c92a2a;
  --stbd: #2b8a3e;

  box-sizing: content-box;
  width: 277mm;
  height: 189mm;
  padding: 10mm;
  display: grid;
  grid-template-columns: 182mm minmax(0, 1fr);
  gap: 5mm;
  background: #ffffff;
  color: #000000;
  font-size: 8.5pt;
  line-height: 1.35;
  overflow: hidden;
  box-shadow: 0 2px 14px rgb(0 0 0 / 0.3);
  zoom: var(--preview-zoom, 1);
  print-color-adjust: exact;
  -webkit-print-color-adjust: exact;
}

.page-sheet svg.sheet {
  width: 182mm;
  height: auto;
}

.page-side {
  display: flex;
  flex-direction: column;
  gap: 2.4mm;
  min-height: 0;
  height: 189mm;
}

.page-head h2 {
  font-size: 12.5pt;
  letter-spacing: 0.02em;
}

.page-head p {
  display: flex;
  align-items: center;
  gap: 3mm;
  margin: 0.8mm 0 0;
}

.page-kind {
  border: 0.3mm solid #000000;
  border-radius: 0.8mm;
  padding: 0 1.8mm;
  font-weight: 700;
}

.box {
  border: 0.3mm solid #222222;
  border-radius: 1mm;
  padding: 1.4mm 2mm 2mm;
}

.box h3 {
  margin: 0 0 1mm;
  font-size: 8.5pt;
  line-height: 1.3;
}

.own-line,
.condition {
  margin: 0 0 1.2mm;
}

.condition {
  margin: 1.2mm 0 0;
}

.blank {
  display: inline-block;
  width: 16mm;
  border-bottom: 0.25mm solid #000000;
}

table.form {
  width: 100%;
  border-collapse: collapse;
}

table.form th,
table.form td {
  border: 0.25mm solid #555555;
  padding: 0.7mm 1.4mm;
  text-align: left;
  vertical-align: middle;
}

table.form thead th {
  background: #eceff2;
  text-align: center;
  font-weight: 600;
}

table.form tbody th {
  font-weight: 600;
  white-space: nowrap;
}

table.form small {
  display: block;
  font-size: 6.8pt;
  font-weight: 400;
  color: #2f363f;
}

table.form tr.write td,
table.form td.write {
  height: 7.6mm;
}

table.form.result tbody th {
  width: 30mm;
}

.box-detail {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.box-detail svg.sheet {
  flex: 1;
  min-height: 0;
  width: 100%;
  height: auto;
}

@page {
  size: A4 landscape;
  margin: 0;
}

@media print {
  html,
  body {
    background: #ffffff;
  }

  body > :not(.print-view) {
    display: none !important;
  }

  .print-view {
    display: block !important;
  }

  .print-bar {
    display: none !important;
  }

  .print-pages {
    display: block;
    padding: 0;
  }

  .page {
    zoom: 1;
    box-shadow: none;
    break-after: page;
  }

  .page:last-child {
    break-after: auto;
  }
}
