:root {
  --bg: #f7f8fa;
  --card: #ffffff;
  --text: #1f2430;
  --muted: #6b7280;
  --accent: #4f8cff;
  --accent-dark: #3a6fd8;
  --border: #e5e7eb;
  --radius: 16px;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
  
  /* ИСПРАВЛЕНИЕ ЦЕНТРИРОВАНИЯ И ОТСТУПОВ */
  display: flex;
  align-items: flex-start;
  justify-content: center;
  
  /* Добавляем отступы безопасности для iOS/Android (вырезы, скругления) */
  padding: calc(16px + env(safe-area-inset-top)) 
           calc(16px + env(safe-area-inset-right)) 
           calc(16px + env(safe-area-inset-bottom)) 
           calc(16px + env(safe-area-inset-left));
           
  box-sizing: border-box;
  width: 100%;
  margin: 0;
}
.app {
  background: var(--card);
  border-radius: 20px;
  box-shadow: 0 8px 40px rgba(0,0,0,0.08);
  width: 100%;
  max-width: 720px;
  min-height: 560px;
  padding: 28px 24px;
  display: flex;
  flex-direction: column;
  margin: 0 auto; /* <--- ДОБАВИТЬ ЭТО */
}
.screen { display: none; flex-direction: column; flex: 1; }
.screen.active { display: flex; }

.app-title { font-size: 26px; margin-bottom: 6px; }
.app-subtitle { color: var(--muted); font-size: 15px; margin-bottom: 24px; }

/* МЕНЮ — 5 рядов по 2 */
.menu-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}
.menu-btn {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 22px 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  transition: transform 0.1s, background 0.2s;
  -webkit-tap-highlight-color: transparent;
  text-align: center;
  min-height: 110px;
  justify-content: center;
}
.menu-btn:active { transform: scale(0.97); background: var(--border); }
.menu-btn.empty { opacity: 0.45; cursor: default; }
.menu-btn.empty:active { transform: none; background: var(--bg); }
.menu-icon { font-size: 30px; }
.menu-label { font-size: 14px; font-weight: 600; }
.menu-btn.empty .menu-label { font-weight: 400; color: var(--muted); }

/* ШАПКА ВИДЖЕТА */
.widget-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 20px;
}
.btn-back {
  background: transparent;
  border: none;
  color: var(--accent);
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  padding: 6px 0;
  -webkit-tap-highlight-color: transparent;
}
.widget-title { font-size: 20px; font-weight: 700; }
.widget-body { flex: 1; display: flex; flex-direction: column; }

/* СПРАВОЧНИК */
.search-wrap { margin-bottom: 16px; }
.search-input {
  width: 100%;
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: 12px;
  font-size: 16px;
  background: var(--bg);
  color: var(--text);
  outline: none;
}
.search-input:focus { border-color: var(--accent); }

.guide-list { display: flex; flex-direction: column; gap: 10px; }
.guide-item {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.guide-item:active { background: var(--border); }
.guide-item-title { font-weight: 600; font-size: 15px; }
.guide-item-sub { font-size: 13px; color: var(--muted); margin-top: 2px; }

/* КАРТОЧКА ЗАПИСИ */
.guide-card { display: flex; flex-direction: column; gap: 12px; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field-label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.03em; }
.field-value { font-size: 16px; }

.empty-state {
  text-align: center;
  color: var(--muted);
  padding: 40px 20px;
  font-size: 15px;
}

@media (max-width: 480px) {
  .calc-field {
    padding: 10px 12px;
  }
  .calc-field label {
    font-size: 13px;
  }
  .input-with-unit input, .calc-qty-input {
    width: 70px;
    padding: 6px 8px;
    font-size: 15px;
  }
}
/* ============ КАЛЬКУЛЯТОР ============ */
.calc-header {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 20px;
  background: var(--bg);
  padding: 16px;
  border-radius: 12px;
}
.calc-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.calc-row label { font-weight: 600; font-size: 14px; }
.calc-select {
  padding: 8px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  font-size: 15px;
  width: 60%;
  outline: none;
}
.calc-select:focus { border-color: var(--accent); }

.calc-body {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 20px;
}
.calc-field {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--bg);
  padding: 12px 16px;
  border-radius: 10px;
}
.calc-field label { font-size: 14px; font-weight: 500; }
.input-with-unit {
  display: flex;
  align-items: center;
  gap: 8px;
}
.input-with-unit input {
  width: 90px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 16px;
  text-align: right;
  background: var(--card);
  color: var(--text);
  outline: none;
}
.input-with-unit input:focus { border-color: var(--accent); }
.input-with-unit span { font-size: 13px; color: var(--muted); width: 24px; }

