:root {
  --header-bg: #1565c0;
  --header-text: #ffffff;
  --page-bg: #f4f7f9;
  --text: #333333;
  --button-bg: #90caf9;
  --button-text: #1a1a1a;
  --link: #1565c0;
  --muted: #555555;
  --diagnosis-bg: #fff3e0;
  --tooltip-bg: #333333;
  --tooltip-text: #ffffff;
  --panel-bg: #ffffff;
  --disabled: #6b6b6b;
  --reached: #c62828;
  --blocked: #2e7d32;
  --best-bg: #e8f5e9;
  --high-bg: #fff9c4;
  --low-bg: #ffe0b2;
  --worst-bg: #ffebee;
  --misconfig-bg: #ede7f6;
  --border: #cfd8dc;
  --accent: #a65500;
  --focus: #005a9c;
  --switch-bg: #6b6b6b;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  font-family: system-ui, -apple-system, "Segoe UI", "Hiragino Sans", "Noto Sans JP", sans-serif;
  margin: 0;
  padding: 0;
  background: var(--page-bg);
  color: var(--text);
  transition: background-color 0.3s, color 0.3s;
}

header {
  background-color: var(--header-bg);
  color: var(--header-text);
  padding: 0.75em;
  text-align: center;
}

.header-content {
  display: flex;
  flex-direction: column;
  gap: 0.5em;
  justify-content: center;
  align-items: center;
  position: relative;
  max-width: 800px;
  margin: 0 auto;
}

.header-buttons {
  position: static;
  display: flex;
  gap: 10px;
  align-items: center;
}

.header-content h1 {
  margin: 0;
}

.subtitle {
  font-size: 0.95em;
  margin-top: 0.3em;
}

main {
  padding: 1em;
  max-width: 800px;
  margin: auto;
}

form label.tooltip-container {
  display: flex;
  align-items: center;
  gap: 0.5em;
  min-height: 44px;
  margin: 0.2em 0;
}


button {
  min-height: 44px;
  color: var(--button-text);
  margin-top: 1em;
  padding: 0.6em 1.2em;
  font-size: 1em;
  background-color: var(--button-bg);
  border: none;
  border-radius: 6px;
  cursor: pointer;
}

.diagram-box,
.diagnosis-box {
  background-color: var(--panel-bg);
  border: 2px solid var(--border);
  padding: 1em;
  border-radius: 8px;
  margin-top: 1em;
  position: relative;
}

.diagnosis-box {
  background-color: #fff3e0;
  border-left: 5px solid #ff9800;
  line-height: 1.5;
}

.diagnosis-box p {
  margin: 0.5em 0;
}

.level-badge {
  display: inline-block;
  border: 1px solid currentColor;
  border-radius: 1em;
  padding: 0.15em 0.8em;
  font-weight: bold;
}

.scenario-results {
  padding: 0;
  list-style: none;
}

.scenario-results li {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 0.25em 1em;
  padding: 0.25em;
  border: 2px solid transparent;
}

.scenario-results .is-current {
  border-color: currentColor;
  border-radius: 4px;
}

.footer {
  text-align: center;
  margin-top: 2em;
  margin-bottom: 2em;
  font-size: 0.9em;
  color: var(--muted);
}

.footer a {
  color: var(--link);
  text-decoration: none;
}

.footer a:hover {
  text-decoration: underline;
}

.diagram-box {
  overflow-x: auto;
  white-space: normal;
  font-family: inherit;
}

.diagram-box svg {
  display: block;
  width: 100%;
  min-width: 560px;
  max-width: 680px;
  height: auto;
  margin: auto;
  font-family: system-ui, -apple-system, "Segoe UI", "Hiragino Sans", "Noto Sans JP", sans-serif;
  font-size: 18px;
}

.node {
  fill: var(--text);
}

.node.is-disabled {
  fill: var(--disabled);
}

.node-name {
  paint-order: stroke;
  stroke: var(--panel-bg);
  stroke-width: 6px;
  stroke-linejoin: round;
}

.node-icon,
.attack-icon {
  font-family: "Segoe UI Emoji", "Apple Color Emoji", "Noto Color Emoji", system-ui, sans-serif;
  font-size: 26px;
}

.route {
  fill: none;
  stroke: var(--disabled);
  stroke-width: 2;
}

.route-bypass {
  stroke-dasharray: 6 4;
}

.route-selected {
  stroke-width: 4;
}

.label-blocked {
  fill: var(--blocked);
}

.label-reached {
  fill: var(--reached);
}

html.dark-mode .node {
  fill: var(--text);
}

html.dark-mode .node.is-disabled {
  fill: var(--disabled);
}

html.dark-mode .route {
  stroke: var(--disabled);
}

html.dark-mode .label-blocked {
  fill: var(--blocked);
}

html.dark-mode .label-reached {
  fill: var(--reached);
}

