@import url("fonts.css");

:root {
  --paper: #f5f8fa;
  --paper-edge: #dde6ec;
  --grid-minor: rgb(88 150 190 / 0.30);
  --grid-major: rgb(60 125 172 / 0.62);
  --grid-axis: rgb(40 100 150 / 0.95);
  --ink: #1c2b4a;
  --ink-soft: #3d4c6b;
  --red: #c92222;
  --tape: rgb(238 214 120 / 0.82);
  --hand: "Gaegu", "Kalam", cursive;
  --text: "Kalam", "Gaegu", cursive;
  --mono: "Overpass Mono", ui-monospace, monospace;
  --ease: cubic-bezier(0.16, 1, 0.3, 1);
  color-scheme: light;
}

* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }

body {
  background: var(--paper);
  color: var(--ink);
  font-family: var(--text);
  font-size: 1rem;
  line-height: 1.45;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}

::selection { background: var(--red); color: var(--paper); }

:focus-visible {
  outline: 3px solid var(--ink);
  outline-offset: 3px;
  border-radius: 4px;
}

/* ---------- the sheet ---------- */

.sheet {
  position: fixed;
  inset: 0;
  touch-action: none;
  cursor: grab;
  background: var(--paper);
  background-image: radial-gradient(120% 90% at 50% 40%, transparent 55%, var(--paper-edge) 100%);
}
.sheet.dragging { cursor: grabbing; }

.plane { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }

#grid line { vector-effect: non-scaling-stroke; stroke: var(--grid-minor); stroke-width: 1; }
#grid line.major { stroke: var(--grid-major); stroke-width: 1.4; }
#grid line.axis { stroke: var(--grid-axis); stroke-width: 2.2; }

#land {
  stroke: var(--ink);
  stroke-width: 1.8;
  stroke-linejoin: round;
  stroke-linecap: round;
  vector-effect: non-scaling-stroke;
}

.ticks { position: absolute; inset: 0; pointer-events: none; }
.tick {
  position: absolute;
  font-family: var(--mono);
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: var(--ink-soft);
  font-variant-numeric: tabular-nums;
  padding: 0 0.3em;
  background: var(--paper);
  line-height: 1.2;
}
.tick.x { bottom: calc(env(safe-area-inset-bottom) + 6px); transform: translateX(-50%); }
.tick.y { left: 6px; transform: translateY(-50%); }

/* ---------- points ---------- */

.points { position: absolute; inset: 0; pointer-events: none; }

.pt {
  position: absolute;
  left: 0; top: 0;
  width: 44px; height: 44px;
  margin: -22px 0 0 -22px;
  padding: 0;
  border: 0;
  background: none;
  pointer-events: auto;
  cursor: pointer;
  color: var(--red);
  touch-action: none;
}
.pt svg { width: 100%; height: 100%; display: block; overflow: visible; }
.pt .mark path {
  fill: none; stroke: currentColor; stroke-width: 4; stroke-linecap: round;
  stroke-dasharray: 22; stroke-dashoffset: 22;
  animation: ink 520ms var(--ease) forwards;
  animation-delay: calc(var(--i, 0) * 70ms + 250ms);
}
.pt .ring {
  fill: none; stroke: currentColor; stroke-width: 2;
  transform-origin: 22px 22px; transform: scale(0.4); opacity: 0;
  transition: transform 300ms var(--ease), opacity 200ms;
}
.pt:hover .ring, .pt:focus-visible .ring, .pt[aria-pressed="true"] .ring { transform: scale(1); opacity: 1; }
.pt:focus-visible { outline: none; }
.pt:focus-visible .ring { stroke: var(--ink); stroke-width: 3; }

.pt .tag {
  position: absolute; left: 34px; top: 4px;
  font-family: var(--mono); font-size: 0.72rem; font-weight: 600;
  color: var(--ink); background: var(--paper);
  padding: 0.1em 0.4em; white-space: nowrap;
  font-variant-numeric: tabular-nums;
  opacity: 0; transform: translateX(-4px);
  transition: opacity 160ms, transform 240ms var(--ease);
  pointer-events: none;
}
.pt:hover .tag, .pt:focus-visible .tag, .pt[aria-pressed="true"] .tag { opacity: 1; transform: none; }

