:root {
  --bg: #14161a;
  --panel: rgba(27, 29, 35, 0.975);
  --panel2: #1d1f25;
  --line: rgba(255, 238, 215, 0.13);
  --line2: rgba(255, 238, 215, 0.22);
  --text: #efe9df;
  --muted: #a8a195;
  --accent: #e8493f;
  --accent-soft: rgba(232, 73, 63, 0.18);
  --amber: #f0b24a;
  --tray-h: calc(112px + env(safe-area-inset-bottom, 0px));
  --top-h: calc(54px + env(safe-area-inset-top, 0px));
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0; height: 100%;
  background: var(--bg); color: var(--text);
  font-family: system-ui, -apple-system, "Hiragino Kaku Gothic ProN", "Hiragino Sans", "Yu Gothic UI", "Meiryo", sans-serif;
  font-size: 15px; line-height: 1.5;
  overflow: hidden; overscroll-behavior: none;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}

button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }

#board { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; cursor: default; outline: none; }
#board.dragging { cursor: grabbing !important; }
#vignette {
  position: fixed; inset: 0; pointer-events: none;
  background:
    radial-gradient(120% 90% at 22% 8%, rgba(255, 214, 160, 0.07), rgba(255, 214, 160, 0) 55%),
    radial-gradient(110% 100% at 50% 50%, rgba(0, 0, 0, 0) 55%, rgba(0, 0, 0, 0.42) 100%);
}

/* ---- 上の帯 ---- */
#top {
  position: fixed; top: 0; left: 0; right: 0; z-index: 10;
  display: flex; align-items: center; gap: 10px;
  padding: calc(env(safe-area-inset-top, 0px) + 8px) max(12px, env(safe-area-inset-right, 0px)) 6px max(12px, env(safe-area-inset-left, 0px));
  pointer-events: none;
}
#top > * { pointer-events: auto; }
#top .sp { flex: 1; pointer-events: none; }
.title {
  margin: 0; font-size: 15px; font-weight: 700; letter-spacing: 0.04em;
  display: flex; align-items: center; gap: 8px;
  padding: 0 12px 0 8px; height: 40px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 20px;
}
.logo { display: inline-flex; width: 24px; height: 24px; color: var(--accent); }
.logo svg { width: 24px; height: 24px; }

.ibtn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-width: 44px; height: 44px; padding: 0;
  background: var(--panel); border: 1px solid var(--line); border-radius: 22px;
  color: var(--text); transition: background .15s, border-color .15s, transform .1s;
}
.ibtn:hover { background: #262930; border-color: var(--line2); }
.ibtn:active { transform: scale(0.95); }
.ibtn svg { width: 22px; height: 22px; flex: none; }
.ibtn.sm { min-width: 36px; height: 36px; border-radius: 18px; }
.ibtn.sm svg { width: 18px; height: 18px; }
.ibtn.round { min-width: 44px; width: 44px; }
.ibtn.pill { padding: 0 16px 0 12px; font-weight: 700; font-size: 14px; }
.ibtn.pill[aria-pressed="true"] { background: var(--accent); border-color: transparent; color: #fff; }
.ibtn.pill[aria-pressed="true"]:hover { background: #f0574c; }
#btnMenu, #btnUndo { width: 44px; }
.ibtn:disabled { opacity: 0.35; cursor: default; pointer-events: none; }

#zoom {
  position: fixed; z-index: 9; right: max(12px, env(safe-area-inset-right, 0px)); top: calc(var(--top-h) + 8px);
  display: flex; flex-direction: column; gap: 8px;
}
#zoom .ibtn { min-width: 40px; width: 40px; height: 40px; border-radius: 12px; }
#zoom .ibtn svg { width: 20px; height: 20px; }

/* ---- 通知とヒント ---- */
#toast {
  position: fixed; z-index: 20; left: 50%; top: calc(var(--top-h) + 6px);
  transform: translate(-50%, -8px); opacity: 0; pointer-events: none;
  max-width: min(92vw, 520px); width: max-content;
  padding: 10px 16px; border-radius: 14px;
  background: rgba(36, 38, 45, 0.97); border: 1px solid var(--line2);
  font-size: 13.5px; line-height: 1.55; text-align: center; text-wrap: balance;
  transition: opacity .22s, transform .22s;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.45);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.warn { border-color: rgba(255, 110, 92, 0.7); background: rgba(58, 28, 26, 0.97); }

#hint {
  position: fixed; z-index: 8; left: 50%; bottom: calc(var(--tray-h) + 14px);
  transform: translate(-50%, 6px); opacity: 0; pointer-events: none;
  max-width: min(88vw, 420px); width: max-content;
  padding: 10px 16px; border-radius: 14px; text-align: center;
  background: var(--accent); color: #fff; font-size: 13.5px; font-weight: 600; line-height: 1.5; text-wrap: balance;
  transition: opacity .35s, transform .35s;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
}
#hint::after {
  content: ""; position: absolute; left: 50%; bottom: -6px; width: 12px; height: 12px;
  background: var(--accent); transform: translateX(-50%) rotate(45deg); border-radius: 2px;
}
#hint.show { opacity: 1; transform: translate(-50%, 0); }

