:root {
    --ink: #172033;
    --muted: #68758b;
    --line: #dce4ef;
    --surface: #ffffff;
    --background: #f4f7fb;
    --primary: #5b36e8;
    --primary-dark: #4324bd;
    --teal: #0f9f83;
    --amber: #f59e0b;
    --red: #dc4c64;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    min-height: 100vh;
    color: var(--ink);
    background:
        radial-gradient(circle at 10% -10%, rgba(91, 54, 232, .09), transparent 28rem),
        radial-gradient(circle at 95% 5%, rgba(15, 159, 131, .08), transparent 24rem),
        var(--background);
    font-family: 'Noto Sans Thai', Tahoma, Arial, sans-serif;
}

button, input { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }

.page-shell {
    width: min(1480px, calc(100% - 40px));
    margin: 0 auto;
    padding: 22px 0 64px;
}

.topbar {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    gap: 18px;
    min-height: 70px;
}

.topbar > :last-child { justify-self: end; }

.soft-button {
    width: max-content;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 42px;
    padding: 9px 16px;
    color: #354159;
    background: rgba(255, 255, 255, .9);
    border: 1px solid var(--line);
    border-radius: 14px;
    box-shadow: 0 5px 16px rgba(24, 39, 75, .05);
    font-size: 14px;
    font-weight: 700;
    text-decoration: none;
    transition: .18s ease;
}

