/* =====================================================================
 * BreezeMaxWeb Raffle — site stylesheet
 * Modern features used: nested CSS, container queries, range syntax
 * (`width >= ###`), accent-color, color-mix(), :focus-visible, :has().
 * ===================================================================== */

:root {
  /* === Brand tokens ============================================== */
  --bmw-blue: #38a1da;
  --bmw-blue-deep: #1f6f9b;
  --bmw-blue-dark: #0e2a3a;
  --bmw-blue-50: #eaf5fc;
  --bmw-blue-100: #d3eaf7;
  --bmw-blue-200: #a8d4ee;

  /* === Neutrals ================================================== */
  --ink: #0e2a3a;
  --ink-soft: #4d6577;
  --ink-mute: #7a8a96;
  --line: #cdd9e1;
  --line-soft: #e3eaef;
  --line-faint: #eef3f7;
  --bg: #f6fafd;
  --card: #ffffff;

  /* === Status ==================================================== */
  --danger: #c0392b;
  --danger-deep: #8a1f10;
  --danger-bg: #fde2dd;
  --success: #1e7d34;
  --success-bg: #d1f5d8;
  --warn: #8a5a00;
  --warn-bg: #fff3d1;

  /* === Shape & motion ============================================ */
  --radius-sm: 10px;
  --radius: 14px;
  --radius-lg: 20px;
  --shadow-sm: 0 1px 2px rgb(14 42 58 / 0.08);
  --shadow-md: 0 8px 24px rgb(14 42 58 / 0.12);
  --shadow-lg: 0 24px 64px rgb(14 42 58 / 0.18);
  --ring: 0 0 0 3px color-mix(in srgb, var(--bmw-blue) 35%, transparent);

  /* === Typography ================================================ */
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
               "Helvetica Neue", Arial, sans-serif;
}

/* =====================================================================
 * 1. Reset & element defaults
 * ===================================================================== */

*, *::before, *::after { box-sizing: border-box; }

html, body {
  margin: 0;
  font-family: var(--font-sans);
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

body { accent-color: var(--bmw-blue); }

img, svg { max-width: 100%; display: block; }

a {
  color: var(--bmw-blue-deep);
  text-underline-offset: 2px;

  &:hover { color: var(--bmw-blue); }
  &:focus-visible {
    outline: none;
    box-shadow: var(--ring);
    border-radius: 4px;
  }
}

button { font: inherit; cursor: pointer; }

h1, h2, h3, h4 { color: var(--bmw-blue-dark); line-height: 1.15; }
p { line-height: 1.5; }

/* =====================================================================
 * 2. Form elements (shared across public + admin)
 * ===================================================================== */

.field {
  display: block;
  margin: 12px 0;

  & > span {
    display: block;
    font-size: 0.92rem;
    color: var(--ink-soft);
    margin-bottom: 4px;

    & em { color: var(--danger); font-style: normal; }
  }
}

/* All textual inputs share one rule — including datetime-local, number,
   url, password, search, etc. The previous version omitted half of these. */
.field :is(
  input[type="text"],
  input[type="email"],
  input[type="tel"],
  input[type="url"],
  input[type="password"],
  input[type="number"],
  input[type="search"],
  input[type="date"],
  input[type="time"],
  input[type="datetime-local"],
  input:not([type])
),
textarea,
select {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  font-size: 1rem;
  font-family: inherit;
  background: #fff;
  color: var(--ink);
  min-height: 48px;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;

  &:hover { border-color: color-mix(in srgb, var(--bmw-blue) 35%, var(--line)); }

  &:focus-visible {
    outline: none;
    border-color: var(--bmw-blue);
    box-shadow: var(--ring);
  }

  &:disabled {
    background: var(--bg);
    color: var(--ink-mute);
    cursor: not-allowed;
  }

  &::placeholder { color: var(--ink-mute); }
}

textarea {
  min-height: 120px;
  resize: vertical;
  line-height: 1.45;
  font-size: 0.95rem;
}

/* Checkboxes & radios */
input[type="checkbox"],
input[type="radio"] {
  width: 18px;
  height: 18px;
  margin-right: 8px;
  vertical-align: middle;
  cursor: pointer;

  &:focus-visible { outline: 2px solid var(--bmw-blue); outline-offset: 2px; }
}

/* Consent-style row: checkbox + descriptive text aligned to top. */
.field.consent {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  margin-top: 16px;
  cursor: pointer;
  user-select: none;

  & input[type="checkbox"] {
    margin: 2px 0 0;
    flex-shrink: 0;
  }

  & span {
    margin-bottom: 0;
    color: var(--ink);
    font-size: 0.95rem;
    line-height: 1.4;
  }
}

/* When a label-with-checkbox is in a span (used by the contest form),
   keep the inputs visually inline with their text. */
.field > span:has(> input[type="checkbox"]) {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--ink);
  font-size: 0.95rem;
}

