/* =====================================================================
   SOL RAY + ÇEKMECE  (side_drawer.js ile birlikte çalışır)

   Düzen:
     >= 768px : sol ray; panel ekranın ortasında pencere olarak açılır
                (gübre havuzu penceresiyle aynı dil), arka plan bütünüyle bulanıklaşır
                (perde: overlay_scrim.css).
                Genişlik panele göre: Ayarlar/Hedef geniş, Reçeteler/Oranlar dar.
     <  768px : alt sekme çubuğu + ekranın ortasında pencere
   Sayfa hiçbir genişlikte daralmaz.

   Hesap mantığına dokunmaz; yalnızca taşınan blokların görünümünü
   çekmece içinde yeniden düzenler. Taban kurallar styles.css'te kalır.
   ===================================================================== */

:root {
  --rail-w: 76px;
  --drawer-w: 384px;
  --topbar-h: 61px;
  --bar-h: 64px;
  --drawer-ease: cubic-bezier(.22, .8, .24, 1);
  --drawer-speed: 240ms;
}

/* ---------- Ana sahnenin yerleşimi ---------- */

@media (min-width: 768px) {
  /* Menü solda yer kaplar; içerik sütunu ve başlık çubuğu birlikte kayar (styles.css). */
  :root { --content-shift: var(--rail-w); }
  #calculator-panel,
  #optimization-panel {
    width: min(var(--page-max-width), calc(100% - var(--content-shift)));
    margin-right: 0;
    margin-left: var(--content-shift);
  }
}

/* Çekmece açıkken sayfa yerinde kalır; navigasyon dahil arka plan perdeyle örtülür.
   Perdenin alanı, tonu ve bulanıklığı overlay_scrim.css'te (tüm pencerelerle ortak). */
.side-drawer-backdrop {
  position: fixed;
  z-index: 51; /* Üst bar (50) ve rayın üstünde. */
  opacity: 0;
  visibility: hidden;
  transition: opacity var(--drawer-speed) ease, visibility 0s linear var(--drawer-speed);
}

html[data-drawer] .side-drawer-backdrop { opacity: 1; visibility: visible; transition: opacity var(--drawer-speed) ease, visibility 0s; }
/* Kapanış (side_drawer.js data-drawer-closing): perde tam süreyle açılır; pencere ondan önce söner. */
html[data-drawer][data-drawer-closing] .side-drawer-backdrop {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity var(--drawer-speed) ease, visibility 0s linear var(--drawer-speed);
}
/* Kilit yalnız kök kaydırıcıda: body'ye overflow: hidden verilirse body kaydırma
   kutusu olur, yapışkan üst çubuk sayfa kaymışken yukarı kaçar ve perdenin
   üstünde bulanmamış bir şerit kalır. */
html.drawer-lock { overflow: hidden; }

/* ---------- Ray ---------- */

