:root {
  color-scheme: light;
  --bg: #f4f2ee;
  --surface: #fcfcfb;
  --surface-2: #f0eee9;
  --line: #e2dfd8;
  --text: #17150f;
  --text-2: #55524b;
  --text-3: #85827a;
  --brand-red: #c62f2b;
  --brand-red-ink: #a3241f;
  --brand-green: #2f8a3c;
  --good: #1f7a35;
  --bad: #b3261e;
  --warn: #a86400;
  /* chart series (reference palette, light) */
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100;
  --s5: #e87ba4; --s6: #008300; --s7: #6250d6; --s8: #e34948;
  --pos2: #184f95; --pos1: #6da7ec; --neg1: #f2a27e; --neg2: #b33f12;
  --grid: #e9e6df;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(20, 18, 10, .05), 0 4px 16px rgba(20, 18, 10, .04);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211; --surface: #1a1a19; --surface-2: #232322; --line: #2f2f2d;
    --text: #f0efec; --text-2: #c3c2b7; --text-3: #8f8e86;
    --brand-red: #e5534f; --brand-red-ink: #f07470; --brand-green: #4cae59;
    --good: #5cc471; --bad: #f07470; --warn: #e3a33a;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
    --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
    --pos2: #6da7ec; --pos1: #256abf; --neg1: #a6461f; --neg2: #f2a27e;
    --grid: #2a2a28;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211; --surface: #1a1a19; --surface-2: #232322; --line: #2f2f2d;
  --text: #f0efec; --text-2: #c3c2b7; --text-3: #8f8e86;
  --brand-red: #e5534f; --brand-red-ink: #f07470; --brand-green: #4cae59;
  --good: #5cc471; --bad: #f07470; --warn: #e3a33a;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
  --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
  --pos2: #6da7ec; --pos1: #256abf; --neg1: #a6461f; --neg2: #f2a27e;
  --grid: #2a2a28;
  --shadow: none;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 "Barlow", system-ui, sans-serif;
}
h1, h2, h3 { font-family: "Barlow Condensed", "Barlow", sans-serif; font-weight: 600; margin: 0; letter-spacing: .01em; }
h2 { font-size: 1.2rem; }
h3 { font-size: 1rem; color: var(--text-2); margin-bottom: 6px; }
button, select, input { font: inherit; color: inherit; }
b { font-weight: 600; }

