:root {
  /* 中国传统色 - 春节新年红 */
  --chinese-red: #ff2121;      /* 大红 - 春节主色 */
  --fire-red: #ff2d51;         /* 火红 - 鲜艳红 */
  --vermilion: #ff4c00;        /* 朱红 - 朱砂红 */
  --gold: #d9ac2a;             /* 金黄 - 春节金 */
  --light-gold: #f4e4a6;       /* 浅金 - 柔和金 */
  --dark-gold: #c49a00;        /* 深金 - 稳重金 */
  --bg: linear-gradient(135deg, #ff2121 0%, #ff6b6b 50%, #ff2121 100%);
  --surface: rgba(255, 255, 255, 0.12);
  --surface-hover: rgba(255, 255, 255, 0.2);
  --border: rgba(217, 172, 42, 0.3);
  --border-bright: rgba(217, 172, 42, 0.6);
  --text: #fffef9;
  --text-muted: #f4e4a6;
  --accent: var(--gold);
  --accent-dim: var(--dark-gold);
  --success: #52c41a;
  --error: #ff6b6b;
  --radius: 16px;
  --font: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "微软雅黑", -apple-system, BlinkMacSystemFont, sans-serif;
  --shadow: 0 4px 20px rgba(255, 33, 33, 0.3);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: var(--font);
  background: var(--bg);
  background-attachment: fixed;
  color: var(--text);
  line-height: 1.6;
  position: relative;
  /* PWA 全屏/独立模式下的安全区域（刘海屏、底部横条） */
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* 中国风背景纹理 - 喜庆光晕 */
body::before {
  content: '';
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image: 
    radial-gradient(circle at 20% 30%, rgba(255, 215, 0, 0.15) 0%, transparent 50%),
    radial-gradient(circle at 80% 70%, rgba(255, 255, 255, 0.1) 0%, transparent 50%);
  pointer-events: none;
  z-index: 0;
}

.container {
  max-width: 580px;
  margin: 0 auto;
  padding: 24px 16px;
  position: relative;
  z-index: 1;
}

header {
  text-align: center;
  margin-bottom: 32px;
  padding: 24px 16px;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.25) 0%, rgba(255, 215, 0, 0.2) 100%);
  border: 3px solid var(--gold);
  border-radius: var(--radius);
  box-shadow: 0 6px 30px rgba(0, 0, 0, 0.3), inset 0 1px 0 rgba(255, 255, 255, 0.3);
  position: relative;
  overflow: hidden;
}

/* 标题装饰 - 祥云图案 */
header::before,
header::after {
  content: '🐴';
  position: absolute;
  font-size: 3rem;
  opacity: 0.1;
  top: 50%;
  transform: translateY(-50%);
}

header::before {
  left: 10px;
}

header::after {
  right: 10px;
}

header h1 {
  font-size: 2rem;
  font-weight: 700;
  margin: 0 0 8px;
  color: #fffef9;
  text-shadow: 
    2px 2px 0 var(--gold),
    4px 4px 0 rgba(0, 0, 0, 0.2),
    0 0 20px rgba(255, 215, 0, 0.6);
  letter-spacing: 2px;
}

.subtitle {
  color: #fffef9;
  font-size: 0.95rem;
  margin: 0;
  font-weight: 500;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.3);
}

.upload-section {
  margin-bottom: 24px;
}

/* 同一格内：预览与拍照/相册二选一显示，不出现两块区域 */
.upload-slot {
  display: grid;
  grid-template-areas: "slot";
  grid-template-rows: auto;
  position: relative;
}
.upload-slot .preview-inline {
  grid-area: slot;
  z-index: 1;
}
.upload-slot .upload-entries {
  grid-area: slot;
  display: flex;
  flex-direction: column;
  align-items: stretch;
}
.upload-slot .upload-entries[hidden] {
  display: none;
}

.upload-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 160px;
  padding: 32px 24px;
  background: rgba(255, 255, 255, 0.15);
  border: 3px dashed rgba(255, 215, 0, 0.5);
  border-radius: var(--radius);
  cursor: pointer;
  transition: all 0.3s ease;
  backdrop-filter: blur(10px);
}

