:root {
  --bg: #ffffff;
  --page-bg: #f5f5f5;
  --text: #111111;
  --muted: #666666;
  --border: #222222;
  --status-border: #f2a23a;
  --primary: #2f9e44;
  --primary-soft: #e9f8ee;
  --danger-soft: #ffe8e8;
  --danger-border: #e03131;
  --assistant-soft: #e7f0ff;
  --assistant-border: #3b5bdb;
  --radius-sm: 10px;
  --radius-md: 16px;
  --radius-lg: 24px;
  --shadow: 0 16px 40px rgba(0, 0, 0, 0.12);
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
  overflow-x: hidden;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--page-bg);
  color: var(--text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input,
textarea {
  font: inherit;
}

button {
  min-width: 44px;
  min-height: 44px;
  cursor: pointer;
  touch-action: manipulation;
  white-space: normal;
}

button:focus-visible,
input:focus-visible,
textarea:focus-visible,
[tabindex]:focus-visible {
  outline: 3px solid var(--assistant-border);
  outline-offset: 3px;
}

[hidden] {
  display: none !important;
}

.app-shell {
  width: min(100vw, 430px);
  max-width: 100%;
  height: 100vh;
  height: 100dvh;
  margin: 0 auto;
  position: relative;
  overflow: hidden;
  background: var(--bg);
}

.auth-screen {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: calc(24px + env(safe-area-inset-top)) 18px calc(24px + env(safe-area-inset-bottom));
  background: var(--bg);
}

.auth-card {
  width: 100%;
  display: grid;
  gap: 14px;
}

.auth-card h1 {
  margin: 0 0 6px;
  font-size: 1.6rem;
  line-height: 1.2;
}

.auth-field {
  display: grid;
  gap: 7px;
}

.auth-field label {
  font-weight: 800;
}

.auth-field input {
  width: 100%;
  min-height: 52px;
  border: 2px solid var(--primary);
  border-radius: var(--radius-md);
  padding: 10px 12px;
  background: var(--bg);
  color: var(--text);
}

.auth-submit {
  border: 0;
  border-radius: var(--radius-sm);
  background: var(--primary);
  color: #ffffff;
  font-weight: 800;
}

.auth-submit:disabled {
  cursor: progress;
  opacity: 0.65;
}

.auth-error {
  margin: 0;
  border: 1px solid var(--danger-border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  background: var(--danger-soft);
  color: var(--danger-border);
  font-weight: 700;
  line-height: 1.35;
}

.dashboard {
  height: 100%;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: calc(24px + env(safe-area-inset-top)) 18px calc(124px + env(safe-area-inset-bottom));
  -webkit-overflow-scrolling: touch;
}

.dashboard h1 {
  margin: 0 0 20px;
  font-size: 1.6rem;
  line-height: 1.2;
}

.status-card {
  min-width: 0;
  min-height: 112px;
  border: 2px solid var(--status-border);
  border-radius: var(--radius-md);
  padding: 16px 14px;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
  color: var(--status-border);
}

.status-column {
  min-width: 0;
  display: grid;
  gap: 6px;
  align-content: center;
}

.status-current {
  font-size: 1.12rem;
  font-weight: 800;
  line-height: 1.1;
  overflow-wrap: anywhere;
}

.status-goal {
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.2;
  overflow-wrap: anywhere;
}

.status-label {
  font-size: 0.86rem;
  font-weight: 700;
  line-height: 1.15;
  overflow-wrap: anywhere;
}

.log-section {
  margin-top: 24px;
}

.log-header {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.log-header h2 {
  margin: 0;
  font-size: 1.25rem;
}

.date-switcher {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-width: 0;
  max-width: 100%;
}

.add-meal-button,
.menu-fab {
  border: 0;
  color: #ffffff;
  background: var(--primary);
}

.date-button {
  width: 44px;
  border: 0;
  border-radius: 999px;
  color: var(--text);
  background: transparent;
  font-weight: 800;
}

.date-display {
  color: var(--text);
  font-weight: 700;
  white-space: nowrap;
}

.add-meal-button {
  width: 44px;
  border-radius: var(--radius-sm);
  font-size: 1.35rem;
  font-weight: 700;
}

.meal-list {
  display: grid;
  gap: 12px;
  margin-top: 16px;
  padding-bottom: 12px;
}

.meal-card {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: start;
  gap: 12px;
  min-height: 84px;
  border: 2px solid var(--danger-border);
  border-radius: var(--radius-md);
  background: var(--danger-soft);
  color: var(--danger-border);
  padding: 16px;
}

.meal-card-open {
  min-width: 0;
  border: 0;
  padding: 0;
  background: transparent;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.meal-card-open:focus-visible {
  outline: 3px solid rgba(196, 45, 58, 0.35);
  outline-offset: 4px;
  border-radius: var(--radius-sm);
}

.meal-card-header {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: start;
  gap: 12px;
}

.meal-name {
  margin: 0;
  overflow-wrap: anywhere;
  font-size: 1rem;
  line-height: 1.25;
}

.meal-time {
  color: var(--danger-border);
  font-size: 0.95rem;
  font-weight: 700;
  white-space: nowrap;
}

.meal-card-meta {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 8px;
}

.meal-delete-button {
  min-width: 64px;
  min-height: 36px;
  border: 1px solid var(--danger-border);
  border-radius: var(--radius-sm);
  padding: 0 10px;
  background: var(--bg);
  color: var(--danger-border);
  font-size: 0.82rem;
  font-weight: 800;
}

.meal-delete-button:disabled {
  cursor: progress;
  opacity: 0.65;
}

.meal-summary {
  margin: 12px 0 0;
  color: var(--danger-border);
  font-weight: 700;
  line-height: 1.35;
  overflow-wrap: anywhere;
}

.empty-state,
.log-message {
  border: 1px solid #dddddd;
  border-radius: var(--radius-md);
  padding: 18px;
  color: var(--muted);
  text-align: center;
  overflow-wrap: anywhere;
}

.empty-state p,
.log-message,
.log-message p {
  margin: 0;
}

.empty-action {
  margin-top: 12px;
  border: 1px solid var(--primary);
  border-radius: var(--radius-sm);
  color: var(--primary);
  background: var(--primary-soft);
  font-weight: 700;
}

.log-message-error {
  border-color: var(--danger-border);
  color: var(--danger-border);
  background: var(--danger-soft);
}

.log-retry-button {
  margin-top: 12px;
  border: 1px solid var(--danger-border);
  border-radius: var(--radius-sm);
  color: var(--danger-border);
  background: var(--bg);
  font-weight: 800;
}

.menu-fab {
  position: absolute;
  left: 50%;
  bottom: calc(18px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  min-width: 84px;
  max-width: calc(100% - 32px);
  border-radius: 999px;
  font-size: 1.35rem;
  font-weight: 700;
  box-shadow: var(--shadow);
  z-index: 5;
}

.backdrop {
  position: absolute;
  inset: 0;
  z-index: 10;
  background: rgba(0, 0, 0, 0.38);
}

.modal-host {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  overflow: hidden;
  padding: calc(12px + env(safe-area-inset-top)) 12px calc(12px + env(safe-area-inset-bottom));
  pointer-events: none;
}

.modal {
  width: 100%;
  max-width: 100%;
  max-height: calc(100vh - 24px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  max-height: calc(100dvh - 24px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  overflow: auto;
  overflow-x: hidden;
  pointer-events: auto;
  background: var(--bg);
  border: 2px solid var(--primary);
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  box-shadow: var(--shadow);
}

.modal--full {
  min-height: calc(100vh - 32px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  min-height: calc(100dvh - 32px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  border-radius: var(--radius-lg);
}

.modal--center {
  margin: auto 0;
  border-radius: var(--radius-lg);
}

.modal__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 16px 12px;
  border-bottom: 1px solid var(--primary-soft);
}

.modal__title {
  min-width: 0;
  margin: 0;
  font-size: 1.25rem;
  line-height: 1.2;
  overflow-wrap: anywhere;
}

.modal__close {
  width: 44px;
  border: 0;
  border-radius: var(--radius-sm);
  background: var(--primary-soft);
  color: var(--primary);
  font-size: 1.35rem;
  font-weight: 700;
}

.modal__body {
  padding: 16px;
}

.modal__placeholder {
  margin: 0;
  color: var(--muted);
  line-height: 1.4;
}

.menu-modal {
  min-height: min(420px, calc(100dvh - 48px));
  margin: auto 0 0;
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
}

.menu-modal__body {
  padding: 20px 16px calc(24px + env(safe-area-inset-bottom));
}

.menu-actions {
  display: grid;
  gap: 12px;
}

.menu-action {
  width: 100%;
  min-height: 58px;
  border: 2px solid var(--primary);
  border-radius: var(--radius-md);
  background: var(--primary-soft);
  color: var(--primary);
  font-weight: 800;
  text-align: center;
  overflow-wrap: anywhere;
}

.menu-action:disabled {
  cursor: progress;
  opacity: 0.7;
}

.menu-action--logout {
  margin-top: 16px;
  border-color: var(--danger-border);
  background: var(--danger-soft);
  color: var(--danger-border);
}

.menu-feedback {
  margin: 14px 0 0;
  border-radius: var(--radius-sm);
  padding: 12px;
  font-weight: 700;
  line-height: 1.35;
}

.menu-feedback--error {
  border: 1px solid var(--danger-border);
  background: var(--danger-soft);
  color: var(--danger-border);
}

.menu-feedback--loading {
  border: 1px solid var(--primary);
  background: var(--primary-soft);
  color: var(--primary);
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

.chat-modal.modal--full {
  height: calc(100vh - 24px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  height: calc(100dvh - 24px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.chat-modal .modal__header {
  flex: 0 0 auto;
}

.chat-modal .modal__body {
  flex: 1 1 auto;
  min-height: 0;
  padding: 0;
  display: flex;
}

.chat-shell {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.chat-history {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  overscroll-behavior: contain;
}

.chat-message-row {
  width: 100%;
  display: flex;
}

.chat-message-row--user {
  justify-content: flex-end;
}

.chat-message-row--assistant {
  justify-content: flex-start;
}

.chat-message {
  min-width: 0;
  max-width: 84%;
  border: 2px solid;
  border-radius: var(--radius-md);
  padding: 12px 14px;
  color: var(--text);
  line-height: 1.4;
  overflow-wrap: anywhere;
}

.chat-message--user {
  border-color: var(--danger-border);
  background: var(--danger-soft);
}

.chat-message--assistant {
  border-color: var(--assistant-border);
  background: var(--assistant-soft);
}

.chat-message--loading {
  min-width: 56px;
  text-align: center;
  font-weight: 800;
}

.chat-message--target {
  outline: 3px solid rgba(47, 158, 68, 0.35);
  outline-offset: 3px;
}

.chat-message__text {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.chat-message__attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 0;
  margin: 10px 0 0;
  list-style: none;
}

.chat-message__attachment {
  border: 1px solid currentColor;
  border-radius: 999px;
  padding: 3px 8px;
  font-size: 0.78rem;
  font-weight: 700;
}

.chat-error {
  margin: 0 16px 12px;
  border: 1px solid var(--danger-border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  background: var(--danger-soft);
  color: var(--danger-border);
  font-weight: 700;
  line-height: 1.35;
}

.chat-composer {
  position: sticky;
  bottom: 0;
  z-index: 1;
  flex: 0 0 auto;
  border-top: 2px solid var(--primary);
  padding: 12px 14px calc(12px + env(safe-area-inset-bottom));
  background: var(--primary-soft);
}

.chat-input-label {
  display: block;
  margin-bottom: 8px;
  color: var(--primary);
  font-size: 0.88rem;
  font-weight: 800;
}

.chat-composer__row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  align-items: stretch;
  min-width: 0;
}

.chat-input {
  width: 100%;
  min-width: 0;
  min-height: 88px;
  max-height: 176px;
  resize: vertical;
  border: 2px solid var(--primary);
  border-radius: var(--radius-md);
  padding: 12px;
  background: var(--bg);
  color: var(--text);
  line-height: 1.4;
}

.chat-input:disabled {
  cursor: progress;
  opacity: 0.72;
}

.chat-composer__actions {
  width: 76px;
  display: grid;
  grid-template-rows: 1fr 1fr;
  gap: 8px;
}

.chat-attach-button,
.chat-send-button {
  min-height: 44px;
  border-radius: var(--radius-sm);
  padding: 0 10px;
  font-weight: 800;
}

.chat-attach-button {
  border: 2px solid var(--primary);
  background: var(--bg);
  color: var(--primary);
}

.chat-send-button {
  border: 0;
  background: var(--primary);
  color: #ffffff;
}

.chat-attach-button:disabled,
.chat-send-button:disabled {
  cursor: progress;
  opacity: 0.6;
}

.chat-attachment-list {
  min-height: 0;
  margin-top: 10px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chat-attachment-chip {
  max-width: 100%;
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--primary);
  border-radius: 999px;
  padding: 4px 4px 4px 10px;
  background: var(--bg);
  color: var(--primary);
  font-size: 0.84rem;
  font-weight: 700;
}

.chat-attachment-chip__name {
  min-width: 0;
  max-width: 260px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-attachment-chip__remove {
  flex: 0 0 44px;
  width: 44px;
  min-height: 44px;
  border: 0;
  border-radius: 999px;
  background: var(--primary-soft);
  color: var(--primary);
  font-weight: 900;
}

.goals-modal .modal__body {
  padding: 18px 16px calc(18px + env(safe-area-inset-bottom));
}

.goal-form {
  display: grid;
  gap: 16px;
}

.goal-fields {
  display: grid;
  gap: 12px;
}

.goal-field {
  display: grid;
  gap: 7px;
}

.goal-label {
  color: var(--text);
  font-weight: 800;
  line-height: 1.25;
}

.goal-control {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: stretch;
  overflow: hidden;
  border: 2px solid var(--primary);
  border-radius: var(--radius-md);
  background: var(--bg);
}

.goal-input {
  width: 100%;
  min-height: 50px;
  border: 0;
  padding: 10px 12px;
  color: var(--text);
  background: var(--bg);
}

.goal-input:disabled {
  cursor: progress;
  opacity: 0.72;
}

.goal-unit {
  min-width: 58px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-left: 2px solid var(--primary);
  padding: 0 12px;
  color: var(--primary);
  background: var(--primary-soft);
  font-weight: 800;
}

.goal-error {
  margin: 0;
  border: 1px solid var(--danger-border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  background: var(--danger-soft);
  color: var(--danger-border);
  font-weight: 700;
  line-height: 1.35;
}

.goal-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.goal-cancel-button,
.goal-save-button {
  border-radius: var(--radius-sm);
  font-weight: 800;
}

.goal-cancel-button {
  border: 2px solid var(--primary);
  background: var(--bg);
  color: var(--primary);
}

.goal-save-button {
  border: 0;
  background: var(--primary);
  color: #ffffff;
}

.goal-cancel-button:disabled,
.goal-save-button:disabled {
  cursor: progress;
  opacity: 0.65;
}

@media (max-width: 360px) {
  .dashboard {
    padding-left: 14px;
    padding-right: 14px;
  }

  .status-card {
    gap: 6px;
    padding: 14px 10px;
  }

  .status-current {
    font-size: 0.98rem;
  }

  .status-goal {
    font-size: 0.68rem;
  }

  .status-label {
    font-size: 0.78rem;
  }

  .log-header {
    gap: 8px;
  }

  .date-display {
    font-size: 0.92rem;
  }

  .date-button,
  .add-meal-button {
    width: 44px;
  }

  .chat-history {
    padding: 12px;
  }

  .chat-message {
    max-width: 88%;
    padding: 10px 12px;
  }

  .chat-composer {
    padding-left: 12px;
    padding-right: 12px;
  }

  .chat-composer__actions {
    width: 68px;
  }

  .chat-attach-button,
  .chat-send-button {
    padding: 0 6px;
    font-size: 0.86rem;
  }
}

@media (max-width: 330px) {
  .log-header {
    grid-template-columns: 1fr auto;
  }

  .date-switcher {
    grid-column: 1 / -1;
    justify-content: space-between;
  }
}
