:root {
  --bg: #f4f6fa;
  --surface: #ffffff;
  --surface-alt: #f9fafc;
  --border: #e2e6ee;
  --border-strong: #c8cdd9;
  --text: #1a1f2c;
  --text-muted: #5b6478;
  --text-dim: #7c849a;
  --primary: #2563eb;
  --primary-hover: #1d4fd8;
  --primary-soft: #eaf0ff;
  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --good: #16a34a;
  --warn: #d97706;
  --bad: #dc2626;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.05);
  --shadow-md: 0 4px 14px rgba(15, 23, 42, 0.08);
  --radius: 10px;
  --radius-sm: 6px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.4;
}

h1 { font-size: 22px; margin: 0; }
h2 { font-size: 16px; margin: 0 0 4px 0; }
h3 { font-size: 14px; margin: 0 0 6px 0; font-weight: 600; }

label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 4px;
}

input, select, textarea {
  width: 100%;
  padding: 7px 10px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-size: 13px;
  font-family: inherit;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}
input:focus, select:focus, textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}
input[type="date"] { padding: 6px 10px; }

button {
  cursor: pointer;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 7px 14px;
  font-size: 13px;
  font-weight: 600;
  font-family: inherit;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}

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

.btn-secondary {
  background: var(--surface);
  border-color: var(--border-strong);
  color: var(--text);
}
.btn-secondary:hover { background: var(--surface-alt); border-color: var(--text-muted); }

.btn-danger {
  background: var(--danger);
  color: white;
}
.btn-danger:hover { background: var(--danger-hover); }

.btn-small { padding: 4px 10px; font-size: 12px; }

.hidden { display: none !important; }

/* ----------- Auth screen ----------- */
.auth-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.auth-card {
  width: 100%;
  max-width: 360px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  box-shadow: var(--shadow-md);
}
.auth-actions { margin-top: 18px; }
.auth-actions .btn-primary { width: 100%; padding: 9px; }

.sub { color: var(--text-muted); font-size: 13px; }
.tiny { color: var(--text-dim); font-size: 11px; }
.hint { color: var(--text-muted); font-size: 12px; margin: 4px 0 0; }
.inline-note { color: var(--danger); font-size: 12px; margin-top: 10px; min-height: 16px; }
.empty { color: var(--text-dim); font-style: italic; padding: 20px 0; text-align: center; }

/* ----------- App shell ----------- */
.wrap {
  max-width: 1300px;
  margin: 0 auto;
  padding: 16px 20px 40px;
}

.topbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 14px;
}
.header-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}
.pill {
  display: inline-block;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--primary-soft);
  color: var(--primary);
  font-size: 11px;
  font-weight: 600;
}

/* ----------- Tabs ----------- */
.tab-nav {
  display: flex;
  gap: 4px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 14px;
  padding: 0 4px;
}
.tab-btn {
  background: transparent;
  border: none;
  padding: 9px 16px;
  font-weight: 600;
  color: var(--text-muted);
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  border-radius: 0;
}
.tab-btn:hover { color: var(--text); }
.tab-btn.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
}

.tab-panel { display: block; }
.tab-panel.hidden { display: none; }

/* ----------- Cards & grid ----------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow-sm);
}
.card + .card { margin-top: 14px; }
.card-head { margin-bottom: 8px; }
.card-compact { padding: 14px; }
.card-compact h2 { font-size: 15px; }

.grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
  margin-bottom: 14px;
}
/* Skinnier breakdown card on the right */
.grid-uneven { grid-template-columns: 1.7fr 1fr; }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.grid-2 { grid-template-columns: 1fr 1fr; }

@media (max-width: 1000px) {
  .grid, .grid-uneven, .grid-3, .grid-2 { grid-template-columns: 1fr; }
}

/* ----------- Stats row ----------- */
.stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin: 8px 0 12px;
}
.stat {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
}
.stat .k { font-size: 11px; color: var(--text-muted); font-weight: 600; }
.stat .v { font-size: 20px; font-weight: 700; color: var(--text); margin-top: 2px; }

/* ----------- Tables ----------- */
.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
th, td {
  text-align: left;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
thead th {
  background: var(--surface-alt);
  font-size: 11px;
  text-transform: uppercase;
  color: var(--text-muted);
  font-weight: 700;
  letter-spacing: 0.04em;
}
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: rgba(37, 99, 235, 0.04); }

.clickable-row { cursor: pointer; }
.selected-row, .selected-row:hover {
  background: var(--primary-soft) !important;
}

td input, td select { font-size: 12px; padding: 5px 8px; }

/* Compact breakdown table */
.card-compact table { font-size: 12px; }
.card-compact th, .card-compact td { padding: 6px 8px; }

