:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1e222b;
  --line: #2a2f3a;
  --text: #e7ebf2;
  --muted: #8a93a6;
  --accent: #4f8cff;
  --accent-2: #2b60d9;
  --danger: #e5484d;
  --ok: #30b46a;
  --radius: 10px;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: -apple-system, "Segoe UI", "Hiragino Kaku Gothic ProN", "Yu Gothic UI", Meiryo, system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  -webkit-text-size-adjust: 100%;
}

#app {
  display: grid;
  grid-template-columns: 232px 1fr 268px;
  height: 100vh;
}

.panel {
  background: var(--panel);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow-y: auto;
}
#inspector { border-right: none; border-left: 1px solid var(--line); }

.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 14px 10px;
  font-weight: 600; font-size: 14px;
  position: sticky; top: 0; background: var(--panel); z-index: 2;
}
.logo { font-size: 15px; }
.logo b { color: var(--accent); }
.badge {
  font-size: 10px; color: var(--muted);
  border: 1px solid var(--line); border-radius: 999px; padding: 2px 8px;
}
.hint { color: var(--muted); font-size: 12px; line-height: 1.6; padding: 0 14px 12px; }

/* --- カタログ --- */
#catalog-list { padding: 0 10px 20px; }
.cat-group-title {
  font-size: 11px; color: var(--muted); text-transform: none;
  margin: 12px 4px 6px; letter-spacing: .04em;
}
.cat-item {
  display: flex; align-items: center; gap: 10px;
  width: 100%; text-align: left;
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--line); border-radius: var(--radius);
  padding: 8px 10px; margin-bottom: 6px; cursor: pointer;
  transition: border-color .12s, transform .05s;
}
.cat-item:hover { border-color: var(--accent); }
.cat-item:active { transform: scale(.98); }
.cat-swatch { width: 16px; height: 16px; border-radius: 4px; flex: none; box-shadow: inset 0 0 0 1px rgba(255,255,255,.15); }
.cat-item .n { font-size: 13px; }
.cat-item .d { font-size: 10px; color: var(--muted); margin-top: 1px; }
.cat-item .txt { flex: 1; min-width: 0; }

/* --- ステージ --- */
#stage { position: relative; min-width: 0; background: #0b0d11; }
#scene { display: block; width: 100%; height: 100%; touch-action: none; }

#toolbar {
  position: absolute; top: 0; left: 0; right: 0; z-index: 3;
  display: flex; flex-wrap: wrap; gap: 8px; align-items: center;
  padding: 8px 10px;
  background: linear-gradient(180deg, rgba(15,17,21,.92), rgba(15,17,21,.55) 70%, transparent);
  backdrop-filter: blur(2px);
}
.tb-group { display: flex; align-items: center; gap: 6px; }
.tb-group + .tb-group { margin-left: auto; }
#toolbar label { font-size: 11px; color: var(--muted); display: flex; align-items: center; gap: 4px; }
#toolbar select, #toolbar input {
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--line); border-radius: 8px; padding: 5px 7px; font-size: 12px;
}
#toolbar input[type=number] { width: 62px; }
button {
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--line); border-radius: 8px;
  padding: 6px 10px; font-size: 12px; cursor: pointer;
  transition: border-color .12s, background .12s;
}
button:hover { border-color: var(--accent); }
button.active, button.toggle.active { border-color: var(--accent); background: rgba(79,140,255,.16); color: #cfe0ff; }
button.danger:hover { border-color: var(--danger); color: #ffb4b6; }
button.wide { width: 100%; margin-top: 8px; padding: 9px; }

#poly-bar {
  position: absolute; top: 52px; left: 50%; transform: translateX(-50%); z-index: 6;
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap; justify-content: center;
  background: rgba(20, 24, 32, 0.95); border: 1px solid var(--accent);
  border-radius: 10px; padding: 8px 12px; font-size: 13px; color: var(--text);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45); max-width: 92%;
}
#poly-bar[hidden] { display: none; }
#poly-bar .pb-msg b { color: var(--accent); }
#poly-bar .pb-btns { display: flex; gap: 6px; }
#poly-bar button.primary { border-color: var(--accent); background: rgba(79, 140, 255, 0.2); color: #cfe0ff; }
button.toggle.on { border-color: var(--ok); background: rgba(48, 180, 106, 0.16); color: #9be3bb; }

#stat {
  position: absolute; left: 10px; bottom: 10px; z-index: 3;
  font-size: 11px; color: var(--muted);
  background: rgba(15,17,21,.7); border: 1px solid var(--line);
  border-radius: 8px; padding: 6px 10px; line-height: 1.5;
  pointer-events: none;
}
#empty-hint {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  color: var(--muted); font-size: 13px; pointer-events: none; z-index: 2;
}

