/* ── CSS 变量 ─────────────────────────────────────────────────────── */
:root {
  --primary:    #4A90E2;
  --primary-dk: #2c6fbd;
  --bg:         #f5f5f7;
  --surface:    #ffffff;
  --border:     #e0e0e0;
  --text:       #1c1c1e;
  --text-sub:   #6e6e73;
  --tab-h:      64px;
  --header-h:   44px;
  --radius:     16px;
  --shadow:     0 2px 12px rgba(0,0,0,.08);
}

/* ── Reset ───────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Helvetica Neue", sans-serif;
  font-size: 15px;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

/* ── Layout ──────────────────────────────────────────────────────── */
#app {
  position: fixed;
  top: 0; left: 0; right: 0;
  bottom: var(--tab-h);
  overflow: hidden;
}

.tab-content {
  display: none;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
}
.tab-content.active { display: flex; }

/* ── Bottom Tab Bar ──────────────────────────────────────────────── */
.tab-bar {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  height: var(--tab-h);
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  padding-bottom: env(safe-area-inset-bottom);
}

.tab-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-sub);
  transition: color .15s;
  padding: 6px 0;
}
.tab-btn.active { color: var(--primary); }
.tab-icon { font-size: 22px; }
.tab-label { font-size: 10px; font-weight: 500; }

/* ── Chat Tab ────────────────────────────────────────────────────── */
.chat-header {
  padding: 8px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.new-chat-btn {
  font-size: 13px;
  font-weight: 500;
  color: var(--primary);
  background: none;
  border: 1px solid var(--primary);
  border-radius: 14px;
  padding: 4px 12px;
  cursor: pointer;
  transition: all .15s;
}
.new-chat-btn:hover { background: var(--primary); color: #fff; }

.model-badge {
  display: inline-block;
  font-size: 11px;
  color: var(--text-sub);
  background: var(--bg);
  padding: 2px 8px;
  border-radius: 20px;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  -webkit-overflow-scrolling: touch;
}

.welcome {
  text-align: center;
  color: var(--text-sub);
  padding: 24px 0;
  line-height: 1.8;
}

.message {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: var(--radius);
  line-height: 1.6;
  word-break: break-word;
}
.message.user {
  align-self: flex-end;
  background: var(--primary);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.message.assistant {
  align-self: flex-start;
  background: var(--surface);
  box-shadow: var(--shadow);
  border-bottom-left-radius: 4px;
}
.message.assistant p:not(:last-child) { margin-bottom: 8px; }
.message.assistant ul, .message.assistant ol { padding-left: 20px; margin: 6px 0; }
.message.assistant code {
  font-family: "SF Mono", monospace;
  font-size: 13px;
  background: var(--bg);
  padding: 2px 5px;
  border-radius: 4px;
}
.message.assistant pre {
  background: var(--bg);
  padding: 10px;
  border-radius: 8px;
  overflow-x: auto;
  font-size: 13px;
  margin: 6px 0;
}

.typing-indicator {
  align-self: flex-start;
  background: var(--surface);
  box-shadow: var(--shadow);
  border-radius: var(--radius);
  border-bottom-left-radius: 4px;
  padding: 12px 16px;
  display: flex;
  gap: 5px;
}
.typing-indicator span {
  width: 7px; height: 7px;
  background: var(--text-sub);
  border-radius: 50%;
  animation: bounce .9s infinite;
}
.typing-indicator span:nth-child(2) { animation-delay: .2s; }
.typing-indicator span:nth-child(3) { animation-delay: .4s; }
@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); }
  30%           { transform: translateY(-6px); }
}

.chat-input-area {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
}

#chat-input {
  flex: 1;
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 8px 14px;
  font-size: 15px;
  font-family: inherit;
  resize: none;
  outline: none;
  max-height: 120px;
  overflow-y: auto;
  background: var(--bg);
  transition: border-color .15s;
}
#chat-input:focus { border-color: var(--primary); }

