/* === Reset & Base === */
* { margin: 0; padding: 0; box-sizing: border-box; }
html { font-size: 16px; -webkit-text-size-adjust: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: #f5f5f7;
  color: #1d1d1f;
  min-height: 100vh;
  padding-bottom: env(safe-area-inset-bottom);
}

/* === Header === */
.header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: #fff;
  padding: 12px 16px 8px;
  border-bottom: 1px solid #e5e5e7;
}
.header h1 {
  font-size: 20px;
  font-weight: 700;
  margin-bottom: 8px;
}
.search-bar {
  position: relative;
  display: flex;
  align-items: center;
}
.search-bar input {
  width: 100%;
  padding: 10px 40px 10px 14px;
  border: 1px solid #d2d2d7;
  border-radius: 10px;
  font-size: 16px; /* prevent iOS zoom */
  background: #f5f5f7;
  outline: none;
  transition: border-color 0.2s;
}
.search-bar input:focus {
  border-color: #0071e3;
  background: #fff;
}
.btn-clear {
  position: absolute;
  right: 8px;
  background: #8e8e93;
  color: #fff;
  border: none;
  border-radius: 50%;
  width: 22px;
  height: 22px;
  font-size: 12px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* === Tabs === */
.tabs {
  display: flex;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  background: #fff;
  padding: 0 12px;
  border-bottom: 1px solid #e5e5e7;
  position: sticky;
  top: 89px; /* below header */
  z-index: 99;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  flex-shrink: 0;
  padding: 10px 14px;
  border: none;
  background: none;
  font-size: 14px;
  color: #8e8e93;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  white-space: nowrap;
  transition: all 0.2s;
}
.tab.active {
  color: #0071e3;
  border-bottom-color: #0071e3;
  font-weight: 600;
}

/* === Product Selector === */
.product-selector {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  background: #fff;
  border-bottom: 1px solid #e5e5e7;
}
.product-selector label {
  font-size: 14px;
  color: #8e8e93;
  flex-shrink: 0;
}
.product-selector select {
  flex: 1;
  padding: 8px 12px;
  border: 1px solid #d2d2d7;
  border-radius: 8px;
  font-size: 15px;
  background: #f5f5f7;
  color: #1d1d1f;
  outline: none;
  -webkit-appearance: none;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%238e8e93' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 32px;
}

/* === Template List === */
.template-list {
  padding: 8px 16px 80px;
}
.template-card {
  background: #fff;
  border-radius: 12px;
  padding: 14px 16px;
  margin-bottom: 8px;
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s;
  border: 1px solid #e5e5e7;
}
.template-card:active {
  transform: scale(0.98);
  box-shadow: 0 1px 4px rgba(0,0,0,0.1);
}
.card-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}
.badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: 6px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.3px;
}
.badge-A { background: #e8f5e9; color: #2e7d32; }
.badge-B { background: #e3f2fd; color: #1565c0; }
.badge-C { background: #fff3e0; color: #ef6c00; }
.badge-D { background: #fce4ec; color: #c62828; }
.badge-E { background: #f3e5f5; color: #7b1fa2; }
.badge-F { background: #e0f2f1; color: #00695c; }
.badge-M { background: #fff9c4; color: #f57f17; }
.badge-CUS { background: #e8eaf6; color: #283593; }
.badge-GUIDE { background: #efebe9; color: #4e342e; }
.card-keyword {
  font-size: 15px;
  font-weight: 600;
  flex: 1;
}
.card-preview {
  font-size: 13px;
  color: #8e8e93;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.card-has-image {
  display: inline-block;
  font-size: 11px;
  color: #8e8e93;
  margin-top: 4px;
}

/* === Modal === */
.modal {
  display: none;
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  z-index: 200;
  background: rgba(0,0,0,0.4);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  align-items: flex-end;
  justify-content: center;
}
.modal.show {
  display: flex;
}
.modal-content {
  background: #fff;
  border-radius: 16px 16px 0 0;
  width: 100%;
  max-width: 500px;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  animation: slideUp 0.25s ease-out;
}
@keyframes slideUp {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}
.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 16px 0;
}
.modal-content h2 {
  font-size: 18px;
  font-weight: 700;
  padding: 8px 16px 0;
}
.btn-close {
  background: #e5e5e7;
  border: none;
  border-radius: 50%;
  width: 30px;
  height: 30px;
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #8e8e93;
}
.modal-body {
  flex: 1;
  overflow-y: auto;
  padding: 12px 16px;
}
.rendered-content {
  font-size: 15px;
  line-height: 1.7;
  white-space: pre-wrap;
  word-break: break-word;
  background: #f9f9fb;
  border-radius: 10px;
  padding: 14px;
  border: 1px solid #e5e5e7;
}
.var-highlight {
  background: #fff3cd;
  padding: 1px 4px;
  border-radius: 4px;
  font-weight: 500;
}
.modal-image {
  margin-top: 12px;
  text-align: center;
}
.modal-image img {
  max-width: 100%;
  border-radius: 10px;
  border: 1px solid #e5e5e7;
}
.modal-actions {
  padding: 12px 16px;
  padding-bottom: calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid #e5e5e7;
}
.btn-copy {
  width: 100%;
  padding: 16px;
  background: #0071e3;
  color: #fff;
  border: none;
  border-radius: 12px;
  font-size: 17px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: background 0.2s;
  min-height: 52px; /* touch friendly */
}
.btn-copy:active {
  background: #0062cc;
}
.btn-copy.copied {
  background: #34c759;
}
.copy-toast {
  text-align: center;
  padding: 8px;
  color: #34c759;
  font-weight: 600;
  font-size: 14px;
  opacity: 0;
  transition: opacity 0.3s;
}
.copy-toast.show {
  opacity: 1;
}

/* === Language Toggle === */
.lang-toggle {
  position: fixed;
  bottom: 16px;
  right: 16px;
  display: flex;
  gap: 4px;
  background: #fff;
  border-radius: 10px;
  padding: 4px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.15);
  z-index: 50;
}
.lang-btn {
  padding: 8px 14px;
  border: none;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  background: transparent;
  color: #8e8e93;
  transition: all 0.2s;
}
.lang-btn.active {
  background: #0071e3;
  color: #fff;
}

/* === Empty State === */
.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: #8e8e93;
}
.empty-state .emoji {
  font-size: 48px;
  margin-bottom: 12px;
}
.empty-state p {
  font-size: 15px;
}

/* === Count Badge === */
.result-count {
  padding: 6px 16px;
  font-size: 12px;
  color: #8e8e93;
}

/* === Desktop === */
@media (min-width: 768px) {
  .template-list {
    max-width: 600px;
    margin: 0 auto;
  }
  .modal-content {
    border-radius: 16px;
    margin-bottom: 40px;
    max-height: 70vh;
  }
  .modal {
    align-items: center;
  }
}