.upload-area:hover {
  border-color: var(--gold);
  background: var(--surface-hover);
  box-shadow: 0 0 25px rgba(217, 172, 42, 0.3);
  transform: translateY(-2px);
}

.upload-icon {
  display: block;
  font-size: 3rem;
  margin-bottom: 12px;
  line-height: 1;
}

.upload-text {
  display: block;
  font-weight: 600;
  font-size: 1.1rem;
  margin-bottom: 8px;
  color: #fffef9;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.3);
}

.upload-hint {
  display: block;
  font-size: 0.9rem;
  color: #fffef9;
  opacity: 0.9;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.3);
}

.upload-link {
  margin-top: 12px;
  text-align: center;
  font-size: 0.9rem;
  color: var(--gold);
  text-decoration: underline;
  cursor: pointer;
  transition: all 0.3s ease;
}

.upload-link:hover {
  color: #fffef9;
  text-shadow: 0 0 10px var(--gold);
}

.preview-inline {
  background: var(--surface);
  border: 3px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 0;
  overflow: hidden;
}

.preview-inline .preview-img {
  width: 100%;
  border-radius: 12px;
  margin-bottom: 12px;
  display: block;
  background: rgba(0, 0, 0, 0.2);
  border: 2px solid var(--border);
}

.preview-inline .btn-change {
  margin-top: 0;
  width: 100%;
}

/* 手牌区域：动画逐张出现 */
.hand-section {
  margin-bottom: 24px;
  background: var(--surface);
  padding: 20px 16px;
  border-radius: var(--radius);
  border: 2px solid var(--border);
}

.hand-section-title {
  display: block;
  font-size: 1rem;
  font-weight: 600;
  margin-bottom: 16px;
  color: var(--gold);
  text-align: center;
  letter-spacing: 1px;
}

.hand-tiles {
  display: flex;
  flex-wrap: nowrap;
  justify-content: flex-start;
  gap: 6px;
  overflow-x: auto;
  overflow-y: hidden;
  padding: 4px 0;
  -webkit-overflow-scrolling: touch;
}

.hand-tiles::-webkit-scrollbar {
  height: 4px;
}

.hand-tiles::-webkit-scrollbar-track {
  background: rgba(0, 0, 0, 0.2);
  border-radius: 2px;
}

.hand-tiles::-webkit-scrollbar-thumb {
  background: var(--gold);
  border-radius: 2px;
}

.hand-tiles::-webkit-scrollbar-thumb:hover {
  background: var(--light-gold);
}

