:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f3f6;
  --border: #e2e5ea;
  --input-border: #c3c8d1;
  --text: #1b1f24;
  --muted: #667085;
  --accent: #2563eb;
  --accent-bg: #eef3fe;
  --accent-text: #ffffff;
  --ok: #15803d;
  --ok-bg: #e8f6ee;
  --warn: #b45309;
  --warn-bg: #fdf3e2;
  --err: #b91c1c;
  --err-bg: #fdecec;
  --info: #1d4ed8;
  --info-bg: #e7efff;
  --radius: 8px;
  --sidebar: 240px;
  color-scheme: light;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 22px; margin: 0; }
h2 { font-size: 16px; margin: 0 0 12px; }
code { font: 12px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.nowrap { white-space: nowrap; }
.error-text { color: var(--err); margin: 0; }
.warn-text { color: var(--warn); }
.ok-text { color: var(--ok); }
.empty-val { color: var(--muted); }

/* ---------- Shell ---------- */
.shell { display: grid; grid-template-columns: var(--sidebar) minmax(0, 1fr); min-height: 100vh; }
.sidebar {
  position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column;
  background: var(--surface); border-right: 1px solid var(--border); padding: 14px 12px;
}
.sidebar-top { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.sidebar-brand { display: flex; align-items: center; gap: 10px; font-weight: 600; font-size: 15px; color: var(--text); padding: 4px 6px; }
.sidebar-brand:hover { text-decoration: none; }
.logo {
  display: inline-grid; place-items: center; width: 32px; height: 32px; flex: none; border-radius: 8px;
  background: var(--accent); color: var(--accent-text); font-size: 13px; font-weight: 700; letter-spacing: .5px;
}
.menu-toggle { display: none; border: 1px solid var(--border); background: var(--surface); border-radius: 6px; padding: 4px 10px; font-size: 16px; cursor: pointer; }
.sidebar-nav { display: flex; flex-direction: column; gap: 2px; margin-top: 22px; flex: 1; }
.nav-link {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 6px;
  color: var(--muted); font-weight: 500;
}
.nav-link:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.nav-link[aria-current="page"] { background: var(--accent-bg); color: var(--accent); }
.nav-link svg { width: 18px; height: 18px; flex: none; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.sidebar-foot { display: flex; flex-direction: column; gap: 10px; border-top: 1px solid var(--border); padding-top: 12px; }
.env-badges { display: flex; gap: 6px; flex-wrap: wrap; }
.env-badges:empty { display: none; }
.user { display: flex; align-items: center; gap: 10px; min-width: 0; }
.avatar {
  display: inline-grid; place-items: center; width: 32px; height: 32px; flex: none; border-radius: 50%;
  background: var(--surface-2); font-weight: 600;
}
.user-meta { display: flex; flex-direction: column; min-width: 0; }
.user-meta strong, .user-meta span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.signout { width: 100%; }
.content { padding: 24px 28px 48px; min-width: 0; }
.page { display: flex; flex-direction: column; gap: 16px; max-width: 1400px; margin: 0 auto; }

@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar { position: sticky; z-index: 20; height: auto; padding: 10px 16px; border-right: 0; border-bottom: 1px solid var(--border); }
  .menu-toggle { display: block; }
  .sidebar-nav, .sidebar-foot { display: none; }
  .sidebar.open .sidebar-nav, .sidebar.open .sidebar-foot { display: flex; }
  .sidebar-nav { margin-top: 12px; }
  .sidebar-foot { margin-top: 12px; }
  .content { padding: 16px 16px 40px; }
}

/* ---------- Login ---------- */
.login-page { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 380px; padding: 24px; display: flex; flex-direction: column; gap: 20px; }
.login-brand { display: flex; align-items: center; gap: 12px; }
.login-brand h1 { font-size: 18px; }
.login-brand p { margin: 2px 0 0; }
.login-form { display: flex; flex-direction: column; gap: 14px; }
.login-form .btn.primary { width: 100%; }

/* ---------- Common ---------- */
.page-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; flex-wrap: wrap; }
.page-header p { margin: 4px 0 0; }
.back { font-size: 13px; }
.subhead { font-size: 13px; margin: 16px 0 8px; color: var(--muted); font-weight: 600; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.pad { padding: 16px; }
.notice { padding: 10px 14px; border-radius: 6px; background: var(--surface-2); border: 1px solid var(--border); }
.notice p { margin: 4px 0 0; }
.notice pre { margin: 8px 0; }
.notice.ok { background: var(--ok-bg); border-color: #bfe5cd; }
.notice.error { background: var(--err-bg); border-color: #f3c4c4; color: var(--err); }
.status-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.toolbar-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.detail-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 8px; }
.detail-head h2 { margin: 0; min-width: 0; }
.empty-state { display: flex; flex-direction: column; align-items: center; gap: 10px; padding: 32px; }
.empty-state p { margin: 0; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 7px 14px; border-radius: 6px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  font: inherit; font-weight: 500; white-space: nowrap;
}
a.btn:hover { text-decoration: none; }
.btn:hover:not(:disabled) { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover:not(:disabled) { filter: brightness(1.08); background: var(--accent); }
.btn.danger { color: var(--err); }
.btn.danger:hover:not(:disabled) { background: var(--err-bg); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.small-btn { padding: 3px 10px; font-size: 12px; }

.badge { display: inline-block; font-size: 12px; padding: 2px 8px; border-radius: 999px; font-weight: 500; background: var(--surface-2); color: var(--text); white-space: nowrap; }
.badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge.ok { background: var(--ok-bg); color: var(--ok); }
.badge.err { background: var(--err-bg); color: var(--err); }
.badge.info { background: var(--info-bg); color: var(--info); }
a.badge:hover { text-decoration: underline; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 14px; min-width: 0; }
.stat .label { color: var(--muted); font-size: 12px; }
.stat .value { font-size: 18px; font-weight: 600; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }

.checks summary { padding: 10px 14px; cursor: pointer; font-weight: 500; }
.checks ul { list-style: none; margin: 0; padding: 0 14px 10px; max-height: 320px; overflow: auto; }
.checks li { display: flex; gap: 8px; padding: 3px 0; }
.checks .icon { width: 16px; flex: none; font-weight: 700; }
.checks .ok .icon { color: var(--ok); }
.checks .bad .icon { color: var(--err); }
.checks .warning .icon { color: var(--warn); }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); overflow-x: auto; }
.tabs.inline { margin-bottom: 12px; }
.tab {
  padding: 8px 14px; border: 0; background: none; color: var(--muted); cursor: pointer;
  font: inherit; font-weight: 500; border-bottom: 2px solid transparent; white-space: nowrap;
}
.tab[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); }
.tab .count { font-size: 11px; background: var(--surface-2); border-radius: 999px; padding: 1px 7px; margin-left: 4px; }
.panel { border-top-left-radius: 0; border-top-right-radius: 0; border-top: 0; padding: 16px; min-width: 0; }

.table-wrap { overflow: auto; max-height: 65vh; border: 1px solid var(--border); border-radius: 6px; }
table { border-collapse: collapse; width: 100%; font-variant-numeric: tabular-nums; }
th, td { padding: 7px 10px; border-bottom: 1px solid var(--border); text-align: left; white-space: nowrap; vertical-align: top; }
tbody tr:last-child td { border-bottom: 0; }
th { position: sticky; top: 0; background: var(--surface-2); font-weight: 600; font-size: 12px; z-index: 1; }
td.num, th.num { text-align: right; }
tbody tr:hover td { background: var(--surface-2); }
.clickable tbody tr { cursor: pointer; }
tr[aria-selected="true"] td { background: var(--accent-bg); }
.file-cell { max-width: 320px; overflow: hidden; text-overflow: ellipsis; }
td .table-wrap { max-height: none; }

pre.json {
  margin: 0; padding: 12px; background: var(--surface-2); border-radius: 6px;
  overflow: auto; max-height: 65vh; font: 12px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.raw summary { cursor: pointer; color: var(--muted); font-size: 12px; }
.raw pre { margin-top: 6px; max-height: 360px; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin: 0; }
.kv-row { display: contents; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; font-variant-numeric: tabular-nums; word-break: break-word; }
.kv dd .kv { padding-left: 4px; }
.result { display: flex; flex-direction: column; gap: 10px; }
.result .table-wrap { max-height: 300px; }
.mcs { word-break: break-all; }

/* ---------- Upload ---------- */
.dropzone {
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  padding: 36px 24px; text-align: center;
  border: 2px dashed var(--border); border-radius: 12px; background: var(--surface);
}
.dropzone.drag { border-color: var(--accent); background: var(--accent-bg); }
.dropzone p { margin: 0; }
.dz-title { font-size: 16px; font-weight: 600; }
.split { display: grid; grid-template-columns: minmax(0, 1fr); gap: 16px; align-items: start; }
.split.with-detail { grid-template-columns: minmax(0, 1fr) minmax(0, 400px); }
.split.with-detail .file-cell { max-width: 220px; }
.detail { position: sticky; top: 16px; display: flex; flex-direction: column; gap: 12px; max-height: calc(100vh - 32px); overflow: auto; }
.detail p { margin: 0; }
.detail .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
@media (max-width: 1400px) { .split.with-detail { grid-template-columns: minmax(0, 1fr); } .detail { position: static; max-height: none; } }

/* ---------- Submit ---------- */
.draft-picker { display: flex; align-items: center; gap: 12px; }
.draft-picker label { font-weight: 600; }
.draft-picker select, .lookup-form input {
  flex: 1; min-width: 0; padding: 7px 10px; border-radius: 6px; font: inherit;
  border: 1px solid var(--input-border); background: var(--surface); color: var(--text);
}
@media (max-width: 600px) { .draft-picker { flex-direction: column; align-items: stretch; } }
.draft-name { display: inline-flex; align-items: center; gap: 8px; min-width: 0; }
.draft-name strong { font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 420px; }
.draft-name input { padding: 4px 8px; border-radius: 6px; font: inherit; border: 1px solid var(--input-border); min-width: 260px; }
.link-btn { border: 0; background: none; padding: 0; color: var(--accent); cursor: pointer; font: inherit; }
.link-btn.small { font-size: 12px; }
.link-btn:hover { text-decoration: underline; }
.draft-bar { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; }
.actions-panel { display: flex; flex-direction: column; gap: 12px; }
.actions-panel h2 { margin: 0; }
.steps { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.4fr); gap: 12px; }
@media (max-width: 900px) { .steps { grid-template-columns: 1fr; } }
.step { display: flex; flex-direction: column; align-items: flex-start; gap: 10px; padding: 14px; border: 1px solid var(--border); border-radius: 6px; }
.step p { margin: 0; }
.step-head { display: flex; align-items: center; gap: 8px; width: 100%; flex-wrap: wrap; }
.step-no {
  display: inline-grid; place-items: center; width: 22px; height: 22px; border-radius: 50%;
  background: var(--surface-2); font-size: 12px; font-weight: 600;
}
.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; flex-wrap: wrap; }
.segmented label { display: inline-flex; align-items: center; gap: 4px; padding: 6px 14px; cursor: pointer; font-weight: 500; }
.segmented label + label { border-left: 1px solid var(--border); }
.segmented label.active { background: var(--accent-bg); color: var(--accent); }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented label:has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: -2px; }

