:root {
  --bg: #0f1115;
  --fg: #e9eef5;
  --panel: #171923;
  --muted: #9aa0a6;
  --accent: #4f46e5;
  /* iOS/Android güvenli alan inset'leri */
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  /* Header yüksekliği (JS ile güncellenecek) */
  --header-h: 56px;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 14px/1.4 system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial;
}

#app {
  position: fixed;
  inset: 0;
  display: grid;
  grid-template-rows: auto 1fr;
}

header {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 10px 14px;
  background: transparent;
  border-bottom: none;
  position: relative;
  z-index: 2000; /* Header her zaman üstte */
}

header h1 {
  font-size: 16px;
  margin: 0;
  letter-spacing: .3px;
  color: #fff;
}

.controls {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-left: auto;
  flex-wrap: wrap;
}

.field {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: var(--panel);
  border: 1px solid #2a2f3e;
  padding: 6px 10px;
  border-radius: 10px;
}

.field label {
  color: var(--muted);
  font-size: 12px;
}

.field input[type="color"] {
  width: 28px;
  height: 28px;
  border: none;
  background: transparent;
  padding: 0;
}

.field input[type="range"] {
  accent-color: var(--accent);
}

.btn {
  background: var(--panel);
  border: 1px solid #2a2f3e;
  color: var(--fg);
  padding: 8px 10px;
  border-radius: 10px;
  cursor: pointer;
  transition: .15s ease;
}

.btn:hover {
  border-color: #3b4256;
}

.btn.primary {
  background: var(--accent);
  border-color: #4039c6;
}

#stage {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

video,
canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

/* Aynalı UI: ekrandaki yön elinle aynı olsun */
#video {
  transform: scaleX(-1);
  opacity: .18;
  object-fit: cover;
}

#camOverlay {
  transform: scaleX(-1);
  pointer-events: none;
}

#draw {
  transform: scaleX(-1);
  pointer-events: none;
}

.hud {
  position: absolute;
  left: 12px;
  bottom: 12px;
  background: rgba(0, 0, 0, .35);
  border: 1px solid rgba(255, 255, 255, .08);
  padding: 8px 10px;
  border-radius: 10px;
  font-size: 12px;
  backdrop-filter: blur(6px);
}

/* Renk Paleti */
.color-palette {
  position: absolute;
  right: 20px;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  flex-direction: column;
  gap: 12px;
  z-index: 1000;
}

.color-box {
  width: 50px;
  height: 50px;
  border-radius: 12px;
  border: 3px solid rgba(255, 255, 255, 0.2);
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  backdrop-filter: blur(4px);
  position: relative;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

/* Desktop hover efektleri */
@media screen and (min-width: 769px) and (min-height: 501px) {
  .color-box:hover {
    transform: scale(1.1);
    border-color: rgba(255, 255, 255, 0.6);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.4);
  }

  .color-box.selected {
    transform: scale(1.2);
    border-color: #fff;
    box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.8), 0 8px 25px rgba(0, 0, 0, 0.5);
    animation: pulse 0.6s ease-out;
  }

  .color-box.hover-effect {
    transform: scale(1.15);
    border-color: #fff;
    box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.6), 0 8px 30px rgba(0, 0, 0, 0.6);
    animation: colorHover 0.4s ease-out;
  }

  @keyframes pulse {
    0% { transform: scale(1.2); }
    50% { transform: scale(1.35); }
    100% { transform: scale(1.2); }
  }

  @keyframes colorHover {
    0% { transform: scale(1); }
    50% { transform: scale(1.2); }
    100% { transform: scale(1.15); }
  }
}