/* 麻将牌：象牙白底、立体边 */
.tile {
  width: 32px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(180deg, #fffef9 0%, #e8dfd0 100%);
  border: 1px solid #d4c4a8;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.15);
  position: relative;
  overflow: hidden;
  animation: tile-in 0.4s ease-out backwards;
}

/* Unicode 麻将字符 */
.tile-face {
  font-size: 32px;
  line-height: 1;
  margin-top: -5px;
}

.tile-face-real {
  color: #000;
}

.tile-face-text {
  font-size: 12px;
  color: #333;
}

@keyframes tile-in {
  from {
    opacity: 0;
    transform: scale(0.7);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

.btn {
  padding: 12px 24px;
  border-radius: 10px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  border: none;
  font-family: inherit;
  transition: all 0.3s ease;
}

.btn.secondary {
  background: rgba(255, 255, 255, 0.2);
  color: #fffef9;
  border: 2px solid var(--gold);
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(10px);
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.3);
}

.btn.secondary:hover {
  background: rgba(255, 255, 255, 0.3);
  box-shadow: 0 6px 25px rgba(0, 0, 0, 0.4);
  transform: translateY(-2px);
  border-color: #fffef9;
}

.result-section {
  margin-bottom: 24px;
  background: var(--surface);
  padding: 20px 16px;
  border-radius: var(--radius);
  border: 2px solid var(--border);
}

.section-title {
  display: block;
  font-size: 1.1rem;
  font-weight: 600;
  margin-bottom: 16px;
  color: var(--gold);
  text-align: center;
  letter-spacing: 1px;
}

.result-card {
  background: rgba(255, 255, 255, 0.2);
  border: 3px solid var(--gold);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: 0 6px 30px rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(10px);
}

.result-row {
  margin-bottom: 16px;
  padding: 16px;
  background: rgba(255, 255, 255, 0.15);
  border-radius: 10px;
  border-left: 4px solid var(--gold);
}

.result-row:last-of-type {
  margin-bottom: 0;
}

.result-label {
  display: block;
  font-size: 0.85rem;
  color: #fffef9;
  margin-bottom: 6px;
  font-weight: 600;
  letter-spacing: 0.5px;
  opacity: 0.85;
}

.result-value {
  font-size: 1rem;
  color: #fffef9;
  line-height: 1.6;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.2);
}

.result-value.highlight {
  color: #fffef9;
  font-weight: 700;
  font-size: 1.4rem;
  text-shadow: 
    2px 2px 0 var(--gold),
    4px 4px 0 rgba(0, 0, 0, 0.2),
    0 0 20px rgba(255, 215, 0, 0.6);
}

/* 推荐出牌：与手牌同款牌面展示 */
.result-suggest-tile {
  display: flex;
  align-items: center;
  min-height: 44px;
}
.result-suggest-tile .tile-suggest {
  box-shadow: 0 2px 8px rgba(217, 172, 42, 0.4);
  border-color: var(--gold);
}
.result-value-fallback {
  color: var(--text-muted);
  font-size: 1.2rem;
}

.state-summary {
  margin-top: 16px;
  padding-top: 16px;
  border-top: 2px solid var(--border);
}

.state-summary-title {
  cursor: pointer;
  font-size: 0.95rem;
  color: var(--text-muted);
  font-weight: 600;
  padding: 8px;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.state-summary-title:hover {
  background: rgba(217, 172, 42, 0.12);
  color: var(--gold);
}

.state-summary-pre {
  display: block;
  margin-top: 12px;
  padding: 16px;
  background: rgba(0, 0, 0, 0.4);
  border-radius: 10px;
  font-size: 0.85rem;
  white-space: pre-wrap;
  word-break: break-all;
  color: var(--text-muted);
}

.error-section {
  padding: 20px;
  background: linear-gradient(135deg, rgba(255, 107, 107, 0.15) 0%, rgba(255, 33, 33, 0.1) 100%);
  border: 2px solid var(--error);
  border-radius: var(--radius);
  margin-bottom: 24px;
  box-shadow: 0 4px 15px rgba(255, 107, 107, 0.3);
}

.error-message {
  color: #ff7875;
  margin: 0 0 10px;
  font-weight: 600;
  font-size: 1rem;
}

.error-hint {
  font-size: 0.9rem;
  color: var(--text-muted);
  margin: 0;
}

.loading-section {
  text-align: center;
  padding: 40px 24px;
}

.spinner {
  width: 50px;
  height: 50px;
  margin: 0 auto 20px;
  border: 4px solid rgba(217, 172, 42, 0.2);
  border-top-color: var(--gold);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  box-shadow: 0 0 25px rgba(217, 172, 42, 0.4);
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.loading-text {
  color: var(--gold);
  font-weight: 500;
  font-size: 1rem;
}

footer {
  margin-top: 48px;
  padding-top: 24px;
  border-top: 2px solid var(--border);
  text-align: center;
  font-size: 0.85rem;
  color: var(--text-muted);
}

footer p {
  margin: 0;
  opacity: 0.8;
}

/* 响应式优化 */
@media (max-width: 480px) {
  .container {
    padding: 16px 12px;
  }
  
  header h1 {
    font-size: 1.6rem;
  }
  
  .upload-area {
    min-height: 140px;
    padding: 24px 16px;
  }
  
  .upload-icon {
    font-size: 2.5rem;
  }
  
  .tile {
    width: 28px;
    height: 38px;
  }
  
  .tile-face {
    font-size: 28px;
  }
  
  .result-value.highlight {
    font-size: 1.2rem;
  }
}

/* 节日装饰动画 */
@keyframes float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-10px); }
}

header::before,
header::after {
  animation: float 3s ease-in-out infinite;
}

header::before {
  animation-delay: 0s;
}

header::after {
  animation-delay: 1.5s;
}

/* 可编辑的手牌 */
.tile-editable {
  cursor: pointer;
  transition: all 0.2s ease;
}