/* ---------- header ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 24px;
  padding: 10px 24px;
  background: var(--surface);
  border-bottom: 3px solid var(--brand-red);
  box-shadow: var(--shadow);
}
.brand { display: flex; align-items: center; gap: 14px; flex: none; }
.logo-badge {
  display: grid; place-items: center;
  background: #fff; border-radius: 12px; padding: 4px 6px;
}
.logo-badge img { display: block; height: 54px; width: auto; }
.brand-text h1 { font-size: 1.55rem; line-height: 1; color: var(--brand-red-ink); text-transform: uppercase; }
.brand-text p { margin: 2px 0 0; color: var(--text-2); font-size: .85rem; }
.tabs { display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none; margin-left: auto; }
.tabs::-webkit-scrollbar { display: none; }
.tabs button {
  border: 0; background: transparent; cursor: pointer;
  padding: 8px 14px; border-radius: 999px;
  font-family: "Barlow Condensed", sans-serif; font-size: 1.05rem; font-weight: 600;
  color: var(--text-2); white-space: nowrap;
}
.tabs button:hover { background: var(--surface-2); color: var(--text); }
.tabs button.active { background: var(--brand-red); color: #fff; }

/* ---------- filters ---------- */
.filters {
  padding: 12px 24px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--line);
}
.filters-body { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 12px 20px; }
.filters-toggle { display: none; }
.tabs svg, .tabs .t-short { display: none; }
.f { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.f > span { font-size: .72rem; text-transform: uppercase; letter-spacing: .06em; color: var(--text-3); font-weight: 600; }
select, input[type=password], .dropdown summary {
  height: 36px; padding: 0 10px;
  border: 1px solid var(--line); border-radius: 9px;
  background: var(--surface); min-width: 140px;
}
select:focus-visible, input:focus-visible, button:focus-visible, summary:focus-visible { outline: 2px solid var(--s1); outline-offset: 1px; }
.dropdown { position: relative; }
.dropdown summary {
  list-style: none; cursor: pointer; display: flex; align-items: center;
  min-width: 220px; max-width: 320px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.dropdown summary::-webkit-details-marker { display: none; }
.dropdown summary::after { content: "▾"; margin-left: auto; padding-left: 8px; color: var(--text-3); }
.dropdown-panel {
  position: absolute; z-index: 30; top: 40px; left: 0;
  width: min(340px, calc(100vw - 32px));
  max-height: 360px; overflow: auto;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
  padding: 8px; box-shadow: 0 10px 30px rgba(0,0,0,.15);
}
.dropdown-actions { display: flex; gap: 6px; padding-bottom: 6px; border-bottom: 1px solid var(--line); margin-bottom: 6px; }
.dropdown-actions button { border: 1px solid var(--line); background: var(--surface-2); border-radius: 7px; padding: 2px 10px; cursor: pointer; font-size: .85rem; }
.dropdown label { display: flex; gap: 8px; align-items: center; padding: 5px 6px; border-radius: 7px; cursor: pointer; }
.dropdown label:hover { background: var(--surface-2); }
.dropdown label small { color: var(--text-3); margin-left: auto; }
.chips { display: flex; gap: 4px; }
.chips button, .pick-chips button {
  height: 36px; min-width: 36px; padding: 0 10px;
  border: 1px solid var(--line); border-radius: 9px; background: var(--surface); cursor: pointer;
}
.chips button.on, .pick-chips button.on { background: var(--text); color: var(--surface); border-color: var(--text); }
.chips button:disabled { opacity: .35; cursor: default; }
.btn, .btn-ghost {
  height: 36px; padding: 0 16px; border-radius: 9px; cursor: pointer; font-weight: 600;
}
.btn { border: 0; background: var(--brand-red); color: #fff; }
.btn:hover { filter: brightness(1.07); }
.btn-ghost { border: 1px solid var(--line); background: var(--surface); }
.btn-ghost:hover { background: var(--surface-2); }

/* ---------- layout ---------- */
main { padding: 20px 24px 40px; max-width: 1400px; margin: 0 auto; }
.view { display: none; }
.view.active { display: block; }
.loading { padding: 60px; text-align: center; color: var(--text-2); }
.grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.grid.inner { gap: 20px; margin-top: 8px; }
.span-2 { grid-column: span 2; }
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px 18px; box-shadow: var(--shadow); min-width: 0;
}
.card > p { color: var(--text-2); margin: 8px 0 12px; }
.card-h { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin-bottom: 12px; }
.card-h h2 { margin-right: auto; }
.chart { position: relative; height: 290px; }
.chart.tall { height: 340px; }
.note { font-size: .82rem; color: var(--text-3); margin: 8px 0 0; }
.row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }

.seg { display: inline-flex; background: var(--surface-2); border-radius: 9px; padding: 3px; gap: 2px; }
.seg button { border: 0; background: transparent; padding: 4px 12px; border-radius: 7px; cursor: pointer; color: var(--text-2); font-size: .9rem; }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.12); font-weight: 600; }
.seg.big { margin-bottom: 16px; flex-wrap: wrap; }
.seg.big button { font-family: "Barlow Condensed", sans-serif; font-size: 1.1rem; padding: 6px 18px; }