.calc-qty-input {
  width: 90px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 16px;
  text-align: right;
  background: var(--card);
  color: var(--text);
  outline: none;
}
.calc-qty-input:focus { border-color: var(--accent); }

.btn-calc {
  width: 100%;
  padding: 16px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
  -webkit-tap-highlight-color: transparent;
  margin-bottom: 20px;
}
.btn-calc:active { background: var(--accent-dark); }

.calc-result {
  background: #eef3ff;
  border: 2px solid var(--accent);
  border-radius: 12px;
  padding: 20px;
  text-align: center;
}
.calc-result-label { font-size: 14px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; margin-bottom: 4px; }
.calc-result-value { font-size: 32px; font-weight: 700; color: var(--accent); }
/* ============ КНОПКИ КАЛЬКУЛЯТОРА ============ */
.calc-controls {
  display: flex;
  flex-direction: column;
  gap: 16px;
  margin-bottom: 20px;
  background: var(--bg);
  padding: 16px;
  border-radius: 12px;
}
.control-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.control-label {
  font-size: 13px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

/* Сетки для кнопок */
.btn-grid-5 {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
}
.btn-grid-2 {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

/* Сами кнопки */
.btn-toggle {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--muted);
  cursor: pointer;
  transition: all 0.2s;
  -webkit-tap-highlight-color: transparent;
  display: flex;
  align-items: center;
  justify-content: center;
  
  /* ДОБАВЛЯЕМ ЭТИ СВОЙСТВА, ЧТОБЫ КНОПКИ СТАЛИ КРУПНЕЕ */
  padding: 14px 10px; 
  font-size: 15px;
  font-weight: 600;
  min-height: 48px; /* Минимальная высота, чтобы точно не были полосками */
}

.btn-toggle:active {
  transform: scale(0.96);
}

.btn-toggle.active {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
  box-shadow: 0 2px 8px rgba(79, 140, 255, 0.3);
}

/* Кнопки формы (с иконками) */
.shape-btn {
  padding: 10px;
  height: 56px;
}
.shape-btn svg {
  width: 32px;
  height: 32px;
}

/* Кнопки материала (текстовые) */
.material-btn {
  padding: 12px;
  font-size: 15px;
  font-weight: 600;
}

/* ============ ЛИНЕЙНЫЙ РАСКРОЙ ============ */
.cut-section {
  background: var(--bg);
  padding: 16px;
  border-radius: 12px;
  margin-bottom: 16px;
}
.cut-sec-title {
  font-size: 15px;
  margin-bottom: 12px;
  color: var(--text);
  font-weight: 700;
}

/* Блоки ввода деталей */
.cut-parts-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 12px;
}
.cut-part-row {
  display: flex;
  align-items: center;
  gap: 8px;
}
.cut-part-row .input-with-unit { flex: 1; }
.cut-part-row .input-with-unit input { width: 100%; text-align: left; padding: 8px 10px; }
.cut-part-qty-wrap {
  display: flex;
  align-items: center;
  gap: 4px;
  flex: 1;
}
.cut-part-qty-wrap input {
  width: 100%;
  text-align: center;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 15px;
  color: var(--text);
  background: var(--card);
  outline: none;
}
.btn-qty-plus, .btn-qty-minus {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--accent);
  font-size: 18px;
  font-weight: bold;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
}
.btn-qty-plus:active, .btn-qty-minus:active { background: var(--border); }
.btn-remove-part {
  background: transparent;
  border: none;
  color: var(--crit);
  font-size: 18px;
  cursor: pointer;
  padding: 0 4px;
}