@keyframes ink { to { stroke-dashoffset: 0; } }

/* ---------- worksheet heading ---------- */

.head {
  position: fixed;
  top: 0; left: 0;
  max-width: min(30rem, 100%);
  padding: calc(env(safe-area-inset-top) + 0.9rem) 1.25rem 0.9rem max(1.25rem, env(safe-area-inset-left));
  background: var(--paper);
  border-bottom: 2px solid var(--grid-axis);
  border-right: 2px solid var(--grid-axis);
  border-bottom-right-radius: 4px;
  box-shadow: 2px 4px 14px rgb(28 43 74 / 0.12);
  pointer-events: none;
}
.head h1 {
  margin: 0;
  font-family: var(--hand);
  font-weight: 700;
  font-size: clamp(2.4rem, 7vw, 4rem);
  line-height: 0.95;
  letter-spacing: -0.01em;
  text-wrap: balance;
  transform: rotate(-1.2deg);
  transform-origin: left bottom;
}
.head h1::after {
  content: "";
  display: block;
  height: 6px; width: 70%; margin-top: 0.12em;
  background: var(--red);
  border-radius: 6px;
  transform: rotate(0.8deg);
  animation: underline 700ms var(--ease) 100ms both;
  transform-origin: left;
}
@keyframes underline { from { transform: scaleX(0) rotate(0.8deg); } }
.task { margin: 0.7rem 0 0; font-size: 1rem; color: var(--ink-soft); }
.count {
  margin: 0.15rem 0 0;
  font-family: var(--mono); font-size: 0.8rem; font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.count:empty { display: none; }

/* ---------- empty state ---------- */

.empty {
  position: fixed;
  left: 50%; top: 52%;
  transform: translate(-50%, -50%) rotate(-1.5deg);
  width: min(22rem, calc(100% - 2.5rem));
  padding: 1.1rem 1.3rem 1.2rem;
  background: var(--paper);
  border: 2px dashed var(--red);
  text-align: center;
  pointer-events: none;
}
.empty p { margin: 0; color: var(--ink-soft); }
.empty .empty-title { font-family: var(--hand); font-weight: 700; font-size: 1.9rem; line-height: 1.1; color: var(--ink); margin-bottom: 0.25rem; }

/* ---------- zoom ---------- */

.zoom {
  position: fixed;
  right: max(0.9rem, env(safe-area-inset-right));
  top: calc(env(safe-area-inset-top) + 0.9rem);
  display: grid;
  gap: 0.5rem;
}
.zoom button {
  min-width: 44px; min-height: 44px;
  padding: 0 0.7rem;
  font: 700 1.35rem/1 var(--hand);
  color: var(--ink);
  background: var(--paper);
  border: 2px solid var(--ink);
  border-radius: 6px;
  box-shadow: 1px 3px 6px rgb(28 43 74 / 0.18);
  cursor: pointer;
  transition: transform 160ms var(--ease), background 160ms;
}
.zoom button:hover { background: #fff; }
.zoom button:active { transform: translateY(2px); box-shadow: none; }
#zfit { font-size: 1.1rem; }

/* ---------- taped print ---------- */

.print {
  position: fixed;
  right: max(1.25rem, env(safe-area-inset-right));
  bottom: max(2.4rem, calc(env(safe-area-inset-bottom) + 1.6rem));
  width: min(21rem, calc(100% - 2.5rem));
  padding: 0.8rem 0.8rem 1rem;
  background: #fffdf6;
  box-shadow: 2px 6px 18px rgb(28 43 74 / 0.28), 0 1px 2px rgb(28 43 74 / 0.25);
  transform: rotate(1.6deg);
}
.print:not([hidden]) { animation: tape-on 520ms var(--ease) both; }
@keyframes tape-on { from { opacity: 0; transform: translateY(18px) rotate(5deg); } }

.tape {
  position: absolute; top: -12px; width: 76px; height: 26px;
  background: var(--tape);
  box-shadow: 0 1px 2px rgb(28 43 74 / 0.18);
}
.tape-a { left: 14px; transform: rotate(-5deg); }
.tape-b { right: 14px; transform: rotate(4deg); }

.close {
  position: absolute; top: 0.2rem; right: 0.2rem; z-index: 1;
  width: 44px; height: 44px;
  font: 700 1.8rem/1 var(--hand);
  color: var(--paper); background: rgb(28 43 74 / 0.72);
  border: 0; border-radius: 50%;
  margin: 0.25rem;
  width: 36px; height: 36px;
  cursor: pointer;
}
.close::after { content: ""; position: absolute; inset: -4px; }

.photo {
  margin: 0; aspect-ratio: 4 / 3; overflow: hidden;
  background:
    repeating-linear-gradient(45deg, transparent 0 7px, var(--grid-minor) 7px 8px),
    var(--paper);
  display: grid; place-items: center;
}
.photo img { width: 100%; height: 100%; object-fit: cover; display: block; }
.photo span { font-family: var(--hand); font-weight: 700; font-size: 1.3rem; color: var(--ink-soft); padding: 0 1rem; text-align: center; }

.print h2 {
  margin: 0.7rem 0 0.3rem;
  font-family: var(--hand); font-weight: 700;
  font-size: 1.7rem; line-height: 1.05;
  text-wrap: balance;
}
.print dl { margin: 0; display: grid; gap: 0.1rem; }
.print dl div { display: flex; gap: 0.6rem; align-items: baseline; }
.print dt { font-size: 0.9rem; color: var(--ink-soft); min-width: 3.4rem; }
.print dd { margin: 0; font-family: var(--mono); font-size: 0.85rem; font-weight: 600; font-variant-numeric: tabular-nums; }

.demo-note {
  position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 0.5rem);
  transform: translateX(-50%);
  margin: 0; padding: 0.15em 0.7em;
  background: var(--paper); border: 1.5px dashed var(--red);
  font-size: 0.85rem; white-space: nowrap;
}
.demo-note[hidden] { display: none; }

/* ---------- small screens: print becomes a bottom strip ---------- */

@media (max-width: 40rem) {
  .print {
    right: 0.75rem; left: 0.75rem; width: auto;
    bottom: max(0.9rem, env(safe-area-inset-bottom));
    display: grid; grid-template-columns: 38% 1fr; gap: 0 0.8rem;
    transform: rotate(0.8deg);
  }
  .print .photo { grid-row: 1 / span 2; aspect-ratio: 1; align-self: start; }
  .print h2 { margin: 0 0 0.2rem; font-size: 1.45rem; padding-right: 2rem; }
  .print dl { align-self: start; }
  .print dl div { flex-direction: column; gap: 0; }
  .print dt { min-width: 0; font-size: 0.8rem; }
  .head { max-width: calc(100% - 4.9rem); }
  .empty { top: 58%; }
  .demo-note { top: calc(env(safe-area-inset-top) + 0.5rem); bottom: auto; left: auto; right: 4.4rem; transform: none; }
}

@media (prefers-reduced-motion: reduce) {
  .pt .mark path { animation: none; stroke-dashoffset: 0; }
  .head h1::after, .print:not([hidden]) { animation: none; }
  * { transition-duration: 0.01ms !important; }
}

/* ---------- navigation, add flow ---------- */

.nav { display: flex; gap: 1.1rem; margin-top: 0.55rem; pointer-events: auto; }
.nav a {
  font-family: var(--hand); font-weight: 700; font-size: 1.25rem; line-height: 1;
  color: var(--ink-soft); text-decoration: none;
  padding: 0.5rem 0; min-height: 44px; display: inline-flex; align-items: center;
  text-decoration: underline; text-decoration-color: transparent; text-underline-offset: 0.2em; text-decoration-thickness: 2px;
  transition: text-decoration-color 160ms;
}
.nav a:hover { text-decoration-color: var(--ink); }
.nav a[aria-current="page"] { color: var(--ink); text-decoration-color: var(--ink); }

.add-btn {
  position: fixed;
  left: max(1.25rem, env(safe-area-inset-left));
  bottom: max(2.2rem, calc(env(safe-area-inset-bottom) + 1.4rem));
  min-height: 52px; padding: 0 1.4rem;
  font: 700 1.35rem/1 var(--hand);
  color: var(--paper); background: var(--ink);
  border: 2px solid var(--ink); border-radius: 8px;
  box-shadow: 2px 5px 12px rgb(28 43 74 / 0.3);
  cursor: pointer;
  transition: transform 180ms var(--ease), box-shadow 180ms;
}
.add-btn:hover { transform: translateY(-2px) rotate(-0.6deg); box-shadow: 3px 8px 16px rgb(28 43 74 / 0.32); }
.add-btn:active { transform: translateY(1px); box-shadow: 1px 2px 4px rgb(28 43 74 / 0.3); }
.add-btn:focus-visible { outline-color: var(--red); }
body.has-panel .add-btn { display: none; }

.loaderr {
  position: fixed; left: 50%; top: 52%; transform: translate(-50%, -50%);
  margin: 0; padding: 0.8rem 1.1rem; background: var(--paper); border: 2px solid var(--red);
}
.loaderr[hidden], .empty[hidden], .note[hidden], .print[hidden] { display: none; }

.link-btn {
  margin: 0.6rem 0 0; padding: 0.5rem 0; min-height: 44px;
  font: 400 0.95rem/1 var(--text); color: var(--ink-soft);
  background: none; border: 0; text-decoration: underline; cursor: pointer;
}
.link-btn:hover { color: var(--red); }
.link-btn[hidden] { display: none; }
.print dl div[hidden] { display: none; }

.note {
  position: fixed;
  right: max(1.25rem, env(safe-area-inset-right));
  bottom: max(1.4rem, calc(env(safe-area-inset-bottom) + 0.8rem));
  width: min(22rem, calc(100% - 2.5rem));
  max-height: calc(100% - 2.8rem); overflow-y: auto;
  padding: 1rem 1.1rem 1.2rem;
  background: #fff;
  border: 2px solid var(--ink);
  box-shadow: 2px 6px 18px rgb(28 43 74 / 0.28);
  animation: tape-on 480ms var(--ease) both;
}
.note h2 { margin: 0 2rem 0.1rem 0; font: 700 1.9rem/1 var(--hand); }
.note .close { top: 0.3rem; right: 0.3rem; margin: 0; }
.step { margin: 0.2rem 0 0.7rem; color: var(--ink-soft); }
.ghost {
  min-height: 44px; padding: 0 1rem;
  font: 700 1.1rem/1 var(--hand); color: var(--ink);
  background: transparent; border: 2px dashed var(--ink); border-radius: 6px; cursor: pointer;
}
.ghost:hover { background: var(--paper); }
.fields { margin-top: 0.9rem; display: grid; gap: 0.7rem; }
.fields[hidden] { display: none; }
.picked { margin: 0; font-family: var(--mono); font-weight: 600; font-size: 0.9rem; color: var(--red); font-variant-numeric: tabular-nums; }
.note label { display: grid; gap: 0.1rem; font-size: 0.95rem; color: var(--ink-soft); }
.note .opt { font-size: 0.85rem; }
.note input:not(.hp) {
  width: 100%; min-height: 44px; padding: 0.35rem 0.1rem;
  font: 400 1rem/1.3 var(--text); color: var(--ink);
  background: transparent; border: 0; border-bottom: 2px solid var(--ink); border-radius: 0;
}
.note input[type="file"] { font-size: 0.9rem; border-bottom-style: dashed; padding-top: 0.6rem; }
.note input:focus-visible { outline: none; border-bottom-color: var(--red); background: var(--paper); }
.hp { position: absolute; left: -9999px; width: 1px; height: 1px; opacity: 0; }
.err { margin: 0; padding: 0.4rem 0.6rem; border: 2px solid var(--red); color: var(--ink); background: var(--paper); }
.err[hidden] { display: none; }
.primary {
  min-height: 50px; font: 700 1.3rem/1 var(--hand);
  color: var(--paper); background: var(--ink); border: 0; border-radius: 8px; cursor: pointer;
}
.primary:disabled { opacity: 0.6; cursor: progress; }

.pt.draft { pointer-events: none; color: var(--red); }
.pt.draft[hidden] { display: none; }
.pt.draft svg circle { fill: rgb(245 248 250 / 0.7); stroke: currentColor; stroke-width: 2.5; stroke-dasharray: 5 4; }
.pt.draft svg path { stroke: currentColor; stroke-width: 3; stroke-linecap: round; fill: none; }
.pt.draft.drop svg { animation: drop 420ms var(--ease); }
@keyframes drop { from { transform: scale(1.8); opacity: 0; } }
body.adding .sheet { cursor: crosshair; }

@media (max-width: 40rem) {
  .add-btn { left: 0.75rem; right: 0.75rem; bottom: max(0.9rem, env(safe-area-inset-bottom)); }
  .note {
    left: 0.75rem; right: 0.75rem; width: auto;
    bottom: max(0.75rem, env(safe-area-inset-bottom));
    max-height: 58%;
  }
  body.adding .zoom { display: none; }
}

/* ---------- Statistik-Seite ---------- */

body.page-stats {
  overflow: auto;
  overscroll-behavior: auto;
  background-color: var(--paper);
  background-image:
    linear-gradient(var(--grid-minor) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid-minor) 1px, transparent 1px);
  background-size: 28px 28px;
  min-height: 100%;
}
.s-head {
  max-width: 46rem; margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 1.4rem) 1.25rem 0;
}
.s-head h1 {
  margin: 0; font: 700 clamp(2.6rem, 8vw, 4.4rem)/0.95 var(--hand);
  transform: rotate(-1.2deg); transform-origin: left bottom; text-wrap: balance;
}
.s-head h1::after {
  content: ""; display: block; height: 6px; width: 8.5rem; margin-top: 0.12em;
  background: var(--red); border-radius: 6px; transform: rotate(0.8deg);
}
.s-main { max-width: 46rem; margin: 0 auto; padding: 1.2rem 1.25rem calc(env(safe-area-inset-bottom) + 4rem); }