.icon-btn {
  width: 40px; height: 40px;
  border-radius: 50%;
  border: none;
  background: var(--bg);
  color: var(--primary);
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background .15s;
}
.icon-btn:hover { background: var(--border); }
.icon-btn.primary { background: var(--primary); color: #fff; }
.icon-btn.primary:hover { background: var(--primary-dk); }
.icon-btn.recording { background: #ff3b30; color: #fff; animation: pulse 1s infinite; }
@keyframes pulse { 0%,100%{opacity:1} 50%{opacity:.6} }

/* ── Plans Tab ───────────────────────────────────────────────────── */
.plans-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.nav-btn {
  background: none;
  border: none;
  font-size: 24px;
  color: var(--primary);
  cursor: pointer;
  padding: 0 8px;
  line-height: 1;
}

.date-label {
  font-size: 16px;
  font-weight: 600;
}

.plan-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  background: var(--bg);
  -webkit-overflow-scrolling: touch;
}

/* ── Plan Markdown Rendering ────────────────────────────────────── */
.plan-body h1 {
  font-size: 18px;
  font-weight: 700;
  color: var(--text);
  margin: 20px 0 12px;
  padding-bottom: 8px;
  border-bottom: 2px solid var(--primary);
}
.plan-body h1:first-child { margin-top: 0; }

.plan-body h2 {
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
  margin: 18px 0 10px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border);
}

.plan-body h3 {
  font-size: 15px;
  font-weight: 600;
  color: var(--text-sub);
  margin: 14px 0 8px;
}

.plan-body p {
  font-size: 15px;
  line-height: 1.7;
  margin: 6px 0;
  color: var(--text);
}

.plan-body hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 16px 0;
}

.plan-body ul, .plan-body ol {
  padding-left: 20px;
  margin: 8px 0;
}

.plan-body li {
  font-size: 15px;
  line-height: 1.7;
  margin: 4px 0;
}

.plan-body strong {
  color: var(--primary-dk);
}

.plan-body em {
  color: var(--text-sub);
}

.plan-body code {
  background: #eef4fd;
  color: var(--primary-dk);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 13px;
  font-family: "SF Mono", "Menlo", "Consolas", monospace;
}

.plan-body pre {
  background: var(--surface);
  padding: 14px;
  border-radius: 12px;
  box-shadow: var(--shadow);
  overflow-x: auto;
  margin: 10px 0;
}

.plan-body pre code {
  background: none;
  padding: 0;
  font-size: 13px;
  line-height: 1.6;
  white-space: pre;
  color: var(--text);
}

/* Time-block cards styling for plan sections */
.plan-body blockquote {
  border-left: 3px solid var(--primary);
  margin: 8px 0;
  padding: 8px 12px;
  background: #f8fafd;
  border-radius: 0 8px 8px 0;
  font-size: 14px;
  color: var(--text);
}

/* ── Structured Plan Timeline ──────────────────────────────── */
.plan-summary {
  font-size: 15px;
  line-height: 1.7;
  color: var(--text);
  padding: 14px 16px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 16px;
}

.plan-timeline {
  display: flex;
  flex-direction: column;
  gap: 1px;
  background: var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}

.time-block {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 12px 14px;
  background: var(--surface);
}
.time-block.block-fixed {
  border-left: 3px solid var(--primary);
}
.time-block.block-flexible {
  border-left: 3px solid #34c759;
}

.time-range {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-sub);
  white-space: nowrap;
  min-width: 100px;
  padding-top: 1px;
  font-variant-numeric: tabular-nums;
}

.block-content {
  flex: 1;
  min-width: 0;
}

.block-activity {
  font-size: 15px;
  font-weight: 500;
  color: var(--text);
  line-height: 1.4;
}

.block-type {
  display: inline-block;
  font-size: 11px;
  padding: 1px 6px;
  border-radius: 8px;
  margin-left: 6px;
  vertical-align: middle;
}
.block-fixed .block-type {
  background: #e3f2fd;
  color: var(--primary);
}
.block-flexible .block-type {
  background: #e8f5e9;
  color: #2e7d32;
}

