:root {
  /* Surfaces */
  --bg: #eef1f6;
  --card: #ffffff;
  --surface-2: #f6f8fb;
  --border: #e3e8ef;
  --border-strong: #cfd8e3;
  /* Text */
  --text: #16202e;
  --text2: #64748b;
  /* Brand (navy structure) + accent (interactive blue) */
  --brand: #1e3a5f;
  --brand-hover: #162d4a;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --accent-light: #eaf1ff;
  /* Status */
  --green: #15803d;
  --green-light: #dcfce7;
  --red: #dc2626;
  --red-light: #fee2e2;
  --amber: #b45309;
  --amber-light: #fef3c7;
  /* Shape + depth */
  --r-sm: 6px;
  --r: 10px;
  --r-lg: 14px;
  --ring: 0 0 0 3px rgba(37, 99, 235, .18);
  --shadow-sm: 0 1px 2px rgba(16, 32, 46, .06);
  --shadow: 0 1px 3px rgba(16, 32, 46, .08), 0 1px 2px rgba(16, 32, 46, .04);
  --shadow-md: 0 4px 12px rgba(16, 32, 46, .08), 0 2px 4px rgba(16, 32, 46, .04);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html { -webkit-text-size-adjust: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang HK", "Microsoft JhengHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
.container { max-width: 1400px; margin: 0 auto; padding: 24px 20px; }

/* Accessibility: skip-to-content link (visible only on keyboard focus) */
.skip-link {
  position: absolute; left: 8px; top: -60px; z-index: 100;
  background: var(--brand); color: #fff; padding: 10px 16px;
  border-radius: 0 0 8px 8px; font-weight: 650; font-size: 13px;
  box-shadow: var(--shadow-md); transition: top 140ms ease;
}
.skip-link:focus { top: 0; text-decoration: none; }

/* Accessibility: visible focus for keyboard users */
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 4px;
}
@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .01ms !important; transition-duration: .01ms !important; scroll-behavior: auto !important; }
}

/* ── Header ── */
header {
  position: sticky;
  top: 0;
  z-index: 50;
  background: rgba(255, 255, 255, .88);
  backdrop-filter: saturate(180%) blur(12px);
  -webkit-backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
  padding: 12px 0;
}
.partner-theme .nav-link.active { background: var(--brand); }
.header-inner { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 10px; }
h1 { font-size: 19px; font-weight: 750; letter-spacing: -.01em; display: flex; align-items: center; gap: 8px; }
h1 a { color: var(--brand); }
h1 a:hover { text-decoration: none; }
.brand { display: inline-flex; align-items: center; gap: 10px; }
.brand-text { display: inline-flex; align-items: baseline; gap: 8px; }
.brand-mark { display: inline-grid; place-items: center; width: 28px; height: 28px; border-radius: 8px; background: var(--brand); color: #fff; flex-shrink: 0; box-shadow: var(--shadow-sm); }
.brand-mark-lg { width: 44px; height: 44px; border-radius: 12px; margin: 0 auto 14px; }
h1 small { color: var(--text2); font-weight: 400; font-size: 13px; }
h2 { font-size: 22px; font-weight: 700; letter-spacing: -.01em; margin: 0 0 14px; }
.up-en { font-size: 13px; font-weight: 500; color: var(--text2); margin-left: 6px; }
h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--text2); margin-bottom: 12px; font-weight: 700; }