/* Two-column form rows. Use container queries so the form responds to its
   own card width, not the viewport. */
.row,
.field-group {
  display: grid;
  gap: 12px;
  grid-template-columns: 1fr;
}
@container (width >= 460px) { .row { grid-template-columns: 1fr 1fr; } }
@container (width >= 600px) { .field-group { grid-template-columns: 1fr 1fr; } }

.form-error {
  color: var(--danger);
  margin: 8px 0;
  font-weight: 500;

  &:empty { display: none; }
}

fieldset {
  border: 1px solid var(--line-soft);
  border-radius: var(--radius-sm);
  padding: 16px;
  margin: 20px 0 0;

  & > legend {
    padding: 0 6px;
    color: var(--ink-soft);
    font-weight: 600;
    font-size: 0.92rem;
    letter-spacing: 0.02em;
    text-transform: uppercase;
  }
}

/* Hint line under a field, used for variable lists and helper text. */
.field-hint {
  margin: 6px 0 0;
  font-size: 0.85rem;
  color: var(--ink-mute);
  line-height: 1.5;

  & code {
    background: var(--bmw-blue-50);
    color: var(--bmw-blue-deep);
    border: 1px solid var(--bmw-blue-100);
    border-radius: 4px;
    padding: 1px 6px;
    font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
    font-size: 0.8rem;
    margin-right: 2px;
    white-space: nowrap;
  }
}

/* Live HTML preview of email body fields */
.email-preview {
  margin-top: 10px;
  border: 1px solid var(--line-soft);
  border-radius: var(--radius-sm);
  background: var(--bg);
  overflow: hidden;

  & > summary {
    padding: 8px 12px;
    cursor: pointer;
    color: var(--ink-soft);
    font-size: 0.88rem;
    font-weight: 600;
    letter-spacing: 0.02em;
    user-select: none;
    list-style: none;
    display: flex;
    align-items: center;
    gap: 8px;

    &::-webkit-details-marker { display: none; }

    &::before {
      content: "▸";
      display: inline-block;
      transition: transform 0.15s ease;
      color: var(--bmw-blue);
    }
  }

  &[open] > summary::before { transform: rotate(90deg); }
  &[open] > summary { border-bottom: 1px solid var(--line-soft); }
}

.email-preview-frame {
  width: 100%;
  height: 300px;
  border: 0;
  background: #fff;
  display: block;
}

/* =====================================================================
 * 3. Buttons
 * ===================================================================== */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.85em 1.4em;
  border-radius: var(--radius);
  border: 0;
  font-weight: 600;
  font-size: 1rem;
  min-height: 48px;
  text-decoration: none;
  transition: transform 0.05s ease, background 0.15s ease, box-shadow 0.15s ease;

  &:active { transform: translateY(1px); }
  &:focus-visible { outline: none; box-shadow: var(--ring); }
  &:disabled,
  &[aria-disabled="true"] {
    cursor: not-allowed;
    opacity: 0.7;
  }

  &.btn-primary {
    background: var(--bmw-blue);
    color: #fff;
    &:hover { background: var(--bmw-blue-deep); }
    &:disabled { background: var(--bmw-blue-100); color: var(--ink-soft); opacity: 1; }
  }

  &.btn-ghost {
    background: transparent;
    color: var(--bmw-blue-deep);
    border: 1px solid var(--bmw-blue-100);
    &:hover {
      background: var(--bmw-blue-50);
      border-color: var(--bmw-blue-200);
    }
  }

  &.btn-danger,
  &.danger-btn {
    background: var(--danger);
    color: #fff;
    &:hover { background: var(--danger-deep); }
  }

  &.btn-block { width: 100%; }

  &.btn-sm {
    padding: 6px 12px;
    min-height: 32px;
    font-size: 0.85rem;
    border-radius: var(--radius-sm);
  }
}

.actions {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  margin-top: 20px;
}