.drawer-rail {
  position: fixed;
  z-index: 45;
  top: var(--topbar-h);
  bottom: 0;
  left: 0;
  width: var(--rail-w);
  padding: 14px 8px max(14px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  /* Ray sayfa zeminiyle aynı tonda; yalnız ince çizgiyle ayrılır (ayrı renk bandı ve gölge yok). */
  background: var(--bg);
  border-right: 1px solid var(--line);
}

.drawer-rail-button {
  position: relative;
  width: 64px;
  min-height: 56px;
  padding: 7px 2px 6px;
  display: grid;
  justify-items: center;
  align-content: center;
  gap: 4px;
  border: 0;
  border-radius: 14px;
  background: transparent;
  color: var(--text-2);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background-color .16s ease, color .16s ease, transform .16s ease;
}

.drawer-rail-button::before {
  content: "";
  position: absolute;
  top: 12px;
  bottom: 12px;
  left: -8px;
  width: 3px;
  border-radius: 0 3px 3px 0;
  background: var(--blue);
  opacity: 0;
  transform: scaleY(.3);
  transition: opacity .16s ease, transform .2s var(--drawer-ease);
}

.drawer-rail-icon {
  width: 34px;
  height: 30px;
  display: grid;
  place-items: center;
  border-radius: 10px;
  font-size: 1.05rem;
  font-weight: 800;
  transition: background-color .16s ease;
}

.drawer-rail-icon svg { width: 22px; height: 22px; }

.drawer-rail-label {
  max-width: 100%;
  font-size: .66rem;
  font-weight: 700;
  letter-spacing: .01em;
  line-height: 1.1;
  text-align: center;
  white-space: nowrap;
}

.drawer-rail-button[aria-expanded="true"] { color: var(--blue-dark); }
.drawer-rail-button[aria-expanded="true"] .drawer-rail-icon { background: var(--blue-soft); }
.drawer-rail-button[aria-expanded="true"]::before { opacity: 1; transform: scaleY(1); }
.drawer-rail-button:active { transform: scale(.95); }

.drawer-rail-help {
  margin-top: auto;
  min-height: 50px;
  color: var(--muted);
}

.drawer-rail-help .drawer-rail-icon {
  width: 26px;
  height: 26px;
  border: 1.5px solid currentColor;
  border-radius: 999px;
  font-size: .82rem;
}

/* ---------- Çekmece ---------- */

.side-drawer {
  position: fixed;
  z-index: 52; /* Açık pencere kendi perdesinin üstünde keskin kalır. */
  top: 50dvh;
  bottom: auto;
  left: 50%;
  width: min(var(--drawer-w), calc(100vw - 24px));
  max-height: calc(100dvh - 24px);
  border: 1px solid var(--line);
  border-radius: 18px;
  overflow: hidden;
  opacity: 0;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  box-shadow: 0 24px 64px rgba(var(--shadow-rgb), .28);
  visibility: hidden;
  transform: translate(-50%, -50%) scale(.97);
  transition: opacity var(--drawer-speed) ease, transform var(--drawer-speed) var(--drawer-ease), visibility 0s linear var(--drawer-speed);
}

html[data-drawer] .side-drawer {
  visibility: visible;
  opacity: 1;
  transform: translate(-50%, -50%);
  transition: opacity var(--drawer-speed) ease, transform var(--drawer-speed) var(--drawer-ease), visibility 0s;
}
/* Kapanış: pencere boyutu ve paneli korunarak, perdeden kısa sürede söner. Perde
   bulanıklığı açılırken pencere artık görünmez: yarı saydam pencere netleşen
   sayfanın üstünde "çift görüntü" bırakmaz. */
html[data-drawer][data-drawer-closing] .side-drawer {
  visibility: hidden;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, -50%) scale(.97);
  transition:
    opacity calc(var(--drawer-speed) * .6) ease-in,
    transform calc(var(--drawer-speed) * .6) ease-in,
    visibility 0s linear calc(var(--drawer-speed) * .6);
}

.side-drawer-head {
  padding: 8px 10px 6px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex: none;
}

.side-drawer-title { min-width: 0; margin: 0 auto 0 0; font-size: 1.12rem; line-height: 1.25; }

.side-drawer-body {
  min-height: 0;
  flex: 0 1 auto;
  padding: 4px 12px max(12px, env(safe-area-inset-bottom));
  overflow-y: auto;
  overscroll-behavior: contain;
  border-top: 1px solid var(--line);
}

.drawer-pane { padding-top: 8px; }

/* Panelde sahne açılınca hafif giriş */
html[data-drawer] .drawer-pane:not([hidden]) { animation: drawer-pane-in .26s var(--drawer-ease); }

@keyframes drawer-pane-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: none; }
}

/* --- Bölüm seçici (Hedef ppm / Hedef oranlar) --- */
.drawer-segment {
  margin-bottom: 0;
  padding: 2px;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 2px;
  border-radius: var(--workspace-card-radius, 8px);
  background: var(--sunken);
}

.drawer-segment button {
  min-height: var(--control-h);
  padding: 4px 6px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--text-2);
  font-size: var(--fs-label);
  font-weight: var(--workspace-title-weight, 650);
  cursor: pointer;
  transition: background-color .16s ease, color .16s ease, box-shadow .16s ease;
}

