/* ============================================================
   DICOM Viewer — style.css
   Исправления: 100vh → 100dvh (адресная строка мобильных),
   позиционирование канвы cornerstone, safe-area, отсутствие
   горизонтального скролла, доступные фокус-стили.
   ============================================================ */

:root {
  --bg: #0d0d0f;
  --surface: #1e1f23;
  --surface-2: #2a2c31;
  --border: #3a3d44;
  --text: #e8e8ea;
  --text-dim: #9a9da4;
  --accent: #c49a6c;
  --accent-light: #e0b070;
  --success: #00e676;
  --warn: #ffb300;
  --danger: #ef5350;
  --radius: 8px;
  --radius-sm: 5px;
  --panel-w: 190px;
  --font: 'Segoe UI', Inter, system-ui, -apple-system, sans-serif;
  --mono: ui-monospace, 'SF Mono', 'Cascadia Code', Consolas, monospace;
  --transition: 180ms cubic-bezier(.4, 0, .2, 1);
}

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

html, body {
  height: 100%;
  overscroll-behavior: none;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

#viewer-container {
  display: flex;
  flex-direction: column;
  /* dvh учитывает сворачивающуюся адресную строку — 100vh обрезал нижние кнопки */
  height: 100dvh;
  width: 100%;
  background: var(--bg);
}
@supports not (height: 100dvh) {
  #viewer-container { height: 100vh; }
}

/* ─────────── кнопки (общий вид) ─────────── */
button {
  font-family: inherit;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background var(--transition), border-color var(--transition), transform 80ms;
  user-select: none;
  -webkit-user-select: none;
}
button:hover:not(:disabled) { background: #3a3d44; }
button:active:not(:disabled) { transform: scale(.97); }
button:disabled { opacity: .38; cursor: not-allowed; }
button:focus-visible,
input:focus-visible {
  outline: 2px solid var(--accent-light);
  outline-offset: 2px;
}

/* ─────────── верхняя панель ─────────── */
#toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 12px;
  padding-top: calc(6px + env(safe-area-inset-top, 0px));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  min-height: 46px;
}

.toolbar-logo {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.badge-logo {
  width: 30px;
  height: 30px;
  object-fit: contain;
  border-radius: 4px;
}

.badge-text {
  font-size: .62rem;
  font-weight: 700;
  line-height: 1.15;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: .04em;
  white-space: nowrap;
}

#seriesInfo {
  color: var(--text-dim);
  font-size: .74rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1 1 auto;
  min-width: 0;
}

.toolbar-actions {
  margin-left: auto;
  display: flex;
  gap: 6px;
  align-items: center;
  flex-shrink: 0;
}

#toolbar button {
  padding: 7px 11px;
  font-weight: 600;
  font-size: .8rem;
  white-space: nowrap;
}

/* ─────────── основная область ─────────── */
#mainArea {
  display: flex;
  flex: 1 1 auto;
  min-height: 0;   /* без этого flex-потомок не даёт контейнеру сжиматься */
  overflow: hidden;
}

#imageViewport {
  position: relative;
  flex: 1 1 auto;
  min-width: 0;
  background: #000;
  overflow: hidden;
  /* жесты обрабатываем сами; без этого мобильный браузер скроллит страницу */
  touch-action: none;
  outline: none;
}

#imageViewport.drag-over {
  box-shadow: inset 0 0 0 3px var(--accent);
}

/* cornerstone сам вставляет свой canvas — фиксируем его геометрию */
#imageViewport > canvas {
  position: absolute;
  inset: 0;
  display: block;
}

#measureCanvas {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: block;
  touch-action: none;
  pointer-events: none;   /* включается из JS только когда нужно */
}

/* ─────────── подсказка «перетащите файлы» ─────────── */
#dropHint {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  color: var(--text-dim);
  text-align: center;
  pointer-events: none;
  padding: 20px;
  z-index: 5;
}
#dropHint .drop-icon { font-size: 3rem; opacity: .5; }
#dropHint p { font-size: .9rem; }
#dropHint .dim { font-size: .78rem; opacity: .6; }

