:root {
  --blue: #2563eb;
  --blue-light: #eff6ff;
  --green: #16a34a;
  --green-light: #f0fdf4;
  --red: #dc2626;
  --red-light: #fef2f2;
  --amber: #d97706;
  --amber-light: #fffbeb;
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface2: #f1f5f9;
  --border: #e2e8f0;
  --text-main: #0f172a;
  --text-mid: #475569;
  --text-soft: #94a3b8;
  --radius: 6px;
  --radius-sm: 4px;
}

/* Стили для принудительной темной темы (по классу) */
html.dark-theme {
  --bg: #0c1220;
  --surface: #161f31;
  --surface2: #1e2a3e;
  --border: #2a3a55;
  --text-main: #f1f5f9;
  --text-mid: #94a3b8;
  --text-soft: #475569;
  --blue-light: #1e3a5f;
  --green-light: #0d2818;
  --red-light: #2c0d0d;
  --amber-light: #2d1a03;
}

/* Системная темная тема (сработает только если в настройках стоит 'system') */
@media (prefers-color-scheme: dark) {
  html.system-theme {
    --bg: #0c1220;
    --surface: #161f31;
    --surface2: #1e2a3e;
    --border: #2a3a55;
    --text-main: #f1f5f9;
    --text-mid: #94a3b8;
    --text-soft: #475569;
    --blue-light: #1e3a5f;
    --green-light: #0d2818;
    --red-light: #2c0d0d;
    --amber-light: #2d1a03;
  }
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body {
  font-family: 'Inter', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text-main);
  min-height: 100dvh;
  line-height: 1.4;
  padding-bottom: 80px;
}
.wrapper { max-width: 600px; margin: 0 auto; padding: 0 12px; }
header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 40;
}
.header-inner {
  display: flex; align-items: center; justify-content: center;
  padding: 12px; max-width: 600px; margin: 0 auto; position: relative;
}
.logo { display: flex; align-items: center; gap: 8px; justify-content: center; text-align: center; }
.logo-icon { font-size: 16px; }
.logo-text { font-size: 1rem; font-weight: 700; color: var(--text-main); }

.section-label {
  font-size: .75rem; font-weight: 700; text-transform: uppercase;
  letter-spacing: .05em; color: var(--text-soft);
  margin: 14px 0 6px 0; text-align: center;
}
#cards-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 10px;
  transition: background-color 0.2s, border-color 0.2s;
}
.card.winner {
  border-color: var(--green);
  background: var(--green-light);
}
.card-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 4px;
}
.card-num {
  font-size: .7rem; font-weight: 700; color: var(--text-soft);
  text-transform: uppercase;
}
.winner .card-num {
  color: var(--green);
}
.btn-remove {
  background: none; border: none; cursor: pointer;
  color: var(--text-soft); font-size: .9rem;
  width: 20px; height: 20px; display: flex; align-items: center; justify-content: center;
}
.btn-remove:hover { color: var(--red); }

.card-grid {
  display: grid;
  grid-template-columns: 1fr 1fr auto;
  gap: 6px;
  align-items: end;
}
.field { display: flex; flex-direction: column; gap: 2px; }

.field label {
  font-size: .65rem; font-weight: 600; color: var(--text-mid);
  text-transform: uppercase;
  padding-left: 10px;
}
.winner .field label { color: var(--green); opacity: 0.9; }

.field input {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 3px 4px;
  font-size: 140%;
  font-family: inherit;
  color: var(--text-main);
  outline: none;
  width: 100%;
  height: 36px;
  transition: background-color 0.2s, border-color 0.2s;
}

.field input[type="number"]::-webkit-outer-spin-button,
.field input[type="number"]::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}
.field input[type="number"] {
  -moz-appearance: textfield;
}

.winner .field input {
  background: rgba(255, 255, 255, 0.7);
  border-color: rgba(22, 163, 74, 0.3);
}
html.dark-theme .winner .field input,
html.system-theme .winner .field input {
  @media (prefers-color-scheme: dark) {
    background: rgba(22, 31, 49, 0.7);
  }
}
html.dark-theme .winner .field input {
  background: rgba(22, 31, 49, 0.7);
}