.scenario-options {
  min-width: 0;
  margin: 1em 0;
  padding: 0.5em;
}

.scenario-options label {
  display: flex;
  align-items: center;
  gap: 0.5em;
  min-height: 44px;
}

.scenario-options input {
  flex: 0 0 20px;
  width: 20px;
  height: 20px;
}

.assumptions {
  margin-top: 0.75em;
}

.assumptions summary {
  cursor: pointer;
  min-height: 44px;
}

/* トグルスイッチ */
.pattern-toggle-section {
  margin-top: 2em;
  background-color: var(--panel-bg);
  padding: 1em;
  border-radius: 8px;
  border: 1px solid var(--border);
}

.toggle-switch.tooltip-container {
  display: inline-flex;
  align-items: center;
  cursor: pointer;
  user-select: none;
  min-height: 44px;
  margin-top: 0;
  max-width: 100%;
}

.slider {
  flex: 0 0 50px;
  width: 50px;
  height: 26px;
  background-color: var(--switch-bg);
  border-radius: 26px;
  position: relative;
  transition: background-color 0.3s;
  margin-right: 10px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}

.slider::before {
  content: "";
  position: absolute;
  width: 22px;
  height: 22px;
  left: 2px;
  top: 2px;
  background-color: var(--header-text);
  border-radius: 50%;
  transition: transform 0.3s;
}

.toggle-switch[aria-expanded="true"] .slider {
  background-color: var(--header-bg);
}

.toggle-switch[aria-expanded="true"] .slider::before {
  transform: translateX(24px);
}

.toggle-label {
  font-size: 0.95em;
  color: var(--text);
  font-weight: 500;
}

/* パターン一覧表 */
.pattern-table-section {
  margin-top: 1em;
}

.table-scroll {
  max-width: 100%;
  overflow-x: auto;
}

.pattern-table [aria-current="true"] td {
  box-shadow: inset 0 3px currentColor, inset 0 -3px currentColor;
}

.pattern-table [aria-current="true"] td:first-child {
  border-left: 3px solid currentColor;
}

.pattern-table [aria-current="true"] td:last-child {
  border-right: 3px solid currentColor;
}

.pattern-table-content {
  padding: 1em 0;
}

