:root {
  --bg: #E9EDF0;
  --surface: #FFFFFF;
  --ink: #16202B;
  --muted: #5A6672;
  --line: #CDD5DC;
  --accent: #2346D8;
  --accent-ink: #FFFFFF;
  --piece-empty: #DCE2E7;
  --piece-moving: #9DB0F2;
  --piece-done: #138A61;
  --danger: #B3261E;
  --pause: #A86A00;
  --accent-soft: color-mix(in srgb, var(--accent) 9%, var(--surface));

  /* Bewegung: kurz und mit weichem Auslauf. Ein Rhythmus für alles. */
  --ease: cubic-bezier(.2, .8, .2, 1);
  --dur-fast: 120ms;
  --dur: 220ms;

  --font: "IBM Plex Sans", system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, Consolas, monospace;

  --radius-s: 6px;
  --radius-l: 14px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #10161D;
    --surface: #18212B;
    --ink: #E4E9EE;
    --muted: #96A3AF;
    --line: #2B3743;
    --accent: #7390FF;
    --accent-ink: #0C1220;
    --piece-empty: #25303B;
    --piece-moving: #3E56A8;
    --piece-done: #3CC48F;
    --danger: #FF8A80;
    --pause: #F2B84B;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--ink);
  font: 400 1rem/1.5 var(--font);
  font-variant-numeric: tabular-nums;
}

button, input, textarea { font: inherit; color: inherit; }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

[hidden] { display: none !important; }

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* ---------- Icons ---------- */

.sprite { position: absolute; width: 0; height: 0; overflow: hidden; }

.icon {
  width: 1.125rem;
  height: 1.125rem;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.75;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ---------- Kopfzeile ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  max-width: 40rem;
  margin: 0 auto;
  padding: 1rem 1rem 0;
}

.wordmark {
  display: inline-flex;
  align-items: center;
  gap: .5rem;
  font-weight: 600;
  font-size: 1.0625rem;
  letter-spacing: -.01em;
  color: var(--ink);
  text-decoration: none;
}
.wordmark svg { width: 1.5rem; height: 1.5rem; fill: var(--piece-done); }
.wordmark svg .lit { fill: var(--accent); }

.device-name {
  border: 0;
  background: none;
  padding: .25rem .5rem;
  border-radius: var(--radius-s);
  color: var(--muted);
  font-size: .875rem;
  cursor: pointer;
  transition: color var(--dur) var(--ease), background-color var(--dur) var(--ease);
}
.device-name:hover { color: var(--ink); background: var(--accent-soft); }
.device-name::before { content: "Dieses Gerät: "; }

main {
  max-width: 40rem;
  margin: 0 auto;
  padding: 2.5rem 1rem 4rem;
}

/* ---------- Buttons & Felder ---------- */

.btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .4rem;
  min-height: 2.75rem;
  padding: 0 1.125rem;
  border: 1px solid var(--line);
  border-radius: var(--radius-s);
  background: var(--surface);
  font-weight: 500;
  text-decoration: none;
  cursor: pointer;
  white-space: nowrap;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition:
    background-color var(--dur) var(--ease),
    border-color var(--dur) var(--ease),
    color var(--dur) var(--ease),
    box-shadow var(--dur) var(--ease),
    transform var(--dur-fast) var(--ease);
}
.btn:hover { border-color: var(--muted); }
/* Drücken fühlt sich nach Drücken an: kurz einsinken, beim Loslassen zurückfedern. */
.btn:active:not(:disabled) { transform: scale(.97); }
.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}
.btn.primary:hover {
  background: color-mix(in srgb, var(--accent) 88%, var(--ink));
  border-color: color-mix(in srgb, var(--accent) 88%, var(--ink));
  box-shadow: 0 4px 14px -6px color-mix(in srgb, var(--accent) 70%, transparent);
}
.btn.small { min-height: 2.25rem; padding: 0 .75rem; font-size: .875rem; }
.btn.small .icon { width: 1rem; height: 1rem; }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.ghost:hover { color: var(--ink); background: var(--accent-soft); }
.btn.danger { color: var(--danger); }
.btn:disabled { cursor: default; opacity: .6; }

/* Bestätigung nach Kopieren/Speichern: kurz grün mit Häkchen. */
.btn.is-done,
.btn.is-done:hover {
  color: var(--piece-done);
  border-color: color-mix(in srgb, var(--piece-done) 45%, transparent);
  background: color-mix(in srgb, var(--piece-done) 10%, var(--surface));
  box-shadow: none;
}
.btn.is-done .icon { animation: pop var(--dur) var(--ease); }

/* Wartet auf den Server: kleiner Drehkreis vor dem Text. */
.btn.is-loading::before {
  content: "";
  width: .875rem;
  height: .875rem;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin .7s linear infinite;
}

input[type="text"], input:not([type]), textarea {
  width: 100%;
  min-height: 2.75rem;
  padding: .5rem .75rem;
  border: 1px solid var(--line);
  border-radius: var(--radius-s);
  background: var(--surface);
  transition: border-color var(--dur) var(--ease), box-shadow var(--dur) var(--ease);
}
input:focus-visible, textarea:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent);
}
input[aria-invalid="true"] { border-color: var(--danger); }