.verdict {
  margin: 1.6rem 0 2.6rem; padding: 1rem 1.2rem 1.1rem;
  font: 700 clamp(1.7rem, 5vw, 2.6rem)/1.15 var(--hand);
  background: var(--paper); border: 2px solid var(--ink);
  box-shadow: 2px 5px 14px rgb(28 43 74 / 0.16);
  text-wrap: balance; transform: rotate(-0.4deg);
}
.verdict mark { background: none; color: var(--red); padding: 0 0.1em; font-size: 1.15em; }

.s-main section { margin-top: 2.8rem; }
.s-main h2 {
  margin: 0 0 0.5rem; padding: 0.15rem 0.5rem;
  display: inline-block; background: var(--paper);
  font: 700 1.7rem/1.1 var(--hand); text-wrap: balance;
}
.rows { list-style: none; margin: 0; padding: 0; background: var(--paper); border-top: 2px solid var(--ink); }
.rows li {
  display: grid; grid-template-columns: minmax(7rem, 14rem) 1fr 2.8rem; align-items: center; gap: 0.2rem 0.8rem;
  min-height: 44px; padding: 0.35rem 0.6rem;
  border-bottom: 1px solid var(--grid-major);
  animation: row-in 420ms var(--ease) both; animation-delay: calc(var(--i, 0) * 45ms);
}
.rows li.none { display: block; color: var(--ink-soft); animation: none; }
@keyframes row-in { from { opacity: 0; transform: translateY(6px); } }
.rows .name { font-size: 1.05rem; overflow-wrap: anywhere; line-height: 1.2; }
.rows .name small { display: block; font-size: 0.82rem; color: var(--ink-soft); }
.rows .n { font: 600 0.95rem/1 var(--mono); font-variant-numeric: tabular-nums; text-align: right; }
.tally { display: flex; flex-wrap: wrap; gap: 0.1rem 0.25rem; color: var(--red); }
.tally path { fill: none; stroke: currentColor; stroke-width: 3; stroke-linecap: round; }
.ranked li:first-child .name { font: 700 1.5rem/1.1 var(--hand); }
.loaderr.static { position: static; transform: none; display: inline-block; }

@media (max-width: 40rem) {
  .rows li { grid-template-columns: 1fr 2.4rem; }
  .rows li .tally { grid-column: 1 / -1; grid-row: 2; }
  .rows li .n { grid-row: 1; grid-column: 2; }
}
@media (prefers-reduced-motion: reduce) { .rows li { animation: none; } }
