/* ===== TELEGRAM-INSPIRED CSS FOR IPHONE SEQUENCER ===== */
:root {
  --tg-primary: #0088cc;
  --tg-primary-light: #5ab5e6;
  --tg-primary-dark: #006ba3;
  --tg-bg: #fafafa;
  --tg-bg-secondary: #f0f0f0;
  --tg-bg-elevated: #f5f5f5;
  --tg-text-primary: #000000;
  --tg-text-secondary: #606066;
  --tg-text-tertiary: #7a7a7f;
  --tg-border: #e0e0e0;
  --tg-border-light: #e8e8e8;
  --tg-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
  --tg-shadow-light: 0 1px 3px rgba(0, 0, 0, 0.02);
  --border-radius: 10px;
  --border-radius-small: 6px;
  --border-radius-large: 14px;
  --spacing-xs: 3px;
  --spacing-sm: 6px;
  --spacing-md: 10px;
  --spacing-lg: 14px;
  --spacing-xl: 18px;
}

/* ===== БАЗОВЫЕ СТИЛИ ===== */
* {
  box-sizing: border-box;
}

body {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  height: 100vh;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
  font-size: 20px;
  margin: 0;
  padding: 0;
  background: var(--tg-bg);
  text-transform: uppercase;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: var(--tg-text-primary);
}