.drawer-segment button[aria-pressed="true"] {
  background: var(--surface);
  color: var(--blue-dark);
  box-shadow: none;
}

@media (pointer: coarse) { .drawer-segment button { min-height: 44px; } }

/* ---------- Neden-sonuç parlaması ---------- */

.analysis-card.is-pulsing,
.analysis-tab.is-pulsing { animation: drawer-card-pulse .8s ease-out; }

@keyframes drawer-card-pulse {
  0% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--blue) 50%, transparent), var(--shadow); border-color: var(--blue); }
  100% { box-shadow: 0 0 0 12px transparent, var(--shadow); }
}

/* ---------- İlk açılış rehberi ---------- */

.drawer-tour {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  max-width: none;
  max-height: none;
  margin: 0;
  padding: 0;
  border: 0;
  background: transparent;
  overflow: hidden;
  color: var(--text);
}

html.tour-lock { overflow: hidden; }
.drawer-tour::backdrop { background: rgba(var(--scrim-rgb), .58); }
.drawer-tour.has-spot::backdrop { background: transparent; }

.drawer-tour-spot {
  position: fixed;
  border-radius: 16px;
  box-shadow: 0 0 0 9999px rgba(var(--scrim-rgb), .58);
  outline: 2px solid rgba(255, 255, 255, .9);
  outline-offset: 2px;
  pointer-events: none;
}

/* Kart sabit kalır; yalnız vurgulanan alan ve açıklama değişir. */
.drawer-tour-card {
  position: absolute;
  bottom: max(var(--tour-inset, 16px), env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  box-sizing: border-box;
  width: min(440px, calc(100% - 24px));
  max-height: calc(100% - var(--tour-inset, 16px) - 24px - env(safe-area-inset-top));
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--surface);
  box-shadow: 0 18px 50px rgba(var(--scrim-rgb), .3);
}

.drawer-tour-card .drawer-tour-step { margin: 0 0 8px; color: var(--blue); font-size: .75rem; font-weight: 800; }
.drawer-tour-card h2 { margin: 0 0 8px; font-size: 1.12rem; }
.drawer-tour-card p { margin: 0; color: var(--text-2); font-size: .9rem; line-height: 1.55; }
.drawer-tour-card .drawer-tour-hint { margin-top: 12px; padding: 10px; border-radius: 10px; background: var(--surface-soft); }
.drawer-tour-card .drawer-tour-upcoming { margin-top: 10px; color: var(--muted); font-size: .8rem; }
.drawer-tour-card kbd { padding: 1px 6px; border: 1px solid var(--line-strong); border-bottom-width: 2px; border-radius: 5px; background: var(--surface-soft); font: inherit; font-size: .78rem; font-weight: 700; }

