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

:root {
  --sidebar-bg:     #f0f2f5;
  --sidebar-border: #d0d7de;
  --sidebar-muted:  #57606a;
  --sidebar-hover:  #e4e7eb;
  --primary:        #2ea44f;
  --primary-hover:  #2c974b;
  --bg:             #f6f8fa;
  --surface:        #ffffff;
  --border:         #d0d7de;
  --text:           #24292f;
  --text-muted:     #57606a;
  --danger:         #cf222e;
  --row-hover:      #f3f4f6;
  --shadow:         0 1px 3px rgba(0,0,0,.08);
  --badge-bg:       #ddf4ff;
  --badge-text:     #0969da;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft JhengHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
}

/* ── Layout ────────────────────────────────────────────────── */
#app { display: flex; height: 100vh; }

/* ── Sidebar ───────────────────────────────────────────────── */
#sidebar {
  width: 220px;
  min-width: 220px;
  background: var(--sidebar-bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border-right: 1px solid var(--sidebar-border);
}
.sidebar-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--sidebar-border);
  height: 48px;
}
.sidebar-header h1 { font-size: 15px; font-weight: 700; color: var(--text); letter-spacing: .03em; }

.sidebar-section { flex: 1; overflow-y: auto; padding: 8px 0; }

.section-title {
  display: flex; align-items: center; justify-content: space-between;
  padding: 6px 16px;
  font-size: 11px; font-weight: 600;
  color: var(--sidebar-muted);
  text-transform: uppercase; letter-spacing: .07em;
}

.btn-icon {
  background: none; border: none;
  color: var(--sidebar-muted);
  cursor: pointer; font-size: 15px; line-height: 1;
  padding: 2px 5px; border-radius: 4px;
  transition: color .15s, background .15s;
}
.btn-icon:hover { color: var(--text); background: var(--sidebar-hover); }

#people-list { list-style: none; }

.person-item {
  display: flex; align-items: center;
  padding: 7px 16px; gap: 6px;
  transition: background .1s;
}
.person-item:hover { background: var(--sidebar-hover); }

.person-check {
  flex-shrink: 0;
  width: 15px; height: 15px;
  cursor: pointer;
  accent-color: var(--primary);
}

.person-avatar {
  width: 24px; height: 24px; border-radius: 50%;
  background: var(--primary);
  display: flex; align-items: center; justify-content: center;
  font-size: 11px; font-weight: 600; color: #fff;
  flex-shrink: 0; text-transform: uppercase;
}
.person-name { flex: 1; font-size: 14px; }

.person-actions { display: none; gap: 3px; }
.person-item:hover .person-actions { display: flex; }
.person-actions button {
  background: none; border: none;
  color: var(--sidebar-muted); cursor: pointer;
  font-size: 13px; padding: 2px 4px; border-radius: 3px;
  transition: color .1s, background .1s;
}
.person-actions button:hover       { color: var(--text); background: var(--sidebar-hover); }
.person-actions .btn-del-person:hover { color: var(--danger); background: rgba(207,34,46,.1); }

.people-search-wrap {
  padding: 6px 12px 6px 10px;
  display: flex; gap: 5px; align-items: center;
  border-bottom: 1px solid var(--sidebar-border);
}
.people-search-wrap input[type="text"] {
  flex: 1; min-width: 0; padding: 5px 8px;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--sidebar-border); border-radius: 5px;
  font-size: 13px; outline: none;
  transition: border-color .15s;
}
.people-search-wrap input[type="text"]::placeholder { color: var(--text-muted); }
.people-search-wrap input[type="text"]:focus { border-color: var(--primary); }

.btn-cb-ctrl {
  flex-shrink: 0;
  background: none; border: none;
  font-size: 15px; line-height: 1;
  cursor: pointer; padding: 2px 1px;
  color: var(--text-muted);
  transition: color .1s;
}
.btn-cb-ctrl:hover { color: var(--primary); }

.empty-msg { padding: 10px 16px; font-size: 13px; color: var(--sidebar-muted); font-style: italic; }