.soft-button:hover { transform: translateY(-2px); border-color: #b7c3d5; }

.brand-lockup { display: flex; align-items: center; gap: 13px; text-align: left; }
.brand-icon {
    display: grid;
    place-items: center;
    width: 48px;
    height: 48px;
    border-radius: 16px;
    color: white;
    background: linear-gradient(145deg, #6d4af2, #ec4899);
    box-shadow: 0 10px 25px rgba(91, 54, 232, .2);
    font-size: 23px;
}
.brand-lockup h1 { margin: 0; font-size: clamp(20px, 2.2vw, 30px); font-weight: 900; line-height: 1.2; }
.brand-lockup p { margin: 3px 0 0; color: var(--muted); font-size: 13px; font-weight: 600; }

.safety-banner {
    display: grid;
    grid-template-columns: auto 1fr auto;
    align-items: center;
    gap: 15px;
    margin: 18px 0;
    padding: 18px 21px;
    color: #075f53;
    background: linear-gradient(105deg, #e8fbf5, #f4fffc);
    border: 1px solid #a7ead8;
    border-radius: 20px;
}
.safety-icon { font-size: 27px; }
.safety-banner strong { font-size: 17px; font-weight: 900; }
.safety-banner p { margin: 3px 0 0; color: #3b746c; font-size: 13px; line-height: 1.55; }
code { padding: 1px 5px; color: #5130c6; background: rgba(255,255,255,.8); border-radius: 6px; font-family: ui-monospace, monospace; font-size: .92em; }
.safe-pill, .read-only-badge, .ready-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 30px;
    padding: 5px 10px;
    border-radius: 999px;
    font-size: 11px;
    font-weight: 900;
    letter-spacing: .05em;
    white-space: nowrap;
}
.safe-pill { color: #087461; background: #ccf5e9; }
.read-only-badge { color: #5d43bd; background: #f0ecff; }
.ready-badge { color: #087461; background: #d9f8ef; }

.flow-strip {
    display: flex;
    align-items: stretch;
    justify-content: center;
    gap: 12px;
    margin: 18px 0;
}
.flow-card {
    flex: 1;
    display: flex;
    align-items: center;
    gap: 12px;
    max-width: 340px;
    min-height: 76px;
    padding: 14px 17px;
    background: rgba(255,255,255,.82);
    border: 1px solid var(--line);
    border-radius: 18px;
}
.flow-number {
    display: grid;
    place-items: center;
    flex: 0 0 34px;
    width: 34px;
    height: 34px;
    color: white;
    background: var(--primary);
    border-radius: 11px;
    font-weight: 900;
}
.flow-card strong, .flow-card small { display: block; }
.flow-card strong { font-size: 14px; font-weight: 800; }
.flow-card small { margin-top: 2px; color: var(--muted); font-size: 11px; }
.flow-arrow { align-self: center; color: #a3aec0; font-size: 22px; font-weight: 800; }

.panel {
    margin-top: 18px;
    overflow: hidden;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 24px;
    box-shadow: 0 16px 40px rgba(32, 48, 83, .07);
}

.panel-heading {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 20px;
    padding: 23px 26px 18px;
    border-bottom: 1px solid #edf1f6;
}
.eyebrow { color: var(--primary); font-size: 11px; font-weight: 900; letter-spacing: .08em; }
.panel-heading h2, .import-copy h2 { margin: 4px 0 0; font-size: 22px; font-weight: 900; }
.panel-heading p, .import-copy p { margin: 3px 0 0; color: var(--muted); font-size: 13px; }

.scan-controls {
    display: grid;
    grid-template-columns: minmax(180px, .7fr) minmax(180px, .7fr) minmax(420px, 1.7fr) auto;
    align-items: end;
    gap: 15px;
    padding: 22px 26px 26px;
}
.field > span, .department-fieldset legend { display: block; margin-bottom: 7px; color: #48546a; font-size: 12px; font-weight: 800; }
.field input {
    width: 100%;
    height: 48px;
    padding: 0 13px;
    color: var(--ink);
    background: #f9fbfd;
    border: 1px solid #ced8e5;
    border-radius: 13px;
    outline: none;
    font-weight: 700;
}
.field input:focus { border-color: #8d72ee; box-shadow: 0 0 0 3px rgba(91, 54, 232, .11); }
.department-fieldset {
    position: relative;
    display: flex;
    align-items: center;
    gap: 8px;
    height: 48px;
    margin: 0;
    padding: 4px;
    background: #f1f4f8;
    border: 0;
    border-radius: 13px;
}
.department-fieldset legend { position: absolute; top: -25px; left: 0; transform: none; }
.department-fieldset label { flex: 1; height: 40px; cursor: pointer; }
.department-fieldset input { position: absolute; opacity: 0; pointer-events: none; }
.department-fieldset span {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    padding: 0 10px;
    color: #657187;
    background: transparent;
    border: 1px solid transparent;
    border-radius: 10px;
    font-size: 12px;
    font-weight: 800;
    white-space: nowrap;
    transition: .15s ease;
}
.department-fieldset input:checked + span { color: #4b2bc1; background: white; border-color: #ddd5ff; box-shadow: 0 3px 10px rgba(35, 44, 70, .07); }

.primary-button, .import-button, .open-v2-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    height: 48px;
    padding: 0 20px;
    color: white;
    background: linear-gradient(135deg, #6544ed, #5130cf);
    border: 0;
    border-radius: 13px;
    box-shadow: 0 8px 19px rgba(91, 54, 232, .22);
    font-size: 13px;
    font-weight: 900;
    cursor: pointer;
    transition: .18s ease;
    white-space: nowrap;
}
.primary-button:hover, .import-button:hover, .open-v2-button:hover { transform: translateY(-2px); filter: brightness(1.03); }
.primary-button:disabled, .import-button:disabled { opacity: .45; cursor: not-allowed; transform: none; filter: none; }

.progress-box {
    display: flex;
    align-items: center;
    gap: 13px;
    margin: 0 26px 24px;
    padding: 14px 17px;
    color: #5130c6;
    background: #f5f2ff;
    border: 1px solid #ddd4ff;
    border-radius: 15px;
}
.progress-box strong, .progress-box small { display: block; }
.progress-box strong { font-size: 13px; }
.progress-box small { margin-top: 2px; color: #7c6cae; font-size: 11px; }
.spinner { width: 23px; height: 23px; border: 3px solid #d9d0fa; border-top-color: var(--primary); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.summary-grid {
    display: grid;
    grid-template-columns: repeat(6, 1fr);
    gap: 12px;
    padding: 20px 24px;
    background: #fbfcfe;
    border-bottom: 1px solid #edf1f6;
}
.summary-grid article {
    min-width: 0;
    padding: 16px;
    background: white;
    border: 1px solid #e2e8f0;
    border-radius: 16px;
}
.summary-grid span, .summary-grid strong, .summary-grid small { display: block; }
.summary-grid span { color: #637087; font-size: 11px; font-weight: 700; }
.summary-grid strong { margin: 5px 0 1px; font-size: 25px; font-weight: 900; }
.summary-grid small { color: #99a3b4; font-size: 10px; }
.summary-grid .accent { color: #4f2dcc; border-color: #cfc2ff; background: #f7f4ff; }
.summary-grid .warning { color: #a85d00; border-color: #f6d694; background: #fffbeb; }
.summary-grid .danger { color: #b42c4a; border-color: #f6c0cc; background: #fff4f6; }

.result-grid {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 320px;
    gap: 18px;
    padding: 22px 24px;
}
.table-card, .rule-card { border: 1px solid var(--line); border-radius: 18px; overflow: hidden; }
.card-heading { display: flex; justify-content: space-between; padding: 15px 17px; background: #f8fafc; border-bottom: 1px solid var(--line); }
.card-heading strong, .card-heading small { display: block; }
.card-heading strong { font-size: 14px; font-weight: 900; }
.card-heading small { margin-top: 2px; color: var(--muted); font-size: 11px; }
.table-scroll { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; min-width: 720px; }
th, td { padding: 12px 13px; border-bottom: 1px solid #edf1f5; text-align: center; font-size: 12px; }
th { color: #637087; background: #fbfcfe; font-size: 10px; font-weight: 900; white-space: nowrap; }
td:first-child, th:first-child { text-align: left; }
tbody tr:last-child td { border-bottom: 0; }
.department-name { display: flex; align-items: center; gap: 8px; font-weight: 900; }
.metric-good { color: #087f6d; font-weight: 900; }
.metric-warn { color: #b56b04; font-weight: 900; }
.metric-danger { color: #c72f50; font-weight: 900; }

.rule-card { padding: 21px; color: #47338e; background: linear-gradient(145deg, #f7f4ff, #fcfbff); border-color: #dcd3fb; }
.rule-icon { display: grid; place-items: center; width: 42px; height: 42px; margin-bottom: 12px; background: white; border-radius: 13px; box-shadow: 0 5px 14px rgba(91,54,232,.1); font-size: 20px; }
.rule-card h3 { margin: 0; font-size: 17px; font-weight: 900; }
.rule-card p { margin: 8px 0 12px; color: #6d61a0; font-size: 12px; line-height: 1.55; }
.rule-card ul { margin: 0; padding-left: 18px; color: #594b90; font-size: 11px; line-height: 1.8; }

.warning-section { margin: 0 24px 22px; overflow: hidden; border: 1px solid #f1cf8d; border-radius: 17px; }
.warning-heading { display: flex; align-items: center; justify-content: space-between; padding: 13px 16px; background: #fff8e7; }
.warning-heading strong, .warning-heading small { display: block; }
.warning-heading strong { color: #8a5407; font-size: 13px; }
.warning-heading small { margin-top: 2px; color: #a77d40; font-size: 10px; }
.warning-heading > span { min-width: 30px; padding: 4px 8px; color: #8a5407; background: #ffe8af; border-radius: 999px; text-align: center; font-size: 11px; font-weight: 900; }
.warning-list { max-height: 290px; overflow: auto; }
.warning-item { display: grid; grid-template-columns: 110px 130px 1fr auto; align-items: center; gap: 12px; padding: 11px 15px; border-top: 1px solid #f5e6c5; font-size: 11px; }
.warning-item:first-child { border-top: 0; }
.warning-item .code { font-weight: 900; }
.warning-item .reason { color: #6e7788; }
.warning-item .tag { padding: 4px 7px; border-radius: 8px; color: #a35d02; background: #fff3d0; font-size: 9px; font-weight: 900; white-space: nowrap; }

.existing-notice { margin: 0 24px 22px; padding: 14px 17px; color: #335a80; background: #eef7ff; border: 1px solid #c6def3; border-radius: 15px; font-size: 12px; line-height: 1.55; }

.import-box {
    display: grid;
    grid-template-columns: 1fr auto;
    align-items: center;
    gap: 24px;
    padding: 24px;
    background: linear-gradient(120deg, #f8f6ff, #f2fbf9);
    border-top: 1px solid #e4e5f0;
}
.import-copy { max-width: 850px; }
.option-row { display: flex; align-items: flex-start; gap: 10px; margin-top: 14px; cursor: pointer; }
.option-row input { width: 18px; height: 18px; margin: 2px 0 0; accent-color: var(--primary); }
.option-row span, .option-row strong, .option-row small { display: block; }
.option-row strong { font-size: 12px; font-weight: 800; }
.option-row small { margin-top: 2px; color: var(--muted); font-size: 10px; }
.confirm-row { padding: 12px; background: rgba(255,255,255,.75); border: 1px solid #dfe4ec; border-radius: 13px; }
.import-action { min-width: 250px; text-align: center; }
.import-button { width: 100%; background: linear-gradient(135deg, #12a184, #087d69); box-shadow: 0 8px 19px rgba(15,159,131,.2); }
.import-action small { display: block; margin-top: 8px; max-width: 260px; color: var(--muted); font-size: 10px; }

.import-state {
    display: grid;
    grid-template-columns: auto 1fr auto;
    align-items: center;
    gap: 14px;
    margin: 20px 24px 24px;
    padding: 16px 18px;
    color: #4933a4;
    background: #f4f1ff;
    border: 1px solid #d9d0fa;
    border-radius: 16px;
}
.import-state.success { color: #087461; background: #eafaf5; border-color: #aee8d8; }
.import-state.error { color: #ad2c47; background: #fff2f5; border-color: #f2b7c4; }
.import-state-icon { font-size: 26px; }
.import-state strong { font-size: 14px; }
.import-state p { margin: 2px 0 0; font-size: 11px; }
.open-v2-button { height: 40px; color: white; background: #0f8a73; box-shadow: none; font-size: 11px; text-decoration: none; }

.promotion-panel { border-color: #cbd5e1; }
.production-heading {
    color: #f8fafc;
    background: linear-gradient(120deg, #172033, #26364f);
    border-bottom-color: #334155;
}
.production-heading .eyebrow { color: #93c5fd; }
.production-heading p { color: #cbd5e1; }
.production-badge {
    display: inline-flex;
    align-items: center;
    min-height: 31px;
    padding: 5px 11px;
    color: #cbd5e1;
    background: rgba(255,255,255,.08);
    border: 1px solid rgba(255,255,255,.13);
    border-radius: 999px;
    font-size: 11px;
    font-weight: 900;
    white-space: nowrap;
}
.production-badge.ready { color: #a7f3d0; background: rgba(16,185,129,.14); border-color: rgba(52,211,153,.3); }
.production-badge.stale { color: #fde68a; background: rgba(245,158,11,.14); border-color: rgba(251,191,36,.3); }
.production-warning {
    display: grid;
    grid-template-columns: auto 1fr auto;
    align-items: center;
    gap: 15px;
    padding: 20px 24px;
    color: #334155;
    background: #f8fafc;
    border-bottom: 1px solid #e2e8f0;
}
.production-warning > span { font-size: 28px; }
.production-warning strong { display: block; font-size: 14px; font-weight: 900; }
.production-warning p { margin: 3px 0 0; color: #64748b; font-size: 11px; line-height: 1.55; }
.production-scan-button { background: linear-gradient(135deg, #2563eb, #1d4ed8); box-shadow: 0 8px 19px rgba(37,99,235,.2); }
.production-progress { margin-top: 20px; color: #1d4ed8; background: #eff6ff; border-color: #bfdbfe; }
.promotion-summary .production-total { color: #065f46; border-color: #86efac; background: #f0fdf4; }
.production-notice { margin-top: 22px; }
.production-import-box { background: linear-gradient(120deg, #f8fafc, #eff6ff); }
.production-confirm-row { border-color: #bfdbfe; background: #fff; }
.production-import-button { background: linear-gradient(135deg, #2563eb, #1d4ed8); box-shadow: 0 8px 19px rgba(37,99,235,.2); }

.toast {
    position: fixed;
    left: 50%;
    bottom: 24px;
    z-index: 100;
    transform: translate(-50%, 20px);
    max-width: min(560px, calc(100% - 32px));
    padding: 12px 17px;
    color: white;
    background: #172033;
    border-radius: 12px;
    box-shadow: 0 14px 35px rgba(13, 23, 43, .25);
    font-size: 12px;
    font-weight: 700;
    opacity: 0;
    pointer-events: none;
    transition: .2s ease;
}
.toast.visible { transform: translate(-50%, 0); opacity: 1; }
.toast.error { background: #ba3150; }

[hidden] { display: none !important; }

@media (max-width: 1180px) {
    .scan-controls { grid-template-columns: 1fr 1fr; }
    .department-fieldset { grid-column: 1 / -1; }
    .primary-button { justify-self: end; }
    .summary-grid { grid-template-columns: repeat(3, 1fr); }
}

@media (max-width: 820px) {
    .page-shell { width: min(100% - 20px, 1480px); padding-top: 10px; }
    .topbar { grid-template-columns: auto 1fr; }
    .topbar > :last-child { display: none; }
    .brand-lockup { justify-self: end; }
    .brand-lockup p { display: none; }
    .safety-banner { grid-template-columns: auto 1fr; }
    .safe-pill { display: none; }
    .flow-strip { gap: 7px; }
    .flow-card { justify-content: center; padding: 10px; }
    .flow-card div { display: none; }
    .flow-arrow { font-size: 17px; }
    .panel-heading { align-items: flex-start; padding: 20px; }
    .read-only-badge, .ready-badge { display: none; }
    .scan-controls { grid-template-columns: 1fr; padding: 20px; }
    .department-fieldset { grid-column: auto; }
    .primary-button { width: 100%; justify-self: stretch; }
    .summary-grid { grid-template-columns: repeat(2, 1fr); padding: 16px; }
    .result-grid { grid-template-columns: 1fr; padding: 16px; }
    .warning-section, .existing-notice { margin-left: 16px; margin-right: 16px; }
    .warning-item { grid-template-columns: 80px 1fr; }
    .warning-item .reason { grid-column: 1 / -1; }
    .warning-item .tag { justify-self: start; }
    .import-box { grid-template-columns: 1fr; padding: 20px; }
    .import-action { min-width: 0; }
    .import-action small { max-width: none; }
    .import-state { grid-template-columns: auto 1fr; margin: 16px; }
    .open-v2-button { grid-column: 1 / -1; }
    .production-warning { grid-template-columns: auto 1fr; padding: 18px; }
    .production-scan-button { grid-column: 1 / -1; width: 100%; }
}

@media (max-width: 520px) {
    .soft-button { width: 42px; padding: 0; overflow: hidden; white-space: nowrap; text-indent: -999px; }
    .soft-button::after { content: '←'; text-indent: 0; font-size: 19px; }
    .brand-lockup h1 { font-size: 17px; }
    .brand-icon { width: 40px; height: 40px; border-radius: 13px; }
    .safety-banner { padding: 15px; }
    .safety-banner p { font-size: 11px; }
    .department-fieldset { height: auto; flex-direction: column; padding: 5px; }
    .department-fieldset legend { position: static; transform: none; align-self: flex-start; margin: 0 0 3px 5px; }
    .department-fieldset label { width: 100%; min-height: 40px; }
    .summary-grid { grid-template-columns: 1fr 1fr; gap: 8px; }
    .summary-grid article { padding: 13px; }
    .summary-grid strong { font-size: 21px; }
}
