:root {
  --bg: #eef1f6;
  --panel: #ffffff;
  --border: #dfe4ec;
  --text: #1e2735;
  --text-sub: #677183;
  --accent: #3b74d9;
  --accent-hover: #2f63c2;
  --danger: #c2412f;
  --warn: #a06a10;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(24, 36, 58, 0.08);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Kaku Gothic ProN", "Hiragino Sans", "Noto Sans JP", "Yu Gothic UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  font-size: 14px;
  line-height: 1.6;
}

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

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.brand-mark {
  width: 22px;
  height: 22px;
  align-self: center;
  border-radius: 5px;
}

.brand-name {
  margin: 0;
  font-size: 18px;
  font-weight: 700;
  letter-spacing: 0.04em;
  line-height: inherit;
  display: flex;
  align-items: baseline;
  gap: 8px;
  white-space: nowrap;
}

.brand-sub {
  color: var(--text-sub);
  font-size: 12px;
}

.mode-nav {
  display: flex;
  gap: 2px;
  margin-left: 14px;
  background: #eef1f6;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 2px;
  align-self: center;
}

.mode-nav a {
  text-decoration: none;
  color: var(--text-sub);
  font-size: 12.5px;
  font-weight: 600;
  padding: 4px 12px;
  border-radius: 999px;
  white-space: nowrap;
}

.mode-nav a:hover { color: var(--text); }

.mode-nav a.active {
  background: var(--panel);
  color: var(--accent);
  box-shadow: var(--shadow);
}

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

.layout {
  flex: 1;
  display: flex;
  min-height: 0;
}

.panel {
  width: 330px;
  flex: none;
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.viewport {
  flex: 1;
  position: relative;
  min-width: 0;
  background:
    radial-gradient(1200px 700px at 60% 20%, #ffffff 0%, var(--bg) 70%);
}

.viewport canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

/* ---------------------------------------------------------------- cards & fields */

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px;
}

.card h2 {
  margin: 0 0 10px;
  font-size: 13px;
  color: var(--text-sub);
  font-weight: 600;
  letter-spacing: 0.06em;
}

.field { margin-bottom: 10px; }
.field:last-child { margin-bottom: 0; }

.field-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 2px;
}

.field-head label { font-weight: 600; }

.unit {
  color: var(--text-sub);
  font-size: 12px;
  white-space: nowrap;
}

input[type="number"], input[type="text"], textarea {
  font: inherit;
  color: var(--text);
  background: #f6f8fb;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px 8px;
  width: 78px;
  text-align: right;
}

input[type="text"] { width: 100%; text-align: left; }

textarea {
  width: 100%;
  text-align: left;
  resize: vertical;
  min-height: 66px;
  line-height: 1.7;
  font-variant-numeric: tabular-nums;
}

input[type="number"]:focus, input[type="text"]:focus, textarea:focus {
  outline: 2px solid color-mix(in srgb, var(--accent) 35%, transparent);
  border-color: var(--accent);
}

select {
  font: inherit;
  color: var(--text);
  background: #f6f8fb;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px 8px;
}

select:focus {
  outline: 2px solid color-mix(in srgb, var(--accent) 35%, transparent);
  border-color: var(--accent);
}

select:disabled { opacity: 0.6; }

input.invalid {
  border-color: var(--danger);
  background: #fdf3f1;
}

input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
  margin: 4px 0 0;
}

input[type="checkbox"] { accent-color: var(--accent); }

.check {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 0;
  font-weight: 600;
  cursor: pointer;
}

.field.inline { padding-left: 24px; }
.field.disabled { opacity: 0.45; }
.check.inline-check { padding-left: 24px; font-weight: 500; }

.hint {
  margin: 8px 0 0;
  font-size: 12px;
  color: var(--text-sub);
}

.field-row {
  display: flex;
  gap: 12px;
}

.counter { flex: 1; }
.counter label { font-weight: 600; font-size: 13px; }

.counter-ctrl {
  display: flex;
  gap: 6px;
  margin-top: 4px;
}

.counter-ctrl input[type="number"] {
  flex: 1;
  width: 40px;
  text-align: center;
  -moz-appearance: textfield;
}

.counter-ctrl input::-webkit-outer-spin-button,
.counter-ctrl input::-webkit-inner-spin-button { -webkit-appearance: none; }

