:root {
  /* Светлая тема (по умолчанию) */
  --bg:            #efe9df;
  --panel:         #f7f3ec;
  --text:          #2b2b2b;
  --board-line:    #bdb5a6;

  --empty-stroke:  #b9b1a2;   /* контур свободной точки */
  --white-fill:    #ffffff;
  --white-stroke:  #2b2b2b;
  --black-fill:    #2b2b2b;
  --black-stroke:  #2b2b2b;
  --ring-both:     #2b2b2b;   /* общий контур для «оба цвета» */

  --mill-casing:   #7d7d82; /* контрастная окантовка цветной полосы мельницы */

  --accent:        #c8922a;
  --btn-bg:        #e7e0d3;
  --btn-text:      #514b3f;
  --danger:        #b23b3b;
  --badge-bg:      rgba(0, 0, 0, .10);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg:           #16161a;
    --panel:        #1e1e24;
    --text:         #e9e7e2;
    --board-line:   #3d3d46;

    --empty-stroke: #4a4a54;
    --white-fill:   #f0efe9;
    --white-stroke: #f0efe9;
    --black-fill:   #0c0c0e;
    --black-stroke: #8b8b95;
    --ring-both:    #8b8b95;

    --mill-casing:  #9a9aa2;

    --accent:       #d7a544;
    --btn-bg:       #2a2a31;
    --btn-text:     #cfccc4;
    --danger:       #e0736f;
    --badge-bg:     rgba(255, 255, 255, .14);
  }
}

* { 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;
  -webkit-text-size-adjust: 100%;
}

body {
  min-height: 100dvh;
  display: flex;
  justify-content: center;
}