/* ---------- KPI tiles ---------- */
.kpis { display: grid; grid-template-columns: repeat(auto-fill, minmax(135px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpis.small { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); margin-bottom: 4px; }
.kpi {
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px;
  box-shadow: var(--shadow);
}
.kpis.small .kpi { background: var(--surface-2); box-shadow: none; border: 0; }
.kpi .k-label { font-size: .75rem; text-transform: uppercase; letter-spacing: .06em; color: var(--text-3); font-weight: 600; display: flex; align-items: center; gap: 6px; }
.kpi .k-label i { width: 8px; height: 8px; border-radius: 2px; display: inline-block; }
.kpi .k-value { font-family: "Barlow Condensed", sans-serif; font-size: 2.1rem; font-weight: 600; line-height: 1.1; font-variant-numeric: tabular-nums; }
.kpi .k-sub { font-size: .82rem; color: var(--text-2); }
.kpi .k-bar { height: 4px; background: var(--surface-2); border-radius: 2px; margin-top: 8px; overflow: hidden; }
.kpis.small .kpi .k-bar { background: var(--line); }
.kpi .k-bar span { display: block; height: 100%; border-radius: 2px; }
.up { color: var(--good); }
.down { color: var(--bad); }

/* ---------- matches ---------- */
.match-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 12px; margin-bottom: 18px; }
.match-card {
  text-align: left; cursor: pointer;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px;
  border-left: 5px solid var(--line);
}
.match-card.win { border-left-color: var(--brand-green); }
.match-card.loss { border-left-color: var(--brand-red); }
.match-card.sel { outline: 2px solid var(--text); }
.match-card .mc-type { font-size: .72rem; text-transform: uppercase; letter-spacing: .06em; color: var(--text-3); font-weight: 600; }
.match-card .mc-opp { font-family: "Barlow Condensed", sans-serif; font-size: 1.3rem; font-weight: 600; line-height: 1.2; }
.match-card .mc-res { display: flex; align-items: baseline; gap: 8px; margin-top: 4px; }
.match-card .mc-res b { font-family: "Barlow Condensed", sans-serif; font-size: 1.5rem; }
.match-card .mc-sets { font-size: .82rem; color: var(--text-2); font-variant-numeric: tabular-nums; }
.badge { font-size: .72rem; font-weight: 600; padding: 1px 8px; border-radius: 99px; text-transform: uppercase; letter-spacing: .04em; }
.badge.win { background: color-mix(in srgb, var(--good) 15%, transparent); color: var(--good); }
.badge.loss { background: color-mix(in srgb, var(--bad) 15%, transparent); color: var(--bad); }
.badge.draw { background: var(--surface-2); color: var(--text-2); }
.detail-head { display: flex; flex-wrap: wrap; align-items: center; gap: 12px 24px; margin-bottom: 16px; }
.detail-head h2 { font-size: 1.8rem; }
.set-pills { display: flex; flex-wrap: wrap; gap: 6px; }
.set-pill { border-radius: 9px; padding: 4px 10px; background: var(--surface-2); font-variant-numeric: tabular-nums; font-size: .9rem; }
.set-pill.win { box-shadow: inset 3px 0 0 var(--brand-green); }
.set-pill.loss { box-shadow: inset 3px 0 0 var(--brand-red); }
.set-pill small { color: var(--text-3); margin-right: 4px; }
.roster { display: flex; flex-wrap: wrap; gap: 6px; }
.roster span { background: var(--surface-2); border-radius: 99px; padding: 2px 10px; font-size: .88rem; }
.roster span em { font-style: normal; color: var(--text-3); font-size: .75rem; margin-left: 4px; }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: .9rem; font-variant-numeric: tabular-nums; }
th, td { padding: 7px 10px; text-align: right; white-space: nowrap; border-bottom: 1px solid var(--line); }
th:first-child, td:first-child { text-align: left; }
thead th { font-size: .72rem; text-transform: uppercase; letter-spacing: .05em; color: var(--text-3); font-weight: 600; position: sticky; top: 0; background: var(--surface); }
thead th.sortable { cursor: pointer; user-select: none; }
thead th.sortable:hover { color: var(--text); }
tbody tr:hover { background: var(--surface-2); }
tr.group td { background: var(--surface-2); font-weight: 600; font-family: "Barlow Condensed", sans-serif; font-size: 1rem; text-align: left; }
tr.total td { font-weight: 600; border-top: 2px solid var(--text-3); }
td.muted { color: var(--text-3); }
td .cell-bar { display: inline-block; height: 6px; border-radius: 3px; background: var(--s1); vertical-align: middle; margin-left: 6px; }
.player-link { background: none; border: 0; padding: 0; cursor: pointer; font-weight: 600; text-decoration: underline; text-decoration-color: var(--line); text-underline-offset: 3px; }
.player-link:hover { text-decoration-color: currentColor; }