/* Mobil Uyumluluk */
@media screen and (max-width: 768px), screen and (max-height: 500px) {
  .color-palette {
    position: fixed;
    right: max(12px, var(--safe-right));
    bottom: max(90px, calc(12px + var(--safe-bottom)));
    top: auto;
    transform: none;
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    grid-template-rows: repeat(4, 1fr);
    gap: clamp(6px, 2vh, 10px);
    width: clamp(96px, 14vw, 120px);
    padding: 12px;
    background: rgba(0, 0, 0, 0.4);
    border-radius: 18px;
    backdrop-filter: blur(14px);
    border: 1.5px solid rgba(255, 255, 255, 0.18);
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
    max-height: calc(100dvh - (var(--safe-top) + var(--safe-bottom) + 24px + 90px));
    overflow: auto;
  }

  .color-box {
    width: clamp(34px, 6.5vh, 44px);
    height: clamp(34px, 6.5vh, 44px);
    border-radius: 12px;
    border-width: 2px;
    margin: 0;
  }

  .color-box:hover,
  .color-box.hover-effect {
    transform: scale(1.25);
    border-color: #fff;
    box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.8), 0 6px 25px rgba(0, 0, 0, 0.6);
  }

  .color-box.selected {
    transform: scale(1.3);
    animation: mobilePulse 0.6s ease-out;
  }

  /* Header mobilde daha kompakt */
  header {
    padding: 8px 12px;
    flex-wrap: wrap;
    gap: 8px;
  }

  header h1 {
    font-size: 14px;
    margin-right: 8px;
  }

  .controls {
    gap: 6px;
    font-size: 12px;
  }

  .field {
    padding: 4px 8px;
    border-radius: 8px;
  }

  .field label {
    font-size: 11px;
  }

  .btn {
    padding: 6px 8px;
    font-size: 12px;
    border-radius: 8px;
  }

  /* HUD mobilde daha büyük */
  .hud {
    left: max(12px, var(--safe-left));
    bottom: max(12px, var(--safe-bottom));
    font-size: 16px;
    padding: 12px 16px;
    max-width: calc(100dvw - 180px); /* Renk paleti için yer bırak */
  }
}

/* Çok küçük ekranlar (telefon portre) */
@media screen and (max-width: 480px) {
  .color-palette {
    position: fixed;
    bottom: max(70px, calc(10px + var(--safe-bottom)));
    left: 50%;
    right: auto;
    transform: translateX(-50%);
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
    width: calc(100dvw - 24px);
    max-width: 360px;
    padding: 10px;
    gap: clamp(6px, 2.2vw, 10px);
  }

  .color-box {
    width: clamp(30px, 10vw, 40px);
    height: clamp(30px, 10vw, 40px);
    border-radius: 10px;
    flex: 0 0 auto;
  }

  .hud {
    left: 15px;
    bottom: 15px;
    right: 15px;
    max-width: none;
    text-align: center;
  }

  header {
    padding: 6px 10px;
  }

  header h1 {
    font-size: 13px;
  }

  .controls {
    display: none; /* Çok küçük ekranlarda kontrolleri gizle */
  }

  /* Sadece temel kontrol butonu göster */
  .mobile-controls {
    position: fixed;
    top: max(10px, var(--safe-top));
    right: max(10px, var(--safe-right));
    z-index: 1100;
    display: flex;
    gap: 8px;
  }
}

/* Landscape mobil için özel ayar */
@media screen and (max-height: 500px) and (orientation: landscape) {
  .color-palette {
    position: fixed;
    right: max(12px, var(--safe-right));
    top: calc(max(12px, var(--safe-top)) + var(--header-h));
    bottom: max(12px, var(--safe-bottom));
    transform: none;
    display: flex;
    flex-direction: column;
    width: auto;
    height: auto;
    max-height: calc(100dvh - (var(--safe-top) + var(--safe-bottom) + 24px));
    gap: clamp(6px, 1.8vh, 10px);
    overflow: auto;
  }

  .color-box {
    width: clamp(30px, 7vh, 42px);
    height: clamp(30px, 7vh, 42px);
  }

  .hud {
    left: max(12px, var(--safe-left));
    bottom: max(12px, var(--safe-bottom));
    max-width: calc(100dvw - 120px);
  }
}

/* Çok küçük landscape yükseklikleri için ek küçültme */
@media screen and (max-height: 360px) and (orientation: landscape) {
  .color-box { width: 34px; height: 34px; }
}
@media screen and (max-height: 320px) and (orientation: landscape) {
  .color-box { width: 30px; height: 30px; }
}

@keyframes mobilePulse {
  0% { transform: scale(1.4); }
  50% { transform: scale(1.6); }
  100% { transform: scale(1.4); }
}

/* Touch friendly yapma */
@media (pointer: coarse) {
  .color-box {
    min-width: 50px;
    min-height: 50px;
  }
}