.api-errors { list-style: none; margin: 0; padding: 10px 12px; background: var(--err-bg); border-radius: 6px; display: flex; flex-direction: column; gap: 4px; }
.api-errors li { color: var(--err); }
.err-code { font-weight: 600; font-variant-numeric: tabular-nums; margin-right: 8px; }
.tax { display: flex; flex-direction: column; gap: 12px; }
.tax-total { display: flex; justify-content: space-between; align-items: baseline; padding: 10px 12px; background: var(--surface-2); border-radius: 6px; }
.tax-total strong { font-size: 20px; font-variant-numeric: tabular-nums; }
.tax .kv dd.num { text-align: right; }
.tax-group { display: flex; flex-direction: column; gap: 6px; }
.tax-head { display: flex; justify-content: space-between; gap: 8px; font-weight: 600; }
.tax-head .muted { font-weight: 400; }
.tax-head .num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.tax th:not(:first-child), .tax td:not(:first-child) { text-align: right; }

.status-panel { display: flex; flex-direction: column; gap: 10px; margin-top: 12px; }
.log-events { display: flex; flex-direction: column; gap: 10px; }
.log-event { display: flex; flex-direction: column; gap: 6px; padding: 10px 12px; border: 1px solid var(--border); border-radius: 6px; }
.log-event pre.json { white-space: pre-wrap; word-break: break-all; }