/* =====================================================================
 * 4. Layout primitives & utilities
 * ===================================================================== */

.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  padding: 24px;
  margin: 12px 0;
  container-type: inline-size;

  & > h2:first-child,
  & > h3:first-child,
  & > h1:first-child { margin-top: 0; }
}

/* Title + actions row — used on dashboard and contest detail. */
.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;

  & > .toolbar-actions {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
  }
}

.muted { color: var(--ink-mute); }
.muted-soft { color: var(--ink-soft); }
.no-wrap { white-space: nowrap; }
.stack-tight > * + * { margin-top: 6px; }

/* Tags / status pills */
.tag {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;

  &.tag-active { background: var(--success-bg); color: var(--success); }
  &.tag-pending { background: var(--warn-bg); color: var(--warn); }
  &.tag-drawn { background: var(--bmw-blue-100); color: var(--bmw-blue-deep); }
}

/* Flash messages */
.flash {
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  margin: 10px 0;
  font-weight: 500;
  border: 1px solid transparent;

  &.flash-error { background: var(--danger-bg); color: var(--danger-deep); border-color: color-mix(in srgb, var(--danger) 25%, transparent); }
  &.flash-success { background: var(--success-bg); color: var(--success); border-color: color-mix(in srgb, var(--success) 25%, transparent); }
}

/* =====================================================================
 * 5. Public submission shell (mobile-first form)
 * ===================================================================== */

body.page-public {
  background:
    radial-gradient(circle at 50% -120px, var(--bmw-blue-100), transparent 60%),
    linear-gradient(180deg, var(--bmw-blue-50) 0%, var(--bg) 320px);
  min-height: 100dvh;
}

.public-shell {
  max-width: 560px;
  margin: 0 auto;
  padding: 24px 16px 80px;
  container-type: inline-size;

  & .public-header {
    text-align: center;
    margin: 16px 0 24px;
  }

  & .public-logos {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: clamp(10px, 3cqi, 18px);
    margin: 0 auto 12px;
  }

  & .public-logos-divider {
    display: block;
    align-self: stretch;
    width: 1px;
    background: var(--ink-soft, rgb(14 42 58 / 0.2));
    opacity: 0.4;
  }

  & .public-logo {
    width: clamp(64px, 18cqi, 96px);
    height: clamp(64px, 18cqi, 96px);
    object-fit: contain;
    filter: drop-shadow(0 6px 12px rgb(14 42 58 / 0.12));

    &:only-child {
      width: 96px;
      height: 96px;
    }
  }

  & .public-logo-partner {
    width: auto;
    height: clamp(56px, 18cqi, 96px);
    max-width: clamp(140px, 58cqi, 340px);
  }

  & .public-title {
    font-size: clamp(1.6rem, 5cqi, 2rem);
    margin: 8px 0 4px;
  }

  & .public-desc {
    color: var(--ink-soft);
    margin: 4px 0 12px;
  }

  & .public-countdown {
    display: inline-block;
    padding: 10px 20px;
    border-radius: 999px;
    background: var(--bmw-blue);
    color: #fff;
    font-size: 1.25rem;
    font-weight: 600;
    letter-spacing: 0.02em;
    font-variant-numeric: tabular-nums;

    &:empty { display: none; }
  }

  & .view {
    /* Each view-* swaps in/out via [hidden]. */
    &[hidden] { display: none; }
  }
}

/* =====================================================================
 * 6. Drawing intermission (animated "Picking a winner…")
 * ===================================================================== */

.view-drawing,
.view-winner {
  position: relative;
  margin: 18px 0;
  text-align: center;
  padding: 0;
}

.drawing-card {
  background: linear-gradient(180deg, var(--bmw-blue), var(--bmw-blue-deep));
  color: #fff;
  border-radius: var(--radius);
  padding: 36px 24px;
  box-shadow: var(--shadow-md);
  overflow: hidden;
  position: relative;

  & h2 {
    margin: 8px 0 12px;
    font-size: 1.6rem;
    letter-spacing: 0.04em;
    color: #fff;
  }

  & .drawing-shuffle {
    font-size: 1.4rem;
    font-weight: 600;
    min-height: 1.5em;
    font-variant-numeric: tabular-nums;
    letter-spacing: 0.02em;
  }

  & .drawing-logo-pulse img {
    width: 72px;
    height: 72px;
    margin: 0 auto;
    animation: bmw-pulse 1.4s ease-in-out infinite;
  }
}

