/* 500net CRM500 v2 — 規格 §6.1 共通 UX
   桌面 1366px 為主要設計,平板與手機支援查詢、活動快登、待辦與核准。 */

:root {
  --bg: #f4f6f9;
  --panel: #fff;
  --ink: #1c2434;
  --ink-2: #59677d;
  --ink-3: #8c98ab;
  --line: #e2e7ef;
  --brand: #1f5fa9;
  --brand-2: #17457c;
  --brand-soft: #eaf2fb;
  --ok: #1a7f4b;
  --ok-soft: #e6f5ed;
  --warn: #b26a00;
  --warn-soft: #fdf3e2;
  --danger: #c0392b;
  --danger-soft: #fdeceb;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(22, 34, 51, .08), 0 4px 14px rgba(22, 34, 51, .06);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 14px/1.6 "Microsoft JhengHei", "PingFang TC", "Noto Sans TC", system-ui, sans-serif;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--brand); }

#app { display: flex; min-height: 100vh; }

/* ---------- 側欄 ---------- */
#sidebar {
  /* 寬度直接寫死兩種狀態。用 var() 搭配 transition 在移除 class 後
     computed width 會卡在舊值(實測過),所以不繞自訂屬性。 */
  width: 210px; flex: 0 0 210px;
  background: #16233a; color: #cfd8e6;
  display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh;
  transition: width .18s ease, flex-basis .18s ease;
}
/* 收合模式:只留圖示,把版面讓給右邊內容 */
#sidebar.collapsed { width: 56px; flex: 0 0 56px; }

