/* 内装数量拾いツール (Web版) スタイル
   図面が主役なので、UIはグレー基調で彩度を抑える。
   タッチ操作用にボタンは44px以上を確保している。 */

:root {
  --bg: #F4F6F8;
  --panel: #FFFFFF;
  --line: #D8DEE4;
  --text: #212529;
  --muted: #6B7480;
  --accent: #1E88E5;
  --accent-soft: #E7F0FA;
  --danger: #D32F2F;
  --topbar-h: 52px;
  --status-h: 30px;
  --tool-w: 76px;
  --side-w: 340px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg);
  color: var(--text);
  font-family: "Hiragino Sans", "Yu Gothic UI", "Meiryo UI", system-ui, sans-serif;
  font-size: 14px;
  -webkit-text-size-adjust: 100%;
}

body { display: flex; flex-direction: column; padding-top: var(--safe-top); }

button, input, select, textarea { font: inherit; color: inherit; }

/* ---------------------------------------------------------------- 上部バー */
.topbar {
  height: var(--topbar-h);
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 0 8px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  z-index: 30;
}

.doc-title {
  font-weight: 700;
  margin-left: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 34vw;
}

.spacer { flex: 1; }
.divider { width: 1px; height: 24px; background: var(--line); margin: 0 4px; }

.icon-btn {
  min-width: 40px;
  height: 40px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  font-size: 17px;
  cursor: pointer;
}
.icon-btn:hover { background: var(--accent-soft); }
.icon-btn:disabled { opacity: .35; }
.icon-btn.danger { color: var(--danger); }
.icon-btn.on { background: var(--accent-soft); }
.icon-btn.off { opacity: .45; }

.drawing-select { max-width: 30vw; height: 34px; border-radius: 8px; border: 1px solid var(--line); padding: 0 6px; }
.hidden { display: none !important; }

/* ---------------------------------------------------------------- メニュー */
.menu-panel {
  position: absolute;
  top: calc(var(--topbar-h) + var(--safe-top));
  left: 6px;
  width: 290px;
  max-height: 75vh;
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, .18);
  padding: 6px;
  display: none;
  z-index: 40;
}
.menu-panel.open { display: block; }
.menu-panel hr { border: none; border-top: 1px solid var(--line); margin: 6px 2px; }

.menu-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  min-height: 44px;
  padding: 0 10px;
  border: none;
  border-radius: 8px;
  background: transparent;
  text-align: left;
  cursor: pointer;
}
.menu-item:hover { background: var(--accent-soft); }
.menu-icon { width: 22px; text-align: center; }

.menu-toggles { display: flex; gap: 12px; padding: 6px 10px 2px; }
.toggle { display: flex; align-items: center; gap: 5px; color: var(--muted); }

/* ---------------------------------------------------------------- 本体 */
main { flex: 1 1 auto; display: flex; min-height: 0; }

.toolstrip {
  width: var(--tool-w);
  flex: 0 0 auto;
  background: var(--panel);
  border-right: 1px solid var(--line);
  padding: 6px 4px;
  overflow-y: auto;
  z-index: 20;
}

.tool-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  width: 100%;
  min-height: 56px;
  margin-bottom: 4px;
  border: 1px solid transparent;
  border-radius: 10px;
  background: transparent;
  cursor: pointer;
}
.tool-btn:hover { background: var(--accent-soft); }
.tool-btn.active { background: var(--accent); color: #fff; }
.tool-icon { font-size: 19px; line-height: 1; }
.tool-label { font-size: 10.5px; }

.tool-extra { display: none; padding: 4px 2px; }
.tool-extra.visible { display: block; }
.opening-select { width: 100%; height: 34px; border-radius: 8px; border: 1px solid var(--line); font-size: 11px; }

/* ---------------------------------------------------------------- キャンバス */
.canvas-wrap { position: relative; flex: 1 1 auto; min-width: 0; }
#canvas { width: 100%; height: 100%; display: block; touch-action: none; }

.empty-state {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #5A5F66;
}
.empty-card {
  background: var(--panel);
  border-radius: 14px;
  padding: 28px 32px;
  text-align: center;
  max-width: 420px;
  box-shadow: 0 16px 40px rgba(0, 0, 0, .25);
}
.empty-card h2 { margin: 0 0 8px; font-size: 18px; }
.empty-card p { color: var(--muted); margin: 0 0 16px; }
.empty-card button { margin: 4px; }
.empty-card .hint { font-size: 12px; margin-top: 14px; }

.drawing-actions {
  position: absolute;
  left: 50%;
  bottom: calc(16px + var(--safe-bottom));
  transform: translateX(-50%);
  display: none;
  gap: 8px;
  padding: 8px;
  background: rgba(255, 255, 255, .95);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .2);
}
.drawing-actions.visible { display: flex; }