.pick-chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.pick-chips button { height: 30px; font-size: .88rem; display: inline-flex; align-items: center; gap: 6px; }
.pick-chips button i { width: 10px; height: 10px; border-radius: 3px; display: none; }
.pick-chips button.on i { display: inline-block; }
.pick-chips button.on { background: var(--surface); color: var(--text); border-color: var(--text); }

.cmp-pick { display: flex; flex-wrap: wrap; gap: 16px; margin-bottom: 16px; }
.cmp-pick select { min-width: 240px; }
.diff-pos { color: var(--good); }
.diff-neg { color: var(--bad); }

/* ---------- data tab ---------- */
.upload { display: flex; flex-direction: column; gap: 10px; }
.file { display: flex; align-items: center; gap: 10px; border: 1.5px dashed var(--line); border-radius: 12px; padding: 18px; cursor: pointer; color: var(--text-2); }
.file:hover { border-color: var(--text-3); }
.file input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.file::before { content: "⬆"; font-size: 1.2rem; }
.msg { min-height: 1.4em; margin: 0; font-size: .9rem; }
.msg.ok { color: var(--good); }
.msg.err { color: var(--bad); }
.checks { list-style: none; padding: 0; margin: 0; display: grid; gap: 6px; }
.checks li { padding: 8px 12px; border-radius: 9px; background: var(--surface-2); display: flex; gap: 10px; }
.checks li::before { content: "✓"; color: var(--good); font-weight: 700; }
.checks li.warn::before { content: "!"; color: var(--warn); }
.defs { display: grid; grid-template-columns: max-content 1fr; gap: 6px 18px; margin: 0; }
.defs dt { font-weight: 600; }
.defs dd { margin: 0; color: var(--text-2); }
.src-list { margin: 0 0 6px; padding-left: 18px; color: var(--text-2); }
.empty { padding: 30px; text-align: center; color: var(--text-3); }