.error { color: var(--danger); margin: 1rem 0 0; }

/* ---------- Start ---------- */

#start h1 {
  margin: 0;
  font-size: clamp(2.25rem, 7vw, 3.25rem);
  line-height: 1.05;
  font-weight: 600;
  letter-spacing: -.03em;
  max-width: 12ch;
}

/* Eine Datei in Teilen: füllt sich einmal beim Laden, wie bei einer echten Übertragung. */
.intro-pieces {
  display: flex;
  gap: 4px;
  margin-top: 1.5rem;
}
.intro-pieces span {
  width: 18px;
  height: 10px;
  border-radius: 2px;
  background: var(--piece-empty);
  animation: fill-piece 1.1s var(--ease) both;
  animation-delay: calc(var(--i, 0) * 55ms + 250ms);
}
.intro-pieces span:nth-child(2) { --i: 1; }
.intro-pieces span:nth-child(3) { --i: 2; }
.intro-pieces span:nth-child(4) { --i: 3; }
.intro-pieces span:nth-child(5) { --i: 4; }
.intro-pieces span:nth-child(6) { --i: 5; }
.intro-pieces span:nth-child(7) { --i: 6; }
.intro-pieces span:nth-child(8) { --i: 7; }
.intro-pieces span:nth-child(9) { --i: 8; }
.intro-pieces span:nth-child(10) { --i: 9; }
.intro-pieces span:nth-child(11) { --i: 10; }
.intro-pieces span:nth-child(12) { --i: 11; }

.lede {
  margin: 1.25rem 0 2.5rem;
  max-width: 34rem;
  color: var(--muted);
  font-size: 1.0625rem;
}

.start-actions {
  display: grid;
  gap: 2rem;
  justify-items: start;
}

.join { width: 100%; max-width: 22rem; }
.join label { display: block; margin-bottom: .375rem; font-size: .875rem; color: var(--muted); }
.join-row { display: flex; gap: .5rem; }
#join-code {
  font-family: var(--mono);
  font-size: 1.25rem;
  letter-spacing: .12em;
  text-transform: uppercase;
}
#join-code::placeholder { color: var(--line); }
#join-code.shake { animation: shake .32s var(--ease); }

/* ---------- Session ---------- */

.session {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1.5rem;
  padding-bottom: 1.5rem;
  border-bottom: 1px solid var(--line);
}

.session-code .label {
  display: block;
  font-size: .875rem;
  color: var(--muted);
  transition: color var(--dur) var(--ease);
}
.session-code .label.is-done { color: var(--piece-done); }

/* Der Code ist anklickbar und kopiert sich selbst. */
#code {
  display: block;
  margin: .125rem 0 .875rem -.375rem;
  padding: 0 .375rem;
  border: 0;
  border-radius: var(--radius-s);
  background: none;
  font-family: var(--mono);
  font-size: clamp(2.5rem, 11vw, 3.75rem);
  font-weight: 500;
  line-height: 1.1;
  letter-spacing: .06em;
  cursor: copy;
  touch-action: manipulation;
  transition: background-color var(--dur) var(--ease), transform var(--dur-fast) var(--ease);
}
#code:hover { background: var(--accent-soft); }
#code:active { transform: scale(.98); }

.session-actions { display: flex; flex-wrap: wrap; gap: .5rem; }

.qr {
  flex: none;
  width: 112px;
  height: 112px;
  padding: 8px;
  background: #FFFFFF;
  border-radius: var(--radius-s);
}

/* ---------- Verbundene Geräte ---------- */

.peers { padding: 1.25rem 0; }
.peers-empty { margin: 0; color: var(--muted); }
#peer-list { list-style: none; margin: 0; padding: 0; display: grid; gap: .5rem; }
#peer-list li { display: flex; align-items: baseline; flex-wrap: wrap; gap: .25rem .625rem; }
#peer-list li.enter { animation: rise var(--dur) var(--ease); }
.peer-dot {
  position: relative;
  width: .5rem; height: .5rem;
  border-radius: 50%;
  background: var(--piece-moving);
  flex: none;
  transform: translateY(-.1em);
}
/* Verbindet noch: Punkt pulsiert. Gerade verbunden: ein einzelner Ring als Bestätigung. */
.peer-connecting .peer-dot { animation: breathe 1.2s ease-in-out infinite; }
.peer-ok .peer-dot { background: var(--piece-done); }
.peer-ok.just-connected .peer-dot::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 2px solid var(--piece-done);
  animation: ping .7s var(--ease) both;
}
.peer-failed .peer-dot { background: var(--danger); }
.peer-name { font-weight: 500; }
.peer-state { color: var(--muted); font-size: .875rem; }

/* ---------- Senden ---------- */

.send { display: grid; gap: .75rem; transition: opacity var(--dur) var(--ease); }
.send.disabled { opacity: .45; pointer-events: none; }