/* ---------- XML to Excel ---------- */
.conversion { display: flex; flex-direction: column; gap: 12px; }
.conversion p { margin: 0; }
.conversion .detail-head { flex-wrap: wrap; }
.invoice-inputs { display: grid; grid-template-columns: minmax(0, 240px) minmax(0, 180px) 1fr; gap: 12px; align-items: end; }
@media (max-width: 640px) { .invoice-inputs { grid-template-columns: 1fr; } }
.notes { margin: 0; padding: 10px 14px 10px 30px; background: var(--warn-bg); color: var(--warn); border-radius: 6px; display: flex; flex-direction: column; gap: 4px; }

/* ---------- Declarations ---------- */
.lookup-form { display: grid; grid-template-columns: 1.4fr .8fr .6fr 1fr auto; gap: 10px; align-items: end; margin-bottom: 12px; }
@media (max-width: 720px) { .lookup-form { grid-template-columns: 1fr 1fr; } .lookup-form .btn { grid-column: 1 / -1; } }

/* ---------- Form ---------- */
.declaration-form { min-width: 0; }
.form-root { border: 0; margin: 0; padding: 0; min-width: 0; }
.form-sections, .records { display: flex; flex-direction: column; gap: 16px; }
.form-group { border: 1px solid var(--border); border-radius: 6px; padding: 10px 12px 12px; margin: 0; min-width: 0; }
.form-group legend, .form-group > summary { font-weight: 600; font-size: 13px; padding: 0 4px; }
details.form-group > summary { cursor: pointer; padding: 2px 0; }
details.form-group[open] > summary { margin-bottom: 10px; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 10px 12px; }
.form-field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.form-field.wide { grid-column: span 2; }
.form-field.new-row { grid-column-start: 1; }
@media (max-width: 520px) { .form-field.wide { grid-column: auto; } }
.form-field label { font-size: 12px; color: var(--muted); display: flex; align-items: baseline; gap: 4px; flex-wrap: wrap; }
.form-field .hint { font-size: 11px; color: var(--muted); }
.form-field input, .form-field select {
  width: 100%; min-width: 0; padding: 6px 8px; border-radius: 6px; font: inherit;
  border: 1px solid var(--input-border); background: var(--surface); color: var(--text);
}
.form-field input:focus, .form-field select:focus, .draft-picker select:focus, .lookup-form input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.form-field input.invalid { border-color: var(--err); }
.form-field input.missing, .form-field select.missing { border-color: #e29a9a; background: #fffafa; }
.form-field input.fixed { background: var(--surface-2); color: var(--muted); cursor: default; }
.form-field input.fixed:focus { outline: none; }
fieldset:disabled .form-field input, fieldset:disabled .form-field select { background: var(--surface-2); color: var(--text); }
.req { color: var(--err); font-weight: 700; margin-left: 2px; }
.legend-note { margin: 0; }

.record { padding: 12px 14px; display: flex; flex-direction: column; gap: 12px; }
.record-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.record-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.record-actions { display: flex; gap: 6px; flex: none; }

/* Standalone converter: no sidebar */
.content.standalone { max-width: 1400px; margin: 0 auto; }
