@font-face { font-family: 'Zen Maru Gothic'; src: url('fonts/ZenMaruGothic-Regular-sub.ttf') format('truetype'); font-weight: 400; font-display: swap; }
[hidden] { display: none !important; }
@font-face { font-family: 'Zen Maru Gothic'; src: url('fonts/ZenMaruGothic-Black-sub.ttf') format('truetype'); font-weight: 700 900; font-display: swap; }

:root {
  --bg: #f8f3ea; --surface: #fdfbf6; --surface-2: #f0e8da; --line: rgba(32,30,29,.14);
  --text: #201e1d; --muted: #6f675b; --accent: #b2622d; --accent-soft: #ffe1d0; --accent-ink: #fff;
  --green: #728157; --green-soft: #e1eecc; --warn: #a86b00; --warn-soft: #fff1cc; --err: #b3261e; --err-soft: #fde4e1;
  --stage: #fffdf8; --grid: rgba(120,100,70,.10); --grid-5: rgba(120,100,70,.20);
  --radius: 14px; --shadow: 0 1px 2px rgba(40,30,20,.06), 0 4px 16px rgba(40,30,20,.06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1f1c19; --surface: #29251f; --surface-2: #332e27; --line: rgba(255,245,230,.12);
    --text: #f3ece0; --muted: #b3a894; --accent: #e08a52; --accent-soft: #4a2e1c; --accent-ink: #1f1c19;
    --green: #a9bb88; --green-soft: #2f3a24; --warn: #f0b84a; --warn-soft: #3d3016; --err: #ff8a80; --err-soft: #43201d;
    --stage: #fffdf8; --grid: rgba(120,100,70,.10); --grid-5: rgba(120,100,70,.20);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] { --bg: #1f1c19; --surface: #29251f; --surface-2: #332e27; --line: rgba(255,245,230,.12); --text: #f3ece0; --muted: #b3a894; --accent: #e08a52; --accent-soft: #4a2e1c; --accent-ink: #1f1c19; color-scheme: dark; }

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--text); font-family: 'Zen Maru Gothic', 'Hiragino Maru Gothic ProN', 'Hiragino Sans', 'Yu Gothic', sans-serif; font-size: 15px; line-height: 1.6; -webkit-text-size-adjust: 100%; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 1px solid var(--line); background: var(--surface); border-radius: 10px; padding: 7px 12px; transition: background .15s, border-color .15s, transform .05s; }
button:hover { border-color: var(--accent); }
button:active { transform: translateY(1px); }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 700; }
button.primary:hover { filter: brightness(1.06); }
button.ghost { background: transparent; }
button.small, .small button { font-size: 13px; padding: 5px 10px; }
button.wide { width: 100%; margin-top: 8px; }
button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, summary:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
input[type=search], input:not([type]), input[type=text], input[type=number], select, textarea { width: 100%; border: 1px solid var(--line); background: var(--surface); border-radius: 10px; padding: 8px 10px; }
textarea { resize: vertical; }
input[type=range] { accent-color: var(--accent); width: 100%; }
input[type=checkbox] { accent-color: var(--accent); width: 18px; height: 18px; vertical-align: -3px; }
hr { border: 0; border-top: 1px solid var(--line); margin: 14px 0; }
.muted { color: var(--muted); }
.hint { color: var(--muted); font-size: 13.5px; margin: 6px 0 10px; }
.small { font-size: 13px; }

/* 上の帯 */
.top { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 18px; border-bottom: 1px solid var(--line); background: var(--bg); position: sticky; top: 0; z-index: 5; }
.brand { display: flex; align-items: center; gap: 10px; }
.logo { width: 36px; height: 36px; color: var(--accent); }
.brand h1 { font-size: 20px; margin: 0; letter-spacing: .04em; font-weight: 800; }
.tagline { margin: 0; font-size: 12px; color: var(--muted); }
.top-actions { display: flex; gap: 6px; }

/* 3列 */
.layout { display: grid; grid-template-columns: 320px minmax(0, 1fr) 360px; gap: 14px; padding: 14px; height: calc(100dvh - var(--top-h, 64px)); }
.layout > * { min-width: 0; }
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); overflow: auto; min-height: 0; }
.left, .right { padding: 12px; }
.center { display: flex; flex-direction: column; padding: 0; overflow: hidden; }