/* ===== ОСНОВНОЙ КОНТЕЙНЕР ===== */
.main-container {
  margin: 0;
  display: grid;
  height: 100vh;
  grid-template-rows: auto auto auto 01fr;
  width: 96%;
  min-width: 320px;
  max-width: 380px;
  gap: var(--spacing-sm);
  overflow: hidden;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* ===== ОБЩИЕ КОМПОНЕНТЫ ===== */
.control-section-top,
.control-section-bottom,
.top-section,
.bottom-section {
  background: var(--tg-bg-elevated);
  border: 1px solid var(--tg-border-light);
  box-shadow: var(--tg-shadow-light);
  border-radius: var(--border-radius-large);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

/* ===== СЕКЦИИ УПРАВЛЕНИЯ ===== */
.control-section-top {
  margin-top: var(--spacing-sm);
  display: flex;
  flex-direction: row;
  gap: var(--spacing-xs);
  justify-content: center;
  padding: var(--spacing-md);
  overflow-x: auto;
  overflow-y: hidden;
  min-height: 2.3em;
  height: auto;
  flex-wrap: nowrap;
}

.control-section-bottom {
  display: flex;
  flex-direction: row;
  gap: var(--spacing-xs);
  justify-content: center;
  padding: var(--spacing-md);
  overflow-x: auto;
  overflow-y: hidden;
  min-height: 2.3em;
  height: auto;
  flex-wrap: nowrap;
}

/* Горизонтальная прокрутка для control-section */
.control-section-top::-webkit-scrollbar,
.control-section-bottom::-webkit-scrollbar {
  height: 3px;
}

.control-section-top::-webkit-scrollbar-track,
.control-section-bottom::-webkit-scrollbar-track {
  background: transparent;
  border-radius: 10px;
}

.control-section-top::-webkit-scrollbar-thumb,
.control-section-bottom::-webkit-scrollbar-thumb {
  background: var(--tg-primary-light);
  border-radius: 10px;
}

/* ===== ГРУППЫ УПРАВЛЕНИЯ ===== */
.control-group {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  width: 100%;
  min-width: 0;
  flex-shrink: 1;
}

.control-label {
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
  padding: 1px 0;
  font-size: 0.5em;
  color: var(--tg-text-secondary);
  font-weight: 600;
  letter-spacing: 0.2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ===== БАЗОВЫЕ СТИЛИ КНОПОК ===== */
.control,
.box,
.button,
.suggestion {
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
  border: 1px solid var(--tg-border-light);
  border-radius: var(--border-radius-small);
  background: var(--tg-bg-elevated);
  cursor: pointer;
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  font-weight: 500;
  color: var(--tg-text-primary);
  box-shadow: var(--tg-shadow-light);
  position: relative;
  overflow: hidden;
  outline: none;
}

.control:hover,
.box:hover,
.button:hover,
.suggestion:hover {
  background: var(--tg-bg-secondary);
  transform: translateY(-0.5px);
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.04);
}

.control:focus,
.box:focus,
.button:focus,
.suggestion:focus {
  background: var(--tg-bg-secondary);
  box-shadow: 0 0 0 2px var(--tg-primary-light), var(--tg-shadow-light);
}

.control:active,
.box:active,
.button:active,
.suggestion:active {
  transform: translateY(0) scale(0.98);
  transition: transform 0.1s ease;
}

/* Фокус для доступности */
.control:focus-visible,
.box:focus-visible,
.button:focus-visible,
.suggestion:focus-visible {
  outline: 2px solid var(--tg-primary);
  outline-offset: 2px;
}

/* ===== КОНТРОЛЫ ===== */
.control {
  width: 100%;
  padding: 0px 0px 0px 0px;
  height: 90%;
  font-weight: 600;
  font-size: 1em;
  min-width: 0;

}

.control.focused {
  background: var(--tg-bg-secondary) !important;
  box-shadow: 0 0 0 2px var(--tg-primary-light), var(--tg-shadow-light);
}

/* ===== КНОПКА PLAY ALL ===== */
.control[data-type="playAllButton"] {
  background: var(--tg-bg-elevated);
  color: var(--tg-text-primary);
  border: 1px solid var(--tg-border-light);
  font-weight: 600;
  text-shadow: none;
  letter-spacing: normal;
  z-index: 10;
  min-width: 3ch;
}

.control[data-type="playAllButton"].playing {
  border-color: var(--tg-primary-light);
  animation: pulse-shadow-playing 1.5s ease-in-out infinite;
}

.control[data-type="playAllButton"]:hover {
  background: var(--tg-bg-secondary);
}

.control[data-type="playAllButton"].focused {
  background: var(--tg-bg-secondary);
  box-shadow: 0 0 0 2px var(--tg-primary-light), var(--tg-shadow-light);
}

@keyframes pulse-shadow-playing {
  0% {
    box-shadow: 0 2px 8px rgba(0, 136, 204, 0.1);
  }
  50% {
    box-shadow: 0 2px 16px rgba(0, 136, 204, 0.2);
  }
  100% {
    box-shadow: 0 2px 8px rgba(0, 136, 204, 0.1);
  }
}

/* ===== СЕКЦИЯ ПАТТЕРНОВ ===== */
.top-section {
  padding: var(--spacing-md);
  display: flex;
  flex-direction: column;
  gap: 6px;
  overflow: hidden;
  min-height: 0;
  height: 40vh;
}

.pattern {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-height: 0;
  counter-reset: div-counter;
  position: relative;
  -webkit-overflow-scrolling: touch;
}

/* Кастомный скроллбар */
.pattern::-webkit-scrollbar {
  width: 3px;
}

.pattern::-webkit-scrollbar-track {
  background: transparent;
  border-radius: 10px;
}

.pattern::-webkit-scrollbar-thumb {
  background: var(--tg-primary-light);
  border-radius: 10px;
}

/* ===== ЗАГОЛОВОЧНАЯ СТРОКА ===== */
.labels-row {
  position: sticky;
  top: 0;
  background: var(--tg-bg-elevated);
  z-index: 10;
  display: flex;
  gap: var(--spacing-sm);
  padding: 4px 0px 0px 0px;
  border-bottom: 1px solid var(--tg-border-light);
  margin-bottom: 2px;
  justify-content: space-around;
}

.labels-row .label {
  display: flex;
  text-align: center;
  padding: 1px 0;
  font-size: 0.5em;
  color: var(--tg-text-secondary);
  font-weight: 600;
  letter-spacing: 0.2px;
}

.labels-row::before {
  display: flex;
  text-align: center;
  padding: 1px 0;
  font-size: 0.5em;
  color: var(--tg-text-secondary);
  font-weight: 600;
  letter-spacing: 0.2px;
  content: 'STEP';
}

/* ===== СТРОКИ ПАТТЕРНА ===== */
.pattern .row {
  counter-increment: div-counter;
  position: relative;
  display: flex;
  gap: var(--spacing-xs);
  align-items: center;
  padding: 4px 0;
  border-radius: var(--border-radius-small);
  transition: all 0.2s ease;
}

.pattern .row::before {
  content: counter(div-counter, decimal-leading-zero);
  display: inline-block;
  text-align: right;
  font-size: 0.7em;
  margin-right: 2px;
  min-width: 2.2ch;
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  color: var(--tg-text-tertiary);
  font-weight: 500;
}

/* Подсветка номера строки */
.pattern .row:focus-within::before {
  font-weight: 700;
  color: var(--tg-primary);
  background: rgba(0, 136, 204, 0.08);
  padding: 2px 3px;
  border-radius: 3px;
}

.pattern .row.playing::before {
  font-weight: 700;
  color: var(--tg-primary);
  background: rgba(0, 136, 204, 0.12);
  padding: 2px 3px;
  border-radius: 3px;
}

.pattern .row.playing:focus-within::before {
  background: rgba(0, 136, 204, 0.15);
}

.pattern .row:focus-within {
  background: var(--tg-bg-secondary);
  border-radius: var(--border-radius-small);
}

/* ===== ЭЛЕМЕНТЫ СТРОКИ ===== */
.box {
  min-width: 3ch;
  width: 100%;
  padding: 6px 0px 6px 0px;
  height: 1.5em;
  font-size: 1em;
  flex: 1;
  vertical-align: middle;
}

.button {
  min-width: 3ch;
  width: 100%;
  height: 1.5em;
  font-size: 1em;
  flex: 1;
  vertical-align: middle;
}

.box.focused {
  background: var(--tg-bg-secondary) !important;
  box-shadow: 0 0 0 2px var(--tg-primary-light), var(--tg-shadow-light);
}

/* Подсветка играющей строки */
.row.playing .box,
.row.playing .button {
  background: rgba(0, 136, 204, 0.1) !important;
  border-color: var(--tg-primary-light);
}

/* Пустые значения */
.box[data-value="non"] {
  color: transparent !important;
}

.box[data-value="non"]::after {
  content: "...";
  color: var(--tg-text-tertiary);
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  font-weight: 600;
}

/* ===== СЕКЦИЯ ПОДСКАЗОК ===== */
.bottom-section {
  padding: var(--spacing-md);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  min-height: 0;
  align-content: flex-start;
  /* Убрана фиксированная высота и добавлен отступ сверху */
  flex: 1;
  margin-bottom: 5px;
}

.suggestions {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(6em, 1fr));
  gap: var(--spacing-sm);
  align-content: start;
  justify-items: stretch;
  padding: var(--spacing-xs);
  margin: 0 -2px;
}

.suggestion {
  padding: 8px 6px;
  min-height: 32px;
  height: 32px;
  width: 100%;
  font-size: 1em;
  margin: 0 2px;
}

.suggestion:hover {
  transform: translateY(-1px);
}

/* ===== ТЕМНАЯ ТЕМА ===== */
@media (prefers-color-scheme: dark) {
  :root {
    --tg-primary: #2aabee;
    --tg-primary-light: #5ab5e6;
    --tg-primary-dark: #1e8bc4;
    --tg-bg: #1a1a1c;
    --tg-bg-secondary: #242426;
    --tg-bg-elevated: #2a2a2c;
    --tg-text-primary: #ffffff;
    --tg-text-secondary: #a0a0a5;
    --tg-text-tertiary: #7a7a7f;
    --tg-border: #3a3a3c;
    --tg-border-light: #404044;
    --tg-shadow: 0 2px 12px rgba(0, 0, 0, 0.2);
    --tg-shadow-light: 0 1px 3px rgba(0, 0, 0, 0.15);
  }
  
  body {
    background: var(--tg-bg);
    color: var(--tg-text-primary);
  }
  
  .pattern .row:focus-within::before {
    background: rgba(42, 171, 238, 0.15);
  }
  
  .pattern .row.playing::before {
    background: rgba(42, 171, 238, 0.2);
  }
  
  .row.playing .box,
  .row.playing .button {
    background: rgba(42, 171, 238, 0.15) !important;
  }
  
  @keyframes pulse-shadow-playing-dark {
    0% {
      box-shadow: 0 2px 8px rgba(42, 171, 238, 0.15);
    }
    50% {
      box-shadow: 0 2px 16px rgba(42, 171, 238, 0.25);
    }
    100% {
      box-shadow: 0 2px 8px rgba(42, 171, 238, 0.15);
    }
  }
  
  .control[data-type="playAllButton"].playing {
    animation: pulse-shadow-playing-dark 1.5s ease-in-out infinite;
  }
}

/* ===== АДАПТИВНОСТЬ ===== */
/* ProMotion displays */
@media (pointer: fine) {
  .control,
  .box,
  .button,
  .suggestion {
    transition-duration: 0.15s;
  }
}

/* Маленькие экраны */
@media (max-width: 360px) {
  .main-container {
    width: 98%;
    max-width: 360px;
    gap: var(--spacing-xs);
  }
  
  .control-section-top,
  .control-section-bottom {
    padding: var(--spacing-sm);
    gap: 2px;
  }
  
  .control-label {
    font-size: 0.5em;
    width: 7ch;
  }
  
  .labels-row .label {
    font-size: 0.5em;
    min-width: 2.5ch;
  }
  
  .control,
  .box,
  .button {
    padding: 4px 1px;
    font-size: 1em;
  }
  
  .suggestions {
    grid-template-columns: repeat(auto-fit, minmax(5.5em, 1fr));
    gap: var(--spacing-xs);
  }
  
  .suggestion {
    padding: 6px 4px;
    min-height: 28px;
    height: 28px;
    font-size: 1em;
  }
  
  .top-section {
    gap: 4px;
    padding: var(--spacing-sm);
  }
  
  .pattern {
    gap: 2px;
  }
  
  .pattern .row {
    padding: 2px 0;
  }
  
  .box,
  .button {
    min-width: 2.5ch;
  }
  
  /* Адаптивный отступ для маленьких экранов */
  .bottom-section {
    margin-top: 4px;
  }
}

/* iOS оптимизация */
@supports (-webkit-touch-callout: none) {
  .main-container {
    -webkit-overflow-scrolling: touch;
  }
}

/* ===== ПЛАВНЫЕ ПЕРЕХОДЫ ===== */
.control,
.box,
.button,
.suggestion,
.pattern .row::before {
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}