/* --- インスペクタ --- */
#inspector { padding-bottom: 20px; }
#insp-body { padding: 0 14px; }
.insp-name { font-size: 16px; font-weight: 600; margin-bottom: 2px; }
.insp-dims { font-size: 12px; color: var(--muted); margin-bottom: 14px; }
.field { margin-bottom: 14px; }
.field > span { font-size: 11px; color: var(--muted); display: block; margin-bottom: 6px; }
.row { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.rot-val { min-width: 42px; text-align: center; font-variant-numeric: tabular-nums; }
#swatches .sw { width: 22px; height: 22px; border-radius: 6px; cursor: pointer; border: 2px solid transparent; }
#swatches .sw.on { border-color: #fff; }
.fit { margin-top: 12px; font-size: 12px; border-radius: 8px; padding: 8px 10px; display: none; }
.fit.warn { display: block; background: rgba(229,72,77,.14); color: #ffb4b6; border: 1px solid rgba(229,72,77,.4); }
.fit.ok { display: block; background: rgba(48,180,106,.12); color: #9be3bb; border: 1px solid rgba(48,180,106,.35); }

/* --- 間取りトリミング モーダル --- */
#crop-modal {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(5, 7, 12, 0.86);
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
#crop-modal[hidden] { display: none; }
.crop-box {
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  padding: 14px; max-width: 96vw; max-height: 94vh;
  display: flex; flex-direction: column; gap: 10px;
}
.crop-head { display: flex; align-items: center; justify-content: space-between; gap: 14px; font-size: 14px; }
.crop-head b { color: var(--accent); }
.crop-head .r { display: flex; gap: 8px; }
.crop-head button.primary { border-color: var(--accent); background: rgba(79, 140, 255, 0.18); color: #cfe0ff; }
#crop-stage {
  position: relative; display: inline-block; line-height: 0; align-self: center;
  cursor: crosshair; touch-action: none; overflow: hidden; border-radius: 6px;
}
#crop-img { display: block; max-width: 88vw; max-height: 68vh; user-select: none; -webkit-user-drag: none; }
#crop-sel {
  position: absolute; display: none; border: 2px solid var(--accent);
  background: rgba(79, 140, 255, 0.14); box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.45);
}
.crop-hint { font-size: 12px; color: var(--muted); text-align: center; }

/* --- レスポンシブ（スマホ） --- */
@media (max-width: 820px) {
  #app { grid-template-columns: 1fr; grid-template-rows: 1fr auto; }
  #catalog {
    order: 2; flex-direction: row; overflow-x: auto; overflow-y: hidden;
    border: none; border-top: 1px solid var(--line); max-height: 128px;
  }
  .panel-head, #catalog .hint { display: none; }
  #catalog-list { display: flex; gap: 6px; padding: 8px; }
  .cat-group-title { display: none; }
  .cat-item { flex-direction: column; width: 96px; margin: 0; align-items: flex-start; }
  #stage { order: 1; }
  #inspector {
    order: 3; position: fixed; bottom: 0; left: 0; right: 0; z-index: 10;
    max-height: 46vh; border-left: none; border-top: 1px solid var(--line);
    box-shadow: 0 -8px 24px rgba(0,0,0,.5);
  }
  #inspector[data-empty="true"] { display: none; }
}