/* セグメント選択（板の構造） */
.seg {
  display: flex;
  gap: 2px;
  background: #eef1f6;
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 2px;
  margin-bottom: 10px;
}

.seg-item {
  flex: 1;
  text-align: center;
  font-weight: 600;
  font-size: 13px;
  color: var(--text-sub);
  padding: 5px 0;
  border-radius: 7px;
  cursor: pointer;
  user-select: none;
  /* 中の隠しラジオを閉じ込めるための基準。これが無いと絶対配置の基準が
     初期包含ブロック（＝ドキュメント）になり、パネルの下の方にあるセグメントほど
     ドキュメント自体の高さを押し広げる。その状態でラベルをクリックすると
     フォーカス移動でページ全体がガクッとスクロールしてしまう */
  position: relative;
}

.seg-item:has(input:checked) {
  background: var(--panel);
  color: var(--accent);
  box-shadow: var(--shadow);
}

/* 見た目は消すがフォーカスは受けられる隠し方（スクリーンリーダーからは読める）。
   ラベルの左上に 1px で固定して、セグメントの外へはみ出させない */
.seg-item input {
  position: absolute;
  top: 0;
  left: 0;
  width: 1px;
  height: 1px;
  margin: 0;
  opacity: 0;
  pointer-events: none;
}

/* 項目数が多いセグメント（棚メーカーの板の構造）は 4 列で折り返す */
.seg.wrap { flex-wrap: wrap; }

.seg.wrap .seg-item {
  flex: 1 0 calc(25% - 3px);
  font-size: 12px;
  padding: 5px 2px;
}

.seg-item:has(input:focus-visible) {
  outline: 2px solid color-mix(in srgb, var(--accent) 55%, transparent);
  outline-offset: 1px;
}

#lattice-fields.disabled { opacity: 0.45; }
#mount-fields.disabled { opacity: 0.45; }

/* 有孔ボードの収納パーツのプリセット */
.preset-row { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin: 8px 0 4px; }
.preset-row .hint { margin: 0; }

/* パーツ一覧 */
.parts-list {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 220px;
  overflow-y: auto;
}

.parts-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 0;
  border-bottom: 1px solid #f0f3f8;
}

.parts-item:last-child { border-bottom: none; }

.parts-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.parts-dim {
  color: var(--text-sub);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.btn.mini {
  padding: 2px 8px;
  font-size: 11.5px;
  border-radius: 6px;
}

/* ケースメーカー: 穴の一覧・エディタ */
.parts-item.hole-item {
  cursor: pointer;
  padding: 4px 6px;
  margin: 0 -6px;
  border-radius: 6px;
}

.parts-item.hole-item:hover { background: #f6f8fb; }
.parts-item.hole-item.selected { background: #eef4ff; }
.parts-item.excluded .parts-name { color: var(--warn); }

.hole-editor {
  border-top: 1px solid var(--border);
  margin-top: 8px;
  padding-top: 10px;
}

.hole-editor-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 8px;
}

.hole-editor-head .chip {
  background: #eef4ff;
  border-color: #c9d9f5;
  color: var(--accent);
}

.seg.hole-shape { flex: 1; margin-bottom: 0; }

.hole-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 4px;
  margin-bottom: 8px;
}

.hole-grid .field-head { margin-bottom: 0; }
.hole-grid input[type="number"] { width: 72px; }

details.sub summary {
  cursor: pointer;
  color: var(--accent);
  font-size: 13px;
  user-select: none;
  margin-top: 4px;
}

details.sub[open] summary { margin-bottom: 8px; }

/* ---------------------------------------------------------------- stats & messages */

.stats { margin: 0; }

.stat {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 3px 0;
}

.stat dt { color: var(--text-sub); flex: none; }
.stat dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }

.msg { margin: 6px 0 0; font-size: 12.5px; }
.msg.error { color: var(--danger); font-weight: 600; }
.msg.warn { color: var(--warn); }

/* ---------------------------------------------------------------- export format */

.export-group {
  display: flex;
  align-items: center;
  gap: 8px;
}

.fmt-select {
  font-weight: 600;
  padding: 7px 8px;
}

#btn-export { white-space: nowrap; }

