:root {
  --bg: #f7f5fb;
  --panel: #ffffff;
  --text: #17202a;
  --muted: #65717d;
  --line: #ded7ee;
  --primary: #8b6fd6;
  --primary-dark: #624bad;
  --danger: #b42318;
  --success: #0f7b54;
  --warning: #946200;
  --watch: #7257c8;
  --body-font-size: 16px;
  --table-font-size: 15px;
  --control-font-size: 15px;
  --title-font-size: 24px;
  font-family: "Segoe UI", "Noto Sans TC", Arial, sans-serif;
}

body.font-level-0 {
  --body-font-size: 15px;
  --table-font-size: 14px;
  --control-font-size: 14px;
  --title-font-size: 22px;
}

body.font-level-1 {
  --body-font-size: 16px;
  --table-font-size: 15px;
  --control-font-size: 15px;
  --title-font-size: 24px;
}

body.font-level-2 {
  --body-font-size: 18px;
  --table-font-size: 17px;
  --control-font-size: 16px;
  --title-font-size: 26px;
}

body.font-level-3 {
  --body-font-size: 20px;
  --table-font-size: 20px;
  --control-font-size: 18px;
  --title-font-size: 30px;
}

body.font-level-4 {
  --body-font-size: 23px;
  --table-font-size: 23px;
  --control-font-size: 20px;
  --title-font-size: 34px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding-bottom: 28px;
  background: var(--bg);
  color: var(--text);
  font-size: var(--body-font-size);
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  border: 0;
  border-radius: 6px;
  padding: 9px 13px;
  cursor: pointer;
  background: #eeeaf7;
  color: var(--text);
  font-size: var(--control-font-size);
}

button:hover {
  filter: brightness(0.97);
}

button:disabled {
  opacity: 0.48;
  cursor: not-allowed;
}

.primary {
  background: var(--primary);
  color: white;
}

.secondary {
  background: #efebf8;
}

.ghost {
  background: transparent;
  border: 1px solid var(--line);
}

.danger {
  background: #fbe9e7;
  color: var(--danger);
}

.hidden {
  display: none !important;
}

.login-view {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.login-panel {
  width: min(420px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 28px;
  box-shadow: 0 16px 40px rgba(80, 55, 125, 0.12);
}

.login-panel h1 {
  margin: 0 0 8px;
  font-size: var(--title-font-size);
}

.login-panel p {
  color: var(--muted);
  margin: 0 0 22px;
}

label {
  display: grid;
  gap: 6px;
  color: #33404c;
  font-size: var(--control-font-size);
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 9px 10px;
  background: white;
  color: var(--text);
  font-size: var(--control-font-size);
}

textarea {
  resize: vertical;
}

.form-error {
  color: var(--danger);
  min-height: 20px;
  margin-top: 12px;
}

.app-shell {
  min-height: 100vh;
}

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 20px;
  align-items: center;
  padding: 18px 24px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 5;
}

.topbar h1 {
  margin: 0;
  font-size: var(--title-font-size);
}

.topbar p {
  margin: 4px 0 0;
  color: var(--muted);
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.role-badge,
.status-pill {
  display: inline-flex;
  align-items: center;
  min-height: 26px;
  border-radius: 999px;
  padding: 3px 9px;
  font-size: calc(var(--table-font-size) - 1px);
  background: #edf2f7;
  color: #2f3b47;
  white-space: nowrap;
}

main {
  padding: 20px 24px 32px;
}

.toolbar {
  display: grid;
  gap: 14px;
  margin-bottom: 16px;
}

.tabs {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.tab-button {
  border: 1px solid var(--line);
  background: white;
}

.tab-button.active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

.filters {
  display: grid;
  grid-template-columns: minmax(260px, 1fr) 210px 190px 140px 130px minmax(220px, max-content);
  gap: 12px;
  align-items: end;
}

.toggle-label {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 39px;
}

.toggle-label input {
  width: auto;
}

.font-control {
  border: 0;
  display: grid;
  gap: 6px;
  margin: 0;
  padding: 0;
}

.font-control legend {
  color: #33404c;
  font-size: var(--control-font-size);
  padding: 0;
}

.font-stepper {
  display: grid;
  grid-template-columns: minmax(54px, auto) minmax(72px, 1fr) minmax(54px, auto);
  align-items: center;
  gap: 6px;
}

.font-stepper button {
  min-height: 39px;
  padding: 7px 12px;
  border: 1px solid var(--line);
  background: white;
  font-weight: 800;
}

.font-stepper button:not(:disabled):hover {
  background: var(--primary);
  border-color: var(--primary);
  color: white;
}

#fontLevelLabel {
  min-height: 39px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 7px 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #f7f2ff;
  color: var(--primary-dark);
  font-size: var(--control-font-size);
  font-weight: 700;
}

.hint {
  margin: -4px 0 0;
  color: var(--primary-dark);
  font-size: var(--control-font-size);
}

.list-shell {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: visible;
}

.table-scroll {
  overflow-x: auto;
  border-radius: 8px 8px 0 0;
}

.bottom-scroll {
  height: 22px;
  overflow-x: auto;
  overflow-y: hidden;
  position: sticky;
  bottom: 0;
  z-index: 4;
  background: #fbfaff;
  border-top: 1px solid var(--line);
  border-radius: 0 0 8px 8px;
}

.bottom-scroll div {
  height: 1px;
}

table {
  width: 100%;
  min-width: 1480px;
  border-collapse: collapse;
}

th,
td {
  border-bottom: 1px solid var(--line);
  padding: 10px 11px;
  text-align: left;
  vertical-align: top;
  font-size: var(--table-font-size);
}

th {
  background: #f1edf9;
  color: #33404c;
  position: sticky;
  top: 0;
  z-index: 2;
}

tbody tr {
  background: white;
}

tbody tr.dragging {
  opacity: 0.55;
  outline: 2px solid var(--primary);
}

tbody tr.drop-target {
  box-shadow: inset 0 3px 0 var(--primary);
}

.drag-handle {
  width: 34px;
  padding: 8px;
  text-align: center;
  cursor: grab;
  background: #f0eafd;
}

.long-text {
  max-width: 420px;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  white-space: pre-wrap;
}

.long-text.expanded {
  display: block;
  max-width: 680px;
  overflow: visible;
}

.text-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 22px;
  margin-top: 4px;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--primary-dark);
  font-size: calc(var(--table-font-size) + 1px);
  line-height: 1;
  opacity: 0.62;
}