.field input:focus {
  border-color: var(--blue);
  background: var(--surface);
}
.field input.error { border-color: var(--red); background: var(--red-light); }

.actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  margin-bottom: 12px;
}
.btn {
  display: flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 12px; border-radius: var(--radius-sm);
  border: none; cursor: pointer;
  font-size: .85rem; font-weight: 600; font-family: inherit;
  min-height: 38px;
  transition: background-color 0.2s, color 0.2s, border-color 0.2s;
}
.btn img {
  width: 16px;
  height: 16px;
  vertical-align: middle;
}

.btn-add { 
  background: var(--surface); 
  color: var(--green); 
  border: 1px solid var(--green); 
}
.btn-add:hover {
  background: var(--green-light);
}

.btn-clear { 
  background: var(--surface); 
  color: var(--red); 
  border: 1px solid var(--red); 
}
.btn-clear:hover {
  background: var(--red-light);
}

.btn-calc { background: var(--blue); color: #fff; grid-column: 1 / -1; font-size: .9rem; }
.btn-calc:hover { background: #1d4ed8; }

.error-banner {
  background: var(--red-light); border: 1px solid var(--red);
  border-radius: var(--radius-sm); padding: 8px 12px;
  color: var(--red); font-size: .85rem;
  display: none; margin-bottom: 10px; gap: 6px;
}
.error-banner.visible { display: flex; align-items: center; }

.error-banner.warn {
  background: var(--amber-light);
  border-color: var(--amber);
  color: var(--amber);
}

#results { display: none; margin-top: 12px; }
#results.visible { display: block; }
.results-title { font-size: .9rem; font-weight: 700; margin-bottom: 6px; text-align: center; }

.result-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 8px 10px; margin-bottom: 6px;
}
.result-card.winner { border-color: var(--green); background: var(--green-light); }
.result-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 4px; }
.result-name { font-weight: 700; font-size: .85rem; }
.badge-winner { background: var(--green); color: #fff; font-size: .6rem; font-weight: 700; padding: 1px 5px; border-radius: 100px; text-transform: uppercase; }
.badge-diff { color: #d97706; font-size: .65rem; font-weight: 700; }

.result-stats { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; }
.result-stats.single-stat { grid-template-columns: 1fr; }
.stat { background: var(--surface2); border-radius: var(--radius-sm); padding: 4px 8px; }
.winner .stat { background: rgba(255, 255, 255, 0.7); border: 1px solid rgba(22, 163, 74, 0.2); }
html.dark-theme .winner .stat, html.system-theme .winner .stat {
  @media (prefers-color-scheme: dark) {
    background: rgba(22, 31, 49, 0.7);
  }
}
html.dark-theme .winner .stat {
  background: rgba(22, 31, 49, 0.7);
}
.stat-label { font-size: .6rem; font-weight: 600; color: var(--text-soft); text-transform: uppercase; }
.winner .stat-label { color: var(--green); opacity: 0.8; }
.stat-value { font-size: .9rem; font-weight: 700; }
.winner .stat-value { color: var(--green); }

.result-rank { display: flex; align-items: center; gap: 6px; font-size: .65rem; color: var(--text-soft); margin-top: 4px; }
.rank-bar-wrap { flex: 1; height: 4px; background: var(--border); border-radius: 100px; overflow: hidden; }
.rank-bar { height: 100%; background: var(--blue); }
.winner .rank-bar { background: var(--green); }

/* Общие стили для выпадающих меню темы и языка в подвале */
.controls-panel {
  position: fixed; bottom: 12px; left: 12px; right: 12px;
  display: flex; justify-content: space-between; pointer-events: none; z-index: 100;
}
.ctrl-box { position: relative; pointer-events: auto; }
.ctrl-btn {
  display: flex; align-items: center; justify-content: center; gap: 4px;
  background: var(--surface); color: var(--text-main);
  border: 1px solid var(--border); border-radius: 100px;
  width: 75px; height: 32px; font-size: .75rem; font-weight: 600;
  cursor: pointer; box-shadow: 0 2px 8px rgba(0,0,0,.1);
}

.ctrl-menu {
  position: absolute; bottom: calc(100% + 4px);
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-sm); display: none; min-width: 110px;
  box-shadow: 0 4px 12px rgba(0,0,0,.1);
  z-index: 110;
}
.ctrl-menu.open { display: block; }
.ctrl-menu-left { left: 0; }
.ctrl-menu-right { right: 0; }

.ctrl-item {
  display: block; padding: 8px 10px; font-size: .8rem; cursor: pointer;
  border: none; background: none; color: var(--text-main); width: 100%;
  text-align: left; font-family: inherit;
}
.ctrl-item:hover { background: var(--surface2); }
.ctrl-item.active { color: var(--blue); font-weight: 700; }

.install-bar { display: none; position: fixed; bottom: 60px; left: 12px; right: 12px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 8px 12px; align-items: center; gap: 10px; z-index: 90; box-shadow: 0 4px 16px rgba(0,0,0,.15); }
.install-bar.visible { display: flex; }
.btn-install { background: var(--blue); color: #fff; border: none; border-radius: 6px; padding: 6px 10px; font-size: .8rem; font-weight: 700; cursor: pointer; }

footer { text-align: center; padding: 16px 12px; font-size: .7rem; color: var(--text-soft); display: flex; flex-direction: column; gap: 6px; justify-content: center; align-items: center; width: 100%; }
.footer-link { color: var(--blue); text-decoration: none; font-weight: 500; cursor: pointer; margin-top: 2px; }
.footer-link:hover { text-decoration: underline; }


/* --- НАСТРОЙКА МЕНЮ ВЫБОРА ЕДИНИЦ (ГР / ШТ) В КАРТОЧКЕ --- */

.unit-ctrl-box {
  position: relative;
  display: block;
}

/* Кнопка «Ед.» внутри сетки */
.card-unit-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--surface);
  color: var(--text-main);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  width: 60px;
  height: 36px;
  font-size: 140%; /* Шрифт в 140%, как у инпутов */
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: border-color 0.15s, background-color 0.15s;
}
.card-unit-btn:focus {
  border-color: var(--blue);
}

