:root {
  --blue: #003580;
  --blue-dark: #002250;
  --blue-light: #1b56a5;
  --bg: #eef1f6;
  --panel: #ffffff;
  --ink: #10233d;
  --muted: #64748b;
  --line: #dbe2ec;
  --good: #178a3f;
  --danger: #c8102e;
  --shadow: 0 1px 2px rgba(16, 35, 61, .10), 0 1px 3px rgba(16, 35, 61, .06);
  --radius: 8px;
  font-synthesis: none;
}

* { box-sizing: border-box; }

/* hidden-attribuutti voittaa aina display-säännöt (esim. .modal-overlay: display:grid) */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 26px;
  background: var(--blue);
  color: #fff;
  box-shadow: var(--shadow);
  position: sticky;
  top: 0;
  z-index: 20;
}
.brand { display: flex; align-items: center; gap: 14px; min-width: 0; }
.brand-mark { font-size: 30px; line-height: 1; }
.brand-text { display: flex; flex-direction: column; min-width: 0; }
.brand-text strong { font-size: 19px; letter-spacing: .2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand-sub { font-size: 12.5px; opacity: .75; text-transform: uppercase; letter-spacing: 2px; }
.netbanner {
  background: #fff3cd; color: #7a5b00; text-align: center; padding: 9px 16px;
  font-weight: 700; font-size: 14px; border-bottom: 1px solid #f0d98a; position: sticky; top: 0; z-index: 15;
}
.conn { text-transform: none; letter-spacing: .2px; font-weight: 600; }
.cdot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 5px; vertical-align: baseline; }
.cdot.ok { background: #3ad07a; box-shadow: 0 0 0 3px rgba(58,208,122,.28); }
.cdot.off { background: #ffcc4d; }
.cdot.local { background: #9fb3c8; }

.nav { display: flex; gap: 8px; flex-shrink: 0; }
.nav-btn {
  color: #fff;
  text-decoration: none;
  padding: 9px 16px;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 600;
  background: rgba(255,255,255,.10);
  border: 1px solid rgba(255,255,255,.14);
  transition: background .15s, transform .1s;
}
.nav-btn:hover { background: rgba(255,255,255,.22); }
.nav-btn.active { background: #fff; color: var(--blue); border-color: #fff; }
.nav-btn--ghost { background: transparent; }

/* ---------- Layout ---------- */
.app { max-width: 1280px; margin: 0 auto; padding: 30px 26px 70px; }
.app.wide { max-width: none; padding: 22px 30px 40px; }

.page-head { margin-bottom: 22px; }
.page-head h1 { margin: 0 0 4px; font-size: 26px; }
.page-head p { margin: 0; color: var(--muted); font-size: 15px; }

/* ---------- Home ---------- */
.home-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 22px; }
.home-card {
  background: var(--panel);
  border-radius: var(--radius);
  padding: 34px;
  text-decoration: none;
  color: var(--ink);
  box-shadow: var(--shadow);
  border: 1px solid var(--line);
  transition: transform .12s, box-shadow .12s;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.home-card:hover { transform: translateY(-1px); box-shadow: 0 2px 6px rgba(16,35,61,.12); }
.home-card .ico { font-size: 44px; }
.home-card h2 { margin: 4px 0 0; font-size: 22px; }
.home-card p { margin: 0; color: var(--muted); font-size: 15px; line-height: 1.5; }
.home-stat { margin-top: 26px; display: flex; gap: 30px; justify-content: center; }
.home-stat .big { font-size: 34px; font-weight: 800; color: var(--blue); }
.home-stat .lbl { font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: 1px; }

/* ---------- Counting ---------- */
.count-bar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; margin-bottom: 22px; flex-wrap: wrap;
}
.counted-pill {
  background: var(--panel); border: 1px solid var(--line); border-radius: 8px;
  padding: 10px 20px; font-weight: 700; box-shadow: var(--shadow);
}
.counted-pill b { color: var(--blue); font-size: 20px; }
.counted-pill--mine b { color: var(--good); }
.count-pills { display: flex; gap: 10px; flex-wrap: wrap; }
.count-actions { display: flex; gap: 10px; flex-wrap: wrap; }

.cand-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 16px;
}
.cand-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-left: 8px solid var(--c, var(--blue));
  border-radius: 8px;
  padding: 18px;
  cursor: pointer;
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  gap: 14px;
  transition: transform .1s, box-shadow .1s;
  text-align: left;
  font: inherit;
  color: inherit;
  width: 100%;
}
.cand-card:hover { transform: translateY(-1px); box-shadow: 0 2px 6px rgba(16,35,61,.14); }
.cand-card:active { transform: translateY(0) scale(.99); }
.cand-card.flash { animation: flash .6s; }
@keyframes flash {
  0% { box-shadow: 0 0 0 0 rgba(23,138,63,.6); }
  100% { box-shadow: 0 0 0 18px rgba(23,138,63,0); }
}
.avatar {
  width: 48px; height: 64px; border-radius: 6px;
  display: grid; place-items: center; color: #fff; font-weight: 800; font-size: 18px;
  background: var(--c, var(--blue)); flex-shrink: 0; overflow: hidden;
}
.avatar img, .bar-avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }
.num-badge { font-size: 26px; font-weight: 800; text-shadow: 0 1px 3px rgba(0,0,0,.4); }   /* ehdokasnumero kuvan tilalla (laskenta) */
.hint code { background: #eef1f6; border: 1px solid var(--line); border-radius: 4px; padding: 1px 5px; font-size: 12.5px; }
.modal .m-cand .num-badge { font-size: 30px; }
.cand-meta { min-width: 0; }
.cand-name { font-weight: 700; font-size: 17px; }
.cand-votes { color: var(--muted); font-size: 13.5px; margin-top: 2px; }
.cand-votes b { color: var(--ink); font-size: 15px; }

/* ---------- Buttons ---------- */
.btn {
  border: none; border-radius: 6px; padding: 11px 18px; font-size: 15px;
  font-weight: 700; cursor: pointer; font-family: inherit;
  transition: transform .1s, filter .1s; box-shadow: var(--shadow);
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--blue); color: #fff; }
.btn-primary:hover { filter: brightness(1.08); }
.btn-good { background: var(--good); color: #fff; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-ghost { background: #fff; color: var(--ink); border: 1px solid var(--line); box-shadow: none; }
.btn-ghost:hover { background: #f5f7fb; }
.btn-lg { padding: 15px 26px; font-size: 17px; }
.btn:disabled { opacity: .45; cursor: not-allowed; }

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(6,18,36,.55);
  display: grid; place-items: center; z-index: 50; padding: 20px;
  animation: fade .12s;
}
@keyframes fade { from { opacity: 0; } }
.modal {
  background: #fff; border-radius: 10px; padding: 30px; width: min(460px, 100%);
  box-shadow: 0 20px 50px rgba(0,0,0,.3); animation: pop .16s;
}
@keyframes pop { from { transform: scale(.94); opacity: .4; } }
.modal .m-eyebrow { color: var(--muted); text-transform: uppercase; letter-spacing: 2px; font-size: 12px; font-weight: 700; }
.modal .m-cand { display: flex; align-items: center; gap: 16px; margin: 16px 0 8px; }
.modal .m-cand .avatar { width: 60px; height: 80px; font-size: 22px; }
.modal .m-cand .n { font-size: 24px; font-weight: 800; }
.modal .m-q { color: var(--muted); font-size: 15px; margin: 10px 0 16px; }
.modal .m-input {
  width: 100%; padding: 11px 13px; border: 1px solid var(--line); border-radius: 6px;
  font-size: 16px; font-family: inherit; margin-bottom: 20px; text-transform: uppercase;
}
.modal .m-input:focus { outline: 2px solid var(--danger); border-color: var(--danger); }
.modal .m-actions { display: flex; gap: 12px; }
.modal .m-actions .btn { flex: 1; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; bottom: 26px; left: 50%; transform: translateX(-50%) translateY(20px);
  background: var(--ink); color: #fff; padding: 13px 22px; border-radius: 8px;
  font-weight: 700; box-shadow: var(--shadow); z-index: 60; opacity: 0;
  transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast .dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 8px; vertical-align: middle; }

/* ---------- Results (YLE-style) ---------- */
.results-head {
  display: flex; align-items: flex-end; justify-content: space-between;
  gap: 20px; margin-bottom: 8px; flex-wrap: wrap;
}
.results-head h1 { margin: 0; font-size: 30px; }
.results-head .sub { color: var(--muted); font-size: 15px; margin-top: 4px; }
.results-total {
  text-align: right;
}
.results-total .num { font-size: 40px; font-weight: 800; color: var(--blue); line-height: 1; }
.results-total .lbl { font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: 1.5px; }

.chart {
  margin-top: 26px; background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 28px 26px 22px;
}
.bars {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  gap: 18px;
  align-items: end;
  height: 46vh;
  min-height: 320px;
}
.bar-col { display: flex; flex-direction: column; align-items: center; height: 100%; justify-content: flex-end; gap: 10px; }
.bar-col.col-hidden { display: none; }   /* tuloksissa näkyy vain top 4 */
.bar-figures { text-align: center; }
.bar-num { font-size: clamp(22px, 2.8vw, 44px); font-weight: 800; line-height: 1; }
.bar-num-lbl { font-size: 12px; color: var(--muted); margin-top: 3px; text-transform: uppercase; letter-spacing: 1px; }
.bar-track { width: 100%; display: flex; align-items: flex-end; justify-content: center; flex: 1; }
.bar {
  width: min(78%, 96px);
  background: var(--c, var(--blue));
  border-radius: 8px 8px 0 0;
  height: 0;
  min-height: 40px;                 /* varmuusraja; korkeus lasketaan pikseleinä JS:ssä */
  transition: height .6s cubic-bezier(.22,.61,.36,1);
  position: relative;
  box-shadow: inset 0 -6px 12px rgba(0,0,0,.12);
}
.bar-val {
  position: absolute; top: 7px; left: 0; right: 0; text-align: center;
  color: #fff; font-weight: 800; font-size: clamp(15px, 1.9vw, 28px); line-height: 1;
  text-shadow: 0 1px 4px rgba(0,0,0,.5), 0 0 2px rgba(0,0,0,.4);   /* luettavuus myös vaaleilla pylväillä */
}
.bar-col.leader .bar { box-shadow: inset 0 -6px 12px rgba(0,0,0,.12), 0 0 0 4px rgba(255,215,0,.55); }
.bar-foot { text-align: center; }
.bar-avatar {
  width: 60px; height: 80px; border-radius: 8px; margin: 0 auto 6px;
  display: grid; place-items: center; color: #fff; font-weight: 800; background: var(--c, var(--blue));
  border: 2px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.15); overflow: hidden;
}
.bar-name { font-weight: 700; font-size: 15px; }
.bar-name .bn-last { margin-left: .3em; }                 /* normaalinäkymä: etu- ja sukunimi samalla rivillä */
.bar-crown { font-size: 16px; visibility: hidden; margin-right: 3px; }

.empty-note {
  background: var(--panel); border: 1px dashed var(--line); border-radius: var(--radius);
  padding: 40px; text-align: center; color: var(--muted); margin-top: 20px;
}
.empty-note h3 { color: var(--ink); margin: 0 0 8px; }
.empty-note p { margin: 0 auto; max-width: 460px; }
.err-actions { display: flex; gap: 10px; justify-content: center; margin-top: 18px; }

/* ---------- Setup ---------- */
.setup-panel {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 24px; max-width: 760px;
}
.field { margin-bottom: 18px; }
.field label { display: block; font-weight: 700; margin-bottom: 6px; font-size: 14px; }
.field input[type=text] {
  width: 100%; padding: 12px 14px; border: 1px solid var(--line); border-radius: 11px;
  font-size: 16px; font-family: inherit;
}
.field input[type=text]:focus { outline: 2px solid var(--blue-light); border-color: var(--blue-light); }

.cand-rows { display: flex; flex-direction: column; gap: 10px; margin: 10px 0 16px; }
.cand-row { display: flex; align-items: center; gap: 12px; }
.cand-row .swatch { width: 30px; height: 30px; border-radius: 8px; flex-shrink: 0; border: 1px solid rgba(0,0,0,.1); cursor: pointer; }
.field .cand-row .num-in { flex: 0 0 54px; width: 54px; max-width: 54px; text-align: center; padding: 11px 6px; border: 1px solid var(--line); border-radius: 8px; font-size: 15px; font-family: inherit; }
.cand-row input { flex: 1; padding: 11px 13px; border: 1px solid var(--line); border-radius: 10px; font-size: 15px; font-family: inherit; }
.cand-row .rm { background: #fff; border: 1px solid var(--line); color: var(--danger); border-radius: 8px; width: 44px; height: 44px; cursor: pointer; font-size: 18px; flex-shrink: 0; }
.cand-row .rm:hover { background: #fdeef0; }
.cand-row .photo-btn {
  width: 44px; height: 58px; border-radius: 6px; flex-shrink: 0; cursor: pointer; overflow: hidden;
  display: grid; place-items: center; border: 1px solid var(--line); background: #f4f6fa;
  font-size: 18px; position: relative;
}
.cand-row .photo-btn img { width: 100%; height: 100%; object-fit: cover; }
.cand-row .photo-btn .ph { font-size: 12px; font-weight: 700; color: var(--muted); }
.cand-row .photo-btn:hover { border-color: var(--blue-light); background: #eef2fa; }
.cand-row .photo-rm {
  width: 32px; height: 32px; border-radius: 50%; flex-shrink: 0; cursor: pointer; margin-left: -4px;
  border: 1px solid var(--line); background: #fff; color: var(--danger); font-size: 14px; line-height: 1;
}
.cand-row .photo-rm:hover { background: #fdeef0; }
.setup-actions { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 8px; }
.setup-actions .spacer { flex: 1; }

.hint { color: var(--muted); font-size: 13.5px; margin-top: 10px; line-height: 1.5; }

.sp-h { margin: 0 0 4px; font-size: 16px; }

/* Salasanaportti (laskenta / asetukset) */
.gate {
  max-width: 380px; margin: 8vh auto 0; background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 30px; text-align: center;
}
.gate h2 { margin: 0 0 6px; }
.gate p { margin: 0 0 18px; color: var(--muted); }
.gate-input {
  width: 100%; padding: 13px 14px; border: 1px solid var(--line); border-radius: 8px;
  font-size: 17px; font-family: inherit; text-align: center; margin-bottom: 12px;
}
.gate-input:focus { outline: 2px solid var(--blue-light); border-color: var(--blue-light); }
.gate .btn { width: 100%; }
.pw-err { color: var(--danger); font-weight: 700; font-size: 14px; margin-bottom: 12px; }

.danger-zone { margin-top: 20px; border-color: #f0d0d5; }
.danger-zone h3 { margin: 0 0 6px; color: var(--danger); font-size: 16px; }
.danger-zone p { margin: 0 0 14px; color: var(--muted); font-size: 14px; line-height: 1.5; }

/* ---------- Fullscreen (tulokset ja laskenta) ---------- */
body.fs .topbar { display: none; }
body.fs .page-head { display: none; }
body.fs .app.wide { padding: 22px 30px; }
/* Laskenta kokonäytössä: isommat kortit */
body.fs .cand-grid { grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); }
body.fs .cand-card { padding: 22px; }
body.fs .cand-card .avatar { width: 60px; height: 80px; font-size: 22px; }
body.fs .cand-name { font-size: 20px; }
body.fs .chart { padding: 20px 26px; }
body.fs .bars { height: calc(100vh - 150px); }
body.fs #resEmpty { display: none !important; }
body.fs .bar { min-height: 60px; }        /* varmuusraja; korkeus pikseleinä JS:ssä */
body.fs .bar-val { font-size: clamp(22px, 2.6vw, 46px); }
body.fs .bar-avatar { width: 84px; height: 112px; }
/* Kokonäytössä nimi kahdella rivillä (etunimi ylhäällä, sukunimi alhaalla) -> pilarit tasakokoiset */
body.fs .bar-name { font-size: 20px; line-height: 1.25; }
body.fs .bar-name .bn-first, body.fs .bar-name .bn-last { display: block; margin: 0; }
body.fs .bar-name .bn-last { min-height: 1.25em; }         /* varaa rivi myös yksinimisille */
/* Kokonäytön yläpalkki omalla rivillään, ei pylväiden päällä */
.fs-bar { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.fs-total { font-size: 18px; font-weight: 600; color: var(--muted); }
.fs-total b { color: var(--blue); font-size: 26px; }
.fs-exit {
  width: 42px; height: 42px; border-radius: 8px; border: 1px solid var(--line); cursor: pointer;
  background: #fff; color: var(--ink); font-size: 18px; line-height: 1;
}
.fs-exit:hover { background: #f5f7fb; }

/* ---------- Kuvan rajaus ---------- */
.crop-overlay {
  position: fixed; inset: 0; background: rgba(6,18,36,.62);
  display: grid; place-items: center; z-index: 80; padding: 20px; animation: fade .12s;
}
.crop-box {
  background: #fff; border-radius: 10px; padding: 22px;
  box-shadow: 0 20px 50px rgba(0,0,0,.32); animation: pop .16s;
}
.crop-title { font-weight: 800; font-size: 18px; }
.crop-hint { color: var(--muted); font-size: 13px; margin: 4px 0 14px; }
.crop-frame {
  position: relative; overflow: hidden; border-radius: 6px; background: #0b1a2e;
  margin: 0 auto; touch-action: none; cursor: grab; user-select: none;
}
.crop-frame:active { cursor: grabbing; }
.crop-img { position: absolute; max-width: none; -webkit-user-drag: none; user-select: none; }
.crop-zoom { width: 100%; margin: 16px 0; accent-color: var(--blue); }
.crop-actions { display: flex; gap: 12px; }
.crop-actions .btn { flex: 1; }

@media (max-width: 720px) {
  .home-grid { grid-template-columns: 1fr; }
  .nav-btn { padding: 8px 12px; font-size: 13px; }
  .brand-text strong { font-size: 15px; }
  .bars { height: 40vh; }
}