/* ── Main ──────────────────────────────────────────────────── */
#main { flex: 1; display: flex; flex-direction: column; overflow: hidden; min-width: 0; }

.main-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  gap: 12px;
  height: 48px;
}
.main-header h2 { font-size: 17px; font-weight: 600; }

.toolbar { display: flex; gap: 10px; align-items: center; }

#search-input {
  padding: 6px 12px;
  border: 1px solid var(--border); border-radius: 6px;
  font-size: 14px; width: 200px; outline: none;
  background: var(--bg);
  transition: border-color .15s, box-shadow .15s;
}
#search-input:focus {
  border-color: #0969da; background: #fff;
  box-shadow: 0 0 0 3px rgba(9,105,218,.12);
}

.btn-primary {
  padding: 6px 14px; background: var(--primary); color: #fff;
  border: none; border-radius: 6px; font-size: 14px; font-weight: 500;
  cursor: pointer; white-space: nowrap; transition: background .15s;
}
.btn-primary:hover { background: var(--primary-hover); }

.btn-secondary {
  padding: 6px 14px; background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px;
  font-size: 14px; font-weight: 500; cursor: pointer;
  transition: background .15s;
}
.btn-secondary:hover { background: var(--bg); }
.btn-secondary:disabled { opacity: 0.4; cursor: default; }
.btn-secondary.active,
.dropdown.open > .btn-secondary {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary-hover);
}
.btn-secondary.active:hover,
.dropdown.open > .btn-secondary:hover {
  background: var(--primary-hover);
}

.btn-group {
  display: flex;
  border: 1px solid var(--border); border-radius: 6px;
  overflow: hidden;
}
.btn-group .btn-secondary {
  border: none; border-radius: 0;
  border-right: 1px solid var(--border);
}
.btn-group .btn-secondary:last-child { border-right: none; }

.btn-group .btn-secondary.armed {
  color: var(--danger);
  background: rgba(207,34,46,.07);
  font-weight: 600;
}

/* ── Dropdown ──────────────────────────────────────────────── */
.dropdown { position: relative; }

.dropdown-menu {
  display: none;
  position: absolute; right: 0; top: calc(100% + 4px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0,0,0,.12);
  z-index: 50; min-width: 160px; overflow: hidden;
  padding: 4px 0;
}
.dropdown.open .dropdown-menu { display: block; }

.dropdown-menu button {
  display: block; width: 100%;
  padding: 8px 14px; text-align: left;
  background: none; border: none;
  cursor: pointer; font-size: 14px; color: var(--text);
  transition: background .1s;
}
.dropdown-menu button:hover { background: var(--bg); }

.dropdown-divider {
  height: 1px; background: var(--border); margin: 4px 0;
}

/* ── Table ─────────────────────────────────────────────────── */
.table-container { flex: 1; overflow-y: auto; padding: 20px 24px; }

#relation-table {
  width: 100%; border-collapse: collapse; table-layout: fixed;
  background: var(--surface);
  border-radius: 8px; overflow: hidden;
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
}
#relation-table thead tr { background: var(--bg); }
#relation-table th {
  padding: 6px 12px; text-align: center;
  font-size: 11px; font-weight: 600; color: var(--text-muted);
  text-transform: uppercase; letter-spacing: .05em;
  border-bottom: 1px solid var(--border);
}
#relation-table td {
  padding: 6px 12px; font-size: 13px; text-align: center;
  border-bottom: 1px solid var(--border);
  transition: background .1s;
}
#relation-table tbody tr:last-child td { border-bottom: none; }
#relation-table tbody tr:hover td { background: var(--row-hover); }
#relation-table tbody tr.row-diff td { background: #fffbdd; }
#relation-table tbody tr.row-diff:hover td { background: #fff5b1; }
#relation-table tbody tr.row-max td { background: #f0fdf4; }
#relation-table tbody tr.row-max:hover td { background: #dcfce7; }