.btn-add-part {
  width: 100%;
  padding: 10px;
  background: transparent;
  border: 1px dashed var(--border);
  border-radius: 8px;
  color: var(--muted);
  font-weight: 600;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.btn-add-part:active { background: var(--bg); }

/* Блок действий */
.cut-actions {
  display: flex;
  gap: 12px;
  margin-bottom: 20px;
}
.btn-reset {
  padding: 14px;
  background: transparent;
  border: 2px solid var(--border);
  border-radius: 12px;
  color: var(--muted);
  font-weight: 600;
  cursor: pointer;
  flex: 1;
  -webkit-tap-highlight-color: transparent;
}
.btn-reset:active { background: var(--bg); }
.cut-actions .btn-calc { flex: 2; margin-top: 0; }

/* Результаты */
.result-box {
  background: var(--card);
  border: 1px solid var(--border);
  box-shadow: 0 4px 12px rgba(0,0,0,0.03);
}
.cut-stat {
  display: flex;
  justify-content: space-between;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}
.cut-stat:last-child { border-bottom: none; }

/* Карта раскроя */
.cut-maps {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.cut-map-row {
  border-bottom: 1px solid var(--border);
  padding-bottom: 12px;
}
.cut-map-row:last-child { border-bottom: none; padding-bottom: 0; }
.cut-map-header {
  font-size: 13px;
  font-weight: 700;
  color: var(--accent);
  margin-bottom: 6px;
}
.cut-map-body {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px;
}
.cut-tag {
  display: inline-block;
  border: 1px solid var(--accent);
  color: var(--accent);
  border-radius: 6px;
  padding: 2px 6px;
  font-size: 13px;
  font-weight: 600;
}
.cut-blade-mark {
  color: var(--muted);
  font-weight: 400;
  margin: 0 2px;
}
.cut-remainder {
  display: inline-block;
  border: 1px dashed var(--crit);
  color: var(--crit);
  border-radius: 6px;
  padding: 2px 6px;
  font-size: 13px;
  margin-left: 6px;
}

@media (max-width: 480px) {
  .cut-part-row { flex-wrap: wrap; }
  .cut-part-row .input-with-unit { flex: 1 1 100%; }
  .cut-part-row .cut-part-qty-wrap { flex: 1 1 70%; }
  .cut-actions { flex-direction: column; }
  .cut-actions .btn-reset, .cut-actions .btn-calc { flex: 1; }
}

/* ============ КАЛЬКУЛЯТОР РИСУНКОВ (PATTERN) ============ */
.pat-section {
  margin-bottom: 20px;
}

.pattern-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr); /* Изменено на 5 столбцов */
  gap: 8px; /* Чуть уменьшил отступы, чтобы на телефоне влезало */
}

.pattern-btn {
  background: var(--bg);
  border: 2px solid transparent;
  border-radius: 8px; /* Чуть меньше скругление для компактности */
  padding: 0;
  overflow: hidden;
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  flex-direction: column;
  -webkit-tap-highlight-color: transparent;
  box-shadow: 0 2px 6px rgba(0,0,0,0.04);
}

.pattern-btn:active {
  transform: scale(0.96);
}

.pattern-btn.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(79, 140, 255, 0.2);
}

.pattern-img-wrap {
  width: 100%;
  aspect-ratio: 1 / 1; /* Строго квадратная */
  background: var(--card);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  border-bottom: 1px solid var(--border);
}

.pattern-img-wrap img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

/* Заглушка на случай отсутствия картинки */
.pattern-fallback {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  font-weight: 700;
  color: var(--muted);
  opacity: 0.5;
  width: 100%;
  height: 100%;
  background: var(--border);
}

.pattern-num {
  padding: 4px;
  font-size: 12px;
  font-weight: 700;
  color: var(--text);
  background: var(--bg);
  width: 100%;
}

.pattern-btn.active .pattern-num {
  background: var(--accent);
  color: white;
}

/* Адаптация под мобильные (если 5 кнопок слишком мелкие) */
@media (max-width: 480px) {
  .pattern-grid {
    gap: 4px;
  }
  .pattern-num {
    padding: 2px;
    font-size: 11px;
  }
  .pattern-fallback {
    font-size: 16px;
  }
}

/* ============ СЕТКА ДЛЯ КНОПОК (ВИЗАРД) ============ */
.btn-grid-3 {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

/* Пример: если Telegram передал свой цвет фона, используем его */
body {
    background-color: var(--tg-theme-bg-color, #f7f8fa); /* fallback на наш цвет */
}

/* Можно также адаптировать цвет текста и акцентный цвет */