/* =====================================================================
   図面加工ツールの見た目（Illustrator に近い暗い色の画面）
   ===================================================================== */

:root {
  /* 背景（暗い順） */
  --bg-deep: #1f1f1f; /* 読み込み画面・案内欄 */
  --bg-panel: #2d2d2d; /* 上部バー・ツールバー・レイヤー一覧 */
  --bg-raised: #3a3a3a; /* ボタン */
  --bg-hover: #474747;
  --pasteboard: #535353; /* 図面のまわり（Illustrator の作業領域の色） */
  --border: #4d4d4d;

  /* 文字（暗い背景の上でも十分読める明るさ。--text は背景との明るさの比が 10 以上、--text-muted も 7 以上） */
  --text: #f0f0f0;
  --text-muted: #c4c4c4;

  /* 強調色 */
  --accent: #1f6fd6; /* 選択中のツール・主なボタン（白文字との比 4.9） */
  --accent-hover: #2a7de8;
  --danger: #c93a3a;
  --notice: #ffc94d; /* 案内欄の注意書き */

  /* 四角の色（レイヤー一覧の印にも使う） */
  --crop: #3b8cff;
  --erase: #ff6b6b;
  --marker: #ffe600; /* js/config.js の MARKER_COLOR と同じ色にする */
  --selection: #3b8cff;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

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

body {
  background: var(--bg-deep);
  color: var(--text);
  font-family: system-ui, -apple-system, 'Hiragino Sans', 'Yu Gothic UI', 'Meiryo', sans-serif;
  font-size: 13px;
  line-height: 1.5;
  overflow: hidden;
}

svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
}
svg .fill {
  fill: currentColor;
  stroke: none;
}

/* ---------- スマホ（幅 540px 未満）では案内だけを出す ---------- */
.mobile-block {
  display: none;
}
@media (max-width: 539.98px) {
  .app,
  .modal {
    display: none !important;
  }
  .mobile-block {
    display: flex;
    height: 100%;
    align-items: center;
    justify-content: center;
    padding: 24px;
    text-align: center;
    font-size: 18px;
    font-weight: bold;
  }
}

.app {
  height: 100%;
}

/* ---------- ボタン ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  height: 30px;
  padding: 0 12px;
  font: inherit;
  color: var(--text);
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover:not(:disabled) {
  background: var(--bg-hover);
}
.btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}
.btn svg {
  width: 16px;
  height: 16px;
}
.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: bold;
}
.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
}
.btn-danger {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
  font-weight: bold;
}
.btn-danger:hover:not(:disabled) {
  background: #d94c4c;
}
.btn-large {
  height: 44px;
  padding: 0 28px;
  font-size: 16px;
}
.btn-square {
  width: 30px;
  padding: 0;
  font-size: 15px;
}

/* =====================================================================
   読み込み画面
   ===================================================================== */
.load-screen {
  height: 100%;
  padding: 24px;
}
.drop-zone {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  border: 3px dashed #6b6b6b;
  border-radius: 16px;
  background: var(--bg-panel);
  text-align: center;
}
.drop-zone.is-dragover {
  border-color: var(--crop);
  background: #2b3a4f;
}
.drop-zone p {
  margin: 0;
}
.drop-zone__icon {
  width: 72px;
  height: 72px;
  stroke-width: 1.2;
  color: var(--text-muted);
}
.drop-zone__title {
  font-size: 24px;
  font-weight: bold;
}
.drop-zone__or {
  color: var(--text-muted);
  font-size: 15px;
}
.drop-zone__note {
  color: var(--text-muted);
  font-size: 14px;
  margin-top: 6px !important;
}
.drop-zone__message {
  min-height: 1.5em;
  color: var(--notice);
  font-size: 14px;
}

/* =====================================================================
   編集画面の配置
   ┌────────── 上部バー ──────────┐
   │ツール│      図面       │レイヤー│
   └────────── 案内欄 ────────────┘
   ===================================================================== */
.edit-screen {
  height: 100%;
  display: grid;
  grid-template-columns: 72px minmax(0, 1fr) 240px;
  grid-template-rows: 48px minmax(0, 1fr) 32px;
  grid-template-areas:
    'top top top'
    'tools canvas layers'
    'guide guide guide';
}

