  :root {
    --bg: #0d1117;
    --panel: #161b22;
    --panel-2: #1f242c;
    --border: #2a313c;
    --text: #e6edf3;
    --muted: #8b949e;
    --accent: #2ea043;
    --accent-2: #238636;
    --danger: #da3633;
    --warn: #d29922;
    --field-1: #2f7a36;
    --field-2: #2a6c30;
    --highlight: #58a6ff;
  }
  * { box-sizing: border-box; }
  html, body {
    margin: 0; padding: 0;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    height: 100%;
    overflow: hidden;
  }
  header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 16px;
    background: var(--panel);
    border-bottom: 1px solid var(--border);
    height: 56px;
  }
  header .title {
    font-weight: 700;
    font-size: 18px;
    display: flex;
    align-items: center;
    gap: 8px;
  }
  header .title .ball { font-size: 22px; }
  header .controls {
    display: flex;
    gap: 8px;
    align-items: center;
  }
  select, button, input {
    background: var(--panel-2);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 8px 12px;
    font-size: 14px;
    font-family: inherit;
  }
  button { cursor: pointer; }
  button:hover { background: #2a313c; }
  button.primary { background: var(--accent-2); border-color: var(--accent-2); }
  button.primary:hover { background: var(--accent); }
  button.danger { background: transparent; border-color: var(--danger); color: var(--danger); }
  button.danger:hover { background: rgba(218,54,51,0.1); }
  button.ghost { background: transparent; }
  button:disabled { opacity: 0.5; cursor: not-allowed; }
  input { width: 100%; }
  input:focus, select:focus, button:focus { outline: 2px solid var(--highlight); outline-offset: -1px; }

  .layout {
    display: grid;
    grid-template-columns: 320px 1fr 380px;
    gap: 8px;
    padding: 8px;
    height: calc(100vh - 56px);
  }
  .panel {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 10px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
  }
  .panel-header {
    padding: 12px 14px;
    border-bottom: 1px solid var(--border);
    font-weight: 600;
    font-size: 13px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--muted);
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  .panel-body {
    padding: 12px;
    overflow-y: auto;
    flex: 1;
    min-height: 0;
  }

  /* Formation picker (initial screen) */
  .picker {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    padding: 24px;
    gap: 16px;
  }
  .picker h2 {
    margin: 0;
    text-align: center;
    font-size: 24px;
  }
  .picker p {
    margin: 0;
    color: var(--muted);
    text-align: center;
    max-width: 480px;
  }
  .picker-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 14px;
    width: 100%;
    max-width: 720px;
    margin-top: 12px;
  }
  .picker-card {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 16px;
    cursor: pointer;
    transition: all 0.15s ease;
    display: flex;
    flex-direction: column;
    gap: 8px;
    align-items: center;
  }
  .picker-card:hover {
    border-color: var(--accent);
    transform: translateY(-2px);
  }
  .picker-card .name { font-weight: 700; font-size: 16px; }
  .picker-card .mini-field { width: 100%; height: 100px; }

  /* Selected position popover — fixed-positioned so it can never be clipped
     by ancestor overflow. JS computes its top/left in pixels next to the dot. */
  .pos-popover {
    position: fixed;
    width: 360px;
    max-height: 80vh;
    overflow-y: auto;
    background: rgba(22, 27, 34, 0.97);
    border: 1px solid var(--highlight);
    border-radius: 10px;
    padding: 10px;
    box-shadow: 0 8px 28px rgba(0,0,0,0.55);
    z-index: 1000;
    backdrop-filter: blur(4px);
  }
  .pos-popover .pop-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 8px;
    padding-bottom: 8px;
    border-bottom: 1px solid var(--border);
  }
  .pos-popover .pop-role {
    font-weight: 700;
    font-size: 16px;
    color: var(--highlight);
  }
  .pos-popover .pop-count {
    color: var(--muted);
    font-size: 11px;
  }
  .pos-popover .pop-close {
    background: transparent;
    border: none;
    color: var(--muted);
    cursor: pointer;
    font-size: 20px;
    padding: 0 4px;
    line-height: 1;
  }
  .pos-popover .pop-close:hover { color: var(--text); }
  .pos-popover .hint {
    color: var(--muted);
    font-size: 11px;
    margin-top: 8px;
    padding-top: 8px;
    border-top: 1px solid var(--border);
    line-height: 1.4;
  }
  .pos-popover .empty-cands {
    color: var(--muted);
    font-size: 12px;
    text-align: center;
    padding: 12px 0;
  }
  @media (max-width: 900px) {
    .pos-popover {
      width: min(calc(100vw - 24px), 320px);
    }
  }
  .candidate {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px;
    border-radius: 6px;
    margin-top: 4px;
    flex-wrap: wrap;
  }
  .candidate.active { background: rgba(46,160,67,0.15); }
  .candidate .cnum {
    background: var(--border);
    color: var(--muted);
    font-size: 10px;
    font-weight: 700;
    min-width: 18px;
    height: 18px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
  }
  .candidate.active .cnum {
    background: var(--accent-2);
    color: white;
  }
  .candidate .cname { flex: 1 1 100px; min-width: 0; font-size: 13px; }
  .candidate .cname .pname {
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .candidate .cname .cmeta {
    color: var(--muted);
    font-size: 11px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .candidate .badge { background: var(--accent-2); color: white; font-size: 10px; padding: 2px 6px; border-radius: 4px; }
  .candidate-actions { display: flex; gap: 4px; margin-left: auto; flex-wrap: wrap; justify-content: flex-end; }
  .candidate-actions button { padding: 4px 8px; font-size: 11px; }

  /* Search */
  .search-row {
    display: flex; gap: 6px;
    margin-bottom: 10px;
  }
  .search-row input { flex: 1; }
  .search-results { display: flex; flex-direction: column; gap: 6px; }
  .result {
    background: var(--panel-2);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 8px 10px;
    display: flex;
    align-items: center;
    gap: 8px;
  }
  .result .info { flex: 1; min-width: 0; }
  .result .pname { font-weight: 600; font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .result .meta { font-size: 11px; color: var(--muted); }
  .result .mv { font-size: 12px; font-weight: 600; color: var(--accent); white-space: nowrap; }
  .result .add-btn { padding: 4px 10px; font-size: 12px; }
  .result .add-btn:disabled { color: var(--muted); }

  .empty-state {
    color: var(--muted);
    text-align: center;
    padding: 24px 12px;
    font-size: 13px;
  }
  .loader {
    border: 2px solid var(--border);
    border-top: 2px solid var(--accent);
    border-radius: 50%;
    width: 18px;
    height: 18px;
    animation: spin 0.8s linear infinite;
    margin: 0 auto;
  }
  @keyframes spin { to { transform: rotate(360deg); } }

  /* Field */
  .field-wrap {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 12px;
    overflow: visible;
    min-height: 0;
  }
  .field {
    position: relative;
    height: 100%;
    aspect-ratio: 0.66;
    max-height: 100%;
    background: linear-gradient(180deg, var(--field-1) 0%, var(--field-2) 100%);
    border-radius: 8px;
    /* overflow visible so the position popover can extend beyond the field bounds */
    overflow: visible;
    box-shadow: 0 8px 24px rgba(0,0,0,0.4);
  }
  .field svg.lines { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
  .spot {
    position: absolute;
    width: 48px; height: 60px;     /* 4:5 — matches Transfermarkt portrait aspect */
    transform: translate(-50%, -50%);
    cursor: grab;
    user-select: none;
    transition: transform 0.1s ease;
  }
  .spot:active { cursor: grabbing; }
  .spot .dot {
    width: 100%; height: 100%;
    border-radius: 10px;            /* rounded card, not a circle */
    background: white;
    color: #1f242c;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 13px;
    border: 2px solid #fff;
    box-shadow: 0 2px 6px rgba(0,0,0,0.3);
    pointer-events: none;
  }
  .spot .labels {
    position: absolute;
    top: calc(100% + 4px);
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    width: max-content;
    max-width: 110px;
    pointer-events: none;
  }
  .spot.has-player .dot {
    background: #161b22;
    color: white;
    border-color: var(--accent);
  }
  .spot .dot.has-photo {
    background-color: var(--panel-2);
    background-size: cover;
    background-position: center top;
    background-repeat: no-repeat;
    color: transparent;
  }
  .spot .role-badge {
    position: absolute;
    top: -4px; left: -4px;
    background: var(--accent-2);
    color: white;
    font-size: 9px;
    font-weight: 700;
    padding: 1px 5px;
    border-radius: 6px;
    pointer-events: none;
    white-space: nowrap;
    z-index: 1;
    box-shadow: 0 1px 3px rgba(0,0,0,0.3);
  }
  .spot.selected .dot {
    border-color: var(--highlight);
    box-shadow: 0 0 0 4px rgba(88,166,255,0.35);
  }
  .spot.drag-over .dot {
    background: var(--warn);
    color: #000;
  }
  .spot .name {
    font-size: 11px;
    background: rgba(0,0,0,0.65);
    color: white;
    padding: 1px 6px;
    border-radius: 3px;
    max-width: 110px;
    text-align: center;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .spot .alt-names {
    display: flex;
    flex-direction: column;
    gap: 1px;
    align-items: center;
  }
  .spot .alt-name {
    font-size: 9px;
    line-height: 1.2;
    background: rgba(0,0,0,0.45);
    color: rgba(255,255,255,0.75);
    padding: 0 5px;
    border-radius: 2px;
    max-width: 96px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .spot .count-badge {
    position: absolute;
    top: -4px; right: -4px;
    background: var(--warn);
    color: #000;
    font-size: 10px;
    font-weight: 700;
    border-radius: 50%;
    width: 18px; height: 18px;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
  }

  /* Right panel - team table */
  .team-summary {
    display: flex;
    justify-content: space-between;
    padding: 10px 12px;
    background: var(--panel-2);
    border-radius: 8px;
    margin-bottom: 10px;
  }
  .team-summary .stat { text-align: center; }
  .team-summary .stat .label { font-size: 10px; color: var(--muted); text-transform: uppercase; }
  .team-summary .stat .value { font-size: 16px; font-weight: 700; margin-top: 2px; }
  .team-summary .stat .value.green { color: var(--accent); }

  table.team-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 12px;
  }
  table.team-table th {
    text-align: left;
    color: var(--muted);
    font-weight: 600;
    text-transform: uppercase;
    font-size: 10px;
    padding: 6px 8px;
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    background: var(--panel);
  }
  table.team-table td {
    padding: 8px;
    border-bottom: 1px solid var(--border);
    vertical-align: top;
  }
  table.team-table tr:hover td { background: var(--panel-2); }
  table.team-table .pname { font-weight: 600; }
  table.team-table .row-meta { color: var(--muted); font-size: 10px; }
  table.team-table .mv { color: var(--accent); font-weight: 600; white-space: nowrap; }

  .toast {
    position: fixed;
    bottom: 16px; left: 50%;
    transform: translateX(-50%);
    background: var(--panel-2);
    border: 1px solid var(--border);
    padding: 10px 16px;
    border-radius: 8px;
    box-shadow: 0 8px 24px rgba(0,0,0,0.4);
    z-index: 100;
    font-size: 13px;
    max-width: 90%;
  }
  .toast.error { border-color: var(--danger); }

  /* Mini field for picker */
  .mini-field {
    background: linear-gradient(180deg, var(--field-1), var(--field-2));
    border-radius: 4px;
    position: relative;
  }
  .mini-field .mini-dot {
    position: absolute;
    width: 8px; height: 8px;
    background: white;
    border-radius: 50%;
    transform: translate(-50%, -50%);
  }

  /* Scrollbar */
  ::-webkit-scrollbar { width: 8px; height: 8px; }
  ::-webkit-scrollbar-track { background: transparent; }
  ::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
  ::-webkit-scrollbar-thumb:hover { background: #3a414c; }

  @media (max-width: 1100px) {
    .layout { grid-template-columns: 280px 1fr 320px; }
  }
  @media (max-width: 900px) {
    .layout { grid-template-columns: 1fr; grid-template-rows: auto auto auto; height: auto; overflow: auto; }
    body { overflow: auto; }
    .field-wrap { min-height: 500px; }
  }

  /* ---- Header control separator ---- */
  .controls .ctrl-sep {
    width: 1px;
    height: 20px;
    background: var(--border);
    margin: 0 4px;
  }

  /* ---- Avatars (player photos with initial fallback) ----
     4:5 aspect (matches Transfermarkt portrait dimensions) so faces don't
     get cropped at the chin like they do in a square frame. */
  .avatar {
    background-color: var(--panel-2);
    background-size: cover;
    background-position: center top;
    background-repeat: no-repeat;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--muted);
    font-weight: 700;
    border-radius: 6px;
    flex-shrink: 0;
    border: 1px solid var(--border);
    overflow: hidden;
  }
  .avatar.tiny  { width: 28px; height: 35px; font-size: 10px; }
  .avatar.small { width: 36px; height: 45px; font-size: 11px; }
  .avatar.large { width: 72px; height: 90px; font-size: 22px; border-width: 2px; border-radius: 10px; }

  /* ---- Modal (used by comparison view) ---- */
  .modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.7);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 2000;
    padding: 20px;
    overflow: auto;
    backdrop-filter: blur(2px);
  }
  .modal {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 16px;
    max-width: 95vw;
    max-height: 90vh;
    overflow: auto;
    box-shadow: 0 24px 60px rgba(0,0,0,0.5);
  }
  .modal-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 14px;
    padding-bottom: 10px;
    border-bottom: 1px solid var(--border);
    gap: 12px;
  }
  .modal-title {
    font-size: 18px;
    font-weight: 700;
  }
  .modal-sub {
    font-size: 12px;
    color: var(--muted);
    margin-top: 2px;
  }

  /* ---- Comparison grid (FM-style) ---- */
  .compare-modal { width: min(960px, 95vw); }
  .compare-grid {
    display: grid;
    gap: 8px 10px;
    align-items: center;
  }
  .compare-corner {}
  .compare-card {
    background: var(--panel-2);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 10px 8px;
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    gap: 4px;
    min-width: 0;
  }
  .compare-card.starter { border-color: var(--accent); }
  .compare-card .cc-name {
    font-weight: 600;
    font-size: 13px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100%;
  }
  .compare-card .cc-meta {
    font-size: 11px;
    color: var(--muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100%;
  }
  .compare-row-label {
    font-size: 10px;
    color: var(--muted);
    text-transform: uppercase;
    font-weight: 700;
    letter-spacing: 0.5px;
    padding: 6px 0;
    border-top: 1px solid var(--border);
  }
  .compare-cell {
    display: flex;
    flex-direction: column;
    gap: 4px;
    align-items: stretch;
    padding: 4px;
    border-top: 1px solid var(--border);
  }
  .compare-cell.text {
    justify-content: center;
    font-size: 12px;
    text-align: center;
  }
  .bar-wrap {
    background: var(--panel-2);
    height: 8px;
    border-radius: 4px;
    overflow: hidden;
    border: 1px solid var(--border);
  }
  .bar {
    background: var(--border);
    height: 100%;
    border-radius: 4px;
    transition: width 0.3s ease;
  }
  .bar.best { background: var(--accent); }
  .bar-val {
    font-size: 11px;
    text-align: center;
    color: var(--muted);
    font-variant-numeric: tabular-nums;
  }
  .bar-val.best { color: var(--accent); font-weight: 700; }

  /* ---- Login page ---- */
  body.login-page {
    overflow: auto;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    background:
      radial-gradient(1000px 500px at 50% -200px, rgba(46,160,67,0.18), transparent 60%),
      var(--bg);
    padding: 24px;
  }
  .login-card {
    width: 100%;
    max-width: 400px;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 32px 28px;
    box-shadow: 0 20px 60px rgba(0,0,0,0.5);
  }
  .login-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 700;
    font-size: 16px;
    color: var(--muted);
    margin-bottom: 8px;
  }
  .login-brand .ball { font-size: 20px; }
  .login-card h1 {
    margin: 8px 0 4px;
    font-size: 22px;
  }
  .login-sub {
    margin: 0 0 20px;
    color: var(--muted);
    font-size: 13px;
  }
  .login-error {
    background: rgba(218,54,51,0.12);
    border: 1px solid rgba(218,54,51,0.5);
    color: #ff8c8a;
    padding: 10px 12px;
    border-radius: 8px;
    margin-bottom: 14px;
    font-size: 13px;
  }
  .login-card .form-field {
    display: block;
    margin: 0 0 12px 0;
    padding: 0;
    background: transparent !important;
    border: none !important;
    aspect-ratio: auto !important;
    height: auto !important;
    box-shadow: none !important;
  }
  .login-card .form-field > span {
    display: block;
    font-size: 12px;
    color: var(--muted);
    margin-bottom: 4px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    background: transparent;
  }
  .login-card .form-field input {
    width: 100%;
    padding: 10px 12px;
    font-size: 14px;
    background: var(--panel-2) !important;
    border: 1px solid var(--border) !important;
    color: var(--text) !important;
    border-radius: 6px;
    aspect-ratio: auto !important;
    height: auto !important;
  }
  .login-submit {
    width: 100%;
    margin-top: 6px;
    padding: 10px;
    font-size: 14px;
    font-weight: 600;
  }

  /* ---- "How to use" hint card in left panel ---- */
  .how-to {
    background: var(--panel-2);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 14px 16px;
    margin-bottom: 22px;
    font-size: 13px;
  }
  .how-to-title {
    font-weight: 700;
    color: var(--text);
    margin-bottom: 10px;
    font-size: 13px;
    letter-spacing: 0.3px;
  }
  .how-to-steps {
    margin: 0 0 12px 0;
    padding-left: 22px;
    color: var(--text);
  }
  .how-to-steps li {
    margin: 6px 0;
    line-height: 1.6;
  }
  .how-to-tip {
    color: var(--muted);
    font-size: 12px;
    line-height: 1.6;
    padding-top: 10px;
    border-top: 1px dashed var(--border);
    margin-top: 8px;
  }

  /* ---- Logout link in header ---- */
  .controls .logout-link {
    color: var(--muted);
    text-decoration: none;
    font-size: 13px;
    padding: 6px 10px;
    border: 1px solid transparent;
    border-radius: 6px;
  }
  .controls .logout-link:hover {
    color: var(--text);
    border-color: var(--border);
  }
