/* ===== Reset & Base ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --primary: #6C5CE7;
  --primary-light: #A29BFE;
  --primary-dark: #5849C2;
  --accent: #00CEC9;
  --accent-light: #55EFC4;
  --bg: #F8F9FD;
  --card-bg: #FFFFFF;
  --text: #2D3436;
  --text-light: #636E72;
  --text-lighter: #B2BEC3;
  --border: #E9ECF0;
  --danger: #FF6B6B;
  --warning: #FECA57;
  --success: #55EFC4;
  --shadow: 0 2px 16px rgba(108, 92, 231, 0.08);
  --shadow-hover: 0 8px 32px rgba(108, 92, 231, 0.16);
  --radius: 16px;
  --radius-sm: 10px;
  --max-width: 640px;
  --header-h: 56px;
  --tabbar-h: 60px;
}

html { font-size: 16px; -webkit-text-size-adjust: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Noto Sans SC', Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

a { color: var(--primary); text-decoration: none; }
button { font-family: inherit; cursor: pointer; border: none; background: none; }
input, textarea, select { font-family: inherit; font-size: inherit; }

/* ===== Layout ===== */
#app { min-height: 100vh; }

.page {
  max-width: var(--max-width);
  margin: 0 auto;
  padding: calc(var(--header-h) + 12px) 12px calc(var(--tabbar-h) + 24px);
  animation: fadeIn 0.3s ease;
}

@keyframes fadeIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

/* ===== Header ===== */
.header {
  position: fixed; top: 0; left: 0; right: 0; z-index: 100;
  height: var(--header-h);
  background: rgba(255,255,255,0.92);
  backdrop-filter: saturate(180%) blur(12px);
  -webkit-backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 16px;
  max-width: var(--max-width);
  margin: 0 auto;
}

.header .logo { font-weight: 800; font-size: 1.15rem; display: flex; align-items: center; gap: 6px; }
.header .logo span { font-size: 1.4rem; }
.header .user-btn { font-size: 1.5rem; background: none; padding: 4px 8px; border-radius: 8px; }
.header .user-btn:active { background: var(--border); }

/* ===== Tab Bar ===== */
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 100;
  height: var(--tabbar-h);
  background: rgba(255,255,255,0.92);
  backdrop-filter: saturate(180%) blur(12px);
  -webkit-backdrop-filter: saturate(180%) blur(12px);
  border-top: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-around;
  max-width: var(--max-width);
  margin: 0 auto;
  padding-bottom: env(safe-area-inset-bottom, 0);
}

.tabbar button {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  font-size: 0.7rem; color: var(--text-lighter); padding: 6px 12px;
  border-radius: 10px; transition: color 0.2s;
  min-width: 52px;
}
.tabbar button .tab-icon { font-size: 1.4rem; }
.tabbar button.active { color: var(--primary); font-weight: 600; }
.tabbar button:active { transform: scale(0.92); }

/* ===== Components ===== */

/* Hero */
.hero {
  text-align: center; padding: 20px 0 16px;
}
.hero h1 { font-size: 1.5rem; margin-bottom: 6px; }
.hero p { color: var(--text-light); font-size: 0.9rem; }

/* Search */
.search-box {
  display: flex; gap: 8px; margin-bottom: 14px;
}
.search-box input {
  flex: 1; padding: 10px 14px; border: 1px solid var(--border);
  border-radius: var(--radius-sm); background: var(--card-bg); outline: none;
  transition: border-color 0.2s;
}
.search-box input:focus { border-color: var(--primary-light); }
.search-box button {
  padding: 0 16px; background: var(--primary); color: #fff;
  border-radius: var(--radius-sm); font-weight: 600; white-space: nowrap;
}
.search-box button:active { background: var(--primary-dark); }

/* Category chips */
.categories {
  display: flex; gap: 8px; overflow-x: auto; padding-bottom: 10px;
  -webkit-overflow-scrolling: touch; scrollbar-width: none;
}
.categories::-webkit-scrollbar { display: none; }
.chip {
  flex-shrink: 0; padding: 6px 14px; border-radius: 20px;
  background: var(--card-bg); border: 1px solid var(--border);
  font-size: 0.85rem; color: var(--text-light); white-space: nowrap;
  transition: all 0.2s;
}
.chip.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.chip:active { transform: scale(0.95); }

