:root {
  --paper: #efe9de;
  --panel: rgba(251, 248, 242, .94);
  --ink: #26221d;
  --ink-2: #6d6458;
  --ink-3: #a39889;
  --red: #c8402c;
  --red-deep: #9e2e1d;
  --line: rgba(38, 34, 29, .12);
  --shadow: 0 1px 0 rgba(255, 255, 255, .8) inset, 0 10px 28px rgba(70, 48, 22, .14), 0 1px 3px rgba(70, 48, 22, .16);
  --sans: ui-sans-serif, system-ui, -apple-system, "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Yu Gothic UI", "Meiryo", "Noto Sans JP", sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--sans);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; cursor: pointer; -webkit-user-select: none; user-select: none; }
button:focus-visible, input:focus-visible { outline: 2px solid var(--red); outline-offset: 2px; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }

#board {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

/* ---- 上の帯 ---- */
.top {
  position: fixed;
  top: 0; left: 0; right: 0;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: max(14px, env(safe-area-inset-top)) max(18px, env(safe-area-inset-right)) 0 max(20px, env(safe-area-inset-left));
  pointer-events: none;
}
.top > * { pointer-events: auto; }
h1 {
  margin: 4px 0 0;
  font-size: 19px;
  font-weight: 700;
  letter-spacing: .06em;
  line-height: 1.2;
  -webkit-user-select: none; user-select: none;
}
h1 small {
  display: block;
  margin-top: 3px;
  font-size: 11.5px;
  font-weight: 500;
  letter-spacing: .12em;
  color: var(--ink-2);
}
.tools {
  display: flex;
  gap: 2px;
  padding: 4px;
  border-radius: 999px;
  background: var(--panel);
  box-shadow: var(--shadow);
  backdrop-filter: blur(10px);
}
.tools button {
  display: flex;
  align-items: center;
  gap: 6px;
  height: 38px;
  min-width: 38px;
  padding: 0 13px 0 10px;
  border: 0;
  border-radius: 999px;
  background: transparent;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: .04em;
  transition: background .15s, color .15s;
}
.tools button:hover { background: rgba(38, 34, 29, .06); }
.tools button[aria-pressed="true"] { background: var(--ink); color: #fbf7f0; }
#t-sound .off { display: none; }
#t-sound[aria-pressed="false"] .on { display: none; }
#t-sound[aria-pressed="false"] .off { display: inline; }
#t-sound[aria-pressed="false"] { background: transparent; color: var(--ink-2); }
#t-sound[aria-pressed="true"] { background: transparent; color: var(--ink); }
#t-sound:hover { background: rgba(38, 34, 29, .06); }

/* ---- 下の台（モーターと部品箱） ---- */
.dock {
  position: fixed;
  left: 50%;
  bottom: max(16px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px 8px 8px;
  border-radius: 24px;
  background: var(--panel);
  box-shadow: var(--shadow);
  backdrop-filter: blur(10px);
  transition: background .15s, box-shadow .15s;
}
.motor {
  display: flex;
  align-items: center;
  gap: 8px;
  padding-right: 12px;
  border-right: 1px solid var(--line);
}
.round {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  flex: none;
  border: 0;
  border-radius: 50%;
  background: transparent;
}
#m-play { background: var(--red); color: #fff; box-shadow: 0 2px 6px rgba(158, 46, 29, .35), inset 0 1px 0 rgba(255, 255, 255, .25); }
#m-play svg { fill: currentColor; stroke: none; width: 18px; height: 18px; }
#m-play .i-play { display: none; }
#m-play[aria-pressed="false"] .i-play { display: block; margin-left: 2px; }
#m-play[aria-pressed="false"] .i-pause { display: none; }
#m-play[aria-pressed="false"] { background: var(--ink); box-shadow: none; }
#m-dir:hover { background: rgba(38, 34, 29, .06); }
#m-dir[data-dir="-1"] svg { transform: scaleX(-1); }
.speed { display: grid; grid-template-columns: auto auto; grid-template-rows: auto auto; column-gap: 8px; align-items: center; }
.speed .lab { font-size: 10.5px; font-weight: 600; letter-spacing: .1em; color: var(--ink-2); }
.speed output { grid-row: 1; grid-column: 2; justify-self: end; font: 600 12px/1 var(--mono); color: var(--ink); font-variant-numeric: tabular-nums; }
.speed input { grid-row: 2; grid-column: 1 / 3; width: 128px; margin: 6px 0 2px; accent-color: var(--red); }

.tray { display: flex; gap: 2px; }
.part {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  width: 58px;
  padding: 4px 0 3px;
  border: 0;
  border-radius: 14px;
  background: transparent;
  touch-action: none;
  transition: background .15s, transform .15s;
}
.part:hover { background: rgba(38, 34, 29, .06); }
.part:active { transform: scale(.94); }
.part canvas { width: 44px; height: 44px; display: block; pointer-events: none; }
.part span { font: 600 11px/1 var(--mono); color: var(--ink-2); letter-spacing: .02em; pointer-events: none; }
.trash-label {
  position: absolute;
  inset: 0;
  display: none;
  place-items: center;
  border-radius: inherit;
  font-size: 14px;
  font-weight: 700;
  letter-spacing: .08em;
  color: #fff;
  pointer-events: none;
}
.dock.dragging .trash-label { display: grid; color: var(--ink-2); background: rgba(251, 248, 242, .9); }
.dock.trash { background: var(--red); box-shadow: 0 10px 30px rgba(158, 46, 29, .35); }
.dock.trash .trash-label { color: #fff; background: transparent; }
.dock.trash .motor, .dock.trash .tray { visibility: hidden; }

/* ---- 選んだ歯車の札 ---- */
.chip {
  position: fixed;
  left: 0; top: 0;
  z-index: 5;
  padding: 6px 6px 8px;
  border-radius: 18px;
  background: rgba(38, 34, 29, .95);
  color: #fbf7f0;
  box-shadow: 0 12px 30px rgba(38, 24, 10, .28);
  -webkit-user-select: none; user-select: none;
  touch-action: none;
}
.chip[hidden] { display: none; }
.chip-row { display: flex; align-items: center; gap: 2px; }
.chip button {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: inherit;
  font-size: 22px;
  font-weight: 500;
  line-height: 1;
}
.chip button:hover { background: rgba(255, 255, 255, .1); }
.chip button:active { background: rgba(255, 255, 255, .18); }
.chip button[data-act="motor"][aria-pressed="true"] { color: #ff9a84; }
.chip output {
  min-width: 62px;
  text-align: center;
  font: 700 20px/1 var(--mono);
  font-variant-numeric: tabular-nums;
}
.chip output small { margin-left: 2px; font: 600 11px/1 var(--sans); opacity: .7; }
.chip .sep { width: 1px; height: 24px; margin: 0 4px; background: rgba(255, 255, 255, .18); }
.chip-info {
  padding: 3px 8px 0;
  font-size: 11.5px;
  line-height: 1.45;
  letter-spacing: .02em;
  color: rgba(251, 247, 240, .78);
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
.chip-info b { color: #fff; font-weight: 600; }
.chip-info .warn { color: #ff9a84; }

/* ---- 図面の表題欄 ---- */
.titleblock {
  position: fixed;
  right: max(18px, env(safe-area-inset-right));
  bottom: max(16px, env(safe-area-inset-bottom));
  opacity: 0;
  transform: translateY(6px);
  transition: opacity .3s, transform .3s;
  pointer-events: none;
}
.titleblock.show { opacity: 1; transform: none; }
.titleblock table {
  border-collapse: collapse;
  background: rgba(244, 240, 232, .92);
  font: 500 11px/1.2 var(--mono);
  color: rgba(30, 36, 48, .9);
  border: 1.5px solid rgba(30, 36, 48, .85);
}
.titleblock th, .titleblock td { border: .8px solid rgba(30, 36, 48, .6); padding: 5px 8px; text-align: left; white-space: nowrap; }
.titleblock th { font-weight: 600; color: rgba(30, 36, 48, .65); font-family: var(--sans); }
.titleblock td { font-family: var(--sans); }

/* ---- 案内 ---- */
.hint {
  position: fixed;
  left: 0; top: 0;
  z-index: 4;
  max-width: 260px;
  padding: 8px 12px;
  white-space: nowrap;
  border-radius: 12px;
  background: var(--ink);
  color: #fbf7f0;
  font-size: 12.5px;
  line-height: 1.45;
  letter-spacing: .02em;
  box-shadow: 0 8px 22px rgba(38, 24, 10, .25);
  pointer-events: none;
  animation: hint-in .5s ease-out both, hint-bob 2.4s ease-in-out .5s infinite;
}
.hint::after {
  content: "";
  position: absolute;
  left: 22px; top: 100%;
  border: 7px solid transparent;
  border-top-color: var(--ink);
}
.hint.below::after { left: 50%; margin-left: -7px; }
.hint[hidden] { display: none; }
@keyframes hint-in { from { opacity: 0; translate: 0 6px; } }
@keyframes hint-bob { 50% { translate: 0 -3px; } }

.toast {
  position: fixed;
  left: 50%;
  bottom: 118px;
  z-index: 6;
  max-width: min(92vw, 520px);
  padding: 10px 16px;
  border-radius: 14px;
  background: var(--ink);
  color: #fbf7f0;
  font-size: 13px;
  line-height: 1.55;
  letter-spacing: .02em;
  text-align: center;
  box-shadow: 0 10px 26px rgba(38, 24, 10, .3);
  opacity: 0;
  transform: translate(-50%, 8px);
  transition: opacity .2s, transform .2s;
  pointer-events: none;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.warn { background: var(--red-deep); }

/* ---- 遊び方 ---- */
dialog {
  width: min(560px, calc(100vw - 32px));
  max-height: calc(100dvh - 48px);
  padding: 0;
  border: 0;
  border-radius: 20px;
  background: #fbf8f2;
  color: var(--ink);
  box-shadow: 0 24px 60px rgba(38, 24, 10, .3);
}
dialog { overflow: auto; overscroll-behavior: contain; }
dialog::backdrop { background: rgba(38, 30, 20, .35); }
dialog form { position: relative; padding: 22px 24px 20px; }
.help-x {
  position: absolute;
  top: 12px; right: 12px;
  display: grid;
  place-items: center;
  width: 40px; height: 40px;
  border: 0;
  border-radius: 50%;
  background: transparent;
}
.help-x:hover { background: rgba(38, 34, 29, .06); }
dialog h2 { margin: 0 0 10px; font-size: 18px; letter-spacing: .06em; }
dialog h3 { margin: 18px 0 8px; font-size: 14px; letter-spacing: .06em; color: var(--ink-2); }
dialog ul { margin: 0; padding: 0; list-style: none; display: grid; gap: 8px; }
dialog li { font-size: 13.5px; line-height: 1.7; padding-left: 14px; position: relative; }
dialog li::before { content: ""; position: absolute; left: 0; top: .72em; width: 5px; height: 5px; border-radius: 50%; background: var(--red); }
dialog li b { font-weight: 700; }
.help-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 20px; justify-content: flex-end; }
.help-actions button {
  height: 40px;
  padding: 0 16px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
  font-size: 13px;
  font-weight: 600;
}
.help-actions button.primary { background: var(--ink); color: #fbf7f0; border-color: var(--ink); }

/* ---- スマホ ---- */
@media (max-width: 720px) {
  .top { padding-left: max(16px, env(safe-area-inset-left)); padding-right: max(12px, env(safe-area-inset-right)); }
  h1 { font-size: 17px; }
  h1 small { font-size: 10.5px; letter-spacing: .08em; }
  .tools button { padding: 0; width: 40px; justify-content: center; }
  .tools button span { display: none; }
  .dock {
    left: 0; right: 0;
    bottom: 0;
    transform: none;
    flex-direction: column;
    align-items: stretch;
    gap: 2px;
    padding: 8px max(10px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
    border-radius: 22px 22px 0 0;
  }
  .motor { border-right: 0; padding: 0 2px 6px; border-bottom: 1px solid var(--line); }
  .speed { flex: 1; }
  .speed input { width: 100%; }
  .tray { justify-content: space-between; }
  .part { width: auto; flex: 1; }
  .toast { bottom: 172px; }
  dialog form { padding: 18px 18px 16px; }
  dialog li { font-size: 13px; line-height: 1.62; }
}
@media (max-width: 1180px) {
  .titleblock { display: none; }
}
@media (max-width: 360px) {
  .part canvas { width: 38px; height: 38px; }
}
