:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #000000);
  --hint: var(--tg-theme-hint-color, #999999);
  --link: var(--tg-theme-link-color, #2481cc);
  --btn-bg: var(--tg-theme-button-color, #2481cc);
  --btn-text: var(--tg-theme-button-text-color, #ffffff);
  --secondary-bg: var(--tg-theme-secondary-bg-color, #f0f0f0);
  --section-bg: var(--tg-theme-section-bg-color, var(--secondary-bg));

  --radius: 12px;
  --radius-sm: 8px;
  --gap: 12px;
  --protein-color: #4a90d9;
  --fat-color: #e8a838;
  --carbs-color: #5cb85c;
  --danger: #e74c3c;
}

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.4;
  padding-bottom: 70px;
}

/* Tabs bar */
.tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  background: var(--secondary-bg);
  border-top: 1px solid rgba(0,0,0,0.08);
  z-index: 100;
  padding-bottom: env(safe-area-inset-bottom, 0);
}

.tab-bar-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 8px 0;
  gap: 2px;
  cursor: pointer;
  color: var(--hint);
  transition: color 0.2s;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

.tab-bar-item.active {
  color: var(--link);
}

.tab-bar-item svg {
  width: 24px;
  height: 24px;
}

.tab-bar-item span {
  font-size: 11px;
  font-weight: 500;
}

/* Tab content */
.tab-content {
  display: none;
  padding: 16px;
  min-height: calc(100vh - 70px);
  animation: fadeIn 0.2s ease;
}

.tab-content.active {
  display: block;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

/* Cards */
.card {
  background: var(--section-bg);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: var(--gap);
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 20px;
  border-radius: var(--radius-sm);
  border: none;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.2s, transform 0.1s;
  -webkit-tap-highlight-color: transparent;
  width: 100%;
}

.btn:active {
  transform: scale(0.98);
}

.btn:disabled {
  opacity: 0.5;
  pointer-events: none;
}

.btn-primary {
  background: var(--btn-bg);
  color: var(--btn-text);
}

.btn-secondary {
  background: var(--secondary-bg);
  color: var(--text);
}

.btn-outline {
  background: transparent;
  color: var(--link);
  border: 1.5px solid var(--link);
}

.btn-danger {
  background: var(--danger);
  color: #fff;
}

.btn-small {
  padding: 8px 14px;
  font-size: 13px;
  width: auto;
}

/* Inputs */
.input, .textarea {
  width: 100%;
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  border: 1.5px solid rgba(0,0,0,0.1);
  background: var(--secondary-bg);
  color: var(--text);
  font-size: 15px;
  font-family: inherit;
  outline: none;
  transition: border-color 0.2s;
}

.input:focus, .textarea:focus {
  border-color: var(--link);
}

.field-label {
  display: block;
  margin: 2px 0 -3px;
  color: var(--hint);
  font-size: 12px;
  font-weight: 600;
}

.textarea {
  resize: vertical;
  min-height: 80px;
}

.text-input-panel {
  display: flex;
  flex-direction: column;
  min-height: 260px;
}

.text-input-panel .textarea {
  min-height: 116px;
}

.analyze-text-actions {
  margin-top: auto;
  padding-top: 18px;
}

.voice-row {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 34px;
  margin-top: 8px;
  justify-content: flex-end;
}

.voice-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1.5px solid var(--link);
  background: transparent;
  color: var(--link);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.2s, color 0.2s, transform 0.1s;
}

.voice-btn:active {
  transform: scale(0.96);
}

.voice-btn svg {
  width: 19px;
  height: 19px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.voice-btn.listening {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
}

.voice-status {
  min-width: 0;
  color: var(--hint);
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.voice-meter {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  height: 24px;
  padding: 0 4px;
}

.voice-meter span {
  display: block;
  width: 3px;
  height: 8px;
  border-radius: 2px;
  background: var(--danger);
  animation: voicePulse 0.9s ease-in-out infinite;
}

.voice-meter span:nth-child(2) { animation-delay: 0.08s; }
.voice-meter span:nth-child(3) { animation-delay: 0.16s; }
.voice-meter span:nth-child(4) { animation-delay: 0.24s; }
.voice-meter span:nth-child(5) { animation-delay: 0.32s; }
.voice-meter span:nth-child(6) { animation-delay: 0.4s; }

@keyframes voicePulse {
  0%, 100% { height: 7px; opacity: 0.45; }
  50% { height: 22px; opacity: 1; }
}

/* Segmented control */
.segment-control {
  display: flex;
  background: var(--secondary-bg);
  border-radius: var(--radius-sm);
  padding: 3px;
  margin-bottom: 16px;
}

.segment-btn {
  flex: 1;
  padding: 8px;
  text-align: center;
  border-radius: 6px;
  border: none;
  background: transparent;
  color: var(--hint);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.segment-btn.active {
  background: var(--bg);
  color: var(--text);
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

/* Nutrition result card */
.result-card {
  background: var(--section-bg);
  border-radius: var(--radius);
  padding: 20px;
  text-align: center;
}

.result-card .dish-name {
  font-size: 18px;
  font-weight: 700;
  margin-bottom: 4px;
}

.result-card .confidence-badge {
  display: inline-block;
  font-size: 11px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 10px;
  margin-bottom: 12px;
}

.confidence-high { background: #d4edda; color: #155724; }
.confidence-medium { background: #fff3cd; color: #856404; }
.confidence-low { background: #f8d7da; color: #721c24; }

.result-card .calories-big {
  font-size: 48px;
  font-weight: 800;
  line-height: 1;
  margin-bottom: 4px;
}

.result-card .calories-label {
  font-size: 14px;
  color: var(--hint);
  margin-bottom: 16px;
}

.macros-row {
  display: flex;
  gap: 8px;
  justify-content: center;
  margin-bottom: 12px;
}

.macro-badge {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  min-width: 80px;
}

.macro-badge .macro-value {
  font-size: 18px;
  font-weight: 700;
}

.macro-badge .macro-label {
  font-size: 11px;
  font-weight: 500;
  opacity: 0.8;
}

.macro-protein { background: rgba(74,144,217,0.15); color: var(--protein-color); }
.macro-fat { background: rgba(232,168,56,0.15); color: var(--fat-color); }
.macro-carbs { background: rgba(92,184,92,0.15); color: var(--carbs-color); }

.result-edit-text,
.result-edit-number,
.macro-input,
.portion-input {
  appearance: textfield;
  -moz-appearance: textfield;
  border: none;
  outline: none;
  background: transparent;
  color: inherit;
  font-family: inherit;
  text-align: center;
  border-radius: 6px;
}

.result-edit-text:focus,
.result-edit-number:focus,
.macro-input:focus,
.portion-input:focus {
  background: var(--bg);
  box-shadow: inset 0 0 0 1.5px var(--link);
}

.result-edit-text {
  display: block;
  width: 100%;
}

.result-edit-number {
  width: 100%;
}

.macro-input {
  width: 58px;
}

.portion-input {
  width: 64px;
  color: var(--text);
  font-size: 13px;
}

.result-edit-number::-webkit-outer-spin-button,
.result-edit-number::-webkit-inner-spin-button,
.macro-input::-webkit-outer-spin-button,
.macro-input::-webkit-inner-spin-button,
.portion-input::-webkit-outer-spin-button,
.portion-input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

.portion-info {
  font-size: 13px;
  color: var(--hint);
  margin-bottom: 16px;
}

.items-breakdown {
  text-align: left;
  margin-bottom: 16px;
}

.items-breakdown summary {
  font-size: 13px;
  color: var(--link);
  cursor: pointer;
  margin-bottom: 6px;
}

.items-breakdown .item-row {
  display: flex;
  justify-content: space-between;
  padding: 4px 0;
  font-size: 13px;
  border-bottom: 1px solid rgba(0,0,0,0.05);
}

.result-actions {
  display: flex;
  gap: 8px;
  margin-top: 12px;
}

.result-actions .btn {
  flex: 1;
}

/* Meal type selector */
.meal-type-row {
  display: flex;
  gap: 6px;
  margin-bottom: 12px;
  justify-content: center;
}

.meal-type-btn {
  padding: 6px 12px;
  border-radius: 16px;
  border: 1.5px solid rgba(0,0,0,0.1);
  background: transparent;
  color: var(--hint);
  font-size: 13px;
  cursor: pointer;
  transition: all 0.2s;
}

.meal-type-btn.active {
  background: var(--link);
  color: #fff;
  border-color: var(--link);
}

/* Photo preview */
.photo-preview {
  position: relative;
  border-radius: var(--radius);
  overflow: hidden;
  margin-bottom: 12px;
}

.photo-preview img {
  width: 100%;
  max-height: 300px;
  object-fit: cover;
  display: block;
}

.photo-preview .remove-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: rgba(0,0,0,0.5);
  color: #fff;
  border: none;
  cursor: pointer;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Loading spinner */
.spinner {
  display: inline-block;
  width: 40px;
  height: 40px;
  border: 3px solid var(--secondary-bg);
  border-top-color: var(--link);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

.loading-state {
  text-align: center;
  padding: 40px 20px;
}

.loading-state p {
  margin-top: 12px;
  color: var(--hint);
  font-size: 14px;
}

/* Empty state */
.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--hint);
}

.empty-state .empty-icon {
  font-size: 48px;
  margin-bottom: 12px;
}

.empty-state p {
  font-size: 14px;
}

/* Date picker */
.date-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 0;
  margin-bottom: 12px;
}

.date-nav .date-text {
  font-size: 16px;
  font-weight: 600;
}

.date-nav .date-today {
  font-size: 12px;
  color: var(--link);
  font-weight: 500;
}

.date-nav button {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: var(--secondary-bg);
  color: var(--text);
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Meal list items */
.meal-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  background: var(--section-bg);
  border-radius: var(--radius);
  margin-bottom: 8px;
}

.meal-item .meal-icon {
  font-size: 24px;
  flex-shrink: 0;
}

.meal-item .meal-info {
  flex: 1;
  min-width: 0;
}

.meal-item .meal-name {
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.meal-item .meal-meta {
  font-size: 12px;
  color: var(--hint);
}

.meal-item .meal-cals {
  font-size: 16px;
  font-weight: 700;
  flex-shrink: 0;
}

.meal-item .meal-delete {
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  color: var(--danger);
  cursor: pointer;
  font-size: 18px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  opacity: 0.6;
}

.meal-group-header {
  font-size: 13px;
  font-weight: 600;
  color: var(--hint);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 12px 0 6px;
}

/* Daily totals bar */
.daily-totals {
  background: var(--section-bg);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 12px;
}

.daily-totals .total-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.daily-totals .total-cals {
  font-size: 20px;
  font-weight: 700;
}

.daily-totals .total-goal {
  font-size: 13px;
  color: var(--hint);
}

/* Progress bar */
.progress-bar {
  height: 8px;
  background: rgba(0,0,0,0.08);
  border-radius: 4px;
  overflow: hidden;
}

.progress-bar .progress-fill {
  height: 100%;
  border-radius: 4px;
  background: var(--link);
  transition: width 0.3s ease;
}

.progress-bar .progress-fill.over {
  background: var(--danger);
}

/* Stats page */
.stats-hero {
  text-align: center;
  padding: 20px 0;
  margin-bottom: 16px;
}

.progress-ring-container {
  position: relative;
  width: 160px;
  height: 160px;
  margin: 0 auto 12px;
}

.progress-ring-container svg {
  transform: rotate(-90deg);
}

.progress-ring-container .ring-text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
}

.ring-text .ring-value {
  font-size: 28px;
  font-weight: 800;
  display: block;
}

.ring-text .ring-label {
  font-size: 12px;
  color: var(--hint);
}

.macro-stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
  margin-bottom: 16px;
}

.macro-stat-card {
  background: var(--section-bg);
  border-radius: var(--radius-sm);
  padding: 12px;
  text-align: center;
}

.macro-stat-card .stat-value {
  font-size: 20px;
  font-weight: 700;
}

.macro-stat-card .stat-label {
  font-size: 11px;
  color: var(--hint);
}

/* Weekly chart */
.week-chart {
  background: var(--section-bg);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
}

.week-chart h3 {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 12px;
}

.chart-bars {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  height: 120px;
  position: relative;
}

.chart-bar-wrapper {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  height: 100%;
  justify-content: flex-end;
}

.chart-bar {
  width: 100%;
  max-width: 32px;
  border-radius: 4px 4px 0 0;
  background: var(--link);
  transition: height 0.3s ease;
  min-height: 2px;
}

.chart-bar.over {
  background: var(--danger);
}

.chart-bar-label {
  font-size: 10px;
  color: var(--hint);
  margin-top: 4px;
}

.chart-goal-line {
  position: absolute;
  left: 0;
  right: 0;
  border-top: 2px dashed var(--danger);
  opacity: 0.4;
}

/* Goal setting */
.goal-section {
  background: var(--section-bg);
  border-radius: var(--radius);
  padding: 16px;
}

.goal-section h3 {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 12px;
}

.goal-input-row {
  display: flex;
  gap: 8px;
  align-items: center;
}

.goal-input-row .input {
  flex: 1;
}

.goal-input-row .btn {
  width: auto;
  flex-shrink: 0;
}

/* Access and admin */
.access-section,
.admin-section {
  background: var(--section-bg);
  border-radius: var(--radius);
  padding: 16px;
  margin-top: 16px;
}

.access-status {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.premium-actions {
  margin-top: 12px;
}

.access-status h3,
.admin-section h3 {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 4px;
}

.access-status p {
  font-size: 13px;
  color: var(--hint);
}

.access-badge {
  flex-shrink: 0;
  padding: 5px 10px;
  border-radius: 999px;
  background: rgba(0,0,0,0.08);
  color: var(--hint);
  font-size: 12px;
  font-weight: 700;
}

.access-badge.premium {
  background: rgba(232,168,56,0.18);
  color: #a66b00;
}

.admin-form {
  display: grid;
  gap: 9px;
  margin-top: 12px;
}

.admin-pricing {
  padding-bottom: 14px;
  border-bottom: 1px solid rgba(0,0,0,0.08);
  margin-bottom: 14px;
}

.admin-save-btn {
  margin-top: 2px;
  background: rgba(36,129,204,0.1);
  font-weight: 700;
}

.admin-list {
  display: grid;
  gap: 8px;
  margin-top: 12px;
}

.admin-subsection {
  margin-top: 16px;
}

.admin-subsection-title {
  font-size: 12px;
  font-weight: 700;
  color: var(--hint);
  text-transform: uppercase;
  letter-spacing: 0.4px;
}

.admin-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px;
  border-radius: var(--radius-sm);
  background: var(--bg);
}

.admin-user-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  width: 100%;
  padding: 10px;
  border-radius: var(--radius-sm);
  border: none;
  background: var(--bg);
  color: var(--text);
  text-align: left;
  font: inherit;
  cursor: pointer;
}

.admin-user-row:active {
  transform: scale(0.99);
}

.admin-row-main {
  min-width: 0;
}

.admin-row-id {
  font-size: 14px;
  font-weight: 700;
}

.admin-row-meta,
.admin-empty {
  font-size: 12px;
  color: var(--hint);
}

/* Toast */
.toast {
  position: fixed;
  top: 16px;
  left: 16px;
  right: 16px;
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 500;
  text-align: center;
  z-index: 200;
  animation: slideDown 0.3s ease;
  color: #fff;
}

.toast-success { background: #27ae60; }
.toast-error { background: var(--danger); }

@keyframes slideDown {
  from { transform: translateY(-100%); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

/* Action area with big buttons */
.action-area {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 20px 0;
}

.action-btn {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 18px 16px;
  background: var(--section-bg);
  border-radius: var(--radius);
  border: none;
  cursor: pointer;
  text-align: left;
  width: 100%;
  color: var(--text);
  transition: transform 0.1s;
}

.action-btn:active {
  transform: scale(0.98);
}

.action-btn .action-icon {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  flex-shrink: 0;
}

.action-btn .action-icon.photo-icon {
  background: rgba(74,144,217,0.15);
}

.action-btn .action-icon.text-icon {
  background: rgba(92,184,92,0.15);
}

.action-btn .action-text h3 {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 2px;
}

.action-btn .action-text p {
  font-size: 13px;
  color: var(--hint);
}

/* Hidden file input */
.hidden {
  display: none !important;
}