/* ---------- 上部バー ---------- */
.topbar {
  grid-area: top;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 12px;
  background: var(--bg-panel);
  border-bottom: 1px solid #1a1a1a;
  min-width: 0;
}
.topbar__left,
.topbar__right {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}
.app-name {
  font-weight: bold;
  font-size: 14px;
  margin-right: 4px;
  white-space: nowrap;
}
.drawing-no {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}
.drawing-no span {
  color: var(--text-muted);
  white-space: nowrap;
}
.drawing-no input {
  width: 200px;
  min-width: 80px;
  height: 30px;
  padding: 0 8px;
  font: inherit;
  font-size: 14px;
  color: var(--text);
  background: var(--bg-deep);
  border: 1px solid var(--border);
  border-radius: 4px;
}
.drawing-no input:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}
.zoom-label {
  min-width: 48px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

/* 書き出すメニュー */
.export-menu {
  position: relative;
  margin-left: 8px;
}
.export-menu__list {
  position: absolute;
  right: 0;
  top: calc(100% + 4px);
  z-index: 20;
  min-width: 170px;
  padding: 4px;
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: 6px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
}
.export-menu__list button {
  display: block;
  width: 100%;
  padding: 8px 12px;
  font: inherit;
  font-size: 14px;
  text-align: left;
  color: var(--text);
  background: none;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}
.export-menu__list button:hover {
  background: var(--accent);
  color: #fff;
}

/* ---------- 左のツールバー ---------- */
.toolbar {
  grid-area: tools;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 8px 6px;
  background: var(--bg-panel);
  border-right: 1px solid #1a1a1a;
}
.tool {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 2px 6px;
  font: inherit;
  font-size: 12px;
  color: var(--text);
  background: none;
  border: 1px solid transparent;
  border-radius: 6px;
  cursor: pointer;
}
.tool:hover {
  background: var(--bg-hover);
}
.tool[aria-pressed='true'] {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.tool svg {
  width: 24px;
  height: 24px;
}
.tool .marker-swatch {
  fill: var(--marker);
  stroke: none;
}

/* ---------- 中央の図面 ---------- */
.canvas-area {
  grid-area: canvas;
  overflow: auto;
  background: var(--pasteboard);
  /* 図面が画面より小さいときは真ん中に置く（大きいときはスクロールできるよう margin: auto を使う） */
  display: flex;
  padding: 24px;
}
.page-wrap {
  margin: auto;
  flex-shrink: 0;
  position: relative;
  background: #fff;
  box-shadow: 0 2px 16px rgba(0, 0, 0, 0.55);
}
.page-wrap canvas {
  display: block;
}
/* 注意: .overlay に z-index・opacity・transform を付けないこと。
   付けると、黄色マーカーの「乗算」が下の図面と混ざらなくなる */
.overlay {
  position: absolute;
  left: 0;
  top: 0;
  overflow: hidden;
  touch-action: none;
  user-select: none;
}
.overlay[data-tool='crop'],
.overlay[data-tool='erase'],
.overlay[data-tool='marker'] {
  cursor: crosshair;
}

/* ---------- 図面の上の四角 ---------- */
.rect {
  position: absolute;
  pointer-events: none; /* 選択ツールのときだけ、四角をクリックできる（下の .is-selectable） */
}
.rect.is-selectable {
  pointer-events: auto;
  cursor: move;
}

/* 切り出し範囲: 外側を暗くして、残る部分を分かりやすくする */
.rect--crop {
  border: 2px dashed var(--crop);
  box-shadow: 0 0 0 99999px rgba(0, 0, 0, 0.45);
}

/* 消去: 書き出しと同じく白で塗る */
.rect--erase {
  background: #fff;
}
.rect--erase.is-selectable:hover {
  outline: 1px dashed var(--erase);
}

/* 黄色マーカー: 乗算で重ねる（書き出しと同じ見え方） */
.rect--marker {
  background: var(--marker);
  mix-blend-mode: multiply;
}

/* 選んでいる四角 */
.rect.is-selected {
  outline: 1px solid var(--selection);
  outline-offset: 0;
}
.rect--crop.is-selected {
  outline: none;
  border-style: solid;
}
.rect.is-highlight {
  outline: 3px solid #ff9f1c !important;
}

/* 大きさを変えるつまみ（■） */
.rect__handle {
  position: absolute;
  width: 9px;
  height: 9px;
  background: #fff;
  border: 1.5px solid var(--selection);
}
.rect__handle--nw { left: -5px; top: -5px; cursor: nwse-resize; }
.rect__handle--n  { left: calc(50% - 4.5px); top: -5px; cursor: ns-resize; }
.rect__handle--ne { right: -5px; top: -5px; cursor: nesw-resize; }
.rect__handle--e  { right: -5px; top: calc(50% - 4.5px); cursor: ew-resize; }
.rect__handle--se { right: -5px; bottom: -5px; cursor: nwse-resize; }
.rect__handle--s  { left: calc(50% - 4.5px); bottom: -5px; cursor: ns-resize; }
.rect__handle--sw { left: -5px; bottom: -5px; cursor: nesw-resize; }
.rect__handle--w  { left: -5px; top: calc(50% - 4.5px); cursor: ew-resize; }

/* ---------- 右のレイヤー一覧 ---------- */
.layers {
  grid-area: layers;
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--bg-panel);
  border-left: 1px solid #1a1a1a;
}
.layers__title {
  margin: 0;
  padding: 8px 12px;
  font-size: 13px;
  font-weight: bold;
  border-bottom: 1px solid var(--border);
}
.layers__list {
  flex: 1;
  overflow-y: auto;
  list-style: none;
  margin: 0;
  padding: 0;
}
.layer {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 6px 0 10px;
  height: 34px;
  border-bottom: 1px solid #262626;
  cursor: pointer;
}
.layer:hover {
  background: var(--bg-hover);
}
.layer.is-selected {
  background: #24456f;
}
.layer.is-hidden .layer__name {
  color: #9a9a9a;
  text-decoration: line-through;
}
.layer__swatch {
  flex-shrink: 0;
  width: 14px;
  height: 14px;
  border-radius: 2px;
}
.layer__swatch--crop {
  border: 2px dashed var(--crop);
}
.layer__swatch--erase {
  background: #fff;
  border: 2px solid var(--erase);
}
.layer__swatch--marker {
  background: var(--marker);
}
.layer__name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
}
.layer__btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  padding: 0;
  color: var(--text-muted);
  background: none;
  border: 1px solid transparent;
  border-radius: 4px;
  cursor: pointer;
}
.layer__btn:hover {
  color: var(--text);
  background: var(--bg-raised);
  border-color: var(--border);
}
.layer__btn svg {
  width: 16px;
  height: 16px;
}
.layers__empty {
  padding: 12px;
  color: var(--text-muted);
  line-height: 1.7;
}
.layers__size {
  padding: 10px 12px;
  border-top: 1px solid var(--border);
  background: #262626;
}
.layers__size-label {
  display: block;
  color: var(--text-muted);
  font-size: 12px;
}
.layers__size strong {
  font-size: 14px;
  font-variant-numeric: tabular-nums;
}