.block-notes {
  font-size: 13px;
  color: var(--text-sub);
  margin-top: 4px;
  line-height: 1.5;
}

.plan-tips {
  margin-top: 16px;
  padding: 14px 16px;
  background: #fffbeb;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.plan-tips h3 {
  font-size: 14px;
  font-weight: 600;
  color: #b45309;
  margin-bottom: 8px;
}
.plan-tips ul {
  padding-left: 18px;
  margin: 0;
}
.plan-tips li {
  font-size: 14px;
  line-height: 1.7;
  color: #78350f;
  margin: 2px 0;
}

.empty-hint {
  color: var(--text-sub);
  text-align: center;
  padding: 40px 16px;
  line-height: 1.8;
}

/* ── Library Tab ─────────────────────────────────────────────────── */
.lib-tabs {
  display: flex;
  padding: 10px 12px 0;
  gap: 8px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.lib-tab {
  flex: 1;
  padding: 8px 0;
  border: none;
  border-bottom: 2px solid transparent;
  background: none;
  font-size: 14px;
  font-weight: 500;
  color: var(--text-sub);
  cursor: pointer;
  transition: all .15s;
}
.lib-tab.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
}

.library-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  -webkit-overflow-scrolling: touch;
}

.card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
}
.card h3 { font-size: 16px; margin-bottom: 6px; }
.card p  { font-size: 13px; color: var(--text-sub); line-height: 1.5; }
.card .tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.tag {
  font-size: 11px;
  background: var(--bg);
  color: var(--primary);
  padding: 2px 8px;
  border-radius: 10px;
}
.card a { color: var(--primary); font-size: 13px; text-decoration: none; }
.book-status {
  display: inline-block;
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 10px;
  margin-bottom: 6px;
}
.status-reading  { background: #e3f2fd; color: #1565c0; }
.status-want     { background: #f3e5f5; color: #7b1fa2; }
.status-done     { background: #e8f5e9; color: #2e7d32; }

/* ── Settings Tab ────────────────────────────────────────────────── */
.settings-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  -webkit-overflow-scrolling: touch;
}

.settings-section {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}
.settings-section h2 {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-sub);
  text-transform: uppercase;
  letter-spacing: .05em;
  margin-bottom: 12px;
}

.radio-group {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.radio-group label {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 15px;
  cursor: pointer;
}
.radio-group input[type="radio"] {
  accent-color: var(--primary);
  width: 18px; height: 18px;
}

.memory-display {
  font-family: "SF Mono", monospace;
  font-size: 12px;
  line-height: 1.6;
  color: var(--text-sub);
  white-space: pre-wrap;
  word-break: break-all;
  background: var(--bg);
  padding: 12px;
  border-radius: 8px;
  max-height: 200px;
  overflow-y: auto;
}

/* ── Login Overlay ──────────────────────────────────────────── */
.login-overlay {
  display: none;
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: var(--bg);
  z-index: 9999;
  align-items: center;
  justify-content: center;
}
.login-overlay.active { display: flex; }

.login-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 32px 24px;
  width: 100%;
  max-width: 340px;
  text-align: center;
}
.login-card h1 {
  font-size: 24px;
  font-weight: 700;
  color: var(--primary);
  margin-bottom: 4px;
}
.login-subtitle {
  font-size: 14px;
  color: var(--text-sub);
  margin-bottom: 24px;
}
.login-card input[type="password"] {
  width: 100%;
  padding: 12px 16px;
  border: 1px solid var(--border);
  border-radius: 12px;
  font-size: 16px;
  font-family: inherit;
  background: var(--bg);
  outline: none;
  transition: border-color .15s;
  margin-bottom: 12px;
}
.login-card input[type="password"]:focus { border-color: var(--primary); }

.login-submit {
  width: 100%;
  padding: 12px;
  border: none;
  border-radius: 12px;
  background: var(--primary);
  color: #fff;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s;
}
.login-submit:hover { background: var(--primary-dk); }
.login-submit:disabled { opacity: .6; cursor: not-allowed; }

.login-error {
  color: #ff3b30;
  font-size: 13px;
  margin-top: 12px;
  min-height: 18px;
}

/* ── Logout Button ──────────────────────────────────────────── */
.logout-btn {
  width: 100%;
  padding: 12px;
  border: 1px solid #ff3b30;
  border-radius: 12px;
  background: transparent;
  color: #ff3b30;
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  transition: all .15s;
}
.logout-btn:hover { background: #ff3b30; color: #fff; }

/* ── Image Upload Preview ───────────────────────────────────── */
.image-preview-area {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.image-preview-thumb {
  width: 48px;
  height: 48px;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--border);
}
.image-remove-btn {
  background: none;
  border: none;
  font-size: 20px;
  color: var(--text-sub);
  cursor: pointer;
  padding: 4px;
}

/* ── Session Image Bar ─────────────────────────────────────── */
.session-image-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  background: #eef4fd;
  border-top: 1px solid var(--border);
}
.session-image-label {
  font-size: 12px;
  color: var(--primary);
  flex: 1;
}

/* ── Library Card Thumbnail ─────────────────────────────────── */
.card-thumbnail {
  width: calc(100% + 32px);
  max-height: 180px;
  object-fit: cover;
  border-radius: 12px 12px 0 0;
  margin: -14px -16px 10px -16px;
}

/* ── Library Card Summary (clickable) ──────────────────────── */
.card { cursor: pointer; transition: transform .1s; }
.card:active { transform: scale(.98); }
.card-summary { font-size: 13px; color: var(--text-sub); margin-top: 2px; }

/* ── Detail Overlay ────────────────────────────────────────── */
.detail-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: var(--bg);
  z-index: 900;
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform .25s ease;
}
.detail-overlay.active { transform: translateX(0); }

