:root {
    --bg: #f3f4f6;
    --card: #ffffff;
    --ink: #111827;
    --muted: #6b7280;
    --line: #e5e7eb;
    --primary: #1f2937;
    --primary-ink: #ffffff;
    --danger-bg: #fef2f2;
    --danger-ink: #b91c1c;
}
* { box-sizing: border-box; }
body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
    background: var(--bg);
    color: var(--ink);
}
h1 { font-size: 1.5rem; margin: 0 0 .25rem; }
h2 { font-size: 1.15rem; margin: 0 0 .75rem; }
.muted { color: var(--muted); font-size: .9rem; }
.spacer { flex: 1; }

/* Layout: sidebar + content */
.app { display: flex; min-height: 100vh; }
.sidebar {
    width: 220px; flex-shrink: 0; background: var(--card);
    border-right: 1px solid var(--line); padding: 1.25rem 0;
    display: flex; flex-direction: column;
}
.sidebar .brand { font-weight: 700; font-size: 1.05rem; padding: 0 1.25rem 1rem; }
.sidebar nav { display: flex; flex-direction: column; }
.sidebar nav a {
    display: block; padding: .65rem 1.25rem; color: var(--ink);
    text-decoration: none; font-size: .92rem; border-left: 3px solid transparent;
}
.sidebar nav a:hover { background: #f9fafb; }
.sidebar nav a.active {
    background: #f3f4f6; border-left-color: var(--primary); font-weight: 600;
}
.content { flex: 1; min-width: 0; display: flex; flex-direction: column; }

.topbar {
    display: flex; align-items: center; gap: 1rem;
    padding: .85rem 1.5rem; background: var(--card);
    border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; }
.container { max-width: 1000px; margin: 1.5rem auto; padding: 0 1.25rem; width: 100%; }

/* User menu (top-right dropdown) */
.user-menu { position: relative; }
.user-toggle {
    cursor: pointer; background: none; border: 1px solid transparent;
    padding: .45rem .7rem; border-radius: 8px; font: inherit; color: var(--ink);
}
.user-toggle:hover { background: #f3f4f6; }
.user-toggle .caret { color: var(--muted); font-size: .8em; }
.user-dropdown {
    display: none; position: absolute; right: 0; top: calc(100% + 6px);
    min-width: 180px; background: var(--card); border: 1px solid var(--line);
    border-radius: 10px; box-shadow: 0 6px 20px rgba(0,0,0,.10); overflow: hidden; z-index: 20;
}
.user-menu.open .user-dropdown { display: block; }
.user-dropdown a {
    display: block; padding: .6rem .9rem; color: var(--ink);
    text-decoration: none; font-size: .9rem;
}
.user-dropdown a:hover { background: #f3f4f6; }

@media (max-width: 720px) {
    .app { flex-direction: column; }
    .sidebar { width: auto; border-right: none; border-bottom: 1px solid var(--line); }
    .sidebar nav { flex-direction: row; flex-wrap: wrap; }
}
.card {
    background: var(--card); border: 1px solid var(--line);
    border-radius: 12px; padding: 1.5rem; margin-bottom: 1.25rem;
    box-shadow: 0 1px 2px rgba(0,0,0,.04);
}
.row { display: flex; align-items: center; }
.row.between { justify-content: space-between; gap: 1rem; flex-wrap: wrap; }
.row.gap { gap: .5rem; }

/* Auth */
.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: 1rem; }
.auth-card { width: 100%; max-width: 360px; }

/* Forms */
label { display: block; margin: .85rem 0; font-size: .9rem; font-weight: 600; }
label.checkbox { display: flex; align-items: center; gap: .5rem; font-weight: 400; }
input[type=email], input[type=password], input[type=number], input[type=file], select {
    display: block; width: 100%; margin-top: .35rem;
    padding: .6rem .7rem; font-size: .95rem;
    border: 1px solid var(--line); border-radius: 8px; background: #fff;
}
label.checkbox input { width: auto; margin: 0; }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 1rem; }
/* A checkbox cell that lines up with the labelled inputs beside it: an invisible
   label line reserves the same top space, then the checkbox is centred over the
   same height as an input box. */
.field-checkbox .field-spacer { display: block; height: 1.15rem; margin: .85rem 0 .35rem; }
.field-checkbox label.checkbox { margin: 0; min-height: 2.5rem; }

/* Buttons */
.btn {
    display: inline-block; cursor: pointer; text-decoration: none;
    padding: .6rem 1rem; border-radius: 8px; font-size: .9rem; font-weight: 600;
    border: 1px solid var(--line); background: #fff; color: var(--ink);
}
.btn.primary { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.danger { background: var(--danger-ink); color: #fff; border-color: var(--danger-ink); }
.btn:hover { opacity: .92; }
.linklike { background: none; border: none; color: #2563eb; cursor: pointer; padding: 0; font: inherit; }

/* Alerts */
.alert { padding: .6rem .8rem; border-radius: 8px; font-size: .9rem; margin: .5rem 0; }
.alert.error { background: var(--danger-bg); color: var(--danger-ink); }
.alert.warn { background: #fffbeb; color: #92400e; border: 1px solid #fde68a; }
.alert.success { background: #ecfdf5; color: #065f46; border: 1px solid #a7f3d0; }

/* Loading spinner */
.loading { display: flex; align-items: center; gap: .6rem; color: var(--muted); padding: 1.25rem 0; font-size: .92rem; }
.spinner {
    width: 18px; height: 18px; border: 2px solid var(--line);
    border-top-color: var(--primary); border-radius: 50%;
    display: inline-block; animation: spin .7s linear infinite;
}
.spinner-sm { width: 14px; height: 14px; vertical-align: -2px; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Submit button loading state */
.btn.is-loading { pointer-events: none; opacity: .9; }
.btn.primary .spinner, .btn.danger .spinner {
    border-color: rgba(255,255,255,.45); border-top-color: #fff;
}

/* Rank cleanup */
fieldset.combos { border: 1px solid var(--line); border-radius: 10px; padding: 1rem; margin: 1rem 0; }
fieldset.combos legend { font-weight: 700; font-size: .9rem; padding: 0 .4rem; }
.combo-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: .4rem .8rem; }
tr.row-skip td { color: var(--muted); background: #fafafa; }
tr.row-error td { background: var(--danger-bg); color: var(--danger-ink); }
code { background: #f3f4f6; padding: .1rem .35rem; border-radius: 4px; font-size: .85em; }

/* Stats */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 1rem; margin-top: 1rem; }
.stat { background: #f9fafb; border: 1px solid var(--line); border-radius: 10px; padding: 1rem; text-align: center; }
.stat .num { display: block; font-size: 1.6rem; font-weight: 700; }
.stat .lbl { display: block; color: var(--muted); font-size: .8rem; margin-top: .2rem; }

/* Tables */
table.data { width: 100%; border-collapse: collapse; font-size: .9rem; }
table.data th, table.data td { text-align: left; padding: .5rem .6rem; border-bottom: 1px solid var(--line); }
table.data thead th { background: #f9fafb; font-weight: 700; }
table.data tr:nth-child(even) td { background: #fafafa; }
.w-idx { width: 48px; color: var(--muted); text-align: right; }
.w-num { width: 70px; text-align: right; }