.app {
  width: 100%;
  max-width: 460px;
  padding: max(12px, env(safe-area-inset-top)) 14px max(16px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* ---- Переключатель цвета ---- */
.bar { display: flex; }

.switch {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  width: 100%;
  padding: 5px;
  background: var(--panel);
  border-radius: 14px;
}

.seg {
  appearance: none;
  border: 2px solid transparent;
  border-radius: 10px;
  padding: 14px 8px;
  font-size: 17px;
  font-weight: 600;
  letter-spacing: 0.01em;
  color: var(--btn-text);
  background: transparent;
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  transition: background .12s, border-color .12s, color .12s;
}

.seg::before {
  content: "";
  display: inline-block;
  width: 15px; height: 15px;
  margin-right: 9px;
  border-radius: 50%;
  vertical-align: -2px;
  border: 2px solid var(--empty-stroke);
}
.seg--w::before { background: var(--white-fill); border-color: var(--white-stroke); }
.seg--b::before { background: var(--black-fill); border-color: var(--black-stroke); }

.seg.is-active {
  background: var(--bg);
  border-color: var(--accent);
  color: var(--text);
  box-shadow: 0 1px 3px rgba(0,0,0,.12);
}

/* Счётчик собранных мельниц на кнопке цвета */
.seg__count {
  display: inline-block;
  min-width: 1.6em;
  margin-left: 8px;
  padding: 1px 7px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 700;
  line-height: 1.35;
  vertical-align: 1px;
  background: var(--badge-bg);
  color: inherit;
}

/* ---- Доска ---- */
.boardWrap {
  width: 100%;
  background: var(--panel);
  border-radius: 18px;
  padding: 10px;
}

#board {
  display: block;
  width: 100%;
  height: auto;
  touch-action: none; /* свайп по доске рисует линию, а не скроллит страницу */
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

/* Линии доски */
.board-line {
  fill: none;
  stroke: var(--board-line);
  stroke-width: 0.045;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* Подсветка собранной мельницы — цветом того, кто её собрал.
   Полосы белого и чёрного смещены к разным сторонам линии: если мельницу
   собрали оба, видны обе полосы (два цвета по краям линии). */
.mill {
  opacity: 0;
  stroke-linecap: round;
  pointer-events: none;
  transition: opacity .15s;
}
.mill.is-on { opacity: 1; }
.mill--casing { stroke: var(--mill-casing); stroke-width: 0.17; }
.mill--w { stroke: var(--white-fill); stroke-width: 0.10; }
.mill--b { stroke: var(--black-fill); stroke-width: 0.10; }

/* Точки */
.pt { cursor: pointer; }
.pt .hit { fill: transparent; }
.pt .disc {
  /* заливка цветом доски перекрывает линии/спицы внутри свободного кружка */
  fill: var(--panel);
  stroke: var(--empty-stroke);
  stroke-width: 0.04;
}
.pt .half { display: none; }
.pt .ring {
  fill: none;
  stroke: none;
  stroke-width: 0.05;
}

.pt.is-w .disc { fill: var(--white-fill); stroke: var(--white-stroke); stroke-width: 0.05; }
.pt.is-b .disc { fill: var(--black-fill); stroke: var(--black-stroke); stroke-width: 0.05; }

.pt.is-wb .disc { fill: var(--white-fill); stroke: none; }
.pt.is-wb .half { display: block; fill: var(--black-fill); }
.pt.is-wb .ring { stroke: var(--ring-both); }

/* ---- Сброс ---- */
.foot { display: flex; gap: 10px; }

.ghostbtn {
  appearance: none;
  border: 2px solid transparent;
  border-radius: 12px;
  padding: 13px 16px;
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  color: var(--btn-text);
  background: var(--btn-bg);
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.reset {
  appearance: none;
  flex: 1;
  border: 2px solid transparent;
  border-radius: 12px;
  padding: 13px;
  font-size: 15px;
  font-weight: 600;
  color: var(--btn-text);
  background: var(--btn-bg);
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  transition: background .12s, color .12s, border-color .12s;
}

/* ---- Сообщение о победе ---- */
.win {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(0, 0, 0, .45);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
  animation: winFade .18s ease;
}
.win[hidden] { display: none; }
@keyframes winFade { from { opacity: 0; } to { opacity: 1; } }

.win__card {
  width: 100%;
  max-width: 320px;
  padding: 26px 22px 20px;
  text-align: center;
  background: var(--panel);
  color: var(--text);
  border-radius: 18px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, .35);
}
.win__disc {
  display: block;
  width: 54px; height: 54px;
  margin: 0 auto 14px;
  border-radius: 50%;
  border: 2px solid var(--empty-stroke);
}
.win__disc.is-w    { background: var(--white-fill); border-color: var(--white-stroke); }
.win__disc.is-b    { background: var(--black-fill); border-color: var(--black-stroke); }
.win__disc.is-both {
  background: linear-gradient(90deg, var(--white-fill) 0 50%, var(--black-fill) 50% 100%);
  border-color: var(--ring-both);
}
.win__title { font-size: 22px; font-weight: 700; margin-bottom: 6px; }
.win__sub   { font-size: 14px; opacity: .75; margin-bottom: 20px; }

.win__actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.win__btn {
  appearance: none;
  border: 2px solid transparent;
  border-radius: 12px;
  padding: 12px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}
.win__btn--ghost   { background: var(--btn-bg); color: var(--btn-text); }
.win__btn--primary { background: var(--accent); color: #1b1500; }

/* ---- Модальное окно (общее) ---- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(0, 0, 0, .45);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
  animation: winFade .18s ease;
}
.modal[hidden] { display: none; }
.modal__card {
  width: 100%;
  max-width: 420px;
  max-height: 85vh;
  overflow: auto;
  padding: 20px;
  background: var(--panel);
  color: var(--text);
  border-radius: 18px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, .35);
}

/* ---- История партий ---- */
.hist__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}
.hist__title { margin: 0; font-size: 19px; font-weight: 700; }
.hist__x {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 20px;
  line-height: 1;
  padding: 6px;
  opacity: .7;
  cursor: pointer;
  touch-action: manipulation;
}

.chip {
  display: inline-block;
  width: 16px; height: 16px;
  border-radius: 50%;
  border: 2px solid var(--empty-stroke);
  vertical-align: -3px;
}
.chip--w    { background: var(--white-fill); border-color: var(--white-stroke); }
.chip--b    { background: var(--black-fill); border-color: var(--black-stroke); }
.chip--both {
  background: linear-gradient(90deg, var(--white-fill) 0 50%, var(--black-fill) 50% 100%);
  border-color: var(--ring-both);
}

.hist__wins {
  display: flex;
  gap: 18px;
  margin-bottom: 16px;
  font-size: 15px;
}
.wins__item { display: flex; align-items: center; gap: 8px; }
.wins__item b { font-size: 18px; }

.hist__empty { padding: 22px 0; text-align: center; opacity: .7; }

.hist__table { width: 100%; border-collapse: collapse; font-size: 14px; }
.hist__table th, .hist__table td { padding: 8px 6px; text-align: left; }
.hist__table th.num, .hist__table td.num { text-align: center; }
.hist__table thead th { font-weight: 600; border-bottom: 1px solid var(--board-line); }
.hist__sum th { font-weight: 700; border-bottom: 2px solid var(--board-line); }
.hist__table tbody td { border-bottom: 1px solid var(--badge-bg); }

.hist__clear {
  appearance: none;
  width: 100%;
  margin-top: 16px;
  border: 2px solid transparent;
  border-radius: 12px;
  padding: 11px;
  font-size: 14px;
  font-weight: 600;
  color: var(--btn-text);
  background: var(--btn-bg);
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
.hist__clear.is-armed { color: #fff; background: var(--danger); border-color: var(--danger); }

/* ---- Модалка выбора исхода при сбросе ---- */
.rw__title { margin: 0 0 6px; font-size: 20px; font-weight: 700; text-align: center; }
.rw__sub   { margin: 0 0 18px; font-size: 14px; opacity: .75; text-align: center; }
.rw__opts  { display: grid; gap: 10px; }
.rw__opt {
  appearance: none;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  border: 2px solid transparent;
  border-radius: 12px;
  padding: 14px;
  font-size: 16px;
  font-weight: 600;
  color: var(--text);
  background: var(--btn-bg);
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
.rw__opt--skip { color: var(--btn-text); font-weight: 500; }
.rw__cancel {
  appearance: none;
  width: 100%;
  margin-top: 12px;
  border: none;
  background: transparent;
  color: var(--btn-text);
  font-size: 14px;
  padding: 8px;
  opacity: .8;
  cursor: pointer;
  touch-action: manipulation;
}