/* タブ */
.tabs { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; background: var(--surface-2); padding: 4px; border-radius: 12px; margin-bottom: 10px; }
.tabs button { border: 0; background: transparent; padding: 7px 2px; font-size: 13px; border-radius: 9px; white-space: nowrap; letter-spacing: -.02em; }
.tabs button.on { background: var(--surface); box-shadow: var(--shadow); font-weight: 700; color: var(--accent); }
.tabpane { display: none; }
.tabpane.on { display: block; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0; }
.chips button { border-radius: 999px; padding: 4px 11px; font-size: 13px; }
.chips button.on { background: var(--accent-soft); border-color: var(--accent); color: var(--text); font-weight: 700; }
.chips.small button { font-size: 12.5px; padding: 3px 9px; }

/* 一覧 */
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(132px, 1fr)); gap: 8px; }
.card { text-align: left; padding: 8px; border-radius: 12px; background: var(--surface); display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.card.on { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.card .thumb { aspect-ratio: 4 / 3; background: var(--stage); border-radius: 8px; display: grid; place-items: center; overflow: hidden; border: 1px solid var(--line); }
.card .thumb svg { width: 100%; height: 100%; }
.card .name { font-size: 13px; font-weight: 700; line-height: 1.35; }
.card .meta { font-size: 11.5px; color: var(--muted); display: flex; gap: 6px; flex-wrap: wrap; }
.tag-wild { color: var(--accent); }
.cards { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; }
.cards .card { flex-direction: row; align-items: center; }
.cards .card .thumb { width: 84px; flex: none; }
.cards .card .why { font-size: 12px; color: var(--muted); }
.cards .card .body { min-width: 0; flex: 1; }
.cards .card .del { margin-left: auto; }

/* 中央 */
.viewbar { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 14px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.title-line { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.title-line h2 { font-size: 17px; margin: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.view-tools { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.view-tools button { padding: 5px 10px; font-size: 13px; }
.view-tools button.on { background: var(--accent-soft); border-color: var(--accent); }
.layers { position: relative; }
.layers summary { list-style: none; cursor: pointer; border: 1px solid var(--line); border-radius: 10px; padding: 5px 10px; font-size: 13px; background: var(--surface); }
.layers summary::-webkit-details-marker { display: none; }
.layers > div { position: absolute; right: 0; top: 36px; z-index: 4; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow); padding: 8px 12px; width: 200px; }
.layers label { display: flex; gap: 8px; align-items: center; font-size: 13.5px; padding: 3px 0; }
.layers .sw { width: 18px; height: 3px; border-radius: 2px; }
.stage { flex: 1; position: relative; background: var(--stage); overflow: hidden; touch-action: none; cursor: grab; }
.stage:active { cursor: grabbing; }
.stage svg { width: 100%; height: 100%; display: block; font-family: 'Zen Maru Gothic', 'Hiragino Maru Gothic ProN', sans-serif; }
.stage-msg { position: absolute; left: 12px; bottom: 10px; font-size: 12px; color: #6f675b; background: rgba(255,253,248,.85); padding: 2px 8px; border-radius: 8px; pointer-events: none; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 14px; padding: 8px 14px; border-top: 1px solid var(--line); font-size: 12px; color: var(--muted); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend svg, .legend-list svg { width: 26px; height: 8px; flex: none; }
.legend span, .legend-list li { color: var(--muted); }
.save-form { display: flex; gap: 6px; margin-top: 8px; }
.link-box { margin-top: 8px; font-size: 12px; }
button.danger { background: var(--err-soft); color: var(--err); border-color: var(--err); }
.center { scroll-margin-top: calc(var(--top-h, 64px) + 6px); }

/* 右 */
.box { border: 1px solid var(--line); border-radius: 12px; margin-bottom: 10px; background: var(--surface); }
.box > summary { font-weight: 700; padding: 9px 12px; cursor: pointer; display: flex; align-items: center; gap: 8px; }
.box > *:not(summary) { padding: 0 12px 12px; }
.params { display: flex; flex-direction: column; gap: 10px; }
.param label.lab { display: flex; justify-content: space-between; gap: 8px; font-size: 13.5px; }
.param .ctl { display: grid; grid-template-columns: 1fr 92px; gap: 8px; align-items: center; }
.param .ctl input[type=number] { padding: 5px 6px; text-align: right; }
.param .unit { font-size: 11px; color: var(--muted); }
.param .help { font-size: 12px; color: var(--muted); margin-top: 2px; }
.param .shape-btn { display: flex; align-items: center; gap: 10px; width: 100%; text-align: left; }
.param .shape-btn svg { width: 40px; height: 40px; flex: none; }
.param.bool label { display: flex; gap: 8px; align-items: center; }
.row { display: flex; align-items: center; gap: 10px; margin: 8px 0; font-size: 14px; }
.row select, .row input[type=range] { flex: 1; }
.row output { min-width: 3em; text-align: right; font-size: 13px; }
.row-btns { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 10px; }
.badge { font-size: 12px; border-radius: 999px; padding: 1px 9px; font-weight: 700; }
.badge.ok { background: var(--green-soft); color: var(--green); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.err { background: var(--err-soft); color: var(--err); }
.checks { list-style: none; margin: 0; padding: 0 12px 12px; display: flex; flex-direction: column; gap: 6px; font-size: 13.5px; }
.checks li { padding: 6px 10px; border-radius: 10px; background: var(--surface-2); }
.checks li.error { background: var(--err-soft); color: var(--err); }
.checks li.warn { background: var(--warn-soft); color: var(--warn); }
.checks li.ok { background: var(--green-soft); color: var(--green); }
.export { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.export button { font-size: 13px; padding: 7px 8px; }
.export button.primary { grid-column: 1 / -1; font-size: 15px; padding: 10px; }
table.mat { width: 100%; border-collapse: collapse; font-size: 13px; margin: 4px 0 10px; }
table.mat th, table.mat td { border-bottom: 1px solid var(--line); padding: 5px 4px; text-align: left; vertical-align: top; }
table.mat td.n { text-align: right; white-space: nowrap; }
.cost { display: grid; grid-template-columns: 1fr 110px; gap: 6px 8px; align-items: center; font-size: 13px; }
.cost input { padding: 4px 6px; text-align: right; }
.cost .total { font-weight: 800; font-size: 15px; }
.yarn-dot { display: inline-block; width: 12px; height: 12px; border-radius: 3px; border: 1px solid var(--line); vertical-align: -1px; margin-right: 4px; }
#steps ol { padding-left: 1.3em; margin: 4px 0; font-size: 14px; }
#steps li { margin-bottom: 6px; }
#steps h5 { margin: 10px 0 4px; font-size: 13px; }

/* 写真 */
.file { display: block; }
.file input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.file span { display: block; text-align: center; border: 2px dashed var(--line); border-radius: 12px; padding: 18px; cursor: pointer; font-weight: 700; }
.file:hover span { border-color: var(--accent); }
.photo-box { position: relative; margin-top: 10px; border-radius: 10px; overflow: hidden; background: #fff; border: 1px solid var(--line); }
.photo-box canvas { width: 100%; display: block; }
.photo-box canvas.mask { position: absolute; inset: 0; height: 100%; pointer-events: none; }
.sep-prev { margin-top: 8px; }
.sep-prev canvas { width: 100%; border-radius: 10px; border: 1px solid var(--line); }

/* ダイアログ */
.dlg { border: 1px solid var(--line); border-radius: 18px; background: var(--surface); color: var(--text); padding: 16px 18px; max-width: min(560px, 94vw); width: 100%; max-height: 88dvh; box-shadow: 0 20px 60px rgba(0,0,0,.25); }
.dlg::backdrop { background: rgba(20,15,10,.45); }
.wide-dlg { max-width: min(900px, 96vw); }
.dlg-head { display: flex; justify-content: space-between; align-items: center; margin: -4px 0 6px; }
.dlg-head h3 { margin: 0; font-size: 18px; }
.shape-tools { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.text-shape { display: flex; gap: 6px; }
.text-shape button { white-space: nowrap; }
.shape-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(92px, 1fr)); gap: 8px; margin-top: 8px; }
.shape-grid button { display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 8px 4px; font-size: 12px; }
.shape-grid button.on { border-color: var(--accent); background: var(--accent-soft); }
.shape-grid svg { width: 56px; height: 56px; }
.help ol { padding-left: 1.3em; }
.help li { margin-bottom: 6px; }
.legend-list { list-style: none; padding: 0; display: grid; grid-template-columns: 1fr 1fr; gap: 4px 12px; font-size: 13px; }
.legend-list li { display: flex; gap: 8px; align-items: center; }
.calib { display: flex; flex-direction: column; gap: 8px; align-items: flex-start; }
.calib-card { border: 2px solid var(--accent); border-radius: 3.2mm; display: grid; place-items: center; color: var(--muted); font-size: 12px; }

/* プロジェクター */
.projector { position: fixed; inset: 0; background: #000; z-index: 50; }
.projector svg { width: 100%; height: 100%; }
.proj-tools { position: absolute; right: 12px; top: 12px; display: flex; gap: 6px; opacity: .35; transition: opacity .2s; flex-wrap: wrap; justify-content: flex-end; }
@media (hover: none) { .proj-tools { opacity: .9; } }
.proj-tools button.on { background: #6a4a2a; border-color: #e08a52; }
.proj-tools:hover { opacity: 1; }
.proj-tools button { background: #222; color: #eee; border-color: #444; }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(20px); background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 12px; opacity: 0; pointer-events: none; transition: all .25s; z-index: 60; max-width: 90vw; font-size: 14px; }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* 画面の幅ごと */
@media (max-width: 1240px) {
  /* タブレット：選ぶ欄は上に、図と調整は横に並べ、図は画面に止めておく */
  .layout { grid-template-columns: minmax(0, 1fr) 330px; grid-template-rows: auto auto; height: auto; min-height: calc(100dvh - var(--top-h, 64px)); align-items: start; }
  .left { grid-column: 1 / -1; max-height: 44dvh; }
  .center { grid-column: 1; height: calc(100dvh - var(--top-h, 64px) - 28px); position: sticky; top: calc(var(--top-h, 64px) + 14px); }
  .right { grid-column: 2; overflow: visible; }
}
@media (max-width: 820px) {
  body { font-size: 14.5px; }
  .top { padding: 8px 12px; }
  .tagline { display: none; }
  .top-actions button { padding: 6px 8px; font-size: 13px; }
  .layout { display: flex; flex-direction: column; padding: 10px; gap: 10px; align-items: stretch; }
  .left { position: static; max-height: none; }
  /* スマホ：一覧は横にスワイプ（ページの中で縦に二重にスクロールしない） */
  .gallery { grid-auto-flow: column; grid-template-columns: none; grid-template-rows: repeat(2, auto); grid-auto-columns: 148px; overflow-x: auto; width: 100%; scroll-snap-type: x proximity; padding-bottom: 6px; }
  .gallery .card { scroll-snap-align: start; }
  .center { height: 62dvh; min-height: 360px; position: static; }
  .title-line h2 { white-space: normal; font-size: 15.5px; }
  .title-line { flex-wrap: wrap; }
  .shape-tools { grid-template-columns: 1fr; }
  .export { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 420px) {
  .top-actions button span { display: none; }
  #btnBook, #btnSettings { font-size: 0; }
  #btnBook::before { content: '📒'; font-size: 16px; }
  #btnSettings::before { content: '⚙'; font-size: 16px; }
}

/* 指で押す端末は押す所を大きく */
@media (pointer: coarse) {
  button, .chips button { min-height: 40px; }
  .top-actions button { min-width: 42px; min-height: 42px; }
  .cost input { min-height: 38px; }
  .view-tools button, .layers summary { min-height: 38px; }
}
@media (max-width: 820px) and (orientation: landscape) {
  .gallery { grid-auto-columns: 160px; grid-template-rows: auto; }
}