/* Поведение кнопки в выигрышной карточке: сохраняем цвет фона, делаем зеленую рамку */
.winner .card-unit-btn {
  background: var(--surface);
  border-color: var(--green);
}
html.dark-theme .winner .card-unit-btn,
html.system-theme .winner .card-unit-btn {
  @media (prefers-color-scheme: dark) {
    background: var(--surface);
    border-color: var(--green);
  }
}

/* Выпадающее меню, привязанное строго к низу кнопки */
.unit-dropdown-menu {
  position: absolute;
  top: calc(100% + 4px); /* Появляется снизу поля */
  right: 0; /* Выравнивание по правому краю поля */
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  display: none;
  min-width: 130px;
  box-shadow: 0 4px 12px rgba(0,0,0,.15);
  z-index: 120;
}
.unit-dropdown-menu.open {
  display: block;
}

/* Пункты меню: крупный шрифт (140% или 1.15rem) */
.unit-dropdown-menu .ctrl-item {
  display: flex;
  align-items: center;
  padding: 10px 12px;
  font-size: 1.15rem; /* Шрифт крупный, под стать инпутам формы */
  cursor: pointer;
  border: none;
  background: none;
  color: var(--text-main);
  width: 100%;
  text-align: left;
  font-family: inherit;
}
.unit-dropdown-menu .ctrl-item:hover {
  background: var(--surface2);
}
.unit-dropdown-menu .ctrl-item.active {
  color: var(--blue);
  font-weight: 700;
}
.winner .unit-dropdown-menu .ctrl-item.active {
  color: var(--green);
}