.pattern-table {
  width: 100%;
  border-collapse: collapse;
  background-color: var(--panel-bg);
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.pattern-table th {
  min-width: 4em;
  background-color: var(--header-bg);
  color: var(--header-text);
  padding: 12px;
  text-align: center;
  font-weight: bold;
}

.pattern-table td {
  padding: 10px;
  text-align: center;
  border-bottom: 1px solid var(--border);
}

.pattern-table th:nth-last-child(-n + 3),
.pattern-table td:nth-last-child(-n + 3) {
  white-space: nowrap;
}

.level-best {
  background-color: var(--best-bg);
}

.level-high {
  background-color: var(--high-bg);
}

.level-low {
  background-color: var(--low-bg);
}

.level-worst {
  background-color: var(--worst-bg);
}

.level-misconfig {
  background-color: var(--misconfig-bg);
}

@media (max-width: 600px) {
  .pattern-table {
    font-size: 0.875em;
  }
  .pattern-table th,
  .pattern-table td {
    padding: 6px;
  }
  .toggle-label {
    font-size: 0.9em;
  }
}

/* 言語切り替えボタン */
.lang-toggle {
  background: none;
  border: 2px solid var(--header-text);
  border-radius: 50%;
  width: 44px;
  height: 44px;
  padding: 0;
  margin: 0;
  cursor: pointer;
  color: var(--header-text);
  font-size: 0.85em;
  font-weight: 700;
  letter-spacing: 0.05em;
  transition: transform 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
}

.lang-toggle:hover {
  transform: scale(1.1);
}

/* ヘルプボタン */
.help-toggle {
  background: none;
  border: 2px solid var(--header-text);
  border-radius: 50%;
  width: 44px;
  height: 44px;
  padding: 0;
  margin: 0;
  cursor: pointer;
  color: var(--header-text);
  font-size: 1.2em;
  transition: transform 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
}

.help-toggle:hover {
  transform: scale(1.1);
}

/* ダークモード切り替えボタン */
.dark-mode-toggle {
  background: none;
  border: 2px solid var(--header-text);
  border-radius: 50%;
  width: 44px;
  height: 44px;
  padding: 0;
  margin: 0;
  cursor: pointer;
  color: var(--header-text);
  font-size: 1.2em;
  transition: transform 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
}

@media (min-width: 601px) {
  .header-content {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  }

  .header-content h1 {
    grid-column: 2;
    grid-row: 1;
  }

  .header-buttons {
    grid-column: 3;
    grid-row: 1;
    justify-self: end;
  }
}

.dark-mode-toggle:hover {
  transform: scale(1.1);
}

.dark-mode-toggle .dark-icon {
  display: none;
}

/* ダークモード */
html.dark-mode {
  --header-bg: #1565c0;
  --header-text: #ffffff;
  --page-bg: #1a1a1a;
  --text: #e0e0e0;
  --button-bg: #3d6a93;
  --button-text: #ffffff;
  --link: #64b5f6;
  --muted: #999999;
  --diagnosis-bg: #3a2a1a;
  --tooltip-bg: #555555;
  --tooltip-text: #e0e0e0;
  --panel-bg: #2a2a2a;
  --disabled: #9e9e9e;
  --reached: #ff8a80;
  --blocked: #81c784;
  --best-bg: #1b5e20;
  --high-bg: #5d4037;
  --low-bg: #4e342e;
  --worst-bg: #5d1f1f;
  --misconfig-bg: #4a3a6a;
  --border: #777777;
  --accent: #ffb74d;
  --focus: #ffde59;
  --switch-bg: #9e9e9e;
}

html.dark-mode header {
  background-color: var(--header-bg);
}

html.dark-mode .dark-mode-toggle .light-icon {
  display: none;
}

html.dark-mode .dark-mode-toggle .dark-icon {
  display: inline;
}

html.dark-mode .custom-config h2,
html.dark-mode .diagram-section h2,
html.dark-mode .diagnosis-section h2,
html.dark-mode .pattern-table-content h2 {
  color: var(--text);
}

html.dark-mode .diagram-box,
html.dark-mode .diagnosis-box {
  background-color: var(--panel-bg);
  border-color: var(--border);
  color: var(--text);
}

html.dark-mode .diagnosis-box {
  background-color: #3a2a1a;
  border-left-color: #ff9800;
}

html.dark-mode button {
  background-color: var(--button-bg);
  color: var(--button-text);
}

html.dark-mode button:hover {
  background-color: var(--button-bg);
}

html.dark-mode .pattern-toggle-section {
  background-color: var(--panel-bg);
  border-color: var(--border);
}

html.dark-mode .toggle-label {
  color: var(--text);
}

html.dark-mode .slider {
  background-color: var(--switch-bg);
  box-shadow: 0 2px 4px rgba(0,0,0,0.4);
}

html.dark-mode .toggle-switch[aria-expanded="true"] .slider {
  background-color: var(--header-bg);
}

html.dark-mode .pattern-table {
  background-color: var(--panel-bg);
  color: var(--text);
}

html.dark-mode .pattern-table th {
  background-color: var(--header-bg);
}

html.dark-mode .pattern-table td {
  border-bottom-color: var(--border);
}

html.dark-mode .level-best {
  background-color: var(--best-bg);
}

html.dark-mode .level-high {
  background-color: var(--high-bg);
}

html.dark-mode .level-low {
  background-color: var(--low-bg);
}

html.dark-mode .level-worst {
  background-color: var(--worst-bg);
}

html.dark-mode .level-misconfig {
  background-color: var(--misconfig-bg);
}

html.dark-mode .footer {
  color: var(--muted);
}

html.dark-mode .footer a {
  color: var(--link);
}

/* モーダル */
.modal {
  position: fixed;
  z-index: 1000;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0,0,0,0.4);
}

.modal-content {
  background-color: var(--panel-bg);
  margin: 5% auto;
  border: none;
  border-radius: 8px;
  width: 90%;
  max-width: 800px;
  max-height: 80vh;
  max-height: 80dvh;
  overflow-y: auto;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 30px;
  background-color: var(--header-bg);
  color: var(--header-text);
  border-radius: 8px 8px 0 0;
}

.modal-header h2 {
  margin: 0;
}

.close {
  color: var(--header-text);
  background: transparent;
  flex: 0 0 44px;
  width: 44px;
  margin: 0;
  padding: 0;
  font-size: 28px;
  font-weight: bold;
  cursor: pointer;
  line-height: 1;
}

.close:hover {
  background: transparent;
}

.modal-body {
  padding: 30px;
  line-height: 1.6;
}

.modal-body h3 {
  color: var(--link);
  margin-top: 25px;
  margin-bottom: 15px;
}

.modal-body h3:first-child {
  margin-top: 0;
}

.modal-body dl {
  margin: 15px 0;
}

.modal-body dt {
  font-weight: bold;
  margin-top: 15px;
  color: var(--text);
}

.modal-body dd {
  margin: 5px 0 0 20px;
  color: var(--muted);
}

.modal-body ul, .modal-body ol {
  padding-left: 20px;
}

.modal-body li {
  margin: 8px 0;
}

/* ダークモードのモーダル */
html.dark-mode .modal-content {
  background-color: var(--panel-bg);
  color: var(--text);
}

html.dark-mode .modal-header {
  background-color: var(--header-bg);
}

