:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --border: #dde1e7;
  --text: #1b1f24;
  --muted: #677180;
  --accent: #5b4fe0;
  --accent-text: #ffffff;
  --ok: #1f9d55;
  --warn: #c77c00;
  --danger: #d0353f;
  --radius: 10px;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111318;
    --surface: #1a1d24;
    --surface-2: #232733;
    --border: #2e3340;
    --text: #e8eaef;
    --muted: #8f98a8;
    --accent: #7d73ff;
    --ok: #3ccf7c;
    --warn: #f0a52a;
    --danger: #ff5c66;
    --shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
}
a { color: var(--accent); }
h1, h2, h3 { margin: 0; line-height: 1.2; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.mono { font-family: ui-monospace, Consolas, monospace; font-size: 12px; word-break: break-all; }

/* ---- Barre supérieure ---- */
.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.topbar h1 { font-size: 18px; letter-spacing: 0.3px; }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 8px; }
.pill {
  font-size: 12px; padding: 3px 10px; border-radius: 99px; background: var(--surface-2); color: var(--muted);
}
.lang-select { width: auto; padding: 5px 8px; font-size: 13px; background: transparent; }
.count {
  display: inline-block; min-width: 18px; padding: 0 5px; margin-left: 4px; border-radius: 99px;
  background: var(--accent); color: var(--accent-text); font-size: 11px; text-align: center;
}

/* ---- Boutons ---- */
.btn {
  display: inline-flex; align-items: center; gap: 6px; cursor: pointer;
  padding: 7px 14px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font: inherit; text-decoration: none; white-space: nowrap;
}
.btn:hover { border-color: var(--accent); }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); }
.btn.danger:hover { border-color: var(--danger); }
.btn.small { padding: 4px 10px; font-size: 12px; }
.icon-btn { background: none; border: 0; color: var(--text); font-size: 18px; cursor: pointer; padding: 4px 8px; }
.only-mobile { display: none; }

/* ---- Mise en page ---- */
.layout { flex: 1; display: flex; min-height: 0; }
.sidebar {
  width: 260px; flex-shrink: 0; display: flex; flex-direction: column;
  background: var(--surface); border-right: 1px solid var(--border);
}
.sidebar-head, .sidebar-foot { padding: 12px 14px; display: flex; align-items: center; justify-content: space-between; }
.sidebar-head { font-weight: 600; }
.sidebar-foot { border-top: 1px solid var(--border); }
.system-list { flex: 1; overflow-y: auto; padding: 0 8px; }
.system-item {
  display: flex; align-items: center; gap: 8px; width: 100%; padding: 8px 10px; margin-bottom: 2px;
  border: 0; border-radius: 8px; background: none; color: inherit; font: inherit; text-align: left; cursor: pointer;
}
.system-item:hover { background: var(--surface-2); }
.system-item.active { background: var(--accent); color: var(--accent-text); }
.system-item .n { margin-left: auto; font-size: 12px; opacity: 0.7; }
.main { flex: 1; overflow-y: auto; padding: 20px 24px 40px; min-width: 0; }

.empty { max-width: 460px; margin: 12vh auto; text-align: center; }
.empty p { color: var(--muted); margin: 12px 0 20px; }

.system-head { display: flex; flex-wrap: wrap; gap: 16px; align-items: flex-start; justify-content: space-between; }
.system-head p { margin: 4px 0 0; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; }

.dropzone {
  margin: 18px 0 8px; padding: 18px; border: 2px dashed var(--border); border-radius: var(--radius);
  text-align: center; color: var(--muted); display: flex; flex-direction: column; gap: 6px; align-items: center;
}
.dropzone.over { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 8%, transparent); color: var(--text); }
.inline { font-size: 12px; display: inline-flex; align-items: center; gap: 4px; }
.inline input { width: auto; }
.uploads { display: flex; flex-direction: column; gap: 6px; }
.upload-row { display: flex; gap: 10px; align-items: center; font-size: 12px; }
.upload-row progress { flex: 1; }

