:root {
  --fg: #1a1a1a;
  --muted: #666;
  --bg: #f6f6f6;
  --card: #fff;
  --border: #d4d4d4;
  --accent: #c33;
}

* { box-sizing: border-box; }

html, body { margin: 0; padding: 0; background: var(--bg); color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  line-height: 1.4; }

body.form-page {
  background-image: linear-gradient(rgba(0,0,0,0.35), rgba(0,0,0,0.35)),
                    url("images/bg/ifly-flying-styles.jpg");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  background-attachment: fixed;
  min-height: 100vh;
}
body.form-page main { margin-left: 0; padding-left: calc(2.5rem + 45px); }
body.form-page .draw-form {
  background: rgba(255, 255, 255, 0.65);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}

header {
  background: var(--fg);
  color: #fff;
  padding: 0.75rem 1.25rem;
  display: flex;
  align-items: baseline;
  gap: 0.6rem;
}
header a { color: #fff; text-decoration: none; font-weight: 600; }
header .app-version {
  font-size: 0.7rem;
  color: #999;
  font-variant-numeric: tabular-nums;
}

main { max-width: 1100px; margin: 0 auto; padding: 1.5rem 1.25rem 3rem; }

h1 { margin: 0 0 0.75rem; }
h2 { margin: 0 0 0.5rem; font-size: 1.1rem; color: var(--muted); }

.flash {
  list-style: none; padding: 0.75rem 1rem; margin: 0 0 1rem;
  background: #ffe6e6; border: 1px solid var(--accent); border-radius: 4px;
  color: var(--accent);
}

/* Form */
.draw-form {
  background: var(--card); border: 1px solid var(--border); border-radius: 8px;
  padding: 1.5rem; max-width: 480px; display: flex; flex-direction: column; gap: 1rem;
}
.draw-form .field { display: flex; flex-direction: column; gap: 0.35rem; }
.draw-form fieldset.field { border: none; padding: 0; margin: 0; }
.draw-form fieldset.field legend { padding: 0; margin-bottom: 0.35rem; font-weight: 600; }
.draw-form fieldset.field label { display: inline-flex; align-items: center; gap: 0.4rem; margin-right: 1rem; cursor: pointer; }
.draw-form .field.checkbox { flex-direction: row; align-items: center; gap: 0.5rem; cursor: pointer; }
.info {
  display: inline-flex; align-items: center; justify-content: center;
  width: 1.05rem; height: 1.05rem; border-radius: 50%;
  background: var(--border); color: var(--muted);
  font-size: 0.75rem; font-style: normal; cursor: help;
  user-select: none;
  position: relative;
  vertical-align: middle;
}
.info:hover, .info:focus { background: var(--fg); color: #fff; outline: none; }
.info::after {
  content: attr(data-tip);
  position: absolute;
  left: calc(100% + 0.5rem);
  top: 50%;
  transform: translateY(-50%);
  background: #1a1a1a;
  color: #fff;
  padding: 0.5rem 0.7rem;
  border-radius: 4px;
  font-size: 0.8rem;
  line-height: 1.35;
  width: 260px;
  white-space: normal;
  text-align: left;
  z-index: 10;
  opacity: 0;
  pointer-events: none;
  box-shadow: 0 2px 6px rgba(0,0,0,0.2);
}
.info:hover::after, .info:focus::after { opacity: 1; }
.draw-form .field > span { font-weight: 600; }
.draw-form input[type=number] {
  font: inherit; padding: 0.5rem; width: 6rem;
  border: 1px solid var(--border); border-radius: 4px;
}
.draw-form button {
  font: inherit; padding: 0.6rem 1.25rem; border: 0;
  background: var(--fg); color: #fff; border-radius: 4px; cursor: pointer;
}
.draw-form button:hover { background: var(--accent); }
.draw-form .form-buttons { display: flex; gap: 0.6rem; }
.draw-form button.secondary { background: #fff; color: var(--fg); border: 1px solid var(--fg); }
.draw-form button.secondary:hover { background: var(--fg); color: #fff; }

/* Result */
.draw-meta { margin-bottom: 1.5rem; }
.draw-meta p { color: var(--muted); margin: 0; }
.draw-meta code { background: #ececec; padding: 0.1rem 0.35rem; border-radius: 3px; }
.draw-meta .regen-link {
  display: inline-block; margin-left: 0.25rem; padding: 0 0.3rem;
  text-decoration: none; font-size: 1rem; line-height: 1;
  color: var(--muted); border-radius: 3px;
}
.draw-meta .regen-link:hover { color: var(--fg); background: #ececec; }

.round {
  background: var(--card); border: 1px solid var(--border); border-radius: 8px;
  padding: 1rem 1.25rem; margin-bottom: 1rem;
}
.round.tiebreaker { border-color: var(--accent); border-width: 2px; }
.round.tiebreaker h2 { color: var(--accent); }

.items {
  display: flex; gap: 1rem; flex-wrap: wrap; align-items: flex-start;
}

figure.item {
  margin: 0; background: #fff; border: 1px solid var(--border); border-radius: 4px;
  padding: 0.5rem; display: flex; flex-direction: column; align-items: center;
  width: 150px;
}
figure.item.block { width: 150px; }
figure.item.block.wide { width: 320px; }
figure.item img {
  display: block; max-width: 100%; height: auto;
}
figure.item figcaption {
  margin-top: 0.4rem; text-align: center; font-size: 0.85rem;
}
figure.item figcaption .code {
  display: block; font-weight: 700; font-size: 1rem;
}
figure.item figcaption .name {
  display: block; color: var(--muted); font-size: 0.8rem;
}

/* Builder */
.builder-meta { margin-bottom: 1rem; }
.builder-meta p { color: var(--muted); margin: 0.25rem 0; }
.builder-meta .builder-help { font-size: 0.9rem; }
.x-mark { display: inline-block; font-weight: 700; }

.builder {
  display: grid;
  grid-template-columns: 320px 1fr;
  gap: 1rem;
  align-items: start;
}
.builder .palette {
  background: var(--card); border: 1px solid var(--border); border-radius: 8px;
  padding: 0.75rem 1rem; position: sticky; top: 0.5rem; max-height: calc(100vh - 1rem);
  overflow-y: auto;
}
.builder .palette h2 { margin: 0 0 0.5rem; font-size: 0.95rem; }
.builder .palette h3 {
  margin: 0.75rem 0 0.4rem; font-size: 0.8rem; color: var(--muted);
  text-transform: uppercase; letter-spacing: 0.05em;
}
.builder .palette-grid {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 0.4rem;
}
.builder .rounds { display: flex; flex-direction: column; gap: 0.75rem; }

.round-box {
  background: var(--card); border: 1px solid var(--border); border-radius: 8px;
  padding: 0.75rem 1rem;
}
.round-box.tiebreaker { border-color: var(--accent); border-width: 2px; }
.round-box.tiebreaker .round-head h2 { color: var(--accent); }
.round-head {
  display: flex; justify-content: space-between; align-items: baseline;
  margin-bottom: 0.5rem;
}
.round-head h2 { margin: 0; font-size: 1rem; color: var(--fg); }
.round-total { font-size: 0.85rem; font-weight: 600; }
.round-total.ok { color: #2a7a2a; }
.round-total.low, .round-total.high { color: var(--accent); }

.round-zone {
  min-height: 110px; border: 2px dashed var(--border); border-radius: 6px;
  padding: 0.5rem; display: flex; gap: 0.4rem; flex-wrap: wrap; align-items: flex-start;
  transition: background 0.15s, border-color 0.15s;
}
.round-zone.drag-over { background: #f0f7ff; border-color: #6aa3e8; }
.round-zone.invalid-flash { background: #ffe6e6; border-color: var(--accent); }

.b-item {
  background: #fff; border: 1px solid var(--border); border-radius: 4px;
  padding: 0.3rem; width: 90px; text-align: center; cursor: grab;
  user-select: none; position: relative;
}
.b-item:active { cursor: grabbing; }
.b-item img { display: block; width: 100%; height: auto; }
.b-item .b-code { font-weight: 700; font-size: 0.85rem; margin-top: 0.2rem; }
.b-item .b-name { color: var(--muted); font-size: 0.7rem; line-height: 1.15; }
.b-item.used { opacity: 0.35; cursor: not-allowed; }

.b-item .remove-btn {
  position: absolute; top: -6px; right: -6px;
  width: 18px; height: 18px; border-radius: 50%;
  background: var(--accent); color: #fff; border: 0;
  font-size: 12px; line-height: 1; cursor: pointer;
  display: none;
}
.b-item.placed .remove-btn { display: block; }

.builder-footer {
  margin-top: 1.25rem; display: flex; align-items: center; gap: 1rem;
}
.builder-footer button {
  font: inherit; padding: 0.6rem 1.5rem; border: 0;
  background: var(--fg); color: #fff; border-radius: 4px; cursor: pointer;
}
.builder-footer button:hover:not(:disabled) { background: var(--accent); }
.builder-footer button:disabled { background: #aaa; cursor: not-allowed; }
.builder-status { color: var(--muted); font-size: 0.9rem; }

.mobile-block { display: none; }

@media (max-width: 800px) {
  .builder { grid-template-columns: 1fr; }
  .builder .palette { position: static; max-height: none; }

  /* Drag-and-drop is unusable with touch input; block the builder entirely
     on small screens and show an explanatory message instead. Same threshold
     also hides the form's Create button so users never get into this state. */
  .builder, .builder-footer, .builder-meta .builder-help { display: none; }
  .mobile-block {
    display: block;
    background: var(--card); border: 1px solid var(--border); border-radius: 8px;
    padding: 1rem 1.25rem; margin: 1rem 0;
  }
  .mobile-block p { margin: 0 0 0.5rem; }
  .mobile-block p:last-child { margin-bottom: 0; }
  .draw-form .form-buttons button.secondary { display: none; }
}

@media (max-width: 640px) {
  /* Tooltips on narrow viewports — the desktop placement overflows.
     Drop to a fixed bottom toast so they always fit regardless of icon
     position on the line. */
  .info::after {
    position: fixed;
    left: 0.75rem;
    right: 0.75rem;
    bottom: 1rem;
    top: auto;
    width: auto;
    max-width: none;
    transform: none;
  }

  main { padding: 0.75rem 0.5rem 1.5rem; }
  .round { padding: 0.5rem 0.6rem; }
  .items {
    display: grid;
    grid-template-columns: repeat(var(--max-round-size, 4), 1fr);
    gap: 0.3rem;
    justify-content: start;
  }
  figure.item, figure.item.block {
    width: auto; min-width: 0;
    padding: 0.25rem;
  }
  figure.item.block.wide { grid-column: span 2; }
  figure.item img { width: 100%; max-width: none; }
  figure.item figcaption { margin-top: 0.2rem; }
  figure.item figcaption .code { font-size: 0.8rem; }
  figure.item figcaption .name { font-size: 0.65rem; line-height: 1.2; }
}

@media print {
  @page {
    size: letter portrait;
    margin: 0.6in 0.5in 0.5in 0.5in;
    @top-center {
      font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
      font-size: 9pt;
      color: #555;
      padding-bottom: 0.15in;
    }
  }

  html, body { background: #fff; }
  header, .print-link { display: none; }
  main { max-width: none; padding: 0; }

  /* The on-screen meta block becomes the first page's H1 — the running header
     in @top-center supplies the same info on every subsequent page. */
  /* The running header in @top-center supplies discipline/class/seed/rounds
     on every page; suppress the on-screen meta block entirely in print. */
  .draw-meta { display: none; }

  .round {
    break-inside: avoid;
    page-break-inside: avoid;
    background: #fff;
    border: 1px solid #ccc;
    margin-bottom: 0.15in;
    padding: 0.1in 0.15in;
  }
  .round h2 { font-size: 11pt; margin: 0 0 0.05in; color: #333; }

  .items { gap: 0.1in; }
  figure.item { width: 130px; border-color: #ddd; padding: 0.05in; }
  figure.item figcaption { margin-top: 0.05in; }
  figure.item figcaption .code { font-size: 9pt; }
  figure.item figcaption .name { font-size: 7.5pt; }
}
