@layer hud;

@layer hud {
  #crosshair {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 20px;
    height: 20px;
    mix-blend-mode: difference;
    pointer-events: none;
    
    &::before, &::after {
      content: "";
      position: absolute;
      background: var(--color-crosshair);
    }
    
    &::before {
      top: 50%;
      left: 0;
      width: 100%;
      height: 2px;
      transform: translateY(-50%);
    }
    
    &::after {
      top: 0;
      left: 50%;
      width: 2px;
      height: 100%;
      transform: translateX(-50%);
    }
  }

  #inventory-bar, #hotbar {
    position: absolute;
    bottom: 10px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    flex-direction: row;
    gap: 4px;
    background: var(--color-hud-bg);
    padding: 4px;
    border: 2px solid var(--color-hotbar-border);
    border-radius: 4px;
    height: var(--hud-height);
  }

  .hotbar-slot {
    width: var(--hotbar-slot-size);
    height: var(--hotbar-slot-size);
    position: relative;
    background: rgb(0 0 0 / 0.5);
    border: 2px solid transparent;
    border-radius: 2px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: var(--duration-fast, 120ms) ease-out;
    image-rendering: pixelated;

    &.active, &:focus-visible {
      outline: 2px solid var(--color-hotbar-selected);
      outline-offset: -2px;
    }

    & .slot-number {
      position: absolute;
      bottom: 2px;
      right: 4px;
      font-size: 0.75rem;
      font-family: var(--font-game);
      color: white;
      text-shadow: 1px 1px 0 #000;
    }
  }

  #health-bar {
    position: absolute;
    bottom: calc(var(--hud-height) + 20px);
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    flex-direction: row;
    gap: 2px;

    & .heart {
      width: 16px;
      height: 16px;
      background: var(--color-health);
      /* Membentuk icon hati ala pixel-art menggunakan clip-path */
      clip-path: polygon(
        20% 0, 40% 0, 50% 20%, 60% 0, 80% 0, 
        100% 20%, 100% 50%, 50% 100%, 0 50%, 0 20%
      );
      transition: var(--duration-fast, 120ms) ease-out;

      &.half {
        /* Setengah hati kiri */
        clip-path: polygon(
          20% 0, 40% 0, 50% 20%, 50% 100%, 0 50%, 0 20%
        );
      }

      &.empty {
        opacity: 0.3;
      }
    }
  }

  #debug-menu, #debug-overlay {
    position: absolute;
    top: 10px;
    left: 10px;
    background: var(--color-hud-bg);
    color: white;
    padding: 8px;
    font-family: var(--font-game);
    font-size: 0.875rem;
    line-height: 1.4;
    visibility: hidden;

    :root.debug-active & {
      visibility: visible;
    }

    & p {
      margin: 0;
    }
  }

  #hotbar-label {
    position: absolute;
    bottom: calc(var(--hud-height) + 45px);
    left: 50%;
    transform: translateX(-50%);
    color: white;
    font-family: var(--font-game);
    font-size: 1.25rem;
    text-shadow: 2px 2px 0 #000;
    opacity: 0;
    pointer-events: none;

    &.show {
      animation: fade-label 2s forwards;
      animation-fill-mode: forwards;
    }
  }

  @keyframes fade-label {
    0% { opacity: 1; }
    70% { opacity: 1; }
    100% { opacity: 0; }
  }

  /* Aesthetic */
  img, canvas, .block-icon {
    image-rendering: pixelated;
  }

  /* Mobile UI */
  .mobile-only {
    display: none;
  }

  :root.mobile-active .mobile-only {
    display: block;
  }

  :root.mobile-active #click-to-play {
    display: none !important;
  }

  #mobile-ui {
    position: fixed;
    inset: 0;
    z-index: 99;
    pointer-events: none;
  }

  #joystick-container {
    position: absolute;
    bottom: 30px;
    left: 30px;
    width: 120px;
    height: 120px;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: auto;
    touch-action: none;
  }

  #joystick-base {
    width: 100px;
    height: 100px;
    background: rgba(0, 0, 0, 0.4);
    border: 2px solid rgba(255, 255, 255, 0.25);
    border-radius: 50%;
    position: relative;
    backdrop-filter: blur(8px);
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
  }

  #joystick-knob {
    width: 44px;
    height: 44px;
    background: rgba(255, 255, 255, 0.7);
    border-radius: 50%;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
    pointer-events: none;
  }

  #mobile-actions {
    position: absolute;
    bottom: 30px;
    right: 30px;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 15px;
    pointer-events: auto;
    touch-action: none;
  }

  .action-pair {
    display: flex;
    gap: 12px;
  }

  .mobile-btn {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.4);
    border: 2px solid rgba(255, 255, 255, 0.25);
    color: white;
    font-family: var(--font-game);
    font-size: 0.75rem;
    font-weight: bold;
    display: flex;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(8px);
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
    user-select: none;
    -webkit-user-select: none;
    transition: transform 0.1s, background-color 0.1s;
    outline: none;
  }

  .mobile-btn:active {
    transform: scale(0.9);
    background: rgba(255, 255, 255, 0.3);
    border-color: rgba(255, 255, 255, 0.6);
  }

  .mine-btn {
    background: rgba(239, 68, 68, 0.3);
    border-color: rgba(239, 68, 68, 0.4);
  }
  
  .mine-btn:active {
    background: rgba(239, 68, 68, 0.6);
  }

  .place-btn {
    background: rgba(16, 185, 129, 0.3);
    border-color: rgba(16, 185, 129, 0.4);
  }
  
  .place-btn:active {
    background: rgba(16, 185, 129, 0.6);
  }

  #btn-jump {
    width: 64px;
    height: 64px;
    background: rgba(59, 130, 246, 0.3);
    border-color: rgba(59, 130, 246, 0.4);
  }
  
  #btn-jump:active {
    background: rgba(59, 130, 246, 0.6);
  }

  /* Make inventory responsive and touch-friendly on mobile */
  @media (max-width: 600px) {
    #inventory-bar {
      bottom: 5px;
      gap: 2px;
      padding: 2px;
    }
    .hotbar-slot {
      width: 36px;
      height: 36px;
    }
    .hotbar-slot .slot-number {
      font-size: 0.6rem;
    }
    #health-bar {
      bottom: calc(var(--hud-height) - 10px);
    }
  }
}