/* ---- 選んだ歯車のカード ---- */
#card {
  position: fixed; z-index: 12; left: 50%; bottom: calc(var(--tray-h) + 10px);
  transform: translateX(-50%);
  width: min(calc(100vw - 20px), 540px);
  padding: 10px 12px 12px; border-radius: 18px;
  background: var(--panel); border: 1px solid var(--line2);
  box-shadow: 0 -2px 28px rgba(0, 0, 0, 0.45);
}
.c-head { display: flex; align-items: center; gap: 8px; min-height: 36px; }
.swatch { width: 14px; height: 14px; border-radius: 50%; flex: none; box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.35); }
#cTitle { font-size: 17px; letter-spacing: 0.02em; }
.muted { color: var(--muted); font-size: 12px; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.c-teeth { display: flex; align-items: center; gap: 10px; margin: 2px 0 2px; }
.c-teeth input[type=range] { flex: 1; width: 100%; }
.c-teeth .ibtn.round { height: 40px; width: 40px; min-width: 40px; border-radius: 12px; }
.c-stat { margin: 2px 2px 8px; font-size: 12.5px; line-height: 1.5; min-height: 2.9em; color: #d8d2c6; font-variant-numeric: tabular-nums; }
.c-stat .warn { color: #ff8e7e; font-weight: 700; }
.c-stat b { color: var(--text); }
.c-note { margin: -3px 2px 8px; font-size: 11.5px; line-height: 1.55; color: var(--muted); }
.c-actions { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 8px; }
.act {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  min-height: 50px; padding: 5px 4px; font-size: 11px; font-weight: 600; line-height: 1.2; text-align: center;
  background: rgba(255, 255, 255, 0.05); border: 1px solid var(--line); border-radius: 12px;
  transition: background .15s, border-color .15s;
}
.act:hover { background: rgba(255, 255, 255, 0.1); border-color: var(--line2); }
.act svg { width: 20px; height: 20px; }
.act.on { background: var(--accent-soft); border-color: rgba(232, 73, 63, 0.7); color: #ffb3a9; }
.act.danger { color: #ffb0a4; }
.act[hidden] { display: none; }

/* ---- 入れ物（トレー）---- */
#tray {
  position: fixed; z-index: 11; left: 0; right: 0; bottom: 0; height: var(--tray-h);
  padding: 6px 0 env(safe-area-inset-bottom, 0px);
  background: linear-gradient(to bottom, rgba(27, 29, 35, 0.96), rgba(22, 24, 29, 0.98));
  border-top: 1px solid var(--line2);
}
#trayHead {
  display: flex; justify-content: space-between; gap: 12px; padding: 0 16px; height: 20px;
  font-size: 11px; color: var(--muted); letter-spacing: 0.03em; white-space: nowrap; overflow: hidden;
}
#trayHead b { font-weight: 600; color: #8f897d; }
#trayRow {
  display: flex; gap: 8px; align-items: flex-start; height: 84px;
  padding: 2px max(14px, env(safe-area-inset-left, 0px)) 0;
  overflow-x: auto; overflow-y: hidden; scrollbar-width: none; -webkit-overflow-scrolling: touch;
  touch-action: pan-x;
}
#trayRow::-webkit-scrollbar { display: none; }
@media (min-width: 760px) { #trayRow { justify-content: center; } }
.chip {
  flex: none; width: 62px; height: 80px; padding: 3px 0 0;
  display: flex; flex-direction: column; align-items: center; justify-content: flex-start; gap: 0;
  background: rgba(255, 255, 255, 0.045); border: 1px solid var(--line); border-radius: 14px;
  touch-action: pan-x; transition: background .15s, border-color .15s, transform .12s;
}
.chip canvas { display: block; pointer-events: none; }
.chip span { font-size: 13px; font-weight: 700; font-variant-numeric: tabular-nums; margin-top: -1px; pointer-events: none; }
.chip:hover { background: rgba(255, 255, 255, 0.09); border-color: var(--line2); }
.chip:active { transform: scale(0.96); }
#trashMsg {
  position: absolute; inset: 0; display: none; align-items: center; justify-content: center; gap: 10px;
  background: rgba(150, 36, 30, 0.92); font-weight: 700; font-size: 15px; letter-spacing: 0.04em; pointer-events: none;
}
#trashMsg svg { width: 24px; height: 24px; }
#tray.trash #trashMsg { display: flex; }

/* ---- メニュー ---- */
#sheet, #hob { position: fixed; inset: 0; z-index: 50; display: flex; align-items: flex-end; justify-content: center; }
.sheet-back { position: absolute; inset: 0; background: rgba(4, 5, 7, 0.6); }
.sheet-body {
  position: relative; width: min(580px, 100%); max-height: 88vh; max-height: 88dvh; overflow-y: auto;
  background: var(--panel2); border: 1px solid var(--line2); border-radius: 20px 20px 0 0;
  padding: 14px 16px calc(20px + env(safe-area-inset-bottom, 0px));
  -webkit-user-select: text; user-select: text; touch-action: pan-y;
}
@media (min-width: 700px) { #sheet, #hob { align-items: center; } .sheet-body { border-radius: 20px; } }
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 4px; font-size: 17px; }
.sheet-body h2 { margin: 18px 0 8px; font-size: 12px; color: var(--muted); letter-spacing: 0.08em; font-weight: 600; }
.presets { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.preset {
  text-align: left; padding: 10px 12px; border-radius: 14px; min-height: 66px;
  background: rgba(255, 255, 255, 0.05); border: 1px solid var(--line);
}
.preset:hover { background: rgba(255, 255, 255, 0.1); border-color: var(--line2); }
.preset b { display: block; font-size: 14px; }
.preset small { display: block; margin-top: 2px; color: var(--muted); font-size: 11.5px; line-height: 1.45; }
.row { display: flex; align-items: center; gap: 12px; min-height: 44px; font-size: 14px; }
.row > span:first-child { flex: none; min-width: 5.2em; }
.row input[type=range] { flex: 1; }
.row output { width: 4.5em; text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; font-size: 13px; }
.row.sw { justify-content: space-between; }
.row.btns { gap: 8px; }
.row.btns .act { flex: 1; flex-direction: row; gap: 8px; min-height: 46px; font-size: 13px; }
.seg { display: flex; flex: 1; background: rgba(255, 255, 255, 0.05); border: 1px solid var(--line); border-radius: 12px; padding: 3px; gap: 3px; }
.seg button { flex: 1; min-height: 38px; border: 0; background: transparent; border-radius: 9px; font-size: 13px; font-weight: 600; color: var(--muted); }
.seg button.on { background: var(--accent); color: #fff; }
input[type=checkbox] {
  appearance: none; -webkit-appearance: none; width: 50px; height: 30px; border-radius: 15px; margin: 0; flex: none;
  background: rgba(255, 255, 255, 0.14); position: relative; cursor: pointer; transition: background .15s;
}
input[type=checkbox]::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 50%;
  background: #fff; transition: transform .15s;
}
input[type=checkbox]:checked { background: var(--accent); }
input[type=checkbox]:checked::after { transform: translateX(20px); }
.about { margin-top: 18px; color: #cfc9bd; font-size: 13px; line-height: 1.75; }
.about summary { cursor: pointer; color: var(--muted); font-size: 12px; letter-spacing: 0.08em; font-weight: 600; min-height: 36px; display: flex; align-items: center; }
.about p { margin: 6px 0; }
.about-img { display: block; width: 100%; height: auto; border-radius: 12px; margin: 6px 0 2px; border: 1px solid var(--line); }
.about .cap { font-size: 11.5px; color: var(--muted); margin-top: 2px; }

/* ---- 歯の削り出し ---- */
.hob-body { width: min(620px, 100%); }
#hobCanvas { display: block; margin: 8px auto 6px; border-radius: 14px; border: 1px solid var(--line); max-width: 100%; touch-action: none; }
.hob-note { margin: 6px 2px; font-size: 13px; line-height: 1.7; color: #d8d2c6; }
.hob-note.dim { color: var(--muted); font-size: 12px; }
.hob-body .row.btns { align-items: stretch; flex-wrap: wrap; }
.hob-body .row.btns .act { flex: none; min-width: 112px; }
.hob-body .seg { flex: 1 1 100%; min-width: 130px; }
@media (min-width: 520px) { .hob-body .seg { flex: 1; } }

/* ---- スライダー ---- */
input[type=range] {
  -webkit-appearance: none; appearance: none; background: transparent; height: 40px; margin: 0; min-width: 0; cursor: pointer; touch-action: pan-y;
}
input[type=range]::-webkit-slider-runnable-track { height: 6px; border-radius: 3px; background: rgba(255, 255, 255, 0.16); }
input[type=range]::-moz-range-track { height: 6px; border-radius: 3px; background: rgba(255, 255, 255, 0.16); }
input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 26px; height: 26px; border-radius: 50%; margin-top: -10px;
  background: var(--amber); border: 3px solid #fff2d6; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.5);
}
input[type=range]::-moz-range-thumb { width: 20px; height: 20px; border-radius: 50%; background: var(--amber); border: 3px solid #fff2d6; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.5); }
input[type=range]:focus-visible, button:focus-visible, input[type=checkbox]:focus-visible { outline: 2px solid var(--amber); outline-offset: 2px; }

/* ---- 広い画面：カードは左下へ（盤の真ん中を空ける）---- */
@media (min-width: 900px) {
  #card { left: 16px; transform: none; width: 400px; }
  #hint { bottom: calc(var(--tray-h) + 14px); }
}

@media (pointer: coarse) { #zoom .ibtn { min-width: 44px; width: 44px; height: 44px; } }

/* ---- 低い画面 ---- */
@media (max-height: 700px) {
  :root { --tray-h: calc(92px + env(safe-area-inset-bottom, 0px)); }
  #trayHead { display: none; }
  #trayRow { height: 84px; }
  .c-stat { min-height: 2.4em; margin-bottom: 6px; }
  .c-note { display: none; }
}
/* ---- 横向きの携帯：トレーは右の縦帯、カードは左へ ---- */
@media (max-height: 520px) and (orientation: landscape) and (max-width: 1100px) {
  :root { --tray-w: calc(88px + env(safe-area-inset-right, 0px)); --top-h: calc(52px + env(safe-area-inset-top, 0px)); }
  #tray {
    top: var(--top-h); left: auto; right: 0; bottom: 0; width: var(--tray-w); height: auto;
    padding: 4px env(safe-area-inset-right, 0px) 0 0; border-top: 0; border-left: 1px solid var(--line2);
  }
  #trayRow { flex-direction: column; align-items: center; height: 100%; overflow-x: hidden; overflow-y: auto; padding: 6px 0 10px; touch-action: pan-y; justify-content: flex-start; }
  .chip { touch-action: pan-y; flex: none; width: 66px; height: 78px; }
  #trayHead { display: none; }
  #top { padding-right: calc(var(--tray-w) + 8px); }
  #zoom { right: calc(var(--tray-w) + 10px); top: calc(var(--top-h) + 4px); flex-direction: row; }
  #card { left: 12px; bottom: 8px; transform: none; width: min(440px, calc(100vw - var(--tray-w) - 28px)); padding: 6px 10px 8px; }
  .c-head { min-height: 30px; }
  .c-teeth { margin: 0; }
  .c-stat { min-height: 0; margin: 0 2px 6px; }
  #hint { bottom: 14px; left: calc(50% - var(--tray-w) / 2); }
  #toast { left: calc(50% - var(--tray-w) / 2); }
  #sheet { padding-right: 0; }
}
@media (max-width: 430px) {
  #top { gap: 6px; }
  .title { font-size: 14px; padding: 0 12px; }
  .title .logo { display: none; }
  .ibtn.pill { padding: 0 12px 0 9px; }
}
@media (prefers-reduced-motion: reduce) {
  #toast, #hint { transition: none; }
}
