:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #f0f3f6;
  --text: #1d2733;
  --muted: #64707d;
  --border: #dde3ea;
  --accent: #1f6feb;
  --accent-hover: #1858c4;
  --accent-soft: #e6effd;
  --ok: #1a7f37;
  --ok-soft: #e3f4e8;
  --warn: #9a6700;
  --warn-soft: #fff4d6;
  --err: #cf222e;
  --err-soft: #fde7e9;
  --line: #9aa5b1;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 2px 8px rgba(16, 24, 40, .06);
  --radius: 12px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11161c;
    --surface: #1a2028;
    --surface-2: #222a34;
    --text: #e6ebf1;
    --muted: #9aa6b2;
    --border: #2e3844;
    --accent: #4c8dff;
    --accent-hover: #6aa1ff;
    --accent-soft: #1d2b44;
    --ok: #4ac26b;
    --ok-soft: #17301f;
    --warn: #d4a72c;
    --warn-soft: #33290f;
    --err: #ff6b6b;
    --err-soft: #3a1c1f;
    --line: #6b7785;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Meiryo", "Hiragino Sans", "Yu Gothic UI", system-ui, sans-serif;
  font-size: 15px;
  line-height: 1.6;
}

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

/* ---------- ヘッダー ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; }
.logo {
  display: inline-grid; place-items: center;
  width: 30px; height: 30px; border-radius: 8px;
  background: var(--accent); color: #fff; font-size: 18px;
}

main { max-width: 1180px; margin: 0 auto; padding: 24px 16px 64px; }

h1 { font-size: 30px; margin: 32px 0 8px; text-align: center; }
h2 { font-size: 17px; margin: 0; display: flex; align-items: center; gap: 8px; }
.lead { text-align: center; color: var(--muted); margin: 0 0 28px; }
.step {
  display: inline-grid; place-items: center;
  width: 24px; height: 24px; border-radius: 50%;
  background: var(--accent); color: #fff; font-size: 13px;
}

/* ---------- ドロップゾーン ---------- */
.dropzone {
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  max-width: 640px; margin: 0 auto;
  padding: 48px 24px;
  background: var(--surface);
  border: 2px dashed var(--border);
  border-radius: 18px;
  cursor: pointer;
  transition: border-color .15s, background .15s, transform .15s;
}
.dropzone:hover, .dropzone:focus-visible, .dropzone.over {
  border-color: var(--accent); background: var(--accent-soft); outline: none;
}
.dropzone.over { transform: scale(1.01); }
.drop-icon { font-size: 40px; color: var(--accent); line-height: 1; }
.drop-main {
  margin-top: 8px; padding: 12px 28px; border-radius: 10px;
  background: var(--accent); color: #fff; font-weight: 700; font-size: 18px;
}
.drop-sub { color: var(--muted); font-size: 14px; }
.privacy { text-align: center; color: var(--muted); font-size: 13px; margin-top: 16px; }

/* ---------- 作業エリア ---------- */
.work {
  display: grid;
  grid-template-columns: 1fr 300px;
  gap: 20px;
  align-items: start;
}
.col-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.file-list { display: flex; flex-direction: column; gap: 12px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.file-head { display: flex; align-items: center; gap: 12px; padding: 12px 14px; }
.file-icon {
  flex: none; display: grid; place-items: center;
  width: 42px; height: 50px; border-radius: 6px;
  background: var(--accent-soft); color: var(--accent);
  font-size: 11px; font-weight: 700;
}
.file-meta { flex: 1; min-width: 0; }
.file-name { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-sub { color: var(--muted); font-size: 13px; }

.badge {
  flex: none; font-size: 12px; font-weight: 700;
  padding: 3px 10px; border-radius: 999px;
  background: var(--surface-2); color: var(--muted);
}
.badge.ready { background: var(--accent-soft); color: var(--accent); }
.badge.busy { background: var(--warn-soft); color: var(--warn); }
.badge.done { background: var(--ok-soft); color: var(--ok); }
.badge.error { background: var(--err-soft); color: var(--err); }

.icon-btn {
  flex: none; width: 30px; height: 30px; border-radius: 8px;
  border: none; background: transparent; color: var(--muted);
  font-size: 20px; cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); color: var(--err); }

.file-body { border-top: 1px solid var(--border); padding: 12px 14px; display: none; }
.file-card.has-info .file-body { display: block; }

.layer-tools { display: flex; align-items: center; gap: 12px; margin-bottom: 8px; }
.sel-count { margin-left: auto; color: var(--muted); font-size: 13px; }
.layer-groups { display: flex; flex-direction: column; gap: 10px; max-height: 260px; overflow: auto; padding-right: 4px; }
.group-name { font-size: 12px; font-weight: 700; color: var(--muted); margin-bottom: 4px; }
.chips-wrap { display: flex; flex-wrap: wrap; gap: 6px; }

.layer-chip { position: relative; }
.layer-chip input { position: absolute; opacity: 0; pointer-events: none; }
.layer-chip span {
  display: inline-block; padding: 4px 12px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface);
  font-size: 13px; cursor: pointer; user-select: none;
}
.layer-chip input:checked + span { background: var(--accent); border-color: var(--accent); color: #fff; }
.layer-chip input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.layer-chip.empty span { opacity: .55; }

.file-error, .result-error { display: none; padding: 10px 14px; color: var(--err); background: var(--err-soft); border-top: 1px solid var(--border); border-radius: 0 0 var(--radius) var(--radius); font-size: 14px; }
.file-error.show, .result-error.show { display: block; }

/* ---------- 設定 ---------- */
.side {
  position: sticky; top: 76px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 16px;
  display: flex; flex-direction: column; gap: 16px;
}
.field { display: flex; flex-direction: column; gap: 6px; }
.field.inline { flex-direction: row; align-items: center; gap: 10px; margin-top: 12px; }
.label { font-size: 13px; font-weight: 700; color: var(--muted); }

select {
  padding: 8px 10px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface);
}