.pair-cell  { width: 160px; }
.snap-label { font-size: 11px; color: #adb5bd; margin-bottom: 2px; }
.pair-row  { display: flex; align-items: center; justify-content: center; gap: 4px; font-weight: 500; line-height: 1.5; }
.pair-sep  { color: var(--text-muted); flex-shrink: 0; }
.pair-divider { border: none; border-top: 1px solid var(--border); margin: 2px 0; }
.value-cell  { width: 160px; white-space: nowrap; }
#relation-table td.actions-cell { width: 90px; white-space: nowrap; text-align: right; }

/* ── Stepper ───────────────────────────────────────────────── */
.stepper { display: inline-flex; align-items: center; gap: 4px; }

.stepper-val {
  min-width: 20px; text-align: center;
  font-size: 14px; font-weight: 600;
}

.btn-stepper {
  width: 26px; height: 26px;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 5px;
  cursor: pointer; font-size: 14px; font-weight: 600;
  display: inline-flex; align-items: center; justify-content: center;
  padding: 0; transition: background .1s, border-color .1s;
}
.btn-stepper:hover:not(:disabled) { background: var(--bg); border-color: #adb5bd; }
.btn-stepper:disabled { opacity: 0.35; cursor: default; }

.btn-stepper-full {
  width: auto; padding: 0 8px;
  background: var(--badge-bg); color: var(--badge-text);
  border-color: #b6daed; font-size: 12px;
}
.btn-stepper-full:hover:not(:disabled) { background: #c0e6f8; border-color: #7bbcd8; }

.btn-del-rel {
  padding: 4px 10px; background: transparent;
  color: var(--danger); border: 1px solid var(--danger);
  border-radius: 5px; font-size: 12px; cursor: pointer;
  opacity: 0; transition: opacity .15s, background .15s, color .15s;
}
tr:hover .btn-del-rel { opacity: 1; }
.btn-del-rel:hover { background: var(--danger); color: #fff; }

/* ── Empty state ───────────────────────────────────────────── */
#empty-state {
  text-align: center; padding: 60px 20px;
  color: var(--text-muted); font-size: 14px;
}

/* ── Modal ─────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.45); z-index: 100;
}
.modal {
  position: fixed; top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  background: var(--surface);
  border-radius: 10px;
  box-shadow: 0 8px 32px rgba(0,0,0,.22);
  z-index: 101; width: 440px; max-width: 90vw;
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px; border-bottom: 1px solid var(--border);
}
.modal-header h3 { font-size: 15px; font-weight: 600; }
.modal-close {
  background: none; border: none; color: var(--text-muted);
  cursor: pointer; font-size: 15px;
  padding: 4px 6px; border-radius: 5px;
  transition: background .1s;
}
.modal-close:hover { background: var(--bg); color: var(--text); }

.modal-body  { padding: 20px; display: flex; flex-direction: column; gap: 16px; }
.form-row    { display: flex; gap: 12px; align-items: flex-end; }
.form-arrow  { font-size: 20px; color: var(--text-muted); padding-bottom: 8px; flex-shrink: 0; }
.form-group  { display: flex; flex-direction: column; gap: 6px; flex: 1; }
.form-group label { font-size: 12px; font-weight: 600; color: var(--text-muted); }
.form-group input,
.form-group select {
  padding: 8px 10px; border: 1px solid var(--border);
  border-radius: 6px; font-size: 14px; outline: none;
  background: #fff;
  transition: border-color .15s, box-shadow .15s;
}
.form-group input:focus,
.form-group select:focus {
  border-color: #0969da;
  box-shadow: 0 0 0 3px rgba(9,105,218,.1);
}
.modal-footer {
  display: flex; justify-content: flex-end; gap: 10px;
  padding: 14px 20px; border-top: 1px solid var(--border);
}

/* ── Toast ─────────────────────────────────────────────────── */
#toast {
  position: fixed; bottom: 24px; left: 50%;
  transform: translateX(-50%) translateY(12px);
  background: var(--text); color: #fff;
  padding: 10px 20px; border-radius: 8px;
  font-size: 14px; opacity: 0;
  transition: opacity .2s, transform .2s;
  pointer-events: none; z-index: 200;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.hidden { display: none !important; }
