:root {
  --bg: #f4f6f8;
  --panel: #ffffff;
  --border: #e2e6ea;
  --text: #1f2933;
  --muted: #7b8794;
  --primary: #2563eb;
  --primary-d: #1d4ed8;
  --danger: #dc2626;
  --shadow: 0 1px 2px rgba(16,24,40,.06), 0 1px 3px rgba(16,24,40,.08);
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
/* Ensure the `hidden` attribute always wins, even over display:flex elements
   (bulk bar, modal) — otherwise a class rule overrides the UA [hidden] default. */
[hidden] { display: none !important; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; gap: 1.5rem;
  background: var(--panel); border-bottom: 1px solid var(--border);
  padding: 0 1.25rem; height: 56px; position: sticky; top: 0; z-index: 10;
}
.brand { font-weight: 700; font-size: 1.05rem; letter-spacing: .2px; }
.mainnav { display: flex; gap: .25rem; }
.mainnav a {
  padding: .45rem .8rem; border-radius: 6px; color: var(--text); font-weight: 500;
}
.mainnav a:hover { background: #eef2f7; text-decoration: none; }
.mainnav a.active { background: var(--primary); color: #fff; }
.userbox { margin-left: auto; display: flex; align-items: center; gap: .6rem; }
.who { font-weight: 600; }
.inline { display: inline; margin: 0; }
.linkbtn { background: none; border: none; color: var(--muted); cursor: pointer; font-size: .9rem; }
.linkbtn:hover { color: var(--danger); text-decoration: underline; }

/* ---------- layout ---------- */
.content { max-width: 1400px; margin: 0 auto; padding: 1.25rem; }
.page-head { display: flex; align-items: baseline; gap: 1rem; margin: .25rem 0 1rem; }
.page-head h1 { font-size: 1.4rem; margin: 0; }
.card {
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: var(--shadow); padding: 1rem 1.15rem; margin-bottom: 1rem;
}
.card h2 { font-size: 1rem; margin: 0 0 .75rem; }
.muted { color: var(--muted); }
.small { font-size: .82rem; }

/* ---------- flashes (toast popup) ---------- */
.flashes {
  position: fixed; top: 68px; left: 50%; transform: translateX(-50%);
  z-index: 200; display: grid; gap: .5rem; width: min(520px, calc(100vw - 2rem));
}
.flash {
  padding: .7rem 1rem; border-radius: 10px; border: 1px solid; cursor: pointer;
  box-shadow: 0 8px 24px rgba(16,24,40,.18); animation: toast-in .2s ease-out;
}
.flash::before { font-weight: 700; margin-right: .4rem; }
.flash-success { background: #ecfdf3; border-color: #abefc6; color: #067647; }
.flash-success::before { content: "✓"; }
.flash-error { background: #fef3f2; border-color: #fecdca; color: #b42318; }
.flash-error::before { content: "✕"; }
@keyframes toast-in { from { opacity: 0; transform: translateY(-8px); } to { opacity: 1; transform: translateY(0); } }

/* ---------- forms / buttons ---------- */
input, select, button { font: inherit; }
input[type=text], input[type=search], input[type=password], input[type=number],
input[type=date], select {
  padding: .45rem .6rem; border: 1px solid var(--border); border-radius: 7px;
  background: #fff; color: var(--text);
}
input:focus, select:focus { outline: 2px solid #bfdbfe; border-color: var(--primary); }
.filters { display: flex; flex-wrap: wrap; gap: .6rem; align-items: center; }
.filters .grow, .filters .grow[type=search] { flex: 1 1 320px; }
.btn {
  padding: .45rem .85rem; border: 1px solid var(--border); background: #fff;
  border-radius: 7px; cursor: pointer; font-weight: 500; color: var(--text);
}
.btn:hover { background: #f3f5f8; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-d); }
.btn-danger { color: var(--danger); border-color: #f3c1c1; }
.btn-danger:hover { background: #fef2f2; }
.btn-sm { padding: .3rem .55rem; font-size: .85rem; }
.block { width: 100%; }

/* ---------- bulk bar ---------- */
.bulk-bar { display: flex; flex-wrap: wrap; align-items: center; gap: .8rem; padding: .7rem 1rem; }
.bulk-label { font-weight: 600; color: var(--muted); }
.bulk-group { display: inline-flex; gap: .4rem; align-items: center; }

/* ---------- tables ---------- */
.table-scroll { overflow-x: auto; }
table.items { width: 100%; border-collapse: collapse; white-space: nowrap; }
table.items th, table.items td {
  text-align: left; padding: .5rem .6rem; border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
table.items thead th {
  background: #fafbfc; font-size: .8rem;
  text-transform: uppercase; letter-spacing: .03em; color: var(--muted);
  border-bottom: 2px solid var(--border);
}
table.items tbody tr:hover { background: #f8fafc; }
.table-meta { display: flex; justify-content: space-between; color: var(--muted); padding: .4rem .2rem; font-size: .85rem; }
.col-name { max-width: 340px; }
.col-name span { display: inline-block; max-width: 340px; overflow: hidden; text-overflow: ellipsis; vertical-align: bottom; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.dt { color: #52606d; font-variant-numeric: tabular-nums; }
.s3 span { display: inline-block; max-width: 200px; overflow: hidden; text-overflow: ellipsis; vertical-align: bottom; }
.err { max-width: 260px; color: #b42318; }
.err span { display: inline-block; max-width: 260px; overflow: hidden; text-overflow: ellipsis; vertical-align: bottom; }
.cb { width: 34px; text-align: center; }
.empty { text-align: center; color: var(--muted); padding: 2rem !important; }
.sort { color: var(--muted); }
.sort.active { color: var(--text); font-weight: 700; }
.sort .arrow { font-size: .7rem; }

/* ---------- per-row actions (icon + popup) ---------- */
.actions-col { width: 1%; text-align: center; }
td.row-actions { text-align: center; }
.iconbtn {
  border: 1px solid transparent; background: none; cursor: pointer;
  font-size: 1rem; line-height: 1; padding: .3rem .45rem; border-radius: 6px;
  color: var(--muted);
}
.iconbtn:hover { background: #eef2f7; color: var(--primary); border-color: var(--border); }
.reset-sort { font-weight: 600; }
.linkish { border: none; background: none; color: var(--muted); }
.linkish:hover { background: none; color: var(--text); text-decoration: underline; }

/* ---------- modal ---------- */
.modal { position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center; }
.modal[hidden] { display: none; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(16,24,40,.45); }
.modal-card {
  position: relative; z-index: 1; width: 340px; max-width: calc(100vw - 2rem);
  background: var(--panel); border-radius: 12px; box-shadow: 0 10px 30px rgba(16,24,40,.25);
  padding: 1.1rem 1.2rem; display: grid; gap: .7rem;
}
.modal-head { display: flex; align-items: center; justify-content: space-between; font-weight: 700; font-size: 1.05rem; }
.modal-card label { display: grid; gap: .3rem; font-size: .85rem; color: var(--muted); }
.modal-card select, .modal-card input { width: 100%; }
.modal-actions { display: flex; justify-content: flex-end; gap: .5rem; margin-top: .3rem; }
#edit-name, #user-name { margin: 0; word-break: break-word; }
.modal-section { display: grid; gap: .5rem; padding-top: .75rem; border-top: 1px solid var(--border); }
.modal-section:first-of-type { border-top: none; padding-top: 0; }
.modal-section .btn { justify-self: start; }

/* ---------- pagination ---------- */
.pager { display: flex; align-items: center; gap: .4rem; padding: .8rem .2rem; }
.pgbtn { padding: .35rem .7rem; border: 1px solid var(--border); border-radius: 7px; background: #fff; }
.pgbtn:hover { background: #f3f5f8; text-decoration: none; }
.pgbtn.disabled { opacity: .4; pointer-events: none; }
.pgnow { color: var(--muted); padding: 0 .4rem; }

/* ---------- status + role chips ---------- */
.status, .role {
  display: inline-block; padding: .12rem .5rem; border-radius: 999px;
  font-size: .78rem; font-weight: 600; border: 1px solid transparent; white-space: nowrap;
}
.status-todo { background:#eef2f7; color:#475467; }
.status-doing { background:#fff7ed; color:#c2410c; }
.status-crawled_meta_data { background:#eff6ff; color:#1d4ed8; }
.status-crawled_no_magnet { background:#f5f3ff; color:#6d28d9; }
.status-downloading { background:#fefce8; color:#a16207; }
.status-downloaded_course { background:#ecfdf3; color:#067647; }
.status-download_failed { background:#fef3f2; color:#b42318; }
.status-sending_subzero { background:#f0f9ff; color:#0369a1; }
.status-subzero_failure { background:#fef3f2; color:#b42318; }
.status-inserted_to_subzero { background:#dcfce7; color:#15803d; }
.role-user { background:#eef2f7; color:#475467; }
.role-admin { background:#eff6ff; color:#1d4ed8; }
.role-superadmin { background:#fef2f2; color:#b42318; }

/* ---------- stat cards ---------- */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 1rem; margin-bottom: 1rem; }
.stat-card { background: var(--panel); border: 1px solid var(--border); border-radius: 10px; box-shadow: var(--shadow); padding: 1rem 1.15rem; }
.stat-num { font-size: 1.8rem; font-weight: 700; }
.stat-label { color: var(--muted); font-size: .85rem; }
.section-title { font-size: .95rem; margin: 0 0 .6rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.range-summary { display: flex; flex-wrap: wrap; gap: 1.1rem; margin: .5rem 0 1rem; font-size: .9rem; }
.range-summary strong { font-variant-numeric: tabular-nums; }
.status-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: .6rem; }
.status-pill { display: flex; align-items: center; justify-content: space-between; gap: .5rem; padding: .45rem .7rem; border: 1px solid var(--border); border-radius: 8px; }

/* ---------- vertical bar chart ---------- */
.chart-legend { display: flex; gap: 1.2rem; margin: .75rem 0 1rem; }
.legend { font-size: .82rem; color: var(--text); display: inline-flex; align-items: center; gap: .4rem; }
.legend::before { content: ""; width: 12px; height: 12px; border-radius: 3px; display: inline-block; }
.legend-1::before, .vbar.bar-1 { background: #3b82f6; }
.legend-2::before, .vbar.bar-2 { background: #22c55e; }
.legend-3::before, .vbar.bar-3 { background: #f59e0b; }

.vchart { overflow-x: auto; padding: 1.4rem .25rem .25rem; }
.vplot {
  display: flex; align-items: flex-end; gap: 1.4rem;
  min-height: 260px; padding-bottom: .25rem;
}
.vgroup { display: flex; flex-direction: column; align-items: center; justify-content: flex-end; gap: .5rem; }
.vbars { display: flex; align-items: flex-end; gap: 5px; height: 220px; }
.vbar {
  width: 20px; border-radius: 4px 4px 0 0; min-height: 2px;
  position: relative; transition: height .2s;
}
.vbar .vval {
  position: absolute; top: -1.15rem; left: 50%; transform: translateX(-50%);
  font-size: .7rem; color: var(--muted); font-variant-numeric: tabular-nums;
}
.vlabel { font-size: .72rem; color: var(--muted); white-space: nowrap; font-variant-numeric: tabular-nums; }
.chart-empty { color: var(--muted); padding: 2rem; text-align: center; }

/* ---------- login ---------- */
.login-wrap { min-height: 80vh; display: flex; align-items: center; justify-content: center; }
.login-card { width: 320px; display: grid; gap: .8rem; }
.login-card h1 { margin: 0; font-size: 1.3rem; }
.login-card label { display: grid; gap: .3rem; font-size: .85rem; color: var(--muted); }
.login-card input { width: 100%; }

/* ---------- users ---------- */
.row-actions { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.row-actions input[type=password] { width: 130px; }
.actions-col { width: 1%; }
.error-card { text-align: center; padding: 3rem; }
.error-card h1 { font-size: 1.6rem; }