.tile-editable:hover {
  transform: translateY(-4px) scale(1.05);
  box-shadow: 0 4px 12px rgba(217, 172, 42, 0.6);
  border-color: var(--gold);
}

.tile-editable::after {
  content: '✎';
  position: absolute;
  top: 2px;
  right: 2px;
  font-size: 8px;
  color: var(--gold);
  opacity: 0;
  transition: opacity 0.2s ease;
}

.tile-editable:hover::after {
  opacity: 1;
}

/* 牌选择器模态框 - 适配安全区与不同机型 */
.tile-selector-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(20px, env(safe-area-inset-top)) max(20px, env(safe-area-inset-right)) max(20px, env(safe-area-inset-bottom)) max(20px, env(safe-area-inset-left));
}

.tile-selector-modal[hidden] {
  display: none;
}

.tile-selector-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(5px);
}

.tile-selector-content {
  position: relative;
  max-width: min(600px, calc(100vw - 40px));
  width: 100%;
  max-height: min(80vh, calc(100dvh - 40px));
  min-height: 0;
  background: linear-gradient(135deg, rgba(255, 33, 33, 0.95) 0%, rgba(255, 107, 107, 0.95) 100%);
  border: 3px solid var(--gold);
  border-radius: var(--radius);
  box-shadow: 0 10px 50px rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
  animation: modal-in 0.3s ease-out;
}

@keyframes modal-in {
  from {
    opacity: 0;
    transform: scale(0.9) translateY(20px);
  }
  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

.tile-selector-header {
  padding: 20px;
  border-bottom: 2px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.tile-selector-header h3 {
  margin: 0;
  font-size: 1.3rem;
  color: #fffef9;
  text-shadow: 2px 2px 0 var(--gold), 0 0 20px rgba(255, 215, 0, 0.6);
}

.tile-selector-close {
  background: none;
  border: none;
  font-size: 2rem;
  line-height: 1;
  color: #fffef9;
  cursor: pointer;
  padding: 0;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: all 0.2s ease;
}

.tile-selector-close:hover {
  background: rgba(255, 255, 255, 0.2);
  transform: rotate(90deg);
}

.tile-selector-body {
  padding: 20px;
  overflow-y: auto;
  flex: 1;
  min-height: 0;
}

.tile-category {
  margin-bottom: 24px;
}

.tile-category:last-child {
  margin-bottom: 0;
}

.tile-category-title {
  font-size: 1rem;
  font-weight: 600;
  color: var(--gold);
  margin-bottom: 12px;
  text-align: center;
  letter-spacing: 1px;
}

/* 响应式网格：根据可用宽度自动列数，避免固定 7+2 造成的空隙 */
.tile-category-tiles {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(36px, 11vw), 1fr));
  gap: 8px 10px;
  justify-items: center;
}

.tile-selectable {
  cursor: pointer;
  transition: all 0.2s ease;
  width: 100%;
  max-width: 44px;
  aspect-ratio: 32 / 44;
  height: auto;
  display: flex;
  align-items: center;
  justify-content: center;
}

.tile-selectable .tile-face {
  font-size: clamp(14px, 4vw, 28px);
  line-height: 1;
  margin-top: -4px;
}

.tile-selectable:hover {
  transform: translateY(-4px) scale(1.1);
  box-shadow: 0 6px 20px rgba(217, 172, 42, 0.8);
  border-color: var(--gold);
  z-index: 10;
}

.tile-selectable:active {
  transform: translateY(-2px) scale(1.05);
}

/* 响应式优化 - 牌选择器按机型适配 */
@media (max-width: 480px) {
  .tile-selector-content {
    max-width: calc(100vw - 24px);
    max-height: min(90vh, calc(100dvh - 24px));
  }

  .tile-selector-header h3 {
    font-size: 1.1rem;
  }

  .tile-category-tiles {
    grid-template-columns: repeat(auto-fill, minmax(min(32px, 10vw), 1fr));
    gap: 6px 8px;
  }

  .tile-selectable {
    max-width: 40px;
  }
}

@media (min-width: 520px) {
  .tile-category-tiles {
    grid-template-columns: repeat(auto-fill, minmax(40px, 1fr));
    gap: 10px 12px;
  }

  .tile-selectable {
    max-width: 48px;
  }
}
