:root {
  --bg: var(--tg-theme-bg-color, #f4f6f8);
  --text: var(--tg-theme-text-color, #1a1a1a);
  --hint: var(--tg-theme-hint-color, #6b7280);
  --btn: var(--tg-theme-button-color, #2563eb);
  --btn-text: var(--tg-theme-button-text-color, #fff);
  --secondary: var(--tg-theme-secondary-bg-color, #fff);
  --accent: var(--tg-theme-link-color, #2563eb);
}

* { box-sizing: border-box; }

html {
  height: 100%;
  overflow: hidden;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  overscroll-behavior: none;
  position: fixed;
  inset: 0;
}

#app {
  position: fixed;
  left: 0;
  right: 0;
  max-width: 720px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  z-index: 1;
}

.bottom-dock {
  position: fixed;
  left: 0;
  right: 0;
  max-width: 720px;
  margin: 0 auto;
  z-index: 40;
  background: var(--secondary);
  border-top: 1px solid rgba(0,0,0,.06);
  padding-bottom: env(safe-area-inset-bottom);
}

.header {
  flex-shrink: 0;
  padding: 16px 16px 8px;
  padding-top: calc(12px + env(safe-area-inset-top));
  border-bottom: 1px solid rgba(0,0,0,.06);
  background: var(--secondary);
}

.header h1 {
  margin: 0;
  font-size: 1.15rem;
}

.subtitle {
  margin: 4px 0 0;
  font-size: 0.85rem;
  color: var(--hint);
}

.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

.msg {
  max-width: 92%;
  padding: 10px 12px;
  border-radius: 14px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
}

.msg.user {
  align-self: flex-end;
  background: var(--btn);
  color: var(--btn-text);
  border-bottom-right-radius: 4px;
}

.msg.assistant {
  align-self: flex-start;
  background: var(--secondary);
  color: var(--text);
  border: 1px solid rgba(0,0,0,.06);
  border-bottom-left-radius: 4px;
}

.msg.error {
  align-self: flex-start;
  background: #fef2f2;
  color: #991b1b;
  border: 1px solid #fecaca;
}

.choice-panel {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 8px;
}

.choice-btn {
  width: 100%;
  text-align: left;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid rgba(0,0,0,.08);
  background: var(--bg);
  color: var(--text);
  font-size: 0.95rem;
  cursor: pointer;
}

.choice-btn:active { opacity: 0.85; }

.dish-actions {
  padding: 10px 12px 0;
  max-height: 34vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.dish-actions.hidden {
  display: none;
}

.action-panel {
  display: flex;
  flex-direction: column;
  gap: 0;
}

.action-toggle {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 2px;
  border: none;
  background: transparent;
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.action-toggle-title {
  font-weight: 600;
  font-size: 0.95rem;
  line-height: 1.3;
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.action-toggle-icon {
  flex-shrink: 0;
  font-size: 0.72rem;
  color: var(--hint);
  transition: transform 0.2s ease;
}

.action-body {
  overflow: hidden;
}

.action-panel.expanded .action-body {
  padding-top: 8px;
}

.action-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.action-btn {
  padding: 10px 8px;
  border-radius: 10px;
  border: 1px solid rgba(0,0,0,.08);
  background: var(--btn);
  color: var(--btn-text);
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  text-align: center;
}

.action-btn:active { opacity: 0.85; }

.data-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 8px;
  font-size: 0.82rem;
}

.data-table th,
.data-table td {
  padding: 6px 4px;
  border-bottom: 1px solid rgba(0,0,0,.06);
  text-align: left;
  vertical-align: top;
}

.data-table th {
  color: var(--hint);
  font-weight: 600;
}

.calc-block {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.calc-metrics {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.calc-metric {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 8px 6px;
  border-radius: 10px;
  background: rgba(37, 99, 235, 0.1);
  border: 1px solid rgba(37, 99, 235, 0.18);
  text-align: center;
}

.calc-metric-label {
  font-size: 0.72rem;
  color: var(--hint);
  line-height: 1.2;
}

.calc-metric-value {
  font-size: 0.9rem;
  font-weight: 700;
  line-height: 1.2;
  color: var(--text);
}

.data-table .col-date {
  background: rgba(37, 99, 235, 0.12);
  font-weight: 600;
  white-space: nowrap;
}

.calc-heading {
  display: block;
  font-size: 1rem;
  line-height: 1.3;
}

.calc-metric-sub {
  font-size: 0.68rem;
  color: var(--hint);
  line-height: 1.2;
}

.calc-summary-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}

.calc-summary-table th {
  width: 42%;
  padding: 5px 8px 5px 0;
  text-align: left;
  color: var(--hint);
  font-weight: 600;
  vertical-align: top;
}

.calc-summary-table td {
  padding: 5px 0;
  font-weight: 600;
}

.calc-section-title {
  margin-top: 4px;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--hint);
}

.calc-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 8px;
  font-size: 0.85rem;
}

.calc-table td {
  padding: 4px 0;
  vertical-align: top;
}

.calc-fallback {
  margin: 0;
  white-space: pre-wrap;
  font-family: inherit;
  font-size: 0.85rem;
}

.composer {
  display: flex;
  gap: 8px;
  padding: 10px 12px;
  background: var(--secondary);
}

body.keyboard-open .header {
  display: none;
}

body.keyboard-open .dish-actions {
  display: none !important;
}

@media (max-width: 480px) {
  .calc-metrics {
    grid-template-columns: 1fr;
  }

  .data-table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  #send-btn {
    padding: 10px 12px;
    font-size: 0.9rem;
  }

  .dish-actions {
    max-height: none;
    padding-top: 6px;
  }

  .action-body {
    max-height: 0;
    transition: max-height 0.25s ease;
  }

  .action-panel.expanded .action-body {
    max-height: 280px;
  }

  .action-panel.expanded .action-toggle-icon {
    transform: rotate(180deg);
  }
}

@media (min-width: 481px) {
  .action-toggle {
    cursor: default;
    pointer-events: none;
    padding-bottom: 4px;
  }

  .action-toggle-icon {
    display: none;
  }
}

#chat-input {
  flex: 1;
  border: 1px solid rgba(0,0,0,.12);
  border-radius: 12px;
  padding: 10px 12px;
  font-size: 1rem;
  background: var(--bg);
  color: var(--text);
}

#send-btn {
  border: none;
  border-radius: 12px;
  padding: 10px 14px;
  background: var(--btn);
  color: var(--btn-text);
  font-weight: 600;
  cursor: pointer;
}

#send-btn:disabled { opacity: 0.5; cursor: default; }

.typing {
  font-size: 0.85rem;
  color: var(--hint);
  padding: 4px 2px;
}