:root {
  --bg: #f5f7f4;
  --panel: #ffffff;
  --panel-soft: #f8faf7;
  --text: #1d2722;
  --muted: #657168;
  --line: #dbe4dc;
  --primary: #18775b;
  --primary-dark: #0f5e47;
  --accent: #c94f32;
  --warning: #9b6516;
  --warning-bg: #fff4df;
  --danger: #a83434;
  --danger-bg: #fff0f0;
  --good: #237a42;
  --info: #2f6f9f;
  --shadow: 0 18px 50px rgba(31, 50, 39, 0.12);
  font-family: "Segoe UI", "Microsoft YaHei", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    linear-gradient(180deg, rgba(24, 119, 91, 0.08), rgba(245, 247, 244, 0) 360px),
    var(--bg);
  color: var(--text);
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.62;
}

.app-shell {
  width: min(1180px, calc(100% - 32px));
  margin: 0 auto;
  padding: 28px 0 44px;
}

.topbar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(280px, 440px);
  gap: 20px;
  align-items: end;
  margin-bottom: 22px;
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--primary);
  font-size: 0.76rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2,
h3,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: clamp(2rem, 5vw, 3.7rem);
  line-height: 1.02;
  letter-spacing: 0;
}

h2 {
  margin-bottom: 0;
  font-size: 1.25rem;
}

h3 {
  margin-bottom: 0;
  font-size: 1rem;
}

input,
select,
textarea {
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  padding: 9px 11px;
  outline: none;
}

textarea {
  resize: vertical;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(24, 119, 91, 0.14);
}

.main-grid {
  display: grid;
  grid-template-columns: minmax(0, 0.92fr) minmax(0, 1.25fr);
  gap: 18px;
  align-items: start;
}

.panel {
  min-width: 0;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 18px;
}

.history-panel {
  grid-column: 1 / -1;
}

.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
}

.state-pill {
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  border-radius: 999px;
  background: #e7f2ec;
  color: var(--primary-dark);
  padding: 5px 10px;
  font-size: 0.8rem;
  font-weight: 800;
  white-space: nowrap;
}

.state-pill.muted {
  background: #eef0ee;
  color: var(--muted);
}

.state-pill.warn {
  background: var(--warning-bg);
  color: var(--warning);
}

.state-pill.bad {
  background: var(--danger-bg);
  color: var(--danger);
}

.upload-form {
  display: grid;
  gap: 14px;
}

.drop-zone {
  position: relative;
  display: grid;
  place-items: center;
  min-height: 142px;
  border: 1.5px dashed #9eb7aa;
  border-radius: 8px;
  background: var(--panel-soft);
  text-align: center;
  padding: 18px;
}

.drop-zone input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

.drop-title {
  display: block;
  margin-bottom: 8px;
  color: var(--text);
  font-size: 1.08rem;
  font-weight: 800;
}

.drop-copy {
  display: block;
  color: var(--muted);
  font-size: 0.92rem;
}

.preview-wrap {
  position: relative;
  overflow: hidden;
  min-height: 220px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #eef3ef;
}

.preview-wrap img {
  display: block;
  width: 100%;
  height: 100%;
  max-height: 360px;
  object-fit: contain;
}

.preview-wrap.empty img {
  display: none;
}

.preview-empty {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: var(--muted);
  font-weight: 700;
}

