:root {
  --bg: #eef2f6;
  --panel: #ffffff;
  --ink: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --brand: #0f766e;
  --brand-2: #14b8a6;
  --danger: #b91c1c;
  --warn: #b45309;
  --ok: #047857;
  --sider: #0b1324;
  --sider-2: #121a2f;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; font-family: "Segoe UI", "Noto Sans TC", sans-serif; color: var(--ink); background: var(--bg); }
a { color: inherit; text-decoration: none; }
input, select, textarea, button { font: inherit; }

.app-shell { display: grid; grid-template-columns: 240px 1fr; min-height: 100vh; }
.sider { background: linear-gradient(180deg, var(--sider), var(--sider-2)); color: #dbe4f0; padding: 20px 14px; }
.brand { display: flex; gap: 10px; align-items: center; margin-bottom: 22px; }
.brand span { display: block; font-size: 12px; color: #93a4c0; }
.logo-mark { width: 40px; height: 40px; border-radius: 12px; display: grid; place-items: center; background: linear-gradient(135deg, var(--brand), var(--brand-2)); color: #fff; font-weight: 800; }
.logo-mark.sm { width: 34px; height: 34px; border-radius: 10px; }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav a { padding: 8px 12px; border-radius: 8px; color: #c5d0e2; }
.nav a.active, .nav a:hover { background: rgba(20,184,166,.16); color: #fff; }
.nav-label { margin: 14px 12px 6px; font-size: 11px; letter-spacing: .12em; color: #7f8eaa; text-transform: uppercase; }
.main { display: flex; flex-direction: column; min-width: 0; }
.topbar { height: 64px; display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; padding: 0 24px; background: #fff; border-bottom: 1px solid var(--line); gap: 16px; }
.topbar-company { text-align: center; font-weight: 700; font-size: 22px; color: var(--ink); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: min(52vw, 640px); }
.userbox { display: flex; gap: 14px; align-items: center; justify-self: end; font-size: 16px; }
.userbox a { color: var(--brand); }
.content { padding: 24px; }
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; margin-bottom: 18px; }
.page-head h1 { margin: 0; font-size: 24px; }
.page-head p { margin: 4px 0 0; color: var(--muted); }

.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin-bottom: 16px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 16px; }
.kpi b { display: block; font-size: 26px; margin-top: 6px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--line); font-size: 14px; }
th { color: var(--muted); font-weight: 600; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.num input, input.num, input[type="number"] { text-align: right; font-variant-numeric: tabular-nums; }

.btn { border: 0; border-radius: 10px; padding: 8px 12px; cursor: pointer; background: #e2e8f0; }
.btn.primary { background: var(--brand); color: #fff; }
.btn.ghost { background: transparent; border: 1px solid var(--line); }
.btn.ok { background: var(--ok); color: #fff; }
.btn.warn { background: var(--warn); color: #fff; }
.btn.block { width: 100%; padding: 12px; }
.btn.danger { background: var(--danger); color: #fff; }
a.btn { display: inline-flex; align-items: center; justify-content: center; }
.form-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.actions { display: flex; flex-wrap: wrap; gap: 6px; }
.toolbar { display: flex; gap: 8px; margin-bottom: 12px; }
.toolbar .grow { flex: 1; }

form.stack, .stack { display: grid; gap: 8px; }
label { font-size: 13px; color: var(--muted); }
input, select, textarea { width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: #fff; }
input[readonly] { background: #f8fafc; color: var(--muted); cursor: not-allowed; }
.form-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; margin-bottom: 14px; }
.line-table { width: 100%; margin-top: 4px; }
.line-table th { font-size: 13px; }
.line-table .col-qty, .line-table .col-price, .line-table .col-amt { width: 120px; }
.line-table .col-del { width: 72px; }
.line-foot { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-top: 10px; }
.line-foot strong { font-size: 16px; }
a.doc-no { color: var(--brand); font-weight: 700; }
a.doc-no:hover { text-decoration: underline; }
.alert { background: #fef2f2; color: var(--danger); padding: 10px 12px; border-radius: 10px; margin-bottom: 10px; }
.okbox { background: #ecfdf5; color: var(--ok); padding: 10px 12px; border-radius: 10px; margin-bottom: 10px; }
.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; background: #e2e8f0; font-size: 12px; }
.muted { color: var(--muted); }
pre.json { white-space: pre-wrap; margin: 0; font-size: 13px; }

.login-body { min-height: 100vh; display: grid; place-items: center; background: radial-gradient(circle at 20% 20%, #134e4a, #0b1324 55%); }
.login-shell { width: min(920px, calc(100% - 32px)); display: grid; grid-template-columns: 1.1fr .9fr; background: #0f172a; border-radius: 24px; overflow: hidden; box-shadow: 0 30px 80px rgba(0,0,0,.35); }
.login-brand { padding: 48px 40px; color: #e2e8f0; }
.login-brand h1 { margin: 16px 0 8px; }
.login-brand p, .login-brand li { color: #94a3b8; }
.login-card { background: #fff; padding: 40px; display: grid; align-content: center; gap: 8px; }
.login-card h2 { margin: 0 0 8px; }
.hint { color: var(--muted); font-size: 12px; margin-top: 8px; }
.password-field { position: relative; }
.password-field input { padding-right: 44px; }
.password-toggle { position: absolute; right: 4px; top: 50%; transform: translateY(-50%); width: 36px; height: 36px; border: 0; padding: 0; background: transparent; color: var(--muted); cursor: pointer; display: grid; place-items: center; border-radius: 8px; }
.password-toggle:hover, .password-toggle:focus { color: var(--ink); background: #f1f5f9; outline: none; }
.password-toggle svg { width: 20px; height: 20px; display: block; }
.password-toggle .icon-eye-off { display: none; }
.password-toggle[aria-pressed="true"] .icon-eye { display: none; }
.password-toggle[aria-pressed="true"] .icon-eye-off { display: block; }

@media (max-width: 720px) {
  .app-shell, .kpis, .grid-2, .form-grid, .login-shell { grid-template-columns: 1fr; }
}

.so-page { display: flex; flex-direction: column; min-height: calc(100vh - 120px); }
.so-top { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; margin-bottom: 12px; }
.so-top h1 { margin: 0; font-size: 24px; }
.so-top p { margin: 4px 0 0; color: var(--muted); font-size: 13px; }
.so-tools { display: flex; gap: 10px; }
.so-tool { width: 72px; height: 64px; border: 0; border-radius: 12px; color: #fff; cursor: pointer; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px; font-weight: 700; font-size: 13px; }
.so-tool svg { width: 26px; height: 26px; }
a.so-tool { text-decoration: none; }
.so-tool-new { background: linear-gradient(135deg, var(--brand), var(--brand-2)); }
.so-tool-print { background: var(--sider); }
.so-tool-search { background: var(--ok); }
.so-desk { display: grid; grid-template-columns: 176px minmax(760px, 1fr); gap: 14px; flex: 1; min-height: 0; overflow: auto; }
.so-nav { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; display: flex; flex-direction: column; min-height: 520px; overflow: hidden; }
.so-tabs { display: grid; grid-template-columns: 1fr 1fr; background: #f8fafc; border-bottom: 1px solid var(--line); }
.so-tabs a { text-align: center; padding: 10px 6px; font-weight: 700; color: var(--muted); }
.so-tabs a.is-on { background: #fff; color: var(--brand); box-shadow: inset 0 -2px 0 var(--brand); }
.so-nav-head { background: var(--brand); color: #fff; font-weight: 700; padding: 8px 12px; font-size: 16px; text-align: center; }
.so-nav-search { padding: 8px; border-bottom: 1px solid var(--line); }
.so-nav-search input { padding: 8px 10px; border-radius: 10px; font-size: 13px; }
.so-nav-search input::placeholder { font-size: 11px; color: var(--muted); }
.so-nav-list { overflow: auto; flex: 1; background: #fff; }
.so-doc { display: block; padding: 10px 12px; font-family: Consolas, "Noto Sans TC", monospace; font-size: 16px; text-align: center; border-bottom: 1px solid var(--line); color: var(--ink); }
.so-doc:hover { background: #f0fdfa; }
.so-doc.is-on { background: var(--brand); color: #fff; font-weight: 700; }
.so-empty, .so-blank { padding: 24px 16px; color: var(--muted); text-align: center; }
.so-stage { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; display: flex; flex-direction: column; min-width: 0; padding: 16px; }
.so-head { background: #f8fafc; border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; margin-bottom: 10px; }
.so-fields { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px 14px; }
.so-field.so-span { grid-column: auto; }
.so-field label { display: flex; align-items: center; gap: 6px; font-weight: 600; color: var(--muted); margin-bottom: 4px; }
.so-field label::before { content: ""; width: 10px; height: 10px; border-radius: 50%; background: linear-gradient(135deg, var(--brand), var(--brand-2)); flex: 0 0 auto; }
.so-field input, .so-field select { padding: 7px 10px; border-radius: 10px; border-color: var(--line); background: #fff; }
.so-doctype, input.so-doctype { color: var(--danger); font-weight: 700; }
.so-grid-wrap { background: #fff; border: 1px solid var(--line); border-radius: 12px; min-height: 280px; overflow: auto; flex: 1; display: flex; flex-direction: column; }
.so-grid { width: 100%; border-collapse: collapse; table-layout: fixed; }
.so-grid th { background: #f8fafc; color: var(--muted); font-weight: 700; padding: 10px 8px; border-bottom: 1px solid var(--line); font-size: 16px; }
.so-grid td { padding: 8px; border-bottom: 1px solid var(--line); font-size: 16px; background: #fff; }
.so-grid tr.is-on td { background: #f0fdfa; }
.so-grid tr.is-on input, .so-grid tr.is-on select { background: #fff; }
.so-grid .col-code { width: 120px; }
.so-grid .col-name { width: auto; }
.so-grid .col-unit { width: 48px; }
.so-grid .num { text-align: right; width: 88px; }
.so-grid .col-note { width: 88px; }
.so-grid .col-del { width: 36px; text-align: center; }
.so-grid td.col-code { padding-left: 6px; padding-right: 6px; }
.so-grid .col-code select {
  width: 100%;
  min-width: 0;
  padding: 8px 26px 8px 8px;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24'%3E%3Cpath fill='%2364748b' d='M7 10l5 5 5-5z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
}
.so-grid .num input[type="number"] { -moz-appearance: textfield; padding-right: 8px; }
.so-grid .num input[type="number"]::-webkit-outer-spin-button,
.so-grid .num input[type="number"]::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.so-grid .col-name, .so-grid .line-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.so-grid input, .so-grid select { padding: 8px 10px; border-radius: 8px; font-size: 16px; }
.so-field.so-add-slot { display: flex; align-items: flex-end; justify-content: flex-end; }
.so-date-wrap { position: relative; }
.so-date-wrap input { padding-right: 40px; }
.so-cal-btn { position: absolute; right: 4px; top: 50%; transform: translateY(-50%); width: 32px; height: 32px; border: 0; padding: 0; background: transparent; color: var(--muted); cursor: pointer; display: grid; place-items: center; border-radius: 8px; }
.so-cal-btn:hover { color: var(--brand); background: #f0fdfa; }
.so-cal-btn svg { width: 18px; height: 18px; display: block; }
.cal-pop { position: fixed; z-index: 40; width: 280px; background: #fff; border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 12px 32px rgba(15,23,42,.16); padding: 10px; }
.cal-head { display: flex; align-items: center; gap: 4px; margin-bottom: 8px; }
.cal-title { flex: 1; text-align: center; font-weight: 700; font-size: 14px; color: var(--ink); }
.cal-nav { width: 28px; height: 28px; border: 0; border-radius: 8px; background: #f1f5f9; cursor: pointer; color: var(--ink); font-size: 14px; }
.cal-nav:hover { background: #e2e8f0; }
.cal-week, .cal-days { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px; text-align: center; }
.cal-week { margin-bottom: 4px; }
.cal-week span { font-size: 12px; color: var(--muted); font-weight: 700; padding: 4px 0; }
.cal-week .sun, .cal-days .sun { color: var(--danger); }
.cal-days button { height: 32px; border: 0; border-radius: 8px; background: transparent; cursor: pointer; font-size: 13px; }
.cal-days button:hover { background: #f0fdfa; }
.cal-days .today { box-shadow: inset 0 0 0 1px var(--brand); }
.cal-days .is-on { background: var(--brand); color: #fff; }
.cal-days .is-on.sun { color: #fff; }
.cal-foot { display: flex; justify-content: flex-end; margin-top: 8px; }
.cal-today { border: 0; background: transparent; color: var(--brand); font-weight: 700; cursor: pointer; padding: 4px 8px; border-radius: 8px; }
.cal-today:hover { background: #f0fdfa; }
.so-x { width: 26px; height: 26px; border: 0; border-radius: 8px; background: var(--danger); color: #fff; cursor: pointer; font-size: 16px; line-height: 1; }
.so-x:disabled { opacity: .4; cursor: not-allowed; }
.so-add-line { display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-width: 0; width: 100%; border: 0; border-radius: 10px; padding: 8px 16px; background: linear-gradient(135deg, var(--brand), var(--brand-2)); color: #fff; font-weight: 800; font-size: 15px; letter-spacing: .04em; cursor: pointer; }
.so-add-line:hover { filter: brightness(1.06); }
.so-add-line:active { transform: translateY(1px); }
.so-add-line svg { width: 20px; height: 20px; display: block; }
.so-foot { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px 14px; margin-top: 12px; background: #f8fafc; border: 1px solid var(--line); border-radius: 12px; padding: 12px; align-items: start; }
.so-foot .so-field input, .so-foot .so-field select { min-height: 38px; }
.so-foot-gap { visibility: hidden; pointer-events: none; }
.so-red { color: var(--danger) !important; font-weight: 700; }
.so-actions { margin-top: 12px; }
.content:has(.so-page) { padding: 24px; }

@media (max-width: 720px) {
  .so-desk { grid-template-columns: 1fr; }
  .so-fields { grid-template-columns: 1fr; }
  .so-foot { grid-template-columns: 1fr; }
  .so-foot-gap { display: none; }
  .so-nav { min-height: 180px; }
}
@media print {
  .sider, .topbar, .so-tools, .so-nav, .so-actions, .so-add-slot, .so-nav-search { display: none !important; }
  .app-shell { display: block; }
  .content { padding: 0; }
  .so-desk { display: block; background: #fff; border: 0; }
  .so-stage { background: #fff; }
}