.toolbar { display: flex; gap: 10px; align-items: center; margin: 16px 0 12px; }
.toolbar input[type=search] { flex: 1; max-width: 360px; }
input, select, textarea {
  font: inherit; color: inherit; background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 7px 10px; width: 100%;
}
.toolbar select { width: auto; }
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--accent) 50%, transparent); border-color: var(--accent); }

/* ---- Grille de jeux ---- */
.games { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 16px; }
.game {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  overflow: hidden; cursor: pointer; display: flex; flex-direction: column; transition: transform 0.1s, box-shadow 0.1s;
}
.game:hover { transform: translateY(-2px); box-shadow: var(--shadow); }
.cover {
  aspect-ratio: 3 / 4; background: var(--surface-2); display: flex; align-items: center; justify-content: center;
  color: var(--muted); font-size: 12px; padding: 8px; text-align: center; position: relative;
}
.cover img { width: 100%; height: 100%; object-fit: contain; position: absolute; inset: 0; }
.game-info { padding: 8px 10px 10px; display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.game-title { font-weight: 600; font-size: 13px; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.game-sub { font-size: 11px; color: var(--muted); display: flex; justify-content: space-between; gap: 6px; }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; flex-shrink: 0; margin-top: 4px; }
.dot.ok { background: var(--ok); }
.dot.none { background: var(--border); }
.dot.notfound { background: var(--warn); }
.dot.error { background: var(--danger); }

/* ---- Dialogues ---- */
.dialog {
  border: 1px solid var(--border); border-radius: 14px; padding: 0; width: min(520px, calc(100vw - 32px));
  background: var(--surface); color: var(--text); box-shadow: var(--shadow); max-height: calc(100vh - 40px);
}
.dialog.wide { width: min(860px, calc(100vw - 32px)); }
.dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
.dialog-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 18px; border-bottom: 1px solid var(--border); margin: 0; }
.dialog-foot { display: flex; justify-content: flex-end; align-items: center; gap: 10px; padding-top: 12px; flex-wrap: wrap; }
.dialog-foot.split { justify-content: space-between; }
.form { display: flex; flex-direction: column; gap: 10px; padding: 16px 18px 18px; }
.form label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.form label > * { color: var(--text); font-size: 14px; }
.row { display: flex; gap: 10px; }
.row > label { flex: 1; min-width: 0; }
.tabs { display: flex; gap: 4px; padding: 10px 18px 0; }
.tab { border: 0; background: none; color: var(--muted); padding: 8px 12px; border-bottom: 2px solid transparent; cursor: pointer; font: inherit; }
.tab.active { color: var(--text); border-bottom-color: var(--accent); }
.tab-panel { padding: 12px 18px 18px; display: flex; flex-direction: column; gap: 10px; }
.tab-panel p { margin: 0; }
.platform-list { max-height: 50vh; overflow-y: auto; border: 1px solid var(--border); border-radius: 8px; }
.platform-row { display: flex; align-items: center; gap: 10px; padding: 7px 12px; border-bottom: 1px solid var(--border); cursor: pointer; }
.platform-row:last-child { border-bottom: 0; }
.platform-row input { width: auto; }
.platform-row .tag { margin-left: auto; font-size: 11px; color: var(--muted); }
.sys-image { display: flex; gap: 14px; align-items: center; }
.sys-image-preview {
  width: 160px; aspect-ratio: 16 / 9; flex-shrink: 0; border-radius: 8px; background: var(--surface-2);
  display: flex; align-items: center; justify-content: center; overflow: hidden; font-size: 12px;
}
.sys-image-preview img { width: 100%; height: 100%; object-fit: contain; }
.sys-image-actions { display: flex; flex-direction: column; gap: 8px; font-size: 12px; }
.system-item .thumb { width: 28px; height: 20px; object-fit: contain; flex-shrink: 0; }
.system-head { align-items: center; }
.system-head .head-image { height: 56px; max-width: 120px; object-fit: contain; margin-right: 14px; }
.system-title { display: flex; align-items: center; }
.players li { display: flex; gap: 8px; align-items: flex-start; }
.players li > div { flex: 1; min-width: 0; }
.players .remove { flex-shrink: 0; }
.copy-players { display: flex; flex-direction: column; gap: 6px; margin-top: 10px; font-size: 12px; }
.copy-players .row { align-items: center; }
.players { margin: 8px 0 0; padding-left: 18px; font-size: 12px; }
.players li { margin-bottom: 4px; }