@keyframes bmw-pulse {
  0%, 100% { transform: scale(1); opacity: 0.95; }
  50% { transform: scale(1.12); opacity: 1; }
}

/* =====================================================================
 * 7. Winner reveal
 * ===================================================================== */

.winner-card {
  position: relative;
  background: #fff;
  border-radius: var(--radius);
  padding: 32px 24px;
  box-shadow: var(--shadow-md);
  border: 4px solid var(--bmw-blue);
  text-align: center;
  z-index: 2;

  & .kicker {
    text-transform: uppercase;
    font-size: 0.85rem;
    letter-spacing: 0.12em;
    color: var(--ink-soft);
    margin: 6px 0;
  }

  & .winner-name {
    font-size: clamp(2rem, 8cqi, 3.4rem);
    margin: 12px 0 6px;
    color: var(--bmw-blue-dark);
    animation: bmw-pop 0.5s ease-out;
  }

  & .winner-company {
    font-size: 1.25rem;
    color: var(--bmw-blue-deep);
    margin: 0 0 16px;
    font-weight: 600;

    &:empty { display: none; }
  }
}

.confetti {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  pointer-events: none;
  z-index: 1;
}

@keyframes bmw-pop {
  from { transform: scale(0.85); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
  .drawing-logo-pulse img,
  .winner-name {
    animation: none;
  }
}

/* =====================================================================
 * 8. TV display (16:9 fixed layout)
 * ===================================================================== */

body.page-tv {
  background: #000;
  margin: 0;
  overflow: hidden;
}

.tv-stage {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: linear-gradient(135deg, var(--bmw-blue-deep) 0%, var(--bmw-blue-dark) 100%);
  color: #fff;
}

.tv-canvas {
  width: 100vw;
  height: 100vh;
  aspect-ratio: 16 / 9;
  max-width: calc(100vh * 16 / 9);
  max-height: calc(100vw * 9 / 16);
  display: grid;
  grid-template-rows: auto 1fr auto;
  padding: 4vmin;
  position: relative;

  & .tv-header {
    display: flex;
    align-items: center;
    gap: 2vmin;

    & img { width: 8vmin; height: 8vmin; }
    & .brand {
      font-size: 2.4vmin;
      letter-spacing: 0.18em;
      text-transform: uppercase;
      opacity: 0.9;
    }
  }

  & .tv-main {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 4vmin;
    align-items: center;
    &[hidden] { display: none; }

    & h1 {
      font-size: clamp(3rem, 7vmin, 7rem);
      margin: 0 0 3vmin;
      line-height: 1.05;
      color: #fff;
    }

    & p {
      font-size: 3.4vmin;
      line-height: 1.35;
      opacity: 0.92;
      margin: 0 0 2vmin;
    }
  }

  & .tv-countdown {
    display: inline-block;
    font-size: 4.6vmin;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    background: rgb(255 255 255 / 0.16);
    padding: 1.2vmin 2.4vmin;
    border-radius: 1.6vmin;
    margin-top: 2vmin;
  }

  & .tv-qr {
    background: #fff;
    border-radius: 2vmin;
    padding: 3vmin;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2vmin;
    box-shadow: 0 2vmin 6vmin rgb(0 0 0 / 0.35);

    & svg { width: 38vmin; height: 38vmin; }
    & .scan-label {
      color: var(--bmw-blue-dark);
      font-weight: 700;
      font-size: 2.6vmin;
      letter-spacing: 0.06em;
      text-transform: uppercase;
    }
    & .url {
      color: var(--bmw-blue-deep);
      font-weight: 600;
      font-size: 2.4vmin;
      word-break: break-all;
    }
  }

  & .tv-footer {
    display: flex;
    justify-content: space-between;
    align-items: end;
    &[hidden] { display: none; }

    & .left, & .right {
      font-size: 2.2vmin;
      opacity: 0.75;
    }
  }
}

/* TV intermission/winner overlays (full-canvas) */
.tv-drawing,
.tv-winner {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  text-align: center;
  padding: 6vmin;

  /* Hidden attribute must override the explicit display above. */
  &[hidden] { display: none; }
}

.tv-drawing {
  background: linear-gradient(135deg, var(--bmw-blue), var(--bmw-blue-dark));

  & .pulse {
    width: 18vmin;
    height: 18vmin;
    margin: 0 auto 4vmin;
    animation: bmw-pulse 1.4s ease-in-out infinite;
  }

  & h2 {
    font-size: clamp(3rem, 9vmin, 8rem);
    margin: 2vmin 0;
    letter-spacing: 0.04em;
    color: #fff;
  }

  & .shuffle {
    font-size: clamp(2rem, 6vmin, 5rem);
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    min-height: 1.4em;
  }
}

.tv-winner {
  background: radial-gradient(ellipse at top, #fff 0%, var(--bmw-blue-100) 100%);
  color: var(--bmw-blue-dark);

  & .kicker {
    text-transform: uppercase;
    letter-spacing: 0.18em;
    opacity: 0.7;
    font-size: 3vmin;
  }
  & h2 {
    font-size: clamp(4rem, 12vmin, 14rem);
    margin: 2vmin 0;
    line-height: 1;
  }
  & .company {
    font-size: clamp(2.5rem, 6vmin, 6rem);
    color: var(--bmw-blue-deep);
    font-weight: 600;
  }
}

/* =====================================================================
 * 9. Admin
 * ===================================================================== */

body.page-admin { background: var(--bg); }

.admin-shell {
  max-width: 1100px;
  margin: 0 auto;
  padding: 24px 20px 80px;
  container-type: inline-size;

  & > h1 { color: var(--bmw-blue-dark); }
  & > h1:first-of-type { margin-top: 0; }
}

.admin-shell--narrow {
  max-width: 420px;
  padding-top: 64px;
}

.admin-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 24px;
  flex-wrap: wrap;

  & .brand {
    display: flex;
    align-items: center;
    gap: 12px;
    color: inherit;
    text-decoration: none;
    font-weight: 600;

    & img { width: 36px; height: 36px; }

    &:hover { color: var(--bmw-blue-deep); }
    &:focus-visible {
      outline: none;
      box-shadow: var(--ring);
      border-radius: 6px;
    }
  }

  & .admin-bar-right {
    display: flex;
    align-items: center;
    gap: 14px;
    flex-wrap: wrap;
  }

  & form { display: inline; }
}

.admin-grid {
  display: grid;
  gap: 24px;
  grid-template-columns: 1fr;
}
@container (width >= 800px) {
  .admin-grid { grid-template-columns: 1.4fr 1fr; }
}

/* Tables: scroll horizontally if they don't fit, instead of breaking the layout. */
.admin-table-wrap {
  overflow-x: auto;
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  background: #fff;
  margin: 16px 0;
  -webkit-overflow-scrolling: touch;
}

.admin-table {
  width: 100%;
  border-collapse: collapse;
  background: #fff;
  font-size: 0.95rem;

  & th, & td {
    padding: 10px 12px;
    text-align: left;
    border-bottom: 1px solid var(--line-faint);
    vertical-align: middle;
  }

  & th {
    background: var(--bmw-blue-50);
    color: var(--bmw-blue-dark);
    font-weight: 600;
    letter-spacing: 0.02em;
    position: sticky;
    top: 0;
    white-space: nowrap;
  }

  & tbody tr {
    transition: background 0.1s ease;
    &:hover { background: var(--bmw-blue-50); }
  }
  & tr:last-child td { border-bottom: 0; }

  & a strong { color: var(--bmw-blue-dark); }
  & a:hover strong { color: var(--bmw-blue); }
}

/* When the wrapper isn't used (back-compat), keep table tidy. */
.admin-table:not(.admin-table-wrap > .admin-table) {
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}

/* Slug / secondary identifier under a heading */
.slug {
  color: var(--ink-mute);
  font-size: 0.9rem;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

.slug a {
  color: inherit;
  text-decoration: none;
}

.slug a:hover {
  text-decoration: underline;
}

/* Stat list inside a card */
.kv-list {
  list-style: none;
  padding: 0;
  margin: 8px 0 0;

  & li {
    padding: 4px 0;
    border-bottom: 1px solid var(--line-faint);
    display: flex;
    justify-content: space-between;
    gap: 12px;
    font-size: 0.95rem;

    &:last-child { border-bottom: 0; }

    & strong { color: var(--ink); font-weight: 600; }
    & span { color: var(--ink-soft); text-align: right; }
  }
}

/* =====================================================================
 * 10. Error pages
 * ===================================================================== */

body.page-error {
  background: linear-gradient(180deg, var(--bmw-blue-50), var(--bg) 320px);
  min-height: 100dvh;
}
