@layer base, components, states;

@layer base {
  :root {
    color-scheme: light;
    font-family: "SF Pro Display", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    background-color: #0d1117;
    color: #0f1a2a;
    --highlight: #f7b500;
    --accent: #ffce44;
    --accent-dark: #c78600;
    --surface: #ffffff;
    --surface-glass: rgba(255, 255, 255, 0.75);
    --surface-muted: rgba(15, 26, 42, 0.7);
    --success: #2ecc71;
    --danger: #ff4d4f;
    --warning: #f39c12;
    --shadow: 0 20px 40px rgba(15, 26, 42, 0.15);
  }

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

  body {
    margin: 0;
    min-height: 100dvh;
    height: 100dvh;
    overflow: hidden;
    background-color: #030712;
    background-image: url("img/background.png");
    background-repeat: no-repeat;
    background-position: center center;
    background-size: cover;
    background-attachment: fixed;
    color: #0f1a2a;
    display: flex;
    align-items: stretch;
    justify-content: center;
    -webkit-font-smoothing: antialiased;
  }

  img {
    max-width: 100%;
    height: auto;
    display: block;
  }

  button,
  input {
    font: inherit;
  }

  .hidden {
    display: none !important;
  }
}

@layer components {
  .start-screen {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px 20px;
    z-index: 100;
    background: linear-gradient(
      180deg,
      rgba(3, 7, 18, 0.3) 0%,
      rgba(3, 7, 18, 0.2) 50%,
      rgba(3, 7, 18, 0.35) 100%
    );
  }

  .start-content {
    width: 100%;
    max-width: 480px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 32px;
    text-align: center;
  }

  .start-logo {
    width: clamp(220px, 60vw, 360px);
    max-width: 100%;
    height: auto;
    filter:
      drop-shadow(0 12px 25px rgba(0, 0, 0, 0.35))
      drop-shadow(0 4px 10px rgba(0, 0, 0, 0.25));
    animation: logoGlow 2.6s ease-in-out infinite alternate;
  }

  @keyframes logoGlow {
    from {
      filter:
        drop-shadow(0 12px 25px rgba(0, 0, 0, 0.35))
        drop-shadow(0 4px 10px rgba(0, 0, 0, 0.25))
        drop-shadow(0 0 18px rgba(255, 206, 68, 0.4));
    }
    to {
      filter:
        drop-shadow(0 12px 25px rgba(0, 0, 0, 0.35))
        drop-shadow(0 4px 10px rgba(0, 0, 0, 0.25))
        drop-shadow(0 0 30px rgba(255, 206, 68, 0.6));
    }
  }

  .start-instructions {
    display: flex;
    flex-direction: column;
    gap: 20px;
    padding: 28px 24px;
    background: rgba(255, 255, 255, 0.95);
    border-radius: 24px;
    box-shadow:
      0 16px 48px rgba(0, 0, 0, 0.25),
      0 4px 12px rgba(0, 0, 0, 0.15),
      inset 0 1px 0 rgba(255, 255, 255, 0.9);
    backdrop-filter: blur(12px);
    width: 100%;
  }

  .instruction-text {
    margin: 0;
    font-size: clamp(16px, 4vw, 18px);
    font-weight: 600;
    line-height: 1.5;
    color: #0f1a2a;
    letter-spacing: 0.02em;
  }

  .start-button {
    appearance: none;
    border: none;
    border-radius: 999px;
    padding: 20px 64px;
    font-size: clamp(22px, 5.5vw, 28px);
    font-weight: 800;
    letter-spacing: 0.08em;
    color: #ffffff;
    background: linear-gradient(135deg, #119447 0%, #18b85a 55%, #0f7d3c 100%);
    background-size: 220% 100%;
    box-shadow:
      0 10px 28px rgba(17, 148, 71, 0.45),
      0 4px 12px rgba(0, 0, 0, 0.25),
      inset 0 2px 0 rgba(255, 255, 255, 0.25),
      inset 0 -2px 0 rgba(0, 0, 0, 0.2);
    text-transform: uppercase;
    transition: transform 150ms ease, box-shadow 150ms ease, background-position 300ms ease;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    animation: buttonShine 3s ease-in-out infinite;
  }

  @keyframes buttonShine {
    0%, 100% {
      background-position: 0% 50%;
    }
    50% {
      background-position: 100% 50%;
    }
  }

  .start-button::before {
    content: "";
    position: absolute;
    inset: 0;
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.3) 0%, transparent 50%);
    opacity: 0;
    transition: opacity 200ms ease;
  }

  .start-button:hover::before {
    opacity: 1;
  }

  .start-button:active {
    transform: scale(0.96);
    box-shadow:
      0 6px 18px rgba(17, 148, 71, 0.5),
      0 2px 6px rgba(0, 0, 0, 0.2),
      inset 0 2px 0 rgba(255, 255, 255, 0.3);
  }

  .game {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 100dvh;
    background: rgba(255, 255, 255, 0.08);
    backdrop-filter: blur(12px);
    box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.04);
  }

  .game-hud {
    display: flex;
    justify-content: center;
    align-items: stretch;
    gap: 12px;
    padding: 18px 24px;
    width: min(100%, 420px);
    margin: 0 auto;
    flex-wrap: nowrap;
    color: #ffffff;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    font-size: 14px;
    position: relative;
    z-index: 2;
  }

  .hud-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    padding: 12px 18px;
    border-radius: 20px;
    min-width: 0;
    flex: 1 1 0;
    background: rgba(3, 7, 18, 0.65);
    border: 1px solid rgba(255, 255, 255, 0.12);
    box-shadow:
      0 8px 20px rgba(0, 0, 0, 0.35),
      inset 0 1px 0 rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(12px);
  }

  .hud-label {
    opacity: 0.8;
    font-size: 11px;
    letter-spacing: 0.2em;
  }

  .hud-value {
    font-size: 30px;
    font-weight: 800;
    letter-spacing: 0.04em;
    text-shadow: 0 3px 10px rgba(0, 0, 0, 0.35);
  }

  .hud-item:first-child .hud-value {
    color: #ffffff;
  }

  .hud-item:last-child .hud-value {
    color: #ffffff;
  }

  .game-stage {
    position: relative;
    flex: 1;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 32px 24px 24px;
  }

  .fill-meter {
    position: absolute;
    left: 24px;
    top: 50%;
    transform: translateY(-50%);
    display: flex;
    align-items: center;
    gap: 12px;
    color: #ffffff;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.08em;
  }

  .fill-meter-track {
    width: 15px;
    height: 260px;
    border-radius: 999px;
    background: rgba(15, 26, 42, 0.1);
    overflow: hidden;
    position: relative;
    box-shadow: inset 0 0 0 1px rgba(15, 26, 42, 0.08);
  }

  .fill-meter-progress {
    width: 100%;
    height: 0%;
    position: absolute;
    left: 0;
    bottom: 0;
    background: linear-gradient(180deg, var(--accent) 0%, var(--highlight) 100%);
    border-radius: inherit;
    transition: height 80ms linear;
  }

  .fill-meter-target {
    writing-mode: vertical-rl;
    transform: rotate(180deg);
    font-size: 12px;
    opacity: 0.6;
  }

  .crane {
    position: absolute;
    /* top будет устанавливаться через JavaScript */
    left: 46%;
    transform: translateX(-50%);
    width: clamp(45px, 8.75vw, 74px);
    transition: transform 80ms linear, top 100ms ease;
    pointer-events: none;
  }

  @media (max-width: 790px) {
    .crane {
      left: 44%;
    }
  }

  @media (max-width: 640px) {
    .crane {
      left: 40%;
      width: clamp(45px, 8.75vw, 120px);
      /* display: none; */
    }
  }

  .belt {
    position: relative;
    width: min(100%, 420px);
    height: clamp(220px, 42vh, 360px);
    border-radius: 36px;
    background: rgba(255, 255, 255, 0.12);
    box-shadow:
      0 24px 60px rgba(5, 10, 20, 0.35),
      inset 0 0 0 1px rgba(255, 255, 255, 0.18);
    display: flex;
    align-items: flex-end;
    justify-content: center;
    padding: 24px;
  }

  .belt-texture {
    position: absolute;
    inset: auto 16px 16px;
    width: calc(100% - 32px);
    height: auto;
    pointer-events: none;
    opacity: 0.4;
  }

  .container-slot {
    position: relative;
    width: 60%;
    height: 80%;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: visible;
  }

  .cargo {
    position: absolute;
    width: clamp(80px, 28vw, 140px);
    transition:
      transform 900ms cubic-bezier(0.25, 0.9, 0.3, 1.2),
      opacity 320ms ease;
    will-change: transform;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
  }

  .cargo img {
    width: 100%;
  }

  .cargo-label {
    font-size: 14px;
    font-weight: 600;
    letter-spacing: 0.04em;
    color: rgba(15, 26, 42, 0.72);
    text-align: center;
  }

  .cargo.enter {
    transform: translateX(140%);
    opacity: 0;
  }

  .cargo.centered {
    transform: translateX(0);
    opacity: 1;
  }

  .cargo.leaving {
    transform: translateX(-160%);
    opacity: 0;
  }

  .cargo.success img {
    animation: bounce 480ms ease;
  }

  .cargo.fail img {
    animation: wobble 520ms ease;
  }

  .cargo.burst::after {
    content: "";
    position: absolute;
    inset: -12px;
    background: radial-gradient(circle, rgba(255, 77, 79, 0.3), transparent 60%);
    opacity: 0;
    animation: burst 480ms ease;
  }

  .status-message {
    position: absolute;
    bottom: clamp(16px, 4.8vh, 38px);
    left: 50%;
    transform: translateX(-50%);
    width: min(90%, 420px);
    padding: 16px 32px;
    border-radius: 999px;
    background: rgba(15, 26, 42, 0.72);
    color: #fff;
    font-size: 14px;
    letter-spacing: 0.04em;
    text-align: center;
    opacity: 0;
    pointer-events: none;
    transition: opacity 200ms ease, transform 200ms ease;
  }

  .status-message.visible {
    opacity: 1;
    transform: translateX(-50%) translateY(-6px);
  }

  .status-message[data-tone="success"] {
    background: rgba(46, 204, 113, 0.9);
  }

  .status-message[data-tone="warning"] {
    background: rgba(243, 156, 18, 0.9);
  }

  .status-message[data-tone="danger"] {
    background: rgba(255, 77, 79, 0.9);
  }

  .controls {
    padding: 24px;
    display: flex;
    justify-content: center;
  }

  .fill-button {
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
    appearance: none;
    border: none;
    border-radius: 24px;
    padding: 18px 64px;
    font-size: 18px;
    font-weight: 600;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: #fff;
    background:
      linear-gradient(135deg, rgba(255, 255, 255, 0.2), rgba(255, 255, 255, 0)) border-box,
      linear-gradient(135deg, #dd1f26, #ad151b) border-box;
    box-shadow:
      inset 0 1px 0 rgba(255, 255, 255, 0.25),
      0 18px 32px rgba(221, 31, 38, 0.45);
    transition: transform 120ms ease, box-shadow 120ms ease;
    touch-action: none;
  }

  .fill-button:disabled {
    opacity: 0.5;
    background:
      linear-gradient(135deg, rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 0)) border-box,
      linear-gradient(135deg, #666666, #4a4a4a) border-box;
    box-shadow:
      inset 0 1px 0 rgba(255, 255, 255, 0.1),
      0 8px 16px rgba(0, 0, 0, 0.25);
    cursor: not-allowed;
    pointer-events: none;
  }

  .fill-button:active,
  .fill-button.is-pressed {
    transform: scale(0.96);
    box-shadow:
      inset 0 1px 0 rgba(255, 255, 255, 0.25),
      0 10px 22px rgba(173, 21, 27, 0.5);
  }

  .end-screen {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: normal;
    justify-content: center;
    background: rgba(15, 26, 42, 0.6);
    backdrop-filter: blur(10px);
    padding: 15px;
  }

  .end-card {
    width: min(420px, 100%);
    background: var(--surface);
    border-radius: 28px;
    padding: 26px 20px;
    display: flex;
    flex-direction: column;
    gap: 24px;
    overflow-y: auto;  /* вертикальная прокрутка */
    overflow-x: hidden;
    scrollbar-width: none; /* Firefox: скрыть скроллбар */
    -ms-overflow-style: none;
    box-shadow: var(--shadow);
  }

  .end-title {
    margin: 0;
    font-size: 26px;
    text-align: center;
    letter-spacing: 0.04em;
  }
  
  .end-score {
    margin: 0;
    text-align: center;
    font-size: 16px;
    font-weight: 600;
  }

  .result-form {
    display: flex;
    flex-direction: column;
    gap: 16px;
  }

  .success-message {
    padding: 20px;
    text-align: center;
    border-radius: 16px;
    background: rgba(46, 204, 113, 0.1);
    border: 1px solid rgba(46, 204, 113, 0.3);
  }

  .success-message p {
    margin: 0;
    font-size: 16px;
    font-weight: 600;
    color: #2ecc71;
    letter-spacing: 0.02em;
  }

  .error-message {
    padding: 20px;
    text-align: center;
    border-radius: 16px;
    background: rgba(255, 77, 79, 0.1);
    border: 1px solid rgba(255, 77, 79, 0.3);
  }

  .error-message p {
    margin: 0;
    font-size: 16px;
    font-weight: 600;
    color: #ff4d4f;
    letter-spacing: 0.02em;
  }

  .field {
    display: flex;
    flex-direction: column;
    gap: 8px;
    font-size: 14px;
    letter-spacing: 0.02em;
  }

  .field input {
    padding: 14px 16px;
    border-radius: 16px;
    border: 1px solid rgba(15, 26, 42, 0.12);
    background: rgba(15, 26, 42, 0.03);
    font-size: 16px;
  }

  .field input:focus-visible {
    outline: 3px solid rgba(76, 133, 255, 0.35);
    outline-offset: 2px;
  }

  .primary-button,
  .secondary-button {
    appearance: none;
    border-radius: 20px;
    padding: 16px;
    font-size: 16px;
    font-weight: 600;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    border: none;
    transition: transform 120ms ease, box-shadow 120ms ease;
  }

  .primary-button {
    background: linear-gradient(135deg, #119447, #0d6f35);
    color: #ffffff;
    box-shadow:
      inset 0 1px 0 rgba(255, 255, 255, 0.3),
      0 14px 24px rgba(17, 148, 71, 0.35);
  }

  .primary-button:active {
    transform: scale(0.97);
    box-shadow:
      inset 0 1px 0 rgba(255, 255, 255, 0.3),
      0 8px 18px rgba(17, 148, 71, 0.45);
  }

  .secondary-button {
    background: rgba(221, 31, 38, 0.12);
    color: #dd1f26;
    border: 1px solid rgba(221, 31, 38, 0.4);
  }

  .secondary-button:active {
    transform: scale(0.97);
    background: rgba(221, 31, 38, 0.18);
  }

  .leaderboard {
    display: flex;
    flex-direction: column;
    gap: 12px;
  }

  .leaderboard h3 {
    margin: 0;
    text-align: center;
    letter-spacing: 0.04em;
  }

  .leaderboard-list {
    margin: 0;
    padding: 0;
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 10px;
  }

  .leaderboard-list li {
    background: rgba(15, 26, 42, 0.06);
    border-radius: 16px;
    padding: 12px 16px;
    display: flex;
    justify-content: space-between;
    font-size: 14px;
  }

  .leaderboard-list li.current-player {
    background: rgba(255, 206, 68, 0.15);
    border: 1px solid rgba(255, 206, 68, 0.3);
    font-weight: 600;
  }
}

@layer states {
  @keyframes bounce {
    0%,
    100% {
      transform: translateY(0);
    }
    30% {
      transform: translateY(-10px);
    }
    55% {
      transform: translateY(6px);
    }
    75% {
      transform: translateY(-4px);
    }
  }

  @keyframes wobble {
    0% {
      transform: rotate(0deg);
    }
    20% {
      transform: rotate(-4deg);
    }
    45% {
      transform: rotate(4deg);
    }
    65% {
      transform: rotate(-2deg);
    }
    100% {
      transform: rotate(0deg);
    }
  }

  @keyframes burst {
    0% {
      opacity: 0;
      transform: scale(0.8);
    }
    40% {
      opacity: 1;
      transform: scale(1.1);
    }
    100% {
      opacity: 0;
      transform: scale(1.4);
    }
  }

  @media (min-width: 480px) {
    .game-hud {
      padding: 24px 48px;
      font-size: 16px;
    }

    .fill-meter {
      left: 48px;
    }

    .belt {
      width: min(450px, 90%);
      height: clamp(280px, 48vh, 360px);
    }

    .container-slot {
      width: 60%;
    }
  }
}