/* ---------------------------------------------------------------- 右パネル */
.side {
  width: var(--side-w);
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-left: 1px solid var(--line);
  min-height: 0;
}
body.side-hidden .side { display: none; }

.side-tabs { display: flex; border-bottom: 1px solid var(--line); }
.side-tab {
  flex: 1;
  min-height: 42px;
  border: none;
  background: #EDF1F5;
  border-right: 1px solid var(--line);
  cursor: pointer;
}
.side-tab.active { background: var(--panel); font-weight: 700; color: #0D47A1; box-shadow: inset 0 -2px 0 var(--accent); }

.side-panel { display: none; flex: 1; overflow-y: auto; padding: 10px; -webkit-overflow-scrolling: touch; }
.side-panel.active { display: block; }

.empty-note { color: var(--muted); text-align: center; padding: 24px 8px; line-height: 1.7; }
.panel-summary { color: #37474F; padding: 8px 2px; font-weight: 600; }
.panel-actions { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }

/* ---------------------------------------------------------------- 部屋一覧 */
.room-list { display: flex; flex-direction: column; gap: 4px; }
.room-row {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 6px;
  border: 1px solid var(--line);
  border-radius: 8px;
  min-height: 46px;
}
.room-row.selected { border-color: var(--accent); background: var(--accent-soft); }
.room-name { flex: 1; min-width: 0; border: none; background: transparent; font-weight: 600; }
.room-name:focus { outline: 1px solid var(--accent); border-radius: 4px; }
.room-area { color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }

.swatch { width: 14px; height: 14px; border-radius: 4px; flex: 0 0 auto; display: inline-block; }
.swatch.small { width: 10px; height: 10px; margin-right: 5px; }

/* ---------------------------------------------------------------- 数量 */
.seg-tabs { display: flex; gap: 4px; margin-bottom: 8px; }
.seg {
  flex: 1;
  min-height: 34px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  cursor: pointer;
}
.seg.active { background: var(--accent); color: #fff; border-color: var(--accent); }

.total-box {
  background: #FFF8E1;
  border: 1px solid #FFE082;
  border-radius: 8px;
  padding: 8px 10px;
  line-height: 1.7;
  margin-bottom: 8px;
}
.total-box.warn { background: #FFEBEE; border-color: #FFCDD2; color: #B71C1C; }
.warn { color: var(--danger); }

.table-scroll { overflow: auto; max-width: 100%; -webkit-overflow-scrolling: touch; }
table.grid { border-collapse: collapse; width: 100%; font-size: 12.5px; }
table.grid th, table.grid td { border: 1px solid #E4E8EC; padding: 5px 6px; white-space: nowrap; }
table.grid th { background: #EDF1F5; position: sticky; top: 0; font-size: 11.5px; }
table.grid td.num { text-align: right; font-variant-numeric: tabular-nums; }
table.grid tr.selected { background: var(--accent-soft); }
table.grid tr.total-row { font-weight: 700; background: #F1F3F5; }
table.grid input { width: 100%; min-width: 72px; border: 1px solid var(--line); border-radius: 6px; padding: 4px 6px; }
table.grid input.narrow { min-width: 48px; }
table.grid select { min-width: 96px; }

/* ---------------------------------------------------------------- プロパティ */
.side-panel h3 { margin: 2px 0 10px; font-size: 15px; }
.field { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.field > span { width: 104px; flex: 0 0 auto; color: var(--muted); font-size: 12.5px; }
.field input[type="text"], .field input[type="number"], .field select, .field textarea {
  flex: 1;
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px 8px;
  background: #fff;
}
.field input[type="checkbox"] { width: 22px; height: 22px; }
.field input[type="color"] { width: 56px; height: 38px; padding: 2px; border: 1px solid var(--line); border-radius: 8px; }
.field textarea { min-height: 56px; resize: vertical; }

.prop-info {
  background: #E3F2FD;
  border: 1px solid #BBDEFB;
  border-radius: 8px;
  padding: 8px 10px;
  line-height: 1.7;
  color: #0D47A1;
  margin-top: 6px;
}
.prop-info.warn { background: #FFEBEE; border-color: #FFCDD2; color: #B71C1C; }

/* ---------------------------------------------------------------- ボタン */
button.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
button.danger { color: var(--danger); border-color: #F3C0C0; }
button.big { min-height: 48px; padding: 0 24px; font-size: 15px; }

.panel-actions button, .modal-foot button, .empty-card button, .drawing-actions button {
  min-height: 42px;
  padding: 0 16px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--panel);
  cursor: pointer;
}
.panel-actions button:hover, .modal-foot button:hover { background: var(--accent-soft); }

/* 上の共通指定より後に置いて、primary/danger の配色を確実に効かせる */
.panel-actions button.primary, .modal-foot button.primary,
.empty-card button.primary, .drawing-actions button.primary {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}
.panel-actions button.primary:hover, .modal-foot button.primary:hover,
.empty-card button.primary:hover, .drawing-actions button.primary:hover { background: #1976D2; }
.panel-actions button.danger, .modal-foot button.danger {
  color: var(--danger);
  border-color: #F3C0C0;
}

/* ---------------------------------------------------------------- ステータス */
.statusbar {
  height: calc(var(--status-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 14px;
  padding-left: 10px;
  padding-right: 10px;
  background: var(--panel);
  border-top: 1px solid var(--line);
  font-size: 12px;
  color: var(--muted);
}
.status-hint { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.status-cursor, .status-scale { white-space: nowrap; font-variant-numeric: tabular-nums; }

/* ---------------------------------------------------------------- モーダル */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .42);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 100;
}
.modal-card {
  background: var(--panel);
  border-radius: 14px;
  width: min(480px, 100%);
  max-height: 88vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 50px rgba(0, 0, 0, .3);
}
.modal-card.wide { width: min(880px, 100%); }
.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
}
.modal-head h2 { margin: 0; font-size: 16px; }
.modal-body { padding: 14px; overflow-y: auto; }
.modal-foot { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; margin-top: 14px; }

.dialog-text { line-height: 1.8; margin: 0 0 12px; }
.dialog-note { color: var(--muted); font-size: 12.5px; line-height: 1.7; }
.estimate-total { text-align: right; padding: 10px 2px; font-size: 15px; }

.chip-row { display: flex; flex-wrap: wrap; gap: 6px; margin: 4px 0 10px; }
.chip {
  min-height: 38px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 19px;
  background: var(--panel);
  cursor: pointer;
}
.chip:hover { background: var(--accent-soft); }

/* ---------------------------------------------------------------- 通知 */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(52px + var(--safe-bottom));
  transform: translate(-50%, 20px);
  background: rgba(33, 33, 33, .93);
  color: #fff;
  padding: 10px 18px;
  border-radius: 22px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 120;
  max-width: 86vw;
  text-align: center;
}
.toast.visible { opacity: 1; transform: translate(-50%, 0); }

.busy {
  position: fixed;
  inset: 0;
  background: rgba(255, 255, 255, .6);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 110;
}
.busy.visible { display: flex; }
.busy-card {
  background: var(--panel);
  border-radius: 12px;
  padding: 18px 26px;
  display: flex;
  align-items: center;
  gap: 12px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, .2);
}
.spinner {
  width: 22px;
  height: 22px;
  border: 3px solid var(--accent-soft);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------------------------------------------------------------- 画面が狭いとき */
@media (max-width: 900px) {
  :root { --side-w: 300px; --tool-w: 64px; }
  .doc-title { max-width: 26vw; }
}

/* iPad の縦向き(〜834pt)やスマホでは、図面を広く使うため上下に積む */
@media (max-width: 860px) {
  main { flex-direction: column; }
  .toolstrip {
    width: 100%;
    display: flex;
    gap: 4px;
    overflow-x: auto;
    border-right: none;
    border-bottom: 1px solid var(--line);
    padding: 4px;
  }
  .tool-btn { min-width: 62px; margin-bottom: 0; }
  .tool-extra.visible { display: flex; align-items: center; min-width: 130px; }
  .side { width: 100%; height: 44vh; border-left: none; border-top: 1px solid var(--line); }
  body.side-hidden .side { display: none; }
}