html.dark-mode .modal-body h3 {
  color: var(--link);
}

html.dark-mode .modal-body dt {
  color: var(--text);
}

html.dark-mode .modal-body dd {
  color: var(--muted);
}

@media (max-width: 600px) {
  .modal-content {
    margin: 10% auto;
    width: 95%;
  }
  
  .modal-header {
    padding: 15px 20px;
  }
  
  .modal-body {
    padding: 20px;
  }
}

/* ツールチップ */
.tooltip-container {
  position: relative;
  display: inline-block;
}

.tooltip {
  visibility: hidden;
  opacity: 0;
  position: absolute;
  z-index: 1000;
  bottom: 125%;
  left: 0;
  transform: none;
  background-color: var(--tooltip-bg);
  color: var(--tooltip-text);
  text-align: left;
  padding: 12px 16px;
  border-radius: 8px;
  font-size: 14px;
  line-height: 1.5;
  box-sizing: border-box;
  width: min(280px, calc(100vw - 48px));
  max-width: calc(100vw - 32px);
  box-shadow: 0 6px 20px rgba(0,0,0,0.25);
  transition: opacity 0.3s, visibility 0.3s;
  pointer-events: none; /* ツールチップがクリックをブロックしないように */
}

.tooltip::after {
  content: "";
  position: absolute;
  top: 100%;
  left: 50%;
  margin-left: -5px;
  border-width: 5px;
  border-style: solid;
  border-color: var(--tooltip-bg) transparent transparent transparent;
}

.tooltip-container:focus-visible .tooltip,
.tooltip-container:has(input:focus-visible) .tooltip {
  visibility: visible;
  opacity: 1;
}

@media (hover: hover) {
  .tooltip-container:hover .tooltip {
    visibility: visible;
    opacity: 1;
  }
}

/* チェックボックスのラベルのツールチップ調整 */
form label.tooltip-container .tooltip {
  bottom: auto;
  top: -10px;
  left: 200px;
  transform: none;
}

form label.tooltip-container .tooltip::after {
  top: 50%;
  margin-top: -5px;
  left: -5px;
  border-color: transparent var(--tooltip-bg) transparent transparent;
}

/* ダークモード用ツールチップ */
html.dark-mode .tooltip {
  background-color: var(--tooltip-bg);
  color: var(--tooltip-text);
}

html.dark-mode .tooltip::after {
  border-color: var(--tooltip-bg) transparent transparent transparent;
}

/* ボタン用ツールチップ調整 */
button.tooltip-container .tooltip {
  bottom: 120%;
}

/* モバイル対応 */
@media (max-width: 600px) {
  .tooltip {
    font-size: 14px;
    padding: 10px 12px;
    width: min(250px, calc(100vw - 48px));
    max-width: calc(100vw - 32px);
  }

  form label.tooltip-container .tooltip {
    left: 0;
    top: 100%;
    bottom: auto;
  }

  form label.tooltip-container .tooltip::after {
    top: -5px;
    left: 20px;
    margin-top: 0;
    border-color: transparent transparent var(--tooltip-bg) transparent;
  }
}

/* トグルスイッチのツールチップ位置調整 */
.toggle-switch.tooltip-container .tooltip {
  top: 120%;
  bottom: auto;
  left: 0;
  transform: none;
  margin-top: 10px;
  width: min(300px, calc(100vw - 112px));
}

.toggle-switch.tooltip-container .tooltip::after {
  top: -5px;
  bottom: auto;
  border-color: transparent transparent var(--tooltip-bg) transparent;
}

html.dark-mode .toggle-switch.tooltip-container .tooltip::after {
  border-color: transparent transparent var(--tooltip-bg) transparent;
}

/* フォーム内のチェックボックスのダークモード調整 */
html.dark-mode form label.tooltip-container .tooltip::after {
  border-color: transparent var(--tooltip-bg) transparent transparent;
}

/* Input targets and visible focus remain usable with either color scheme. */
.defense-options {
  min-width: 0;
  padding: 0.5em;
}

.defense-options input {
  flex: 0 0 20px;
  width: 20px;
  height: 20px;
}

h2 {
  margin: 0.75em 0 0.25em;
}

.section-description {
  margin: 0.25em 0;
  line-height: 1.5;
}

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

.header-buttons :focus-visible,
.modal-header :focus-visible {
  outline-color: var(--header-text);
}

html.dark-mode .lang-toggle,
html.dark-mode .help-toggle,
html.dark-mode .dark-mode-toggle,
html.dark-mode .close {
  background: transparent;
  color: var(--header-text);
}

@media (max-width: 600px) {
  html.dark-mode form label.tooltip-container .tooltip::after {
    border-color: transparent transparent var(--tooltip-bg) transparent;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition: none !important;
    animation: none !important;
    scroll-behavior: auto !important;
  }
}