.drop {
  display: grid;
  place-items: center;
  gap: .125rem;
  padding: 1.75rem 1rem;
  border: 1.5px dashed var(--line);
  border-radius: var(--radius-l);
  background: var(--surface);
  text-align: center;
  cursor: pointer;
  transition: border-color var(--dur) var(--ease), background-color var(--dur) var(--ease);
}
.drop .drop-icon {
  width: 1.75rem;
  height: 1.75rem;
  margin-bottom: .375rem;
  color: var(--accent);
  transition: transform var(--dur) var(--ease);
}
.drop:hover, .drop:focus-within, .drop.is-over {
  border-color: var(--accent);
  background: var(--accent-soft);
}
.drop:hover .drop-icon, .drop.is-over .drop-icon { transform: translateY(-3px); }
.drop input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.drop strong { font-weight: 600; color: var(--accent); }
.drop span { color: var(--muted); font-size: .875rem; }

.text-send { display: flex; gap: .5rem; align-items: flex-end; }
.text-send textarea { resize: vertical; max-height: 12rem; }

.dragover {
  position: fixed;
  inset: .75rem;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: .5rem;
  border: 2px dashed var(--accent);
  border-radius: var(--radius-l);
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: blur(3px);
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--accent);
  pointer-events: none;
  z-index: 10;
  animation: fade-in var(--dur-fast) var(--ease);
}
.dragover .icon { width: 2.5rem; height: 2.5rem; }

/* ---------- Übertragungen ---------- */

.transfers { list-style: none; margin: 1.5rem 0 0; padding: 0; display: grid; gap: .75rem; }

.transfer {
  padding: .875rem 1rem 1rem;
  background: var(--surface);
  border-radius: var(--radius-l);
}
.transfer.enter { animation: rise var(--dur) var(--ease); }

.t-head { display: flex; align-items: center; gap: .75rem; }
.t-dir {
  flex: none;
  width: 2rem; height: 2rem;
  display: grid; place-items: center;
  border-radius: 50%;
  background: var(--piece-empty);
  color: var(--ink);
  transition: background-color var(--dur) var(--ease), color var(--dur) var(--ease);
}
.t-dir .icon { width: 1rem; height: 1rem; }
.t-dir.is-done { background: var(--piece-done); color: var(--surface); }
.t-dir.is-done .icon { animation: pop var(--dur) var(--ease); }
.t-dir.is-paused { background: color-mix(in srgb, var(--pause) 18%, var(--surface)); color: var(--pause); }
.t-dir.is-failed { background: color-mix(in srgb, var(--danger) 14%, var(--surface)); color: var(--danger); }
.t-meta { flex: 1; min-width: 0; display: grid; }
.t-name { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.t-sub { color: var(--muted); font-size: .8125rem; }
.t-actions { display: flex; gap: .375rem; flex: none; }

.t-text {
  margin: .625rem 0 0;
  padding: .625rem .75rem;
  background: var(--bg);
  border-radius: var(--radius-s);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  max-height: 14rem;
  overflow: auto;
}
.t-text a { color: var(--accent); }

/* Die Datei als Kette ihrer Teile: jedes Kästchen ist ein Stück der Datei,
   grün wird es erst, wenn seine Prüfsumme beim Empfänger stimmt. */
.pieces {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(10px, 1fr));
  gap: 3px;
  margin-top: .75rem;
}
.pieces:empty { display: none; }
.piece {
  height: 10px;
  border-radius: 2px;
  background: var(--piece-empty);
  transition: background-color .25s var(--ease);
}
.piece.moving { background: var(--piece-moving); }
.piece.done { background: var(--piece-done); animation: pop .3s var(--ease); }
.transfer.paused .piece.moving { background: color-mix(in srgb, var(--pause) 45%, var(--piece-empty)); }
.transfer.failed .piece.moving { background: var(--danger); }

/* ---------- Einstellungen ---------- */

.toggle {
  display: flex;
  align-items: center;
  gap: .5rem;
  margin-top: 2rem;
  color: var(--muted);
  font-size: .875rem;
  cursor: pointer;
}
.toggle input { width: 1rem; height: 1rem; accent-color: var(--accent); }

/* ---------- Bewegung ---------- */

@keyframes spin { to { transform: rotate(1turn); } }
@keyframes pop { 0% { transform: scale(.6); } 60% { transform: scale(1.12); } 100% { transform: scale(1); } }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
@keyframes fade-in { from { opacity: 0; } }
@keyframes breathe { 50% { opacity: .35; } }
@keyframes ping { from { transform: scale(1); opacity: .9; } to { transform: scale(3); opacity: 0; } }
@keyframes shake { 20%, 60% { transform: translateX(-4px); } 40%, 80% { transform: translateX(4px); } }
@keyframes fill-piece {
  0% { background: var(--piece-empty); }
  45% { background: var(--piece-moving); }
  100% { background: var(--piece-done); }
}

/* ---------- Schmal ---------- */

@media (max-width: 30rem) {
  main { padding-top: 1.75rem; }
  .session { flex-direction: column; }
  .device-name::before { content: ""; }
  .intro-pieces span { width: 14px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 1ms !important;
    animation-iteration-count: 1 !important;
    animation-delay: 0ms !important;
    transition-duration: 1ms !important;
  }
}