/* ─────────── оверлеи ─────────── */
#infoOverlay {
  position: absolute;
  top: 10px;
  left: 10px;
  max-width: min(340px, calc(100% - 20px));
  max-height: calc(100% - 20px);
  overflow-y: auto;
  background: rgba(0, 0, 0, .78);
  backdrop-filter: blur(3px);
  color: var(--success);
  padding: 10px 12px;
  font-family: var(--mono);
  font-size: .72rem;
  line-height: 1.45;
  border: 1px solid rgba(0, 230, 118, .25);
  border-radius: var(--radius);
  z-index: 20;
}
#infoOverlay div { margin: 1px 0; word-break: break-word; }
#infoOverlay b { color: #b9ffd8; font-weight: 600; }

#toolHint {
  position: absolute;
  top: 10px;
  right: 10px;
  max-width: min(320px, calc(100% - 20px));
  background: rgba(0, 0, 0, .8);
  color: #6fb8ff;
  padding: 10px 12px;
  font-family: var(--mono);
  font-size: .74rem;
  line-height: 1.45;
  border: 1px solid rgba(111, 184, 255, .28);
  border-radius: var(--radius);
  white-space: pre-line;
  z-index: 21;
  pointer-events: none;
}

.hidden { display: none !important; }

/* ─────────── индикатор загрузки ─────────── */
#loadingOverlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  background: rgba(0, 0, 0, .65);
  z-index: 40;
  color: var(--text);
  font-size: .85rem;
}
.spinner {
  width: 38px;
  height: 38px;
  border: 3px solid rgba(255, 255, 255, .18);
  border-top-color: var(--accent-light);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ─────────── правая панель ─────────── */
#rightPanel {
  width: var(--panel-w);
  flex-shrink: 0;
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding: 8px;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}
#rightPanel::-webkit-scrollbar { width: 7px; }
#rightPanel::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

.sheet-grip { display: none; }

.tool-group {
  display: flex;
  align-items: center;
  gap: 5px;
  width: 100%;
}
.tool-group button { flex: 1 1 0; min-width: 0; }

#rightPanel button {
  padding: 9px 8px;
  font-size: .85rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#rightPanel button.active-tool {
  background: var(--accent);
  border-color: var(--accent-light);
  color: #17130d;
  font-weight: 700;
}

#sliceCounter {
  color: var(--accent-light);
  font-weight: 600;
  font-size: .8rem;
  min-width: 58px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

/* ─────────── слайдеры ─────────── */
.slider-group {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
}
.slider-group label {
  color: var(--text-dim);
  font-size: 1.25rem;
  width: 20px;
  text-align: center;
  flex-shrink: 0;
  cursor: pointer;
}
.slider-group input[type="range"] {
  flex: 1 1 auto;
  min-width: 0;
  -webkit-appearance: none;
  appearance: none;
  height: 4px;
  background: var(--border);
  border-radius: 3px;
  outline: none;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background: var(--accent-light);
  cursor: pointer;
  border: none;
}
input[type="range"]::-moz-range-thumb {
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background: var(--accent-light);
  cursor: pointer;
  border: none;
}

/* ─────────── гистограмма ─────────── */
.histogram-container {
  width: 100%;
  margin: 4px 0;
}
#histogramCanvas {
  display: block;
  width: 100%;
  height: 62px;
  background: #101318;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  touch-action: none;
}

/* ─────────── мобильные контролы ─────────── */
#mobileControls {
  display: none;
  gap: 8px;
  justify-content: center;
  align-items: center;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 8px 12px;
  padding-bottom: calc(8px + env(safe-area-inset-bottom, 0px));
  flex-shrink: 0;
  z-index: 70;
}
#mobileControls button {
  padding: 11px 14px;
  font-size: 1rem;
  font-weight: 600;
  min-height: 44px;   /* минимальная зона касания */
}
#mobileControls #mobileToolsBtn { flex: 1 1 auto; }

#mobileOverlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .55);
  z-index: 55;
  animation: fade .2s ease;
}
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

/* ─────────── тосты ─────────── */
#toastContainer {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 200;
  pointer-events: none;
  width: max-content;
  max-width: min(90vw, 460px);
}
.toast {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-left: 3px solid var(--text-dim);
  color: var(--text);
  padding: 10px 14px;
  border-radius: var(--radius);
  font-size: .84rem;
  box-shadow: 0 6px 22px rgba(0, 0, 0, .5);
  opacity: 0;
  transform: translateY(10px);
  transition: opacity .25s, transform .25s;
}
.toast--in { opacity: 1; transform: translateY(0); }
.toast--success { border-left-color: var(--success); }
.toast--warn { border-left-color: var(--warn); }
.toast--error { border-left-color: var(--danger); }

/* ============================================================
   Планшет
   ============================================================ */
@media (max-width: 980px) {
  :root { --panel-w: 168px; }
  #rightPanel button { font-size: .8rem; padding: 8px 6px; }
}

/* ============================================================
   Мобильные — брейкпоинт поднят до 700px:
   на 600–700px старая раскладка ломалась (панель 180px
   съедала половину экрана, но мобильный режим ещё не включался)
   ============================================================ */
@media (max-width: 700px) {
  #toolbar { padding: 5px 8px; gap: 6px; }
  #toolbar button { padding: 8px 10px; font-size: 1.15rem; min-height: 40px; }
  #toolbar .btn-text { display: none; }
  .badge-logo { width: 24px; height: 24px; }
  .badge-text { display: none; }
  #seriesInfo { font-size: .68rem; }

  #mainArea { flex-direction: column; }

  /* правая панель → нижняя шторка */
  #rightPanel {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    max-height: 72dvh;
    border-left: none;
    border-top: 1px solid var(--border);
    border-radius: 14px 14px 0 0;
    padding: 8px 12px;
    padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
    z-index: 60;
    transform: translateY(100%);
    transition: transform .28s cubic-bezier(.32, .72, 0, 1);
    box-shadow: 0 -8px 28px rgba(0, 0, 0, .7);
    /* прежняя версия оставляла display:flex + transform, из-за чего
       скрытая панель всё равно ловила касания у нижней кромки */
    visibility: hidden;
  }
  #rightPanel.open {
    transform: translateY(0);
    visibility: visible;
  }

  .sheet-grip {
    display: block;
    width: 40px;
    height: 4px;
    background: var(--border);
    border-radius: 3px;
    margin: 2px auto 8px;
    flex-shrink: 0;
  }

  #rightPanel button {
    font-size: .95rem;
    padding: 12px 10px;
    min-height: 46px;
  }

  #mobileControls { display: flex; }

  #infoOverlay {
    font-size: .66rem;
    max-width: calc(100% - 16px);
    top: 8px;
    left: 8px;
  }
  #toolHint {
    font-size: .7rem;
    left: 8px;
    right: 8px;
    max-width: none;
    top: auto;
    bottom: 8px;
  }
  #histogramCanvas { height: 70px; }
}

/* Очень узкие экраны */
@media (max-width: 380px) {
  #mobileControls button { padding: 10px 8px; font-size: .9rem; }
  #toolbar button { padding: 7px 8px; font-size: 1rem; }
}

/* Ландшафтная ориентация телефона — экономим вертикаль */
@media (max-height: 480px) and (orientation: landscape) {
  #toolbar { min-height: 38px; padding: 3px 8px; }
  #mobileControls { padding: 4px 8px; }
  #mobileControls button { padding: 7px 10px; min-height: 36px; }
  #rightPanel { max-height: 88dvh; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    transition-duration: .01ms !important;
  }
}

@media print {
  #toolbar, #rightPanel, #mobileControls, #toastContainer { display: none !important; }
}