.text-toggle:hover {
  background: #f7f2ff;
  opacity: 1;
}

.company-cell button {
  padding: 0;
  color: var(--primary-dark);
  background: transparent;
  text-align: left;
  font-weight: 700;
}

.link-list {
  display: grid;
  gap: 6px;
}

a {
  color: var(--primary-dark);
}

.row-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  min-width: 220px;
}

.row-actions button {
  padding: 6px 8px;
}

.empty-state {
  padding: 36px;
  text-align: center;
  color: var(--muted);
}

.compact .detail-column,
.compact td.detail-cell,
.compact th:nth-child(6),
.compact td:nth-child(6),
.compact th:nth-child(7),
.compact td:nth-child(7),
.compact th:nth-child(9),
.compact td:nth-child(9),
.compact th:nth-child(10),
.compact td:nth-child(10),
.compact th:nth-child(12),
.compact td:nth-child(12) {
  display: none;
}

.compact table {
  min-width: 1080px;
}

.status-pill[data-status*="watch" i] {
  background: #f0eafd;
  color: var(--watch);
}

.status-pill[data-status*="投資"] {
  background: #e6f5ee;
  color: var(--success);
}

.status-pill[data-status*="放棄"] {
  background: #fdecec;
  color: var(--danger);
}

.status-pill[data-status*="未立案"] {
  background: #fff4db;
  color: var(--warning);
}

.status-pill[data-status*="評估"] {
  background: #ece7f9;
  color: var(--primary-dark);
}

dialog {
  width: min(960px, calc(100vw - 32px));
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0;
  box-shadow: 0 24px 80px rgba(80, 55, 125, 0.22);
}

dialog::backdrop {
  background: rgba(20, 29, 38, 0.38);
}

.case-form {
  padding: 20px;
}

.dialog-head,
.dialog-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

.dialog-head {
  justify-content: space-between;
  margin-bottom: 18px;
}

.dialog-head h2 {
  margin: 0;
  font-size: var(--title-font-size);
}

.icon-button {
  font-size: 24px;
  line-height: 1;
  width: 36px;
  height: 36px;
  padding: 0;
  background: transparent;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
}

.wide {
  grid-column: 1 / -1;
}

.dialog-actions {
  margin-top: 18px;
}

.spacer {
  flex: 1;
}

.toast {
  position: fixed;
  right: 18px;
  bottom: 34px;
  background: #2c2140;
  color: white;
  padding: 12px 14px;
  border-radius: 6px;
  box-shadow: 0 12px 30px rgba(80, 55, 125, 0.22);
  z-index: 20;
}

@media (max-width: 1280px) {
  .filters {
    grid-template-columns: 1fr 210px 190px 140px;
  }

  .font-control {
    grid-column: span 2;
  }
}

@media (max-width: 980px) {
  .filters {
    grid-template-columns: 1fr 1fr;
  }
}

@media (max-width: 820px) {
  .topbar {
    align-items: flex-start;
    flex-direction: column;
    position: static;
  }

  th {
    top: 0;
  }

  .filters {
    grid-template-columns: 1fr;
  }

  .font-control {
    grid-column: auto;
  }

  .form-grid {
    grid-template-columns: 1fr;
  }
}