.drawer-tour-foot { margin-top: 10px; display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.drawer-tour-foot button {
  min-height: 44px;
  padding: 8px 12px;
  border: 1px solid var(--line-strong);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: .82rem;
  font-weight: 700;
  cursor: pointer;
}
.drawer-tour-foot button:disabled { opacity: .45; cursor: default; }
.drawer-tour-card :focus-visible { outline: 2px solid var(--blue); outline-offset: 3px; }
.drawer-tour-foot .drawer-tour-next { margin-left: auto; border-color: var(--blue); background: var(--blue); color: var(--on-accent); }
.drawer-tour-foot .drawer-tour-skip { border-color: transparent; color: var(--muted); }

/* ---------- Fare ile gezinme ---------- */

@media (hover: hover) and (pointer: fine) {
  .drawer-rail-button:hover { color: var(--blue-dark); }
  .drawer-rail-button:hover .drawer-rail-icon { background: var(--sunken); }
  .drawer-rail-button[aria-expanded="true"]:hover .drawer-rail-icon { background: var(--blue-soft); }
  .drawer-segment button:hover:not([aria-pressed="true"]) { color: var(--text); background: rgba(255, 255, 255, .6); }
  .drawer-tour-foot button:hover:not(:disabled) { border-color: var(--icon); }
  .drawer-tour-foot .drawer-tour-next:hover { border-color: var(--blue-dark); background: var(--blue-dark); }
}

/* ---------- Bütün ekranlarda ortalanmış pencere ---------- */

/* Panel başına genişlik: geniş içerik (tablo, iki sütun) geniş; liste içerikleri dar. */
html[data-drawer="settings"] { --drawer-w: 760px; }
html[data-drawer="targets"] { --drawer-w: 720px; }
html[data-drawer="recipes"] { --drawer-w: 440px; }

.side-drawer-body { padding: 4px 12px 12px; }
/* Pencerede gerçek başlık satırı: panel adı + × (gübre havuzu penceresiyle aynı).
   Aynı başlık satırı mobilde de korunur. */
.side-drawer-head.is-root { position: static; height: auto; padding: 8px 10px 6px 12px; border-bottom: 1px solid var(--line); }
.side-drawer-head.is-root .side-drawer-close { position: static; }
.side-drawer-head .side-drawer-title.visually-hidden {
  position: static !important;
  width: auto;
  height: auto;
  margin: 0 auto 0 0;
  overflow: visible;
  clip: auto;
  white-space: normal;
  font-size: 1.05rem;
}

/* ---------- Mobil: alt çubuk ---------- */

@media (max-width: 767px) {
  #calculator-panel,
  #optimization-panel { padding-bottom: calc(var(--bar-h) + 16px + env(safe-area-inset-bottom)); }
  /* Optimizasyonu çalıştır çubuğu: alt çubuğun hemen üstüne yapışır. */
  .optimization-run-bar { bottom: calc(var(--bar-h) + env(safe-area-inset-bottom)); }

  .drawer-rail {
    top: auto;
    right: 0;
    width: auto;
    height: calc(var(--bar-h) + env(safe-area-inset-bottom));
    padding: 4px max(8px, env(safe-area-inset-right)) env(safe-area-inset-bottom) max(8px, env(safe-area-inset-left));
    flex-direction: row;
    justify-content: space-around;
    gap: 4px;
    border-top: 1px solid var(--line);
    border-right: 0;
    background: var(--surface);
    box-shadow: 0 -4px 14px rgba(var(--shadow-rgb), .05);
  }

  .drawer-rail-button { width: auto; min-width: 64px; flex: 1; min-height: 54px; }
  .drawer-rail-button::before { top: -4px; bottom: auto; left: 22%; right: 22%; width: auto; height: 3px; border-radius: 0 0 3px 3px; transform: scaleX(.3); }
  .drawer-rail-button[aria-expanded="true"]::before { transform: scaleX(1); }
  .drawer-rail-help { margin-top: 0; min-height: 54px; flex: 0 0 64px; }

  .side-drawer-body { padding-inline: 8px; padding-bottom: 18px; }
}

@media (prefers-reduced-motion: reduce) {
  .side-drawer,
  .side-drawer-backdrop,
  .drawer-rail-button,
  .drawer-rail-button::before { transition: none !important; }
  .analysis-card.is-pulsing,
  .analysis-tab.is-pulsing,
  html[data-drawer] .drawer-pane:not([hidden]) { animation: none !important; }
}

/* Yazdırırken çekmece içeriği sayfanın sonuna düz akışla basılır */
@media print {
  .drawer-rail,
  .side-drawer-backdrop,
  .side-drawer-head,
  .drawer-segment,
  .drawer-tour { display: none !important; }
  #calculator-panel,
  #optimization-panel { width: auto; margin: 0 auto; padding-bottom: 0; }
  .side-drawer {
    position: static;
    grid-row: 99;
    width: auto;
    visibility: visible;
    opacity: 1;
    max-height: none;
    overflow: visible;
    transform: none;
    border: 0;
    box-shadow: none;
  }
  .side-drawer-body { overflow: visible; border: 0; padding: 0; }
  .side-drawer .drawer-pane[hidden] { display: block !important; }
}

