:root {
  --bg: #f3f4f6;
  --panel: #ffffff;
  --panel-2: #f7f8fa;
  --line: #e3e6ea;
  --line-strong: #d3d7dd;
  --text: #16191d;
  --muted: #6b7280;
  --brand: #2563eb;
  --brand-ink: #ffffff;
  --live: #e11d48;
  --good: #16a34a;
  --shadow: 0 10px 40px rgba(15, 23, 42, .18);
  --radius: 8px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1116;
    --panel: #161a21;
    --panel-2: #1b2029;
    --line: #262c36;
    --line-strong: #323a46;
    --text: #e8ebf0;
    --muted: #8e98a8;
    --brand: #4f8cff;
    --shadow: 0 10px 40px rgba(0, 0, 0, .55);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font-size: 14px; line-height: 1.35; }
button, input, select, textarea { font: inherit; color: inherit; }
img { display: block; }
[hidden] { display: none !important; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
a { color: var(--brand); }
code.linkbox {
  display: block; padding: 8px 10px; border: 1px dashed var(--line-strong); border-radius: var(--radius);
  background: var(--panel-2); font-size: 12px; word-break: break-all; user-select: all; margin: 6px 0;
}
.muted { color: var(--muted); }
.m-h { margin: 0 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }

.app { display: flex; flex-direction: column; height: 100vh; height: 100dvh; }

/* ---------- top bar ---------- */
.topbar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px 16px;
  padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 17px; letter-spacing: -.01em; }
.brand-icon { width: 30px; height: 30px; border-radius: 7px; }
.controls { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-left: auto; min-width: 0; }
.session-pill {
  font-size: 12px; font-weight: 600; color: var(--muted);
  border: 1px solid var(--line-strong); border-radius: 999px; padding: 4px 12px; white-space: nowrap;
}
.session-pill.low { color: var(--live); border-color: var(--live); }
.ghost {
  height: 34px; padding: 0 10px; border: 1px solid var(--line-strong); border-radius: var(--radius);
  background: transparent; cursor: pointer; font-weight: 600; font-size: 12px; text-decoration: none;
  display: inline-flex; align-items: center;
}
.ghost:disabled { opacity: .5; cursor: default; }
.primary {
  height: 34px; padding: 0 14px; border: 0; border-radius: var(--radius); cursor: pointer;
  background: var(--brand); color: var(--brand-ink); font-weight: 600; font-size: 13px;
}
.primary:disabled { opacity: .5; cursor: default; }

/* ---------- tabs ---------- */
.tabs { display: flex; gap: 4px; padding: 8px 16px 0; background: var(--panel); border-bottom: 1px solid var(--line); overflow-x: auto; }
.tabs button {
  border: 1px solid transparent; border-bottom: none; background: transparent; cursor: pointer;
  padding: 8px 16px; border-radius: var(--radius) var(--radius) 0 0; font-weight: 600; color: var(--muted);
}
.tabs button.on { background: var(--bg); color: var(--text); border-color: var(--line); }

/* ---------- main ---------- */
.main { flex: 1; min-height: 0; overflow: auto; padding: 14px 16px 24px; }
.tab { max-width: 1200px; margin: 0 auto; }

.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-bottom: 12px; }
.toolbar .field { min-width: 0; }
.field select, .field input {
  height: 34px; padding: 0 10px; border: 1px solid var(--line-strong); border-radius: var(--radius);
  background: var(--panel-2); min-width: 0;
}
.field select { max-width: 220px; }
.search input { width: 220px; }
.toolbar input[type="search"] {
  height: 34px; padding: 0 10px; border: 1px solid var(--line-strong); border-radius: var(--radius);
  background: var(--panel-2); width: 220px;
}

/* ---------- lineup ---------- */
.lineup-grid { display: grid; grid-template-columns: minmax(280px, 420px) 1fr; gap: 14px; }
@media (max-width: 860px) { .lineup-grid { grid-template-columns: 1fr; } }