.detail-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.detail-header .nav-btn {
  font-size: 24px;
  min-width: 32px;
}
.detail-title {
  font-size: 17px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.detail-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  -webkit-overflow-scrolling: touch;
}
.detail-body img {
  width: 100%;
  max-height: 280px;
  object-fit: cover;
  border-radius: var(--radius);
  margin-bottom: 16px;
}
.detail-body h3 { font-size: 14px; color: var(--text-sub); margin: 16px 0 6px; }
.detail-body p { font-size: 15px; line-height: 1.7; }
.detail-body .detail-section { margin-bottom: 12px; }
.detail-body .tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 12px; }

/* Ingredient table */
.ingredient-table { width: 100%; border-collapse: collapse; font-size: 15px; }
.ingredient-table td { padding: 6px 0; border-bottom: 1px solid var(--border); }
.ingredient-table td.amount { text-align: right; color: var(--text-sub); white-space: nowrap; }

/* Step list */
.step-list { list-style: none; counter-reset: step; padding: 0; }
.step-list li {
  counter-increment: step;
  position: relative;
  padding: 10px 0 10px 36px;
  border-bottom: 1px solid var(--border);
  font-size: 15px;
  line-height: 1.6;
}
.step-list li::before {
  content: counter(step);
  position: absolute; left: 0; top: 10px;
  width: 24px; height: 24px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  font-size: 13px; font-weight: 600;
  display: flex; align-items: center; justify-content: center;
}
.step-time {
  display: inline-block;
  background: #eef4fd;
  color: var(--primary);
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 10px;
  margin-right: 6px;
  margin-bottom: 2px;
}

/* Step images */
.step-images { display: flex; flex-wrap: wrap; gap: 12px; }
.step-image { flex: 0 0 auto; width: calc(50% - 6px); }
.step-image img { width: 100%; border-radius: 8px; }
.step-image figcaption { font-size: 12px; color: var(--text-sub); margin-top: 4px; text-align: center; }