.segmented { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.segmented label { position: relative; }
.segmented input { position: absolute; opacity: 0; }
.segmented span {
  display: flex; flex-direction: column; align-items: center;
  padding: 10px 6px; border-radius: 10px;
  border: 1px solid var(--border); cursor: pointer; font-weight: 700; text-align: center;
}
.segmented small { font-weight: 400; font-size: 11px; color: var(--muted); }
.segmented input:checked + span { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

button.primary, button.secondary, .ghost {
  border-radius: 10px; padding: 8px 16px; cursor: pointer; font-weight: 700;
}
button.primary { background: var(--accent); color: #fff; border: none; }
button.primary:hover:not(:disabled) { background: var(--accent-hover); }
button.primary:disabled { opacity: .45; cursor: not-allowed; }
button.big { padding: 14px; font-size: 16px; }
button.secondary { background: var(--surface); color: var(--accent); border: 1px solid var(--accent); }
button.secondary:hover { background: var(--accent-soft); }
.ghost { background: transparent; border: 1px solid var(--border); color: var(--text); font-weight: 400; }
.ghost:hover { background: var(--surface-2); }
.small { font-size: 13px; padding: 5px 12px; }
.add-more { display: inline-block; }
button.link { background: none; border: none; color: var(--accent); padding: 0; cursor: pointer; font-size: 13px; }
button.link:hover { text-decoration: underline; }
.hint { margin: 0; color: var(--muted); font-size: 13px; text-align: center; }

/* ---------- 結果 ---------- */
.results { margin-top: 36px; }
.results-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; gap: 12px; flex-wrap: wrap; }
#resultList { display: flex; flex-direction: column; gap: 16px; }

.result-head {
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  padding: 12px 16px; border-bottom: 1px solid var(--border); flex-wrap: wrap;
}
.result-head .file-name { font-size: 16px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 4px; }
.chip { font-size: 12px; padding: 2px 10px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.chip.strong { background: var(--ok-soft); color: var(--ok); font-weight: 700; }
.dl { display: flex; gap: 8px; }
.dl button::before { content: "↓ "; }

.result-body { display: grid; grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); }
.preview-wrap { padding: 12px; border-right: 1px solid var(--border); }
.preview {
  width: 100%; height: 420px; display: block;
  background: var(--surface-2); border-radius: 8px;
}
.preview .src { fill: none; stroke: var(--line); stroke-width: 1; vector-effect: non-scaling-stroke; }
.preview .area { stroke-width: 1.5; vector-effect: non-scaling-stroke; fill-opacity: .28; cursor: pointer; transition: fill-opacity .1s; }
.preview .area.hl { fill-opacity: .65; stroke-width: 3; }
.preview .num { font-weight: 700; text-anchor: middle; dominant-baseline: central; pointer-events: none; fill: var(--text); paint-order: stroke; stroke: var(--surface); }
.preview-legend { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); margin-top: 6px; }
.sw { display: inline-block; width: 14px; height: 10px; border-radius: 2px; margin-left: 8px; }
.sw.src { border-top: 2px solid var(--line); height: 0; }
.sw.area { background: hsl(210 70% 55% / .5); }

.table-wrap { max-height: 470px; overflow: auto; }
.area-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.area-table th, .area-table td { padding: 7px 12px; text-align: right; border-bottom: 1px solid var(--border); white-space: nowrap; }
.area-table th:first-child, .area-table td:first-child { text-align: center; }
.area-table thead th { position: sticky; top: 0; background: var(--surface); color: var(--muted); font-size: 12px; }
.area-table tbody tr { cursor: pointer; }
.area-table tbody tr:hover, .area-table tbody tr.hl { background: var(--accent-soft); }
.area-table tfoot td { font-weight: 700; position: sticky; bottom: 0; background: var(--surface); border-top: 2px solid var(--border); }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 6px; vertical-align: middle; }
.curve { color: var(--muted); font-size: 12px; }

/* ---------- スマホ・狭い画面 ---------- */
@media (max-width: 860px) {
  .work { grid-template-columns: 1fr; }
  .side { position: static; }
  .result-body { grid-template-columns: 1fr; }
  .preview-wrap { border-right: none; border-bottom: 1px solid var(--border); }
  .preview { height: 300px; }
  h1 { font-size: 24px; }
}