.lu-list-wrap { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; overflow: hidden; display: flex; flex-direction: column; min-height: 300px; max-height: 70vh; }
.lu-list { list-style: none; margin: 0; padding: 6px; overflow-y: auto; flex: 1; }
.lu-item {
  display: flex; align-items: center; gap: 10px; padding: 7px 8px; border-radius: var(--radius);
  cursor: grab; border: 1px solid transparent; user-select: none;
}
.lu-item:hover { background: var(--panel-2); }
.lu-item.drag { opacity: .4; }
.lu-item.over { border-color: var(--brand); }
.lu-item.off { opacity: .45; }
.lu-item.sel { background: color-mix(in srgb, var(--brand) 12%, var(--panel)); border-color: var(--brand); }
.lu-item .grip { color: var(--muted); cursor: grab; }
.lu-item .logo { width: 48px; height: 30px; object-fit: contain; flex: none; }
.lu-item .name { flex: 1; min-width: 0; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lu-item .sub { font-size: 11px; color: var(--muted); white-space: nowrap; }
.lu-item .xid { font-size: 11px; color: var(--muted); max-width: 140px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lu-item .del { color: var(--muted); border: 0; background: none; cursor: pointer; font-size: 16px; padding: 0 6px; }
.lu-item .del:hover { color: var(--live); }
.empty { padding: 24px; text-align: center; color: var(--muted); }

.lu-preview { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 12px; max-height: 70vh; overflow-y: auto; }
.now-list-inline { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 10px; }
.card {
  display: flex; flex-direction: column; gap: 8px; padding: 12px; background: var(--panel);
  border: 1px solid var(--line); border-radius: 10px; min-width: 0;
}
.card-head { display: flex; align-items: center; gap: 10px; }
.card-head .logo { width: 56px; height: 32px; object-fit: contain; }
.card-head .name { font-weight: 700; font-size: 13px; }
.cur {
  display: block; width: 100%; text-align: left; border: 0; border-left: 3px solid var(--brand);
  background: var(--panel-2); border-radius: 6px; padding: 8px 10px; cursor: pointer;
}
.cur .tt { display: block; font-weight: 700; font-size: 14px; }
.cur .tm { display: block; font-size: 12px; color: var(--muted); }
.next { border: 0; background: none; padding: 0 2px; text-align: left; cursor: pointer; font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.next b { color: var(--text); font-weight: 600; }

/* ---------- add channels ---------- */
.pick-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 8px; }
.pick {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px; background: var(--panel);
  border: 1px solid var(--line); border-radius: var(--radius); cursor: pointer;
}
.pick:hover { border-color: var(--brand); }
.pick.on { border-color: var(--brand); background: color-mix(in srgb, var(--brand) 8%, var(--panel)); }
.pick .logo { width: 48px; height: 28px; object-fit: contain; flex: none; }
.pick .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.pick .num { color: var(--muted); font-size: 12px; }

/* ---------- match ---------- */
.match-top { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin-bottom: 14px; }
@media (max-width: 860px) { .match-top { grid-template-columns: 1fr; } }
.match-drop, .match-info { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 14px; }
.match-drop input[type="file"] { display: block; margin: 6px 0; }
.or { text-align: center; color: var(--muted); font-size: 12px; margin: 6px 0; }
textarea {
  width: 100%; border: 1px solid var(--line-strong); border-radius: var(--radius); background: var(--panel-2);
  padding: 8px 10px; resize: vertical; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px;
}
.match-list { display: flex; flex-direction: column; gap: 6px; }
.match-row {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px; background: var(--panel);
  border: 1px solid var(--line); border-radius: var(--radius);
}
.match-row .in { flex: 1; min-width: 0; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.match-row select { max-width: 260px; height: 32px; border: 1px solid var(--line-strong); border-radius: var(--radius); background: var(--panel-2); }
.match-row .score { font-size: 11px; color: var(--muted); white-space: nowrap; }
.match-row .score.good { color: var(--good); }
.match-row .score.bad { color: var(--live); }

/* ---------- settings ---------- */
.settings-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 14px; }
.card-block { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 14px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.chip {
  border: 1px solid var(--line-strong); background: var(--panel-2); border-radius: 999px;
  padding: 5px 14px; cursor: pointer; font-size: 13px;
}
.chip.on { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.login-field { display: flex; flex-direction: column; gap: 4px; margin: 10px 0; font-size: 13px; }
.login-field span { color: var(--muted); }
.login-field input, .login-field select {
  height: 36px; padding: 0 10px; border: 1px solid var(--line-strong); border-radius: var(--radius); background: var(--panel-2);
}

/* ---------- login ---------- */
.login-body { display: grid; place-items: center; padding: 20px; }
.login-card {
  width: min(380px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: 14px;
  box-shadow: var(--shadow); padding: 24px; display: flex; flex-direction: column; gap: 4px;
}
.login-brand { font-size: 20px; margin-bottom: 6px; }
.login-sub { color: var(--muted); font-size: 13px; margin: 0 0 12px; }
.login-error { background: color-mix(in srgb, var(--live) 12%, transparent); color: var(--live); border-radius: var(--radius); padding: 8px 10px; font-size: 13px; }
.login-btn { margin-top: 14px; height: 40px; border: 0; border-radius: var(--radius); background: var(--brand); color: var(--brand-ink); font-weight: 700; cursor: pointer; }

/* ---------- context menu ---------- */
.ctx {
  position: fixed; z-index: 60; min-width: 180px; background: var(--panel); border: 1px solid var(--line-strong);
  border-radius: 10px; box-shadow: var(--shadow); padding: 6px; display: flex; flex-direction: column;
}
.ctx button { text-align: left; border: 0; background: none; padding: 8px 10px; border-radius: 6px; cursor: pointer; font-size: 13px; }
.ctx button:hover { background: var(--panel-2); }
.ctx button.danger { color: var(--live); }

/* ---------- add list extras ---------- */
.pick.added { opacity: .55; cursor: default; }
.pick:disabled { cursor: default; }
.tag-added { flex: none; font-size: 11px; font-weight: 700; color: var(--good); }

/* ---------- epg links tab ---------- */
.link-list { display: flex; flex-direction: column; gap: 10px; margin-bottom: 14px; }
.link-card {
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px;
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px 16px;
}
.link-card-head { display: flex; flex-direction: column; gap: 2px; min-width: 180px; }
.link-card-head b { font-size: 15px; }
.link-card-head .muted { font-size: 12px; }
.link-card .linkbox { flex: 1; min-width: 220px; margin: 0; }
.link-actions { display: flex; gap: 6px; }
.ghost.danger { color: var(--live); border-color: color-mix(in srgb, var(--live) 40%, var(--line-strong)); }

.link-form { margin-top: 14px; }
.link-form-grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 8px 14px;
}
.link-channels { margin-top: 10px; border-top: 1px solid var(--line); padding-top: 10px; }
.link-pick { max-height: 300px; overflow-y: auto; }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 28px; transform: translateX(-50%); z-index: 70; max-width: calc(100vw - 32px);
  display: flex; align-items: center; gap: 12px; padding: 10px 14px; border-radius: 10px;
  background: var(--text); color: var(--panel); box-shadow: var(--shadow); font-size: 13px;
}
.toast button { border: 0; background: var(--brand); color: #fff; border-radius: 6px; padding: 4px 10px; cursor: pointer; }

.foot { padding: 4px 16px; font-size: 11px; color: var(--muted); background: var(--panel); border-top: 1px solid var(--line); }

@media (max-width: 720px) {
  .topbar { padding: 8px 12px; }
  .controls { margin-left: 0; width: 100%; }
  .main { padding: 10px 12px 20px; }
  .toolbar .field, .toolbar input[type="search"] { flex: 1 1 140px; width: auto; }
  .pick-list { grid-template-columns: 1fr; }
  .match-row { flex-wrap: wrap; }
  .match-row select { max-width: none; width: 100%; }
  .lu-item .xid { display: none; }
}