/* Score colors */
.score {
  display: inline-block;
  padding: 2px 8px;
  border-radius: var(--radius-sm);
  font-weight: 700;
}
.score.good { background: #dcfce7; color: #166534; }
.score.warn { background: #fef3c7; color: #92400e; }
.score.bad  { background: #fee2e2; color: #991b1b; }

/* Center the numeric/score columns in the results table (cols 2-6: Rank, Raw Avg, Final, Rows, Latest) */
#resultsTableBody td:nth-child(n+2),
table thead th:nth-child(n+2) {
  text-align: center;
}
/* Keep the appraisee name column left-aligned */
#resultsTableBody td:first-child,
table thead th:first-child {
  text-align: left;
}

/* ----------- Brand / Logo ----------- */
.brand {
  display: flex;
  align-items: center;
  gap: 14px;
}
.brand-logo {
  width: 52px;
  height: 52px;
  object-fit: contain;
  border-radius: 8px;
  background: #fff;
  padding: 2px;
  flex-shrink: 0;
}
.brand-text { line-height: 1.2; }
.brand-company {
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 1.5px;
  color: var(--muted, #64748b);
  text-transform: uppercase;
  margin-bottom: 2px;
}
.brand-text h1 { margin: 0; }
.auth-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 14px;
}
.auth-brand .brand-logo { width: 44px; height: 44px; }
.auth-brand .brand-company { font-size: 14px; }

.ok  { color: var(--good); font-weight: 700; }
.bad { color: var(--bad); font-weight: 700; }

/* ----------- Filter bar ----------- */
.filter-bar {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px;
  margin: 8px 0 12px;
}
.filter-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: flex-end;
}
.filter-cell { min-width: 130px; flex: 1 1 130px; }
.filter-cell label { margin-bottom: 4px; }

/* ----------- Toolbar / details ----------- */
details {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 14px;
  box-shadow: var(--shadow-sm);
}
details > summary {
  cursor: pointer;
  padding: 12px 16px;
  font-weight: 600;
  list-style: none;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
details > summary::-webkit-details-marker { display: none; }
details > summary:hover { background: var(--surface-alt); }
details[open] > summary { border-bottom: 1px solid var(--border); }
details .inner { padding: 14px 16px; }

.toolbar {
  display: grid;
  grid-template-columns: repeat(4, auto) 1fr;
  gap: 8px;
  align-items: center;
}
.toolbar-compact { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

/* ----------- Settings matrix ----------- */
.matrix {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
@media (max-width: 800px) { .matrix { grid-template-columns: 1fr; } }
.matrix-card {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px;
}
.matrix-head {
  display: flex;
  justify-content: space-between;
  font-weight: 700;
  margin-bottom: 8px;
}

.weight-row {
  display: grid;
  grid-template-columns: 1fr 90px;
  gap: 8px;
  align-items: center;
  padding: 4px 0;
  border-top: 1px solid var(--border);
}
.weight-row:first-child { border-top: none; }

.row-2 {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
}
@media (max-width: 800px) { .row-2 { grid-template-columns: 1fr 1fr; } }

.explain-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-top: 14px;
}
@media (max-width: 800px) { .explain-grid { grid-template-columns: 1fr; } }
.explain {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px;
}
.footer-note {
  margin-top: 10px;
  font-size: 11px;
  color: var(--text-muted);
}

/* ----------- Statistics tab ----------- */
.pie-host {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 8px;
}
.pie-host svg { max-width: 100%; height: auto; }
.pie-legend {
  width: 100%;
  margin-top: 10px;
  font-size: 12px;
}
.pie-legend-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 3px 0;
}
.pie-legend-swatch {
  width: 12px;
  height: 12px;
  border-radius: 3px;
  flex-shrink: 0;
}
.pie-legend-label { flex: 1; color: var(--text); }
.pie-legend-value { color: var(--text-muted); font-weight: 600; }

.bar-list { margin-top: 6px; }
.bar-row {
  display: grid;
  grid-template-columns: 140px 1fr 60px;
  gap: 10px;
  align-items: center;
  padding: 6px 0;
}
.bar-track {
  background: var(--surface-alt);
  border-radius: 999px;
  height: 16px;
  overflow: hidden;
  border: 1px solid var(--border);
}
.bar-fill {
  height: 100%;
  background: var(--primary);
  border-radius: 999px;
  transition: width 0.3s;
}
.bar-value { text-align: right; font-weight: 700; }

/* ----------- Evaluators tab ----------- */
.row-form {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}
@media (max-width: 700px) { .row-form { grid-template-columns: 1fr; } }
.form-actions {
  grid-column: 1 / -1;
  display: flex;
  gap: 8px;
  margin-top: 4px;
}

.row-form-tight {
  display: flex;
  gap: 8px;
  align-items: stretch;
  margin-bottom: 12px;
}
.row-form-tight input { flex: 1; }

.role-list {
  list-style: none;
  margin: 0;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
}
.role-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 12px;
  border-top: 1px solid var(--border);
  background: var(--surface);
}
.role-list li:first-child { border-top: none; }
.role-list .role-name {
  flex: 1;
  font-weight: 500;
}
.role-list .role-name-edit {
  flex: 1;
  margin-right: 8px;
  padding: 4px 8px;
  font-size: 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
.role-list .role-actions {
  display: flex;
  gap: 6px;
  align-items: center;
}

/* ----------- Print (full report) ----------- */
@media print {
  body { background: white; }
  .topbar, .tab-nav, .filter-bar,
  details, .header-actions, #statsPrintBtn,
  .card-head .hint, .card-head p { display: none !important; }
  .tab-panel.hidden { display: none !important; }
  .tab-panel { display: block !important; }
  .card { box-shadow: none; border-color: #ddd; break-inside: avoid; }
  .grid-3, .grid-2 { grid-template-columns: 1fr 1fr; }
  .wrap { padding: 8px; }
  table { font-size: 11px; }
}
