:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --text: #1c2333;
  --muted: #6b7488;
  --border: #e3e8f0;
  --primary: #2f6df6;
  --primary-dark: #1f53cc;
  --success: #1f9d63;
  --warning: #d98a00;
  --danger: #d6443c;
  --shadow: 0 1px 2px rgba(20, 30, 60, .06), 0 6px 24px rgba(20, 30, 60, .06);
  --radius: 12px;
}

* { box-sizing: border-box; }

/* Alpine初期化前は x-cloak 要素を隠してちらつきを防ぐ */
[x-cloak] { display: none !important; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Kaku Gothic ProN",
    "Noto Sans JP", Meiryo, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
  flex-wrap: wrap;
}

.brand { text-decoration: none; font-weight: 800; font-size: 20px; letter-spacing: .04em; white-space: nowrap; }
.brand-mark { color: var(--primary); }
.brand-sub { color: var(--text); }
.topnav { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.topnav .btn { padding: 7px 12px; }
.user { color: var(--muted); font-size: 14px; padding-left: 6px; }

.container { max-width: 1180px; margin: 0 auto; padding: 28px 24px 64px; }

.page-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 20px; gap: 16px; flex-wrap: wrap; }
.page-head h1 { font-size: 22px; margin: 0; }
.page-sub { color: var(--muted); font-size: 14px; margin: 4px 0 0; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-pad { padding: 20px; }

/* buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 9px 16px; border-radius: 9px; border: 1px solid transparent;
  font-size: 14px; font-weight: 600; cursor: pointer; text-decoration: none;
  white-space: nowrap; line-height: 1.2;
  transition: background .15s, border-color .15s, opacity .15s;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-primary:disabled { opacity: .5; cursor: not-allowed; }
.btn-ghost { background: transparent; border-color: var(--border); color: var(--text); }
.btn-ghost:hover { background: var(--bg); }
.btn-success { background: var(--success); color: #fff; }
.btn-success:hover { filter: brightness(.95); }
.btn-success:disabled { opacity: .5; cursor: not-allowed; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { filter: brightness(.95); }
.btn-danger:disabled { opacity: .5; cursor: not-allowed; }
.btn-sm { padding: 5px 10px; font-size: 12px; border-radius: 7px; }
.btn-ghost.active { border-color: var(--primary); color: var(--primary-dark); background: #eef3ff; }

/* テンプレート登録: ドローキャンバス */
.tpl-canvas { position: relative; display: inline-block; line-height: 0; user-select: none; cursor: crosshair; max-width: 100%; background: #0f1320; border-radius: 8px; }
.tpl-canvas img { max-width: 100%; display: block; border-radius: 8px; }
.tpl-box { position: absolute; border: 2px solid; box-sizing: border-box; pointer-events: none; }
.tpl-box span { position: absolute; top: -18px; left: -2px; font-size: 10px; font-weight: 700; color: #fff; padding: 1px 5px; border-radius: 4px; line-height: 1.4; white-space: nowrap; }
.box-bansen { border-color: #2f6df6; background: rgba(47,109,246,.14); }
.box-bansen span { background: #2f6df6; }
.box-code { border-color: #16a34a; background: rgba(22,163,74,.14); }
.box-code span { background: #16a34a; }
.box-qty { border-color: #f59e0b; background: rgba(245,158,11,.16); }
.box-qty span { background: #f59e0b; }
.box-qty-col { border-color: #f59e0b; background: rgba(245,158,11,.08); border-style: dashed; }
.box-qty-active { border-color: #f59e0b; background: rgba(245,158,11,.35); border-width: 3px; z-index: 2; }
.box-qty-active span { background: #f59e0b; }
.box-draw { border-color: #ef4444; border-style: dashed; background: rgba(239,68,68,.12); }

/* 注文数枠のドラッグ調整 */
.box-qty-edit { border-color: #f59e0b; background: rgba(245,158,11,.16); pointer-events: auto; cursor: move; z-index: 3; }
.box-qty-edit.sel { background: rgba(245,158,11,.40); border-width: 3px; z-index: 5; }
.box-qty-edit span { top: -2px; left: -1px; font-size: 9px; padding: 0 3px; background: #b45309; }
.qh { position: absolute; width: 11px; height: 11px; background: #fff; border: 1px solid #b45309; border-radius: 2px; pointer-events: auto; z-index: 6; box-sizing: border-box; }
.qh-n { top: -6px; left: 50%; transform: translateX(-50%); cursor: ns-resize; }
.qh-s { bottom: -6px; left: 50%; transform: translateX(-50%); cursor: ns-resize; }
.qh-e { right: -6px; top: 50%; transform: translateY(-50%); cursor: ew-resize; }
.qh-w { left: -6px; top: 50%; transform: translateY(-50%); cursor: ew-resize; }
.qh-ne { top: -6px; right: -6px; cursor: nesw-resize; }
.qh-nw { top: -6px; left: -6px; cursor: nwse-resize; }
.qh-se { bottom: -6px; right: -6px; cursor: nwse-resize; }
.qh-sw { bottom: -6px; left: -6px; cursor: nesw-resize; }

/* グリッド一括: 表ブロックとプレビュー格子 */
.box-block { border-color: #7c3aed; border-style: solid; background: rgba(124,58,237,.10); }
.box-block-done { border-color: #7c3aed; border-style: dashed; background: rgba(124,58,237,.05); }
.box-block-done span { background: #7c3aed; }

/* 全画面 拡大編集モーダル */
.zoom-modal { position: fixed; inset: 0; z-index: 1000; display: flex; flex-direction: column; background: rgba(15,19,32,.93); }
.zoom-bar { display: flex; align-items: center; gap: 10px; padding: 10px 16px; background: #11151f; color: #fff; flex-wrap: wrap; }
.zoom-bar .zoom-hint { font-size: 13px; color: #c7cedb; }
.zoom-bar .zbtn { background: #2a3140; color: #fff; border: 1px solid #3a4252; border-radius: 7px; padding: 5px 12px; cursor: pointer; font-size: 15px; line-height: 1; }
.zoom-bar .zbtn:hover { background: #39414f; }
.zoom-body { flex: 1; display: flex; min-height: 0; }
.zoom-stage { flex: 1; overflow: auto; padding: 16px; background: #0f1320; }
.zoom-stage .tpl-canvas { max-width: none; }
.zoom-side { width: 340px; flex: 0 0 340px; overflow-y: auto; background: var(--surface); padding: 16px; border-left: 1px solid var(--border); }
@media (max-width: 760px) { .zoom-side { display: none; } }
.grid-line-v { position: absolute; width: 0; border-left: 1px dashed rgba(124,58,237,.7); pointer-events: none; }
.grid-line-h { position: absolute; height: 0; border-top: 1px dashed rgba(124,58,237,.45); pointer-events: none; }
.grid-col-label { position: absolute; transform: translate(-50%, -120%); font-size: 10px; font-weight: 700; color: #fff; padding: 1px 6px; border-radius: 4px; white-space: nowrap; pointer-events: none; background: #64748b; }
.grid-col-label.role-rank { background: #0ea5e9; }
.grid-col-label.role-volume { background: #6366f1; }
.grid-col-label.role-code { background: #16a34a; }
.grid-col-label.role-quantity { background: #f59e0b; }
.grid-col-label.role-ignore { background: #94a3b8; }

/* グリッド設定パネル */
.grid-config { border: 1px solid var(--border); border-radius: 9px; padding: 12px; background: var(--surface); }
.grid-config-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.grid-col-row { display: flex; align-items: center; gap: 8px; margin-top: 6px; flex-wrap: wrap; }
.grid-col-idx { display: inline-flex; align-items: center; justify-content: center; width: 20px; height: 20px; border-radius: 50%; background: #eef1f7; font-size: 11px; font-weight: 700; color: #64748b; flex: 0 0 20px; }
.grid-config-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; padding-top: 10px; border-top: 1px dashed var(--border); }

/* 多数行のコンパクト表（全幅パネル） */
.lines-panel { margin-top: 20px; }
.lines-panel-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 10px; }
.lines-filter { width: 200px; min-width: 140px; }
.lines-table-wrap { max-height: 520px; overflow: auto; border: 1px solid var(--border); border-radius: 9px; }
.lines-table { width: 100%; border-collapse: collapse; font-size: 13px; table-layout: fixed; }
.lines-table th, .lines-table td { padding: 4px 8px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; height: 34px; }
.lines-table th { position: sticky; top: 0; background: #f7f9fc; color: var(--muted); font-size: 11px; font-weight: 700; z-index: 1; }
.lines-table tr.row-hover { background: #fff8eb; }
.lines-table .lines-idx { color: var(--muted); text-align: center; }
.lines-table .lines-del { text-align: center; padding: 2px 4px; }
.lines-table-wrap { max-height: calc(100vh - 220px); }
.empty-lines { padding: 24px 12px; text-align: center; color: var(--muted); font-size: 13px; border: 1px dashed var(--border); border-radius: 9px; background: #fafbfd; }
.input-xs.cell-bad { background: #fff0f0; border-color: #e57373; }
.bad-banner { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; background: #fff7ed; border: 1px solid #fdba74; color: #9a3412; border-radius: 8px; padding: 6px 10px; margin: 0 0 8px; font-size: 12px; }
.bad-toggle { display: inline-flex; align-items: center; gap: 4px; cursor: pointer; white-space: nowrap; }
.grid-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.grid-list-item { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border: 1px solid var(--border); border-radius: 7px; background: #fff; font-size: 12px; }
.grid-list-item:hover { background: #fff8eb; }
.grid-list-name { font-weight: 700; white-space: nowrap; }
.grid-list-roles { flex: 1; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.box-block-hover { outline: 3px solid #f59e0b; outline-offset: 1px; background: rgba(245,158,11,0.12); }

/* テンプレ編集: 上段=設定 / 下段=画像+商品行 */
.settings-top { margin-bottom: 18px; }
.split-lower { grid-template-columns: 1.05fr 0.95fr; align-items: start; }
.img-col { position: sticky; top: 12px; align-self: start; max-height: calc(100vh - 24px); overflow: auto; }
@media (max-width: 920px) { .img-col { position: static; max-height: none; } }
.input-xs { width: 100%; padding: 4px 6px; font-size: 12px; border: 1px solid var(--border); border-radius: 5px; box-sizing: border-box; min-width: 0; }
.btn-xs { padding: 2px 8px; font-size: 11px; border-radius: 5px; line-height: 1.5; min-width: 0; }
.grid-role-select { flex: 1; min-width: 100px; }
.grid-w-label { display: inline-flex; align-items: center; gap: 4px; white-space: nowrap; flex: 0 0 auto; }
.grid-config-done { margin-top: 8px; background: #f7fdf9; border-color: #b9e6cc; }

/* AI自動取込ボックス */
.ai-detect-box { border: 1px solid #b9e6cc; background: #f1fbf5; border-radius: 9px; padding: 12px; }

.row-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.row-define { display: flex; align-items: center; gap: 10px; }
.line-card { border: 1px solid var(--border); border-radius: 9px; padding: 12px; margin-bottom: 10px; background: var(--surface); }
.line-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; padding-bottom: 8px; border-bottom: 1px dashed var(--border); }
.line-row { display: flex; align-items: center; gap: 10px; margin-top: 8px; flex-wrap: wrap; }
.line-label { font-size: 12px; font-weight: 700; color: var(--text); width: 78px; flex: 0 0 78px; }
.define-row { display: inline-flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.input-sm { width: auto; padding: 6px 8px; font-size: 13px; }

/* ステータスチップ */
.chip { display: inline-flex; align-items: center; gap: 5px; padding: 3px 9px; border-radius: 999px; font-size: 11px; font-weight: 700; white-space: nowrap; }
.chip::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.chip-ok { background: #e6f6ee; color: var(--success); }
.chip-todo { background: #eef1f7; color: #8a93a6; }

/* 操作ガイド */
.guide { background: #eef3ff; border: 1px solid #d4e0fb; border-radius: var(--radius); padding: 14px 18px; margin-bottom: 18px; }
.guide-title { font-weight: 800; color: var(--primary-dark); font-size: 14px; margin-bottom: 8px; }
.guide-steps { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.guide-steps li { display: flex; align-items: flex-start; gap: 10px; font-size: 14px; color: var(--text); }
.guide-steps li.done { color: var(--muted); }
.guide-steps li.done .step-no { background: var(--success); }
.guide-steps .step-no { flex: 0 0 22px; width: 22px; height: 22px; border-radius: 50%; background: var(--primary); color: #fff; font-size: 12px; font-weight: 700; display: inline-flex; align-items: center; justify-content: center; }

/* 指定中バナー */
.draw-banner { padding: 10px 14px; border-radius: 9px; font-size: 14px; font-weight: 600; margin-bottom: 10px; background: #fff5e0; color: var(--warning); border: 1px solid #f3dca6; }
.draw-banner.ready { background: #e6f6ee; color: var(--success); border-color: #b9e6cc; }

.dropzone-lg { padding: 48px 28px; }

/* dropzone */
.dropzone {
  border: 2px dashed var(--border); border-radius: var(--radius);
  padding: 28px; text-align: center; color: var(--muted);
  background: var(--surface); cursor: pointer; transition: border-color .15s, background .15s;
}
.dropzone.drag { border-color: var(--primary); background: #eef3ff; color: var(--primary-dark); }
.dropzone strong { color: var(--text); }

/* table */
.table { width: 100%; border-collapse: collapse; background: var(--surface); }
.table th, .table td { text-align: left; padding: 12px 14px; border-bottom: 1px solid var(--border); font-size: 14px; }
.table th { color: var(--muted); font-weight: 600; font-size: 12px; letter-spacing: .03em; text-transform: uppercase; }
.table tbody tr:hover { background: #fafbff; }
.table tbody tr.clickable { cursor: pointer; }
.mono { font-variant-numeric: tabular-nums; font-family: "SFMono-Regular", Consolas, monospace; }

/* badges */
.badge { display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: 999px; font-size: 12px; font-weight: 700; }
.badge::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.badge-todo { background: #eef1f7; color: #5b6577; }
.badge-processing { background: #fff5e0; color: var(--warning); }
.badge-error { background: #fdeceb; color: var(--danger); }
.badge-confirmed { background: #e6f6ee; color: var(--success); }
.badge-claim { background: #eef3ff; color: var(--primary-dark); }

/* 読み込み・処理中表示 */
.loading-banner {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 16px; border-radius: var(--radius); margin-bottom: 16px;
  font-size: 14px; font-weight: 600;
}
.loading-banner-info { background: #eef3ff; color: var(--primary-dark); border: 1px solid #c7d7fe; }
.loading-banner-warn { background: #fff8e6; color: #92400e; border: 1px solid #fcd34d; }
.spinner {
  display: inline-block; width: 18px; height: 18px; flex: 0 0 18px;
  border: 2px solid currentColor; border-right-color: transparent;
  border-radius: 50%; animation: spin 0.75s linear infinite;
}
.spinner-lg { width: 36px; height: 36px; flex: 0 0 36px; border-width: 3px; }
@keyframes spin { to { transform: rotate(360deg); } }
.page-loading-overlay {
  position: fixed; inset: 0; z-index: 2000;
  background: rgba(15, 19, 32, 0.45);
  display: flex; align-items: center; justify-content: center;
}
.page-loading-box {
  background: var(--surface); border-radius: 12px; padding: 28px 36px;
  box-shadow: var(--shadow); text-align: center; max-width: 90vw;
}
.page-loading-text { margin: 14px 0 0; font-size: 15px; font-weight: 600; color: var(--text); }
.dropzone-disabled { opacity: 0.55; pointer-events: none; cursor: wait; }
.table tbody tr.row-processing { background: #fffbeb; }
.table tbody tr.row-processing .badge-processing::before {
  animation: pulse 1.2s ease-in-out infinite;
}
@keyframes pulse { 50% { opacity: 0.35; } }

/* layout split for detail */
.split { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; align-items: start; }
@media (max-width: 920px) { .split { grid-template-columns: 1fr; } }

.viewer-toolbar { display: flex; gap: 8px; margin-bottom: 12px; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 9px; overflow: hidden; }
.seg button { border: none; background: var(--surface); padding: 8px 14px; font-size: 13px; font-weight: 600; cursor: pointer; color: var(--muted); }
.seg button.active { background: var(--primary); color: #fff; }

.viewer { background: #0f1320; border-radius: var(--radius); overflow: auto; max-height: 72vh; padding: 10px; text-align: center; }
.viewer img { max-width: 100%; border-radius: 6px; }

/* 拡大鏡（ホバーズーム） */
.zoom-wrap { position: relative; display: inline-block; line-height: 0; cursor: crosshair; }
.zoom-wrap img.zoomable { display: block; }
.loupe {
  position: absolute; pointer-events: none; border-radius: 50%;
  border: 2px solid rgba(255,255,255,.9); box-shadow: 0 4px 18px rgba(0,0,0,.45);
  background-repeat: no-repeat; background-color: #fff; z-index: 20;
}
.excerpt-grid { display: flex; flex-direction: column; gap: 10px; }
.excerpt-grid .cell { background: #fff; border-radius: 6px; padding: 6px; }
.excerpt-grid .cell-label { font-size: 11px; color: var(--muted); margin-bottom: 4px; text-align: left; }

/* field rows */
.field { margin-bottom: 16px; }
.field label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; font-weight: 600; }
.input { width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: 9px; font-size: 15px; }
.input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(47,109,246,.15); }
.input.flagged { border-color: var(--danger); background: #fff7f7; }

.compare-row { display: grid; grid-template-columns: 90px 1fr 1fr; gap: 10px; align-items: center; padding: 10px 0; border-bottom: 1px solid var(--border); }
.compare-row .col-img img { max-height: 38px; border: 1px solid var(--border); border-radius: 6px; background:#fff; }
.compare-head { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing:.03em; }
.ocr-val { font-size: 16px; font-weight: 700; }
.ocr-val .conf { font-size: 11px; color: var(--muted); font-weight: 500; margin-left: 6px; }
.row-flag { color: var(--danger); font-size: 11px; font-weight: 700; }

/* LLM抽出の判定状態チップ */
.st-chip {
  display: inline-block; font-size: 11px; font-weight: 700;
  padding: 1px 8px; border-radius: 999px; line-height: 1.6;
}
.st-confirmed { background: #e6f6ee; color: var(--success); }
.st-inferred  { background: #fff5e0; color: var(--warning); }
.st-uncertain { background: #fdeceb; color: var(--danger); }
.st-excluded  { background: #eef0f3; color: var(--muted); }

.alert { padding: 12px 16px; border-radius: 9px; font-size: 14px; margin-bottom: 16px; }
.alert-error { background: #fdeceb; color: var(--danger); border: 1px solid #f6cbc8; }
.alert-warn { background: #fff5e0; color: var(--warning); border: 1px solid #f3dca6; }

.actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 20px; }
.toolbar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.hint { color: var(--muted); font-size: 13px; }
.empty { text-align: center; color: var(--muted); padding: 48px 0; }

/* login */
.login-wrap { min-height: 80vh; display: flex; align-items: center; justify-content: center; }
.login-card { width: 360px; max-width: 92vw; }
.login-card h1 { font-size: 20px; margin: 0 0 4px; }
.login-card p { margin: 0 0 20px; color: var(--muted); font-size: 14px; }