.fmt-list { margin: 8px 0 0; }
.fmt-list dt { font-weight: 600; font-size: 12px; margin-top: 8px; }
.fmt-list dt:first-child { margin-top: 0; }

.fmt-list dd {
  margin: 2px 0 0;
  font-size: 12px;
  line-height: 1.6;
  color: var(--text-sub);
}

/* 視覚的には隠すが読み上げ・翻訳には残すラベル */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

/* ---------------------------------------------------------------- buttons */

.btn {
  font: inherit;
  font-weight: 600;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 7px 14px;
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s;
}

.btn:hover { background: #f2f5fa; }

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.btn.primary:hover { background: var(--accent-hover); }

.btn.primary:disabled {
  background: #a9b6cc;
  border-color: #a9b6cc;
  cursor: not-allowed;
}

.btn.step {
  width: 34px;
  padding: 4px 0;
  text-align: center;
}

.btn.ghost {
  background: rgba(255, 255, 255, 0.82);
  backdrop-filter: blur(4px);
}

.actions {
  display: flex;
  gap: 8px;
}

.actions .btn { flex: 1; }

.site-footer {
  text-align: center;
  color: var(--text-sub);
  font-size: 12px;
  padding: 4px 0 10px;
}

.site-footer a { color: inherit; }

/* ---------------------------------------------------------------- overlays */

.overlay { position: absolute; z-index: 2; }
.overlay.top-left { top: 12px; left: 12px; display: flex; flex-direction: column; gap: 6px; align-items: flex-start; }
.overlay.top-right { top: 12px; right: 12px; display: flex; gap: 6px; }
.overlay.bottom-right { bottom: 12px; right: 12px; }
.overlay.bottom-left { bottom: 12px; left: 12px; max-width: calc(100% - 140px); }

.btn.icon {
  width: 40px;
  padding: 7px 0;
  text-align: center;
  font-size: 15px;
}

.btn:disabled {
  opacity: 0.35;
  cursor: default;
}

.btn.full {
  width: 100%;
  margin-top: 6px;
}

.chip {
  display: inline-block;
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(4px);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 4px 12px;
  font-size: 12.5px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.chip.warn { color: var(--warn); border-color: #e5c98f; background: #fdf6e8; }

.chip.help {
  font-weight: 500;
  font-size: 11.5px;
  color: var(--text-sub);
  background: rgba(255, 255, 255, 0.72);
}

.hidden { display: none !important; }

/* ---------------------------------------------------------------- モデル上の編集UI */

.model-ui {
  position: absolute;
  z-index: 4;
  transform: translate(-50%, -100%) translateY(-10px);
  display: flex;
  gap: 6px;
  align-items: center;
  transition: opacity 0.1s;
}

.model-btn {
  padding: 7px 12px;
  font-size: 12.5px;
  white-space: nowrap;
  background: #ffffff;
  box-shadow: 0 2px 10px rgba(24, 36, 58, 0.18);
  border-color: #c9d2e0;
}

.model-btn:hover {
  background: #eef4ff;
  border-color: var(--accent);
}

.model-hint {
  font-size: 11px;
  color: var(--text-sub);
  background: rgba(255, 255, 255, 0.85);
  border-radius: 999px;
  padding: 4px 10px;
  white-space: nowrap;
}

.toast {
  position: absolute;
  left: 50%;
  bottom: 18px;
  transform: translate(-50%, 8px);
  background: #273449;
  color: #fff;
  border-radius: 999px;
  padding: 8px 18px;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 3;
  white-space: nowrap;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

/* ---------------------------------------------------------------- mobile */

@media (max-width: 760px) {
  .layout { flex-direction: column-reverse; }
  .panel { width: 100%; flex: 1; }
  .viewport { flex: none; height: 46vh; }
  .brand-sub { display: none; }
  .topbar { flex-wrap: wrap; padding: 8px 10px; }
  .mode-nav { margin-left: 8px; }
  .mode-nav a { padding: 3px 9px; font-size: 12px; }
  /* 書き出しはヘッダーの 2 行目を丸ごと使う（指で押せる大きさを確保する） */
  .export-group { flex: 1 0 100%; }
  .export-group .btn { flex: 1; }
  .export-group .btn, .fmt-select { min-height: 44px; }
}