/* Task Card */
.task-card {
  background: var(--card-bg); border-radius: var(--radius); padding: 16px;
  margin-bottom: 12px; box-shadow: var(--shadow);
  transition: box-shadow 0.2s, transform 0.15s;
  cursor: pointer; border: 1px solid transparent;
}
.task-card:active { transform: scale(0.985); box-shadow: var(--shadow-hover); }

.task-card .tc-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.task-card .tc-author { display: flex; align-items: center; gap: 6px; font-size: 0.8rem; color: var(--text-light); }
.task-card .tc-author .avatar { font-size: 1.1rem; }
.task-card .tc-title { font-weight: 700; font-size: 1.05rem; margin-bottom: 6px; line-height: 1.4; }
.task-card .tc-desc { font-size: 0.88rem; color: var(--text-light); line-height: 1.5;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.task-card .tc-footer { display: flex; align-items: center; gap: 8px; margin-top: 12px; flex-wrap: wrap; }

.tag {
  font-size: 0.75rem; padding: 3px 10px; border-radius: 12px; font-weight: 600;
}
.tag-cat { background: #E8F0FE; color: #4285F4; }
.tag-difficulty-easy { background: #E8F8E8; color: #00B894; }
.tag-difficulty-medium { background: #FFF8E1; color: #F39C12; }
.tag-difficulty-hard { background: #FFEBEE; color: #E74C3C; }
.tag-reward { background: #F3E8FF; color: var(--primary); }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 10px 20px; border-radius: var(--radius-sm); font-weight: 600;
  font-size: 0.9rem; transition: all 0.2s; width: 100%;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:active { background: var(--primary-dark); transform: scale(0.97); }
.btn-outline { border: 1.5px solid var(--primary); color: var(--primary); background: transparent; }
.btn-outline:active { background: var(--primary); color: #fff; }
.btn-danger { background: var(--danger); color: #fff; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* Forms */
.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-size: 0.85rem; font-weight: 600; margin-bottom: 6px; color: var(--text-light); }
.form-group input, .form-group textarea, .form-group select {
  width: 100%; padding: 12px 14px; border: 1.5px solid var(--border);
  border-radius: var(--radius-sm); background: var(--card-bg); outline: none;
  transition: border-color 0.2s;
}
.form-group input:focus, .form-group textarea:focus, .form-group select:focus { border-color: var(--primary); }
.form-group textarea { resize: vertical; min-height: 100px; }

/* Auth card */
.auth-card {
  background: var(--card-bg); border-radius: var(--radius); padding: 28px 20px;
  box-shadow: var(--shadow); margin-top: 40px;
}
.auth-card h2 { text-align: center; margin-bottom: 20px; font-size: 1.3rem; }
.auth-card .auth-error { color: var(--danger); font-size: 0.85rem; margin-bottom: 10px; text-align: center; }
.auth-card .switch-auth { text-align: center; margin-top: 16px; font-size: 0.85rem; color: var(--text-light); }
.auth-card .switch-auth a { font-weight: 600; }

/* Task Detail */
.detail-back {
  display: inline-flex; align-items: center; gap: 4px; font-size: 0.9rem;
  color: var(--text-light); margin-bottom: 12px; padding: 4px 8px; border-radius: 8px;
}
.detail-back:active { background: var(--border); }

.detail-card { background: var(--card-bg); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); }
.detail-card h1 { font-size: 1.3rem; margin-bottom: 12px; line-height: 1.4; }
.detail-meta { display: flex; align-items: center; gap: 8px; margin-bottom: 16px; flex-wrap: wrap; }
.detail-desc { font-size: 0.95rem; line-height: 1.7; color: var(--text); margin-bottom: 20px; }

.detail-author { display: flex; align-items: center; gap: 10px; padding: 12px; background: var(--bg); border-radius: var(--radius-sm); margin-bottom: 16px; }
.detail-author .avatar { font-size: 1.8rem; }
.detail-author .info { flex: 1; }
.detail-author .name { font-weight: 600; }
.detail-author .time { font-size: 0.78rem; color: var(--text-lighter); }

/* Comments */
.comments-section { margin-top: 20px; }
.comments-section h3 { font-size: 1rem; margin-bottom: 12px; }
.comment-item { display: flex; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.comment-item .avatar { font-size: 1.3rem; flex-shrink: 0; }
.comment-item .c-body { flex: 1; }
.comment-item .c-name { font-size: 0.8rem; font-weight: 600; color: var(--text-light); }
.comment-item .c-text { font-size: 0.9rem; margin-top: 2px; }
.comment-item .c-time { font-size: 0.72rem; color: var(--text-lighter); }

.comment-form { display: flex; gap: 8px; margin-top: 14px; }
.comment-form input { flex: 1; padding: 10px 14px; border: 1px solid var(--border); border-radius: var(--radius-sm); outline: none; }
.comment-form input:focus { border-color: var(--primary); }
.comment-form button { padding: 0 16px; background: var(--primary); color: #fff; border-radius: var(--radius-sm); font-weight: 600; white-space: nowrap; }

/* Profile */
.profile-header { text-align: center; padding: 20px 0; }
.profile-header .avatar { font-size: 4rem; }
.profile-header h2 { font-size: 1.2rem; margin-top: 8px; }
.profile-header p { color: var(--text-light); font-size: 0.88rem; margin-top: 4px; }
.profile-stats { display: flex; justify-content: space-around; padding: 16px; background: var(--card-bg); border-radius: var(--radius); margin: 16px 0; box-shadow: var(--shadow); }
.profile-stats .stat { text-align: center; }
.profile-stats .stat .num { font-size: 1.3rem; font-weight: 800; color: var(--primary); }
.profile-stats .stat .label { font-size: 0.75rem; color: var(--text-light); }

/* Leaderboard */
.lb-item { display: flex; align-items: center; gap: 12px; padding: 14px 16px; background: var(--card-bg); border-radius: var(--radius); margin-bottom: 8px; box-shadow: var(--shadow); }
.lb-item .rank { font-size: 1.1rem; font-weight: 800; width: 28px; text-align: center; }
.lb-item .rank-1 { color: #FFD700; }
.lb-item .rank-2 { color: #C0C0C0; }
.lb-item .rank-3 { color: #CD7F32; }
.lb-item .avatar { font-size: 1.8rem; }
.lb-item .info { flex: 1; }
.lb-item .name { font-weight: 600; }
.lb-item .pts { font-size: 0.82rem; color: var(--text-light); }
.lb-item .points { font-weight: 700; color: var(--primary); }

/* Empty state */
.empty { text-align: center; padding: 60px 20px; color: var(--text-lighter); }
.empty .emoji { font-size: 3rem; margin-bottom: 12px; }
.empty p { font-size: 0.9rem; }

/* Toast */
.toast {
  position: fixed; top: 70px; left: 50%; transform: translateX(-50%);
  background: rgba(45,52,54,0.92); color: #fff; padding: 10px 20px;
  border-radius: 24px; font-size: 0.85rem; z-index: 9999;
  backdrop-filter: blur(8px); animation: toastIn 0.3s ease;
  max-width: 90%; text-align: center;
}
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, -10px); } to { opacity: 1; transform: translate(-50%, 0); } }

/* Modal / Fullscreen form */
.modal-overlay {
  position: fixed; inset: 0; z-index: 200; background: var(--bg);
  overflow-y: auto; -webkit-overflow-scrolling: touch;
}
.modal-header {
  position: sticky; top: 0; background: rgba(255,255,255,0.92);
  backdrop-filter: blur(12px); border-bottom: 1px solid var(--border);
  padding: 12px 16px; display: flex; align-items: center; gap: 12px;
  z-index: 10; max-width: var(--max-width); margin: 0 auto;
}
.modal-header .close { font-size: 1.3rem; color: var(--text-light); padding: 4px 8px; border-radius: 8px; }
.modal-header .close:active { background: var(--border); }
.modal-header h2 { font-size: 1.1rem; }
.modal-body { max-width: var(--max-width); margin: 0 auto; padding: 16px; }

/* Avatar picker */
.avatar-picker { display: flex; flex-wrap: wrap; gap: 8px; }
.avatar-picker button { font-size: 1.6rem; padding: 8px; border-radius: 10px; border: 2px solid transparent; background: var(--bg); }
.avatar-picker button.selected { border-color: var(--primary); background: #F3E8FF; }

/* Difficulty selector */
.diff-selector { display: flex; gap: 8px; }
.diff-selector button { flex: 1; padding: 10px; border-radius: var(--radius-sm); border: 1.5px solid var(--border); font-weight: 600; font-size: 0.85rem; }
.diff-selector button.selected { border-color: var(--primary); background: #F3E8FF; color: var(--primary); }

/* ===== Welcome Modal ===== */
.welcome-modal { position: fixed; inset: 0; z-index: 9999; display: flex; align-items: center; justify-content: center; }
.welcome-overlay { position: absolute; inset: 0; background: rgba(0,0,0,0.4); backdrop-filter: blur(4px); animation: fadeIn 0.3s; }
.welcome-box {
  position: relative; background: var(--card-bg); border-radius: 24px; padding: 32px 24px;
  text-align: center; max-width: 320px; width: 88%; box-shadow: 0 12px 48px rgba(108,92,231,0.24);
  animation: welcomePop 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.welcome-avatar { font-size: 3.5rem; margin-bottom: 12px; }
.welcome-text { font-size: 1rem; color: var(--text); margin-bottom: 20px; line-height: 1.6; font-weight: 500; }
@keyframes welcomePop { from { opacity: 0; transform: scale(0.8) translateY(20px); } to { opacity: 1; transform: scale(1) translateY(0); } }

/* ===== Desktop (responsive) ===== */
@media (min-width: 768px) {
  :root {
    --max-width: 800px;
    --header-h: 64px;
    --tabbar-h: 64px;
  }
  html { font-size: 17px; }
  .hero h1 { font-size: 1.8rem; }
  .hero p { font-size: 1rem; }
  .task-card { padding: 20px; }
  .task-card .tc-title { font-size: 1.15rem; }
  .detail-card { padding: 28px; }
  .detail-card h1 { font-size: 1.5rem; }
}

@media (min-width: 1024px) {
  :root { --max-width: 900px; }
  .task-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
  .task-grid .task-card { margin-bottom: 0; }
}

/* ===== Data Table ===== */
.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  background: var(--card-bg);
}
.table-wrap::-webkit-scrollbar { height: 4px; }
.table-wrap::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
  min-width: 500px;
}
.data-table thead {
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
}
.data-table thead th {
  color: #fff;
  font-weight: 600;
  font-size: 0.78rem;
  padding: 10px 12px;
  text-align: left;
  white-space: nowrap;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}
.data-table thead th:first-child { border-top-left-radius: var(--radius); }
.data-table thead th:last-child { border-top-right-radius: var(--radius); }
.data-table tbody tr {
  border-bottom: 1px solid var(--border);
  transition: background 0.15s;
}
.data-table tbody tr:nth-child(even) { background: rgba(108, 92, 231, 0.02); }
.data-table tbody tr:active { background: rgba(108, 92, 231, 0.06); }
.data-table tbody tr:last-child { border-bottom: none; }
.data-table tbody td {
  padding: 8px 12px;
  vertical-align: middle;
}

@media (min-width: 768px) {
  .data-table { min-width: 600px; font-size: 0.88rem; }
  .data-table thead th { padding: 12px 16px; }
  .data-table tbody td { padding: 10px 16px; }
}

/* Safe area for iPhone notch */
@supports (padding: env(safe-area-inset-top)) {
  .header { padding-top: env(safe-area-inset-top, 0); height: calc(var(--header-h) + env(safe-area-inset-top, 0)); }
}