.brand { padding: 16px 16px 12px; border-bottom: 1px solid rgba(255,255,255,.08); position: relative; }
.brand-name { font-size: 16px; font-weight: 700; color: #fff; letter-spacing: .5px; white-space: nowrap; }
.brand-sub { font-size: 11px; color: #7f8ea6; margin-top: 2px; white-space: nowrap; }

/* 左右收合鈕:貼在側欄右緣 */
#btn-collapse {
  position: absolute; right: -11px; top: 20px; z-index: 30;
  width: 22px; height: 22px; border-radius: 50%;
  background: #22406b; color: #cfd8e6; border: 1px solid #3a5885;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; font-size: 11px; line-height: 1; padding: 0;
}
#btn-collapse:hover { background: var(--brand); color: #fff; }

#nav { flex: 1; overflow-y: auto; overflow-x: hidden; padding: 8px 0; }

/* 分類標題:可點擊上下摺疊 */
.nav-group {
  display: flex; align-items: center; gap: 6px;
  padding: 10px 16px 4px; font-size: 11px; color: #6b7a92; letter-spacing: 1px;
  cursor: pointer; user-select: none; white-space: nowrap;
}
.nav-group:hover { color: #9fb0c8; }
.nav-group .caret {
  display: inline-block; transition: transform .18s ease; font-size: 9px; flex: 0 0 auto;
}
.nav-group.collapsed .caret { transform: rotate(-90deg); }
.nav-group .g-count {
  margin-left: auto; background: rgba(255,255,255,.08); border-radius: 8px;
  padding: 0 5px; font-size: 10px; line-height: 15px; display: none;
}
.nav-group.collapsed .g-count { display: inline-block; }

.nav-section { overflow: hidden; transition: max-height .18s ease; }
.nav-section.collapsed { max-height: 0 !important; }

.nav-item {
  display: flex; align-items: center; gap: 8px; padding: 8px 16px;
  cursor: pointer; border-left: 3px solid transparent; font-size: 13px; white-space: nowrap;
}
.nav-item:hover { background: rgba(255,255,255,.05); color: #fff; }
.nav-item.active { background: rgba(47,124,207,.18); border-left-color: #4a90d9; color: #fff; font-weight: 600; }
.nav-item .n-icon { flex: 0 0 16px; text-align: center; }
.nav-item .n-label { flex: 1; overflow: hidden; text-overflow: ellipsis; }
.nav-item .n-badge {
  margin-left: auto; background: var(--danger); color: #fff; border-radius: 9px;
  padding: 0 6px; font-size: 11px; line-height: 17px; flex: 0 0 auto;
}

/* 收合後只顯示圖示 */
#sidebar.collapsed .brand-name { font-size: 0; }
#sidebar.collapsed .brand-name::after { content: '500'; font-size: 15px; }
#sidebar.collapsed .brand-sub,
#sidebar.collapsed .nav-group,
#sidebar.collapsed .nav-item .n-label,
#sidebar.collapsed .sidebar-foot .who,
#sidebar.collapsed .ver { display: none; }
#sidebar.collapsed .nav-section { max-height: none !important; }
#sidebar.collapsed .nav-item { justify-content: center; padding: 9px 0; gap: 0; }
#sidebar.collapsed .nav-item .n-badge {
  position: absolute; margin-left: 22px; margin-top: -12px; padding: 0 4px; font-size: 9px; line-height: 14px;
}
#sidebar.collapsed .nav-item { position: relative; }
#sidebar.collapsed .sidebar-foot { padding: 10px 4px; text-align: center; }

.sidebar-foot { padding: 12px 16px; border-top: 1px solid rgba(255,255,255,.08); font-size: 12px; }
.sidebar-foot .link { background: none; border: 0; color: #8fb4dd; cursor: pointer; padding: 4px 0; }
.sidebar-foot .who { cursor: pointer; }
.sidebar-foot .who:hover { color: #fff; }
.ver { color: #5d6b82; font-size: 11px; margin-top: 4px; }

/* ---------- 主區 ---------- */
#main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
#topbar {
  display: flex; align-items: center; gap: 12px; padding: 10px 20px;
  background: var(--panel); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20;
}
#btn-menu { display: none; }
.icon-btn {
  background: none; border: 1px solid var(--line); border-radius: var(--radius);
  padding: 6px 10px; cursor: pointer; position: relative;
}
.search-wrap { flex: 1; max-width: 520px; position: relative; }
#global-search {
  width: 100%; padding: 7px 12px; border: 1px solid var(--line);
  border-radius: var(--radius); background: var(--bg);
}
#global-search:focus { outline: 2px solid var(--brand-soft); border-color: var(--brand); background: #fff; }
.search-results {
  position: absolute; top: 100%; left: 0; right: 0; margin-top: 4px; background: #fff;
  border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow);
  max-height: 60vh; overflow-y: auto; z-index: 30;
}
.search-results .sr-group { padding: 6px 12px; font-size: 11px; color: var(--ink-3); background: var(--bg); }
.search-results .sr-item { padding: 8px 12px; cursor: pointer; border-bottom: 1px solid var(--line); }
.search-results .sr-item:hover { background: var(--brand-soft); }
.topbar-right { margin-left: auto; display: flex; gap: 8px; }
.badge {
  position: absolute; top: -4px; right: -4px; background: var(--danger); color: #fff;
  border-radius: 9px; font-size: 10px; padding: 0 5px; line-height: 16px;
}

#view { padding: 20px; flex: 1; }

/* ---------- 版面元件 ---------- */
.page-head { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.page-head h1 { font-size: 20px; margin: 0; }
.page-head .spacer { flex: 1; }
.crumb { color: var(--ink-3); font-size: 12px; margin-bottom: 4px; }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.card + .card { margin-top: 16px; }
.card-head {
  padding: 12px 16px; border-bottom: 1px solid var(--line); font-weight: 600;
  display: flex; align-items: center; gap: 10px;
}
.card-head .spacer { flex: 1; }
.card-body { padding: 16px; }
.card-body.tight { padding: 0; }

.grid { display: grid; gap: 16px; }
.grid.k4 { grid-template-columns: repeat(4, 1fr); }
.grid.k3 { grid-template-columns: repeat(3, 1fr); }
.grid.k2 { grid-template-columns: repeat(2, 1fr); }
@media (max-width: 1100px) { .grid.k4 { grid-template-columns: repeat(2, 1fr); } .grid.k3, .grid.k2 { grid-template-columns: 1fr; } }

/* KPI 磚 */
.kpi { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; cursor: pointer; }
.kpi:hover { border-color: var(--brand); }
.kpi .k-label { font-size: 12px; color: var(--ink-2); }
.kpi .k-value { font-size: 24px; font-weight: 700; margin-top: 4px; letter-spacing: -.5px; }
.kpi .k-sub { font-size: 11px; color: var(--ink-3); margin-top: 2px; }
.kpi.alert .k-value { color: var(--danger); }
.kpi.good .k-value { color: var(--ok); }

/* 表格 */
table.tbl { width: 100%; border-collapse: collapse; font-size: 13px; }
table.tbl th {
  text-align: left; padding: 9px 12px; background: var(--bg); color: var(--ink-2);
  font-weight: 600; border-bottom: 1px solid var(--line); white-space: nowrap; font-size: 12px;
}
table.tbl th.sortable { cursor: pointer; user-select: none; }
table.tbl td { padding: 9px 12px; border-bottom: 1px solid var(--line); vertical-align: top; }
table.tbl tbody tr:hover { background: #fafbfd; }
table.tbl tbody tr.clickable { cursor: pointer; }
table.tbl td.num, table.tbl th.num { text-align: right; font-variant-numeric: tabular-nums; }
.table-wrap { overflow-x: auto; }

.empty { padding: 40px 20px; text-align: center; color: var(--ink-3); }
.empty .empty-action { margin-top: 12px; }

/* 按鈕 */
.btn {
  border: 1px solid var(--line); background: #fff; border-radius: var(--radius);
  padding: 7px 14px; cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: var(--ink-3); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-2); }
.btn.danger { color: var(--danger); border-color: #f0c8c4; }
.btn.sm { padding: 4px 9px; font-size: 12px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* 標籤 */
.tag {
  display: inline-block; padding: 1px 8px; border-radius: 11px; font-size: 11px;
  background: var(--bg); border: 1px solid var(--line); color: var(--ink-2); white-space: nowrap;
}
.tag.brand { background: var(--brand-soft); border-color: #c5dcf5; color: var(--brand-2); }
.tag.ok { background: var(--ok-soft); border-color: #bfe4cf; color: var(--ok); }
.tag.warn { background: var(--warn-soft); border-color: #f2dcb4; color: var(--warn); }
.tag.danger { background: var(--danger-soft); border-color: #f4c9c4; color: var(--danger); }

/* 表單 */
.form-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px 16px; }
.form-grid.one { grid-template-columns: 1fr; }
@media (max-width: 700px) { .form-grid { grid-template-columns: 1fr; } }
.field { display: flex; flex-direction: column; gap: 4px; }
.field.span2 { grid-column: 1 / -1; }
.field label { font-size: 12px; color: var(--ink-2); }
.field label .req { color: var(--danger); margin-left: 2px; }
.field input, .field select, .field textarea {
  padding: 7px 10px; border: 1px solid var(--line); border-radius: var(--radius); background: #fff; width: 100%;
}
.field input:focus, .field select:focus, .field textarea:focus { outline: 2px solid var(--brand-soft); border-color: var(--brand); }
.field .hint { font-size: 11px; color: var(--ink-3); }
.field.error input, .field.error select, .field.error textarea { border-color: var(--danger); }
.field .err { font-size: 11px; color: var(--danger); }
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: flex-end; }
.filters .field { min-width: 130px; }

/* 看板 SCR-040 */
.board { display: flex; gap: 12px; overflow-x: auto; padding-bottom: 12px; align-items: flex-start; }
.board-col {
  flex: 0 0 250px; background: #eef1f6; border-radius: var(--radius); display: flex; flex-direction: column;
  max-height: calc(100vh - 220px);
}
.board-col.drag-over { background: var(--brand-soft); outline: 2px dashed var(--brand); }
.board-col-head { padding: 10px 12px; border-bottom: 1px solid #dfe4ec; }
.board-col-head .bc-title { font-weight: 600; font-size: 13px; display: flex; gap: 6px; align-items: center; }
.board-col-head .bc-sum { font-size: 11px; color: var(--ink-2); margin-top: 2px; }
.board-col-body { overflow-y: auto; padding: 8px; display: flex; flex-direction: column; gap: 8px; }
.deal-card {
  background: #fff; border: 1px solid var(--line); border-radius: 6px; padding: 10px; cursor: grab;
  border-left: 3px solid var(--brand);
}
.deal-card:hover { box-shadow: var(--shadow); }
.deal-card.overdue { border-left-color: var(--danger); }
.deal-card .dc-name { font-weight: 600; font-size: 13px; margin-bottom: 2px; }
.deal-card .dc-meta { font-size: 11px; color: var(--ink-2); }
.deal-card .dc-amt { font-weight: 600; font-variant-numeric: tabular-nums; }
.deal-card.dragging { opacity: .4; }

/* 時間軸 */
.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { padding: 10px 0 10px 22px; border-left: 2px solid var(--line); position: relative; }
.timeline li::before {
  content: ''; position: absolute; left: -6px; top: 15px; width: 10px; height: 10px;
  border-radius: 50%; background: var(--brand); border: 2px solid #fff;
}
.timeline li.done::before { background: var(--ok); }
.timeline .tl-time { font-size: 11px; color: var(--ink-3); }
.timeline .tl-title { font-weight: 600; }
.timeline .tl-body { font-size: 13px; color: var(--ink-2); white-space: pre-wrap; }

/* 分頁籤 */
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--line); margin-bottom: 16px; flex-wrap: wrap; }
.tab {
  padding: 8px 14px; cursor: pointer; border-bottom: 2px solid transparent; color: var(--ink-2); font-size: 13px;
}
.tab.active { border-bottom-color: var(--brand); color: var(--brand); font-weight: 600; }

/* 進度條 / 長條圖 */
.bar { height: 8px; background: var(--bg); border-radius: 4px; overflow: hidden; }
.bar > span { display: block; height: 100%; background: var(--brand); }
.bar.ok > span { background: var(--ok); }
.bar.warn > span { background: var(--warn); }
.funnel-row { display: grid; grid-template-columns: 110px 1fr 90px 60px; gap: 10px; align-items: center; padding: 5px 0; font-size: 13px; }

/* 彈窗 */
#modal-root:empty { display: none; }
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(18,28,45,.45); display: flex;
  align-items: flex-start; justify-content: center; padding: 40px 16px; z-index: 100; overflow-y: auto;
}
.modal {
  background: #fff; border-radius: 10px; width: 100%; max-width: 720px; box-shadow: 0 18px 50px rgba(0,0,0,.25);
}
.modal.wide { max-width: 1080px; }
.modal.narrow { max-width: 460px; }
.modal-head { padding: 14px 18px; border-bottom: 1px solid var(--line); display: flex; align-items: center; }
.modal-head h2 { margin: 0; font-size: 16px; }
.modal-head .x { margin-left: auto; background: none; border: 0; font-size: 20px; cursor: pointer; color: var(--ink-3); }
.modal-body { padding: 18px; max-height: 70vh; overflow-y: auto; }
.modal-foot { padding: 12px 18px; border-top: 1px solid var(--line); display: flex; gap: 8px; justify-content: flex-end; }
.modal-foot .spacer { flex: 1; }

/* 提示訊息 */
#toast-root { position: fixed; right: 16px; bottom: 16px; z-index: 200; display: flex; flex-direction: column; gap: 8px; }
.toast {
  background: #26344b; color: #fff; padding: 10px 16px; border-radius: var(--radius);
  box-shadow: var(--shadow); max-width: 380px; font-size: 13px;
}
.toast.err { background: var(--danger); }
.toast.ok { background: var(--ok); }
.toast .t-rid { display: block; font-size: 10px; opacity: .65; margin-top: 3px; }

.alert { padding: 10px 14px; border-radius: var(--radius); font-size: 13px; }
.alert.warn { background: var(--warn-soft); border: 1px solid #f2dcb4; color: #7a4a00; }
.alert.danger { background: var(--danger-soft); border: 1px solid #f4c9c4; color: #8e2a1f; }
.alert.info { background: var(--brand-soft); border: 1px solid #c5dcf5; color: var(--brand-2); }
.alert + .alert { margin-top: 8px; }

.kv { display: grid; grid-template-columns: 100px 1fr; gap: 6px 12px; font-size: 13px; }
.kv dt { color: var(--ink-2); }
.kv dd { margin: 0; }

.muted { color: var(--ink-3); }
.small { font-size: 12px; }
.nowrap { white-space: nowrap; }
.right { text-align: right; }
.num { font-variant-numeric: tabular-nums; }
.masked { color: var(--ink-3); font-style: italic; }
.loading { padding: 40px; text-align: center; color: var(--ink-3); }

/* 手機 */
@media (max-width: 860px) {
  #sidebar { position: fixed; left: -210px; z-index: 60; transition: left .2s; }
  #sidebar.open { left: 0; }
  #btn-menu { display: block; }
  #view { padding: 12px; }
  .grid.k4 { grid-template-columns: repeat(2, 1fr); }
}