.header-right { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.nav-links { display: flex; gap: 4px; align-items: center; flex-wrap: wrap; }
.user-chip { display: flex; align-items: center; gap: 9px; padding-left: 14px; border-left: 1px solid var(--border); }
.user-meta { display: flex; flex-direction: column; line-height: 1.25; text-align: right; }
.user-meta b { font-size: 13px; font-weight: 700; color: var(--text); }
.user-meta > span { font-size: 11px; color: var(--text2); text-transform: capitalize; }
.user-logout { display: inline-grid; place-items: center; width: 32px; height: 32px; border-radius: 8px; color: var(--text2); border: 1px solid var(--border); flex-shrink: 0; transition: background 130ms, color 130ms, border-color 130ms; }
.user-logout:hover { background: var(--red-light); color: var(--red); border-color: #fbcaca; text-decoration: none; }
.nav-link {
  font-size: 13.5px; font-weight: 550;
  padding: 7px 13px; border-radius: 8px;
  color: var(--text2);
  transition: background 140ms, color 140ms;
}
.nav-link:hover { background: var(--accent-light); color: var(--accent-hover); text-decoration: none; }
.nav-link.active { background: var(--brand); color: #fff; box-shadow: var(--shadow-sm); }
.nav-link.active:hover { background: var(--brand-hover); color: #fff; }

/* ── Cards / panels ── */
.panel,
.band {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: 22px;
  margin: 18px 0;
  box-shadow: var(--shadow);
}
.tool-head {
  display: flex; justify-content: space-between; align-items: center;
  gap: 12px; margin: 20px 0 8px;
}
.tool-actions,
.link-row { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.tool-head p,
.source { color: var(--text2); font-size: 13px; }

/* ── Forms ── */
label { display: flex; flex-direction: column; gap: 5px; font-size: 12.5px; font-weight: 650; color: var(--text2); }
label span { color: var(--text); }
label small { color: var(--text2); font-weight: 400; }
input,
select,
textarea {
  padding: 9px 12px;
  border: 1.5px solid var(--border-strong);
  border-radius: var(--r-sm);
  background: #fff;
  color: var(--text);
  font: inherit;
  outline: none;
  transition: border-color 130ms, box-shadow 130ms;
}
input:hover,
select:hover,
textarea:hover { border-color: #b6c2d1; }
input:focus,
select:focus,
textarea:focus { border-color: var(--accent); box-shadow: var(--ring); }
textarea { min-height: 76px; resize: vertical; }
input[type="file"] { padding: 6px 8px; cursor: pointer; color: var(--text2); font-size: 13px; }
input[type="file"]::file-selector-button {
  margin-right: 10px;
  padding: 7px 14px;
  border: 1.5px solid var(--border-strong);
  border-radius: var(--r-sm);
  background: var(--card);
  color: var(--brand);
  font: inherit;
  font-weight: 650;
  cursor: pointer;
  transition: background 130ms, border-color 130ms, color 130ms;
}
input[type="file"]::file-selector-button:hover {
  background: var(--accent-light);
  border-color: var(--accent);
  color: var(--accent-hover);
}
::placeholder { color: #9aa7b8; }

.mini-action { width: fit-content; padding: 7px 12px; font-size: 12px; }
.field-status { min-height: 18px; font-weight: 650; font-size: 12px; }
.field-status[data-state="ok"] { color: var(--green); }
.field-status[data-state="warn"] { color: var(--red); }

.cts-picker {
  border: 1px solid var(--border);
  border-left: 4px solid var(--accent);
  border-radius: var(--r);
  padding: 14px;
  margin: 0 0 16px;
  background: var(--surface-2);
}
.inline-control { display: flex; gap: 8px; align-items: center; }
.inline-control input { flex: 1; }
.cts-results { display: grid; gap: 6px; margin-top: 10px; }
.cts-result {
  width: 100%; text-align: left;
  background: var(--card); color: var(--text);
  border: 1.5px solid var(--border-strong);
}
.cts-result:hover { background: var(--accent-light); color: var(--accent-hover); border-color: var(--accent); }

.grid-form { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 16px; }
.required > span::after { content: " *"; color: var(--red); }
.muted-field { opacity: .56; }
label[data-field="case_type"] { gap: 6px; }
label[data-field="case_type"] .field-status {
  display: block; min-height: 0; margin-top: 2px; padding: 10px 12px;
  border: 1px solid var(--border); border-left: 4px solid var(--accent);
  border-radius: var(--r-sm); background: var(--accent-light);
  color: var(--text); font-weight: 650; line-height: 1.4;
}
label[data-field="case_type"] .field-status:empty { display: none; }

/* ── Portal layout ── */
.portal-layout {
  display: grid;
  grid-template-columns: 190px minmax(0, 1fr) 320px;
  gap: 18px; align-items: start;
}
.portal-nav,
.portal-rail { position: sticky; top: 78px; }
.portal-nav {
  display: grid; gap: 4px; padding: 10px;
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--r); box-shadow: var(--shadow-sm);
}
.portal-nav a { color: var(--text2); padding: 8px 11px; border-radius: var(--r-sm); font-weight: 600; transition: background 130ms, color 130ms; }
.portal-nav a:hover { background: var(--accent-light); color: var(--accent-hover); text-decoration: none; }
.portal-nav a.active { background: var(--brand); color: #fff; }
.portal-nav a.active:hover { background: var(--brand-hover); color: #fff; }
.portal-section { scroll-margin-top: 84px; }
.section-heading { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }
.section-heading h2 { margin-bottom: 14px; }
.action-panel { display: grid; gap: 10px; margin-top: 16px; padding: 16px; }
.action-panel button,
.action-panel .secondary { width: 100%; text-align: center; }
.tb-panel { border-left: 4px solid var(--accent); }
.tb-grid { display: grid; grid-template-columns: 1fr; gap: 10px; }
.tb-grid div {
  border: 1px solid var(--border); border-radius: var(--r-sm);
  padding: 11px 13px; background: var(--surface-2); min-height: 62px;
}
.tb-grid span { display: block; color: var(--text2); font-size: 12px; margin-bottom: 6px; }
.tb-grid strong { display: block; font-size: 16px; color: var(--text); }
.tb-grid strong[data-status="warn"] { color: var(--red); }
.tb-grid strong[data-status="ok"] { color: var(--green); }
.action-bar,
.form-actions { display: flex; gap: 12px; align-items: center; margin-top: 16px; }
.primary-actions { padding-top: 14px; border-top: 1px solid var(--border); }

/* ── Buttons ── */
button,
.download,
.secondary {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  padding: 9px 20px;
  border-radius: var(--r-sm);
  border: 1.5px solid var(--brand);
  background: var(--brand);
  color: #fff;
  cursor: pointer;
  font-size: 14px;
  font-weight: 650;
  text-decoration: none;
  transition: background 140ms, box-shadow 140ms, border-color 140ms, transform 60ms;
  box-shadow: var(--shadow-sm);
}
button:hover,
.download:hover,
.secondary:hover { background: var(--brand-hover); border-color: var(--brand-hover); text-decoration: none; }
button:active,
.download:active { transform: translateY(1px); }
.primary-action { min-height: 42px; }
button:disabled { background: #c2ccd8; border-color: #c2ccd8; cursor: not-allowed; box-shadow: none; }

.secondary { background: var(--card); color: var(--brand); border-color: var(--border-strong); }
.secondary:hover { background: var(--surface-2); color: var(--brand-hover); border-color: var(--brand); }
button.secondary:disabled,
button.secondary:disabled:hover { background: var(--surface-2); border-color: var(--border); color: var(--text2); }
.table-actions .secondary,
.table-actions button { padding: 6px 13px; font-size: 13px; }

button.is-busy { position: relative; color: transparent !important; pointer-events: none; }
button.is-busy::after {
  content: ""; position: absolute; top: 50%; left: 50%;
  width: 15px; height: 15px; margin: -8px 0 0 -8px;
  border: 2px solid rgba(255, 255, 255, .45); border-top-color: #fff;
  border-radius: 50%; animation: spin .6s linear infinite;
}
.secondary.is-busy::after { border-color: rgba(30, 58, 95, .25); border-top-color: var(--brand); }
@keyframes spin { to { transform: rotate(360deg); } }

.btn-success,
.factory { background: var(--green); border-color: var(--green); }
.btn-success:hover,
.factory:hover { background: #166534; border-color: #166534; }
.factory {
  min-width: 220px; min-height: 46px; font-size: 15px; letter-spacing: .01em;
  box-shadow: 0 2px 8px rgba(21, 128, 61, .24);
}
.factory:disabled { box-shadow: none; }

/* ── Code / pre ── */
pre {
  overflow: auto;
  background: #0f172a;
  color: #e2e8f0;
  border: 1px solid #1e293b;
  padding: 14px;
  border-radius: var(--r-sm);
  max-height: 420px;
  font-family: "SF Mono", Monaco, Consolas, monospace;
  font-size: 12px;
  line-height: 1.6;
}

/* ── Status pills ── */
.status {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 3px 11px; border-radius: 999px;
  font-size: 12px; font-weight: 700; letter-spacing: .02em;
  margin: 8px 0;
  background: var(--surface-2); color: var(--text2);
}
.status::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.status:hover { text-decoration: none; }
.status.pass { background: var(--green-light); color: var(--green); }
.status.blocked { background: var(--red-light); color: var(--red); }
.error { color: var(--red); font-weight: 600; }
.blockers { color: var(--red); margin: 8px 0 8px 18px; }
.output-list { margin: 8px 0 16px 18px; color: var(--text2); }

.pdf-pane { margin: 16px 0; }
.pdf-pane iframe {
  width: 100%; height: min(78vh, 920px); min-height: 620px;
  border: 1px solid var(--border); border-radius: var(--r-sm); background: #fff;
}

/* ── Tables ── */
.runs-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.runs-table th,
.runs-table td { border-bottom: 1px solid var(--border); padding: 11px 12px; text-align: left; vertical-align: top; }
.runs-table thead th {
  background: var(--surface-2);
  color: var(--text2); font-weight: 700; font-size: 11px;
  text-transform: uppercase; letter-spacing: .05em;
  border-bottom: 1.5px solid var(--border-strong);
}
.runs-table tbody tr { transition: background 120ms; }
.runs-table tbody tr:hover { background: var(--surface-2); }
.table-actions { display: flex; gap: 12px; flex-wrap: wrap; }
.js-proforma-result[data-state="ok"] { color: var(--green); font-weight: 700; }
.js-proforma-result[data-state="bad"] { color: var(--red); font-weight: 700; }

/* ── Studio ── */
.studio-map-layout { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 18px; align-items: start; }
.studio-side { position: sticky; top: 78px; }
.studio-sheet { margin-bottom: 14px; }
.studio-sheet summary { cursor: pointer; font-weight: 700; margin-bottom: 8px; }
.studio-table-wrap,
.studio-rows-scroll { overflow: auto; border: 1px solid var(--border); border-radius: var(--r-sm); background: #fff; }
.studio-sheet-table { border-collapse: collapse; font-size: 12px; }
.studio-sheet-table td {
  position: relative; min-width: 92px; width: 92px; height: 46px;
  border: 1px solid var(--border); vertical-align: top;
  padding: 14px 4px 4px; cursor: pointer; background: #fff;
  transition: background 120ms;
}
.studio-sheet-table td:hover { background: var(--surface-2); }
.studio-sheet-table td.formula { background: var(--surface-2); color: var(--text2); cursor: not-allowed; }
.studio-sheet-table td.fill { background: var(--accent-light); outline: 2px solid var(--accent); outline-offset: -2px; }
.cell-ref { position: absolute; top: 2px; left: 4px; color: var(--text2); font-size: 10px; }
.cell-value { display: block; max-height: 20px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.cell-caption { display: block; color: var(--accent); font-weight: 700; font-size: 11px; max-height: 28px; overflow: hidden; }
.studio-column-list { display: grid; gap: 10px; }
.studio-column-row { display: grid; gap: 8px; padding: 11px; border: 1px solid var(--border); border-radius: var(--r-sm); background: #fff; }
.studio-column-row input,
.studio-column-row select { width: 100%; }
.inline-check { flex-direction: row; align-items: center; gap: 6px; font-weight: 600; }
.studio-block-form { display: grid; gap: 8px; padding: 11px; border: 1px solid var(--border); border-radius: var(--r-sm); background: #fff; margin-bottom: 10px; }
.studio-block-form label { display: grid; gap: 4px; font-size: 12px; }
.studio-block-form input,
.studio-block-form select { width: 100%; }
.studio-block-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.studio-rows-table input { min-width: 180px; }
.source[data-state="ok"] { color: var(--green); font-weight: 700; }
.source[data-state="bad"] { color: var(--red); font-weight: 700; }

/* ── Login ── */
.login {
  min-height: 100vh; display: grid; place-items: center;
  background:
    radial-gradient(1200px 600px at 50% -10%, rgba(37, 99, 235, .10), transparent 60%),
    linear-gradient(180deg, #eef1f6 0%, #e6ebf3 100%);
  padding: 20px;
}
.login-panel {
  width: min(420px, calc(100vw - 40px));
  background: var(--card);
  border: 1px solid var(--border);
  border-top: 4px solid var(--brand);
  border-radius: var(--r-lg);
  padding: 32px 28px;
  box-shadow: var(--shadow-md);
}
.login-panel h1 { color: var(--brand); font-size: 22px; }
.login-panel p { color: var(--text2); margin: 2px 0 20px; }
.login-panel form { display: grid; gap: 16px; }
.login-panel button { width: 100%; min-height: 44px; }

/* ── Page guide (Simplified-Chinese aim + how-to) ── */
.page-guide {
  display: flex; gap: 12px; align-items: flex-start;
  background: var(--accent-light);
  border: 1px solid #d3e2ff; border-left: 4px solid var(--accent);
  border-radius: var(--r);
  padding: 12px 16px; margin: 0 0 18px;
}
.page-guide svg { color: var(--accent); flex-shrink: 0; margin-top: 2px; }
.page-guide-body { font-size: 13px; color: var(--text); line-height: 1.65; }
.page-guide-body .pg-aim { font-weight: 700; }
.page-guide-body .pg-aim b { color: var(--brand); }
.page-guide-body .pg-how { color: var(--text2); }
.page-guide-body kbd {
  font: inherit; font-weight: 650; color: var(--brand);
  background: #fff; border: 1px solid #d3e2ff; border-radius: 4px; padding: 0 5px;
}

/* ── Flash messages ── */
.flash-stack { display: grid; gap: 8px; padding-top: 16px; padding-bottom: 0; }
.flash {
  display: flex; align-items: center; gap: 9px;
  background: var(--green-light); border: 1px solid #b7e4c7; border-left: 4px solid var(--green);
  color: #14532d; border-radius: var(--r-sm); padding: 11px 14px;
  font-size: 13px; font-weight: 600;
  animation: flash-in 240ms ease-out;
}
.flash svg { color: var(--green); flex-shrink: 0; }
@keyframes flash-in { from { opacity: 0; transform: translateY(-6px); } to { opacity: 1; transform: none; } }

/* ── Footer ── */
footer { text-align: center; padding: 28px 20px; color: var(--text2); font-size: 12px; }

/* ── Print ── */
@media print {
  header, footer, .flash-stack, .nav-links, .user-chip, .page-guide,
  .pv-cta, .pv-details, .verify-details, .tool-actions, .runs-toolbar, .co-toolbar,
  button, .secondary, .download, .btn-dl, .btn-rerun { display: none !important; }
  body { background: #fff; color: #000; font-size: 12px; }
  .container { max-width: 100%; padding: 0; margin: 0; }
  .panel, .band { box-shadow: none; border: 1px solid #ccc; margin: 0 0 10px; break-inside: avoid; }
  a { color: inherit; text-decoration: none; }
  pre { max-height: none; background: #f4f4f4; color: #000; border-color: #ccc; }
  .pdf-pane iframe, #preview-frame { height: auto; min-height: 0; }
}

/* ── Responsive ── */
@media (max-width: 768px) {
  .container { padding: 14px; }
  .header-inner { flex-direction: column; align-items: stretch; gap: 10px; }
  .header-right { width: 100%; justify-content: space-between; gap: 10px; flex-wrap: nowrap; }
  .nav-links { flex: 1; min-width: 0; gap: 4px; flex-wrap: nowrap; overflow-x: auto; -webkit-overflow-scrolling: touch; scrollbar-width: none; }
  .nav-links::-webkit-scrollbar { display: none; }
  .nav-link { padding: 7px 11px; flex-shrink: 0; }
  .user-chip { flex-shrink: 0; padding-left: 10px; }
  .portal-layout { grid-template-columns: 1fr; }
  .portal-nav,
  .portal-rail,
  .studio-side { position: static; }
  .portal-nav { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .studio-map-layout { grid-template-columns: 1fr; }
  .grid-form { grid-template-columns: 1fr; }
  .runs-table thead th { position: static; }
  input, select, textarea, button { min-height: 44px; font-size: 16px; }
}
.user-logout-form { display: inline-flex; margin: 0; }
.user-logout-form .user-logout {
  min-height: 32px; padding: 0; background: transparent; color: var(--text2);
  border: 1px solid var(--border); box-shadow: none; font: inherit; cursor: pointer;
}
.user-logout-form .user-logout:hover {
  background: var(--red-light); color: var(--red); border-color: #fbcaca;
}