.preview-wrap:not(.empty) .preview-empty {
  display: none;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.primary-btn,
.secondary-btn,
.icon-btn {
  min-height: 42px;
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 9px 14px;
  font-weight: 800;
}

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

.primary-btn:hover {
  background: var(--primary-dark);
}

.secondary-btn,
.icon-btn {
  background: #fff;
  border-color: var(--line);
  color: var(--text);
}

.secondary-btn:hover,
.icon-btn:hover {
  border-color: #afbeb3;
  background: #f7faf8;
}

.status-box,
.notice,
.warning-list,
.uncertain-panel,
.allergen-panel {
  margin-top: 14px;
  border-radius: 8px;
  padding: 12px;
}

.status-box {
  display: grid;
  gap: 4px;
  background: #edf5f1;
  border: 1px solid #cae1d5;
}

.status-box.error {
  background: var(--danger-bg);
  border-color: #f0c9c9;
}

.status-box.warn {
  background: var(--warning-bg);
  border-color: #edd3a6;
}

.status-box span,
.description,
.history-meta,
.ingredient-meta,
.mini-text {
  color: var(--muted);
  font-size: 0.9rem;
}

.empty-state {
  min-height: 240px;
  display: grid;
  place-items: center;
  border: 1px dashed var(--line);
  border-radius: 8px;
  color: var(--muted);
  text-align: center;
  padding: 22px;
}

.empty-state.compact {
  min-height: 88px;
}

.hidden {
  display: none !important;
}

.dish-editor {
  display: grid;
  gap: 12px;
  padding: 14px;
  background: var(--panel-soft);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.dish-title-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 130px;
  gap: 12px;
  align-items: end;
}

.confidence-card {
  min-height: 42px;
  display: grid;
  align-content: center;
  border-radius: 8px;
  padding: 9px 10px;
  background: #e9f5ed;
  color: var(--good);
  font-weight: 900;
  text-align: center;
}

.confidence-card.low {
  background: var(--danger-bg);
  color: var(--danger);
}

.confidence-card.mid {
  background: var(--warning-bg);
  color: var(--warning);
}

.notice {
  background: #eef6fb;
  border: 1px solid #c8ddeb;
  color: var(--info);
  font-weight: 700;
}

.warning-list {
  background: var(--warning-bg);
  border: 1px solid #edd3a6;
  color: var(--warning);
}

.warning-list ul,
.uncertain-panel ul,
.allergen-panel ul {
  margin: 8px 0 0;
  padding-left: 18px;
}

.ingredients-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 18px 0 10px;
}

.ingredient-editor {
  display: grid;
  min-width: 0;
  gap: 10px;
}

.ingredient-group-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 8px;
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 900;
}

.ingredient-row {
  display: grid;
  grid-template-columns: minmax(72px, 0.7fr) repeat(5, minmax(76px, 1fr)) minmax(54px, 0.55fr);
  grid-template-areas:
    "include name category source visible amount remove"
    "meta meta meta meta meta meta remove";
  gap: 8px;
  align-items: end;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  background: #fff;
}

.ingredient-row > * {
  min-width: 0;
}

.ingredient-include-field {
  grid-area: include;
}

.ingredient-name-field {
  grid-area: name;
}

.ingredient-category-field {
  grid-area: category;
}

.ingredient-source-field {
  grid-area: source;
}

.ingredient-visible-field {
  grid-area: visible;
}

.ingredient-amount-field {
  grid-area: amount;
}

.ingredient-row.low-confidence {
  border-color: #edc2a4;
  background: #fffaf2;
}

.include-toggle {
  min-height: 42px;
  display: flex;
  align-items: center;
  gap: 7px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px;
}

.include-toggle input {
  width: 18px;
  min-height: 18px;
}

.ingredient-meta {
  grid-area: meta;
  align-self: center;
  min-width: 0;
  line-height: 1.35;
  overflow-wrap: anywhere;
}

.source-tag {
  display: inline-flex;
  margin-right: 6px;
  margin-bottom: 4px;
  border-radius: 999px;
  padding: 3px 8px;
  background: #eef0ee;
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 900;
}

.source-tag.visible {
  background: #e7f4ed;
  color: var(--good);
}

.source-tag.inferred {
  background: var(--warning-bg);
  color: var(--warning);
}

.source-tag.low {
  background: var(--danger-bg);
  color: var(--danger);
}

.icon-btn.ingredient-remove {
  grid-area: remove;
  width: 100%;
  color: var(--danger);
  overflow-wrap: anywhere;
}

.uncertain-panel,
.allergen-panel {
  border: 1px solid var(--line);
  background: var(--panel-soft);
}

.notes-box {
  display: block;
  margin-top: 14px;
}

.action-row {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 14px;
}

.history-search {
  display: grid;
  grid-template-columns: minmax(160px, 1fr) 150px 150px auto;
  gap: 10px;
  margin-bottom: 12px;
}

.history-list {
  display: grid;
  gap: 10px;
}