.foot { display: flex; align-items: center; justify-content: center; gap: 10px; padding: 20px; color: var(--text-3); font-size: .85rem; }
.foot img { background: #fff; border-radius: 6px; padding: 2px; }

@media (max-width: 1100px) and (min-width: 761px) {
  .topbar { flex-wrap: wrap; gap: 8px; }
  .tabs { margin-left: 0; width: 100%; }
}

/* ---------- smartphone ---------- */
@media (max-width: 760px) {
  body { font-size: 14px; }
  .topbar {
    position: static; gap: 10px;
    padding: calc(8px + env(safe-area-inset-top)) 16px 8px;
    border-bottom-width: 3px;
  }
  .logo-badge { padding: 3px 4px; border-radius: 10px; }
  .logo-badge img { height: 40px; }
  .brand-text h1 { font-size: 1.3rem; }
  .brand-text p { font-size: .8rem; }

  /* navigazione in basso, come un'app */
  .tabs {
    position: fixed; z-index: 40; left: 0; right: 0; bottom: 0;
    margin: 0; gap: 0; overflow: visible;
    display: grid; grid-template-columns: repeat(6, 1fr);
    padding: 4px 4px calc(4px + env(safe-area-inset-bottom));
    background: var(--surface);
    border-top: 1px solid var(--line);
    box-shadow: 0 -4px 16px rgba(0, 0, 0, .08);
  }
  .tabs button {
    display: flex; flex-direction: column; align-items: center; gap: 2px;
    padding: 6px 0 4px; border-radius: 10px; min-height: 52px;
    font-family: "Barlow", sans-serif; font-size: .68rem; font-weight: 600; letter-spacing: .01em;
  }
  .tabs svg {
    display: block; width: 22px; height: 22px;
    fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round;
  }
  .tabs .t-long { display: none; }
  .tabs .t-short { display: block; }
  .tabs button:hover { background: transparent; }
  .tabs button.active { background: transparent; color: var(--brand-red-ink); }
  .tabs button.active svg { stroke-width: 2.3; }

  /* filtri richiudibili e sempre a portata */
  .filters {
    position: sticky; top: 0; z-index: 30;
    padding: 0 16px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, .06);
  }
  .filters-toggle {
    display: flex; align-items: center; gap: 8px; width: 100%;
    height: 46px; padding: 0; border: 0; background: transparent; cursor: pointer; text-align: left;
  }
  .filters-toggle svg { width: 18px; height: 18px; flex: none; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
  .ft-title { font-weight: 600; }
  .ft-summary { flex: 1; min-width: 0; color: var(--text-2); font-size: .85rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .ft-caret { color: var(--text-3); transition: transform .2s; }
  .filters:not(.collapsed) .ft-caret { transform: rotate(180deg); }
  .filters.collapsed .filters-body { display: none; }
  .filters-body {
    display: grid; grid-template-columns: 1fr 1fr; gap: 10px;
    padding: 4px 0 14px;
  }
  .filters-body .f-matches, .filters-body .f:has(#fSets) { grid-column: 1 / -1; }
  .filters-body select, .dropdown summary { width: 100%; min-width: 0; max-width: none; height: 42px; }
  .chips { display: grid; grid-template-columns: repeat(5, 1fr); }
  .chips button { height: 42px; }
  #fReset { height: 42px; align-self: end; }
  .dropdown-panel { position: fixed; left: 16px; right: 16px; top: auto; width: auto; max-height: 55vh; }
  .dropdown label { padding: 10px 8px; }

  main { padding: 14px 12px 20px; }
  .grid, .grid.inner { grid-template-columns: minmax(0, 1fr); gap: 12px; }
  .span-2 { grid-column: auto; }
  .card { padding: 14px 12px; border-radius: 12px; }
  .card-h { gap: 8px; margin-bottom: 10px; }
  .card-h h2 { width: 100%; font-size: 1.15rem; }
  .card-h .seg { flex: 1 1 auto; }
  .card-h .seg button { flex: 1; padding: 7px 6px; white-space: nowrap; font-size: .85rem; }
  .card-h select { width: 100%; height: 42px; }
  .chart { height: 250px; }
  .chart.tall { height: 270px; }

  .kpis, .kpis.small { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; margin-bottom: 12px; }
  .kpi { padding: 10px 12px; }
  .kpi .k-value { font-size: 1.75rem; }
  .kpi .k-sub { font-size: .78rem; line-height: 1.35; }

  .seg.big { display: grid; grid-template-columns: repeat(4, 1fr); width: 100%; }
  .seg.big button { font-size: 1rem; padding: 8px 4px; }

  /* partite: scorrimento orizzontale */
  .match-list {
    display: flex; overflow-x: auto; scroll-snap-type: x mandatory;
    gap: 10px; margin: 0 -12px 14px; padding: 2px 12px 6px;
    scrollbar-width: none;
  }
  .match-list::-webkit-scrollbar { display: none; }
  .match-card { flex: 0 0 72%; scroll-snap-align: start; }
  .detail-head { gap: 8px 12px; }
  .detail-head h2 { font-size: 1.5rem; }

  /* tabelle: prima colonna fissa */
  .table-wrap { margin: 0 -12px; padding: 0 12px; }
  th, td { padding: 8px 8px; }
  tbody td:first-child, thead th:first-child { position: sticky; left: 0; z-index: 1; background: var(--surface); }
  thead th:first-child { z-index: 2; }
  tr.group td:first-child { background: var(--surface-2); }
  tbody tr:hover td:first-child { background: var(--surface-2); }

  .cmp-pick { display: grid; grid-template-columns: 1fr; gap: 10px; }
  .cmp-pick select { min-width: 0; width: 100%; height: 42px; }
  .pick-chips { flex-wrap: nowrap; overflow-x: auto; margin: 0 -12px 12px; padding: 0 12px 4px; scrollbar-width: none; }
  .pick-chips::-webkit-scrollbar { display: none; }
  .pick-chips button { flex: none; height: 36px; }

  .upload .row .btn, .upload .row .btn-ghost { flex: 1; height: 44px; }
  input[type=password] { height: 44px; }
  .defs { grid-template-columns: 1fr; gap: 2px; }
  .defs dd { margin-bottom: 8px; }
  .foot { padding-bottom: calc(90px + env(safe-area-inset-bottom)); }
}