/* ---------- 下部の案内欄 ---------- */
.guide {
  grid-area: guide;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 0 14px;
  background: var(--bg-deep);
  border-top: 1px solid #1a1a1a;
  min-width: 0;
}
.guide__tool,
.guide__notice {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.guide__notice {
  color: var(--notice);
  flex-shrink: 1;
}

/* =====================================================================
   確認表示
   ===================================================================== */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(0, 0, 0, 0.6);
}
.modal__box {
  display: flex;
  flex-direction: column;
  max-height: 100%;
  padding: 20px 24px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.6);
}
.modal__box--small {
  width: 460px;
}
.modal__box--large {
  width: min(1100px, 100%);
  height: 100%;
}
.modal__box h2 {
  margin: 0 0 12px;
  font-size: 17px;
}
.modal__box p {
  margin: 0 0 8px;
  font-size: 14px;
  line-height: 1.7;
}
.modal__buttons {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 16px;
}
.modal__buttons .btn {
  height: 36px;
  padding: 0 18px;
  font-size: 14px;
}

/* 書き出し前の確認: 画像を大きく表示する */
.export-preview {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: var(--pasteboard);
  border-radius: 6px;
}
.export-preview > img {
  display: block;
  max-width: 100%;
  max-height: 100%;
  background: #fff;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.5);
}
/* PDF のときは「用紙」の中に画像を置いた見た目にする */
.export-preview__paper {
  position: relative;
  max-width: 100%;
  max-height: 100%;
  background: #fff;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.5);
}
.export-preview__paper img {
  position: absolute;
  display: block;
}
.export-details {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 4px 16px;
  margin: 14px 0 0;
  font-size: 14px;
}
.export-details dt {
  color: var(--text-muted);
}
.export-details dd {
  margin: 0;
  word-break: break-all;
}

/* 処理中 */
.modal--busy {
  background: rgba(0, 0, 0, 0.35);
}
.busy__box {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 20px;
  font-size: 14px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.busy__spinner {
  width: 18px;
  height: 18px;
  border: 3px solid var(--border);
  border-top-color: var(--crop);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}