.history-item {
  display: grid;
  grid-template-columns: 74px minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  background: #fff;
}

.history-thumb {
  width: 74px;
  height: 58px;
  border-radius: 8px;
  background: #eef3ef;
  object-fit: cover;
}

.history-name {
  margin-bottom: 5px;
  font-weight: 900;
}

/* ── Auth panel (login / register) ──────────────────────────────────── */

.auth-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  align-items: center;
}

.secondary-btn.small {
  padding: 6px 14px;
  font-size: 0.84rem;
}

.user-area {
  display: flex;
  align-items: center;
  gap: 12px;
  justify-content: flex-end;
}

.user-nickname {
  font-weight: 700;
  font-size: 0.92rem;
  color: var(--primary);
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.auth-panel {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(2px);
}

.auth-panel.hidden {
  display: none;
}

.auth-panel-card {
  position: relative;
  width: min(400px, calc(100vw - 32px));
  padding: 32px 28px 24px;
  background: var(--panel);
  border-radius: 14px;
  box-shadow: var(--shadow);
}

.auth-panel-card h2 {
  margin: 0 0 18px;
  font-size: 1.35rem;
}

.auth-close {
  position: absolute;
  top: 10px;
  right: 12px;
  font-size: 1.4rem;
  color: var(--muted);
  background: none;
  border: none;
  cursor: pointer;
  padding: 4px 8px;
  line-height: 1;
}

.auth-close:hover {
  color: var(--text);
}

.auth-form {
  display: grid;
  gap: 14px;
}

.auth-form label {
  display: grid;
  gap: 4px;
}

.auth-form label span {
  font-size: 0.82rem;
  color: var(--muted);
  font-weight: 600;
}

.auth-error {
  font-size: 0.84rem;
  color: var(--danger);
  min-height: 1.2em;
}

.auth-switch {
  margin-top: 14px;
  text-align: center;
  font-size: 0.86rem;
  color: var(--muted);
}

.auth-switch a {
  color: var(--primary);
  text-decoration: none;
  font-weight: 700;
}

.auth-switch a:hover {
  text-decoration: underline;
}

.stream-panel {
  margin-bottom: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
}

.stream-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  background: #1a1f2e;
  color: #e0e6ed;
  font-size: 0.88rem;
}

.stream-status {
  font-size: 0.78rem;
  font-weight: 700;
  padding: 3px 10px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.12);
  color: #b0bec5;
}

.stream-status.ok {
  background: rgba(76, 175, 80, 0.25);
  color: #81c784;
}

.stream-status.done {
  background: rgba(33, 150, 243, 0.25);
  color: #64b5f6;
}

.stream-status.error {
  background: rgba(244, 67, 54, 0.25);
  color: #ef9a9a;
}

.stream-content {
  margin: 0;
  padding: 14px;
  max-height: 380px;
  min-height: 120px;
  overflow-y: auto;
  background: #fafbfc;
  color: #2d3436;
  font-family: "Cascadia Code", "Fira Code", "JetBrains Mono", "Consolas", monospace;
  font-size: 0.84rem;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-all;
  tab-size: 2;
}

@media (max-width: 980px) {
  .topbar,
  .main-grid {
    grid-template-columns: 1fr;
  }

  .ingredient-row {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-template-areas:
      "include remove"
      "name category"
      "source visible"
      "amount amount"
      "meta meta";
  }
}

@media (max-width: 640px) {
  .app-shell {
    width: min(100% - 20px, 1180px);
    padding-top: 16px;
  }

  .panel {
    padding: 14px;
  }

  .section-head,
  .ingredients-toolbar,
  .action-row {
    align-items: stretch;
    flex-direction: column;
  }

  .api-input-row,
  .form-grid,
  .dish-title-row,
  .history-search,
  .history-item {
    grid-template-columns: 1fr;
  }

  .ingredient-row {
    grid-template-columns: 1fr;
    grid-template-areas:
      "include"
      "name"
      "category"
      "source"
      "visible"
      "amount"
      "meta"
      "remove";
  }

  .history-thumb {
    width: 100%;
    height: 150px;
  }

  .primary-btn,
  .secondary-btn,
  .icon-btn {
    width: 100%;
  }
}