.game-edit { display: flex; gap: 8px; }
.media-col { display: flex; flex-direction: column; gap: 12px; padding: 16px 0 16px 18px; width: 200px; flex-shrink: 0; }
.media-slot { display: flex; flex-direction: column; gap: 6px; font-size: 12px; color: var(--muted); }
.media-slot img { width: 100%; min-height: 80px; max-height: 240px; object-fit: contain; background: var(--surface-2); border-radius: 8px; }
.media-slot img:not([src]), .media-slot img[src=""] { visibility: hidden; }
.game-edit .form { flex: 1; min-width: 0; }

.dup-body { padding: 12px 18px; max-height: 65vh; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; }
.dup-body h4 { margin: 8px 0 0; font-size: 14px; }
.dup-group { border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.dup-group-head { padding: 8px 12px; background: var(--surface-2); font-weight: 600; display: flex; justify-content: space-between; gap: 8px; }
.dup-row { display: flex; align-items: center; gap: 10px; padding: 7px 12px; border-top: 1px solid var(--border); font-size: 13px; cursor: pointer; }
.dup-row input { width: auto; }
.dup-row .name { flex: 1; min-width: 0; word-break: break-all; }
.dup-row .keep { color: var(--ok); font-size: 11px; font-weight: 600; white-space: nowrap; }
.dup-row.marked .name { text-decoration: line-through; color: var(--muted); }
.bios-row { cursor: default; }
.bios-row code { font-size: 12px; }
.bios-state { font-weight: 700; width: 14px; text-align: center; }
.bios-state.ok { color: var(--ok); }
.bios-state.missing { color: var(--danger); }
.tag { font-size: 11px; font-weight: 600; padding: 2px 7px; border-radius: 10px; border: 1px solid var(--border); color: var(--muted); white-space: nowrap; }
.tag.req { color: var(--warn); border-color: var(--warn); }
.tag.ok { color: var(--ok); border-color: var(--ok); }
.tag.mismatch { color: var(--danger); border-color: var(--danger); }
.dup-foot { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 12px 18px; border-top: 1px solid var(--border); }
.jobs { padding: 12px 18px 18px; display: flex; flex-direction: column; gap: 10px; max-height: 60vh; overflow-y: auto; }
.job { border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; display: flex; flex-direction: column; gap: 6px; }
.job-head { display: flex; justify-content: space-between; gap: 8px; }
.job progress { width: 100%; }
.job .err { color: var(--danger); font-size: 12px; }

.toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 10; }
.toast { background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: 8px; box-shadow: var(--shadow); max-width: 360px; }
.toast.error { background: var(--danger); color: #fff; }

@media (max-width: 760px) {
  .only-mobile { display: inline-block; }
  .sidebar { position: fixed; top: 49px; bottom: 0; left: 0; z-index: 5; transform: translateX(-100%); transition: transform 0.2s; }
  .sidebar.open { transform: none; box-shadow: var(--shadow); }
  .main { padding: 16px; }
  .games { grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 12px; }
  .game-edit { flex-direction: column; }
  .media-col { flex-direction: row; width: auto; padding: 16px 18px 0; }
  .media-slot { flex: 1; }
  .row { flex-direction: column; }
  #scraperState { display: none; }
}
