:root {
  --bg: #f7f4ef;
  --card: #ffffff;
  --accent: #b65c3f;
  --accent-dark: #9c4a30;
  --text: #2c2722;
  --muted: #8a8178;
  --border: #e5ded4;
  font-size: 16px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: Georgia, "Times New Roman", serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.hidden { display: none !important; }

.centered {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 2rem 2.5rem;
  width: min(380px, 90vw);
  text-align: center;
  box-shadow: 0 4px 24px rgba(0,0,0,.06);
}

.card h1 { margin: 0 0 .25rem; font-size: 1.4rem; }
.card p { margin: 0 0 1.25rem; color: var(--muted); }

input[type="password"], textarea {
  width: 100%;
  padding: .65rem .8rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font: inherit;
  background: #fffdf9;
}

button {
  font: inherit;
  border: 0;
  background: var(--accent);
  color: #fff;
  padding: .6rem 1.2rem;
  border-radius: 8px;
  cursor: pointer;
}
button:hover { background: var(--accent-dark); }
button:disabled { opacity: .5; cursor: default; }

.card button { width: 100%; margin-top: .75rem; }

.error { color: #a4302c; }

#app-view { flex: 1; display: flex; flex-direction: column; height: 100vh; }

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: .75rem 1.25rem;
  background: var(--card);
  border-bottom: 1px solid var(--border);
}
header h1 { font-size: 1.1rem; margin: 0; }

#content-area {
  flex: 1;
  display: flex;
  overflow: hidden;
  min-height: 0;
}

/* ── left column: audit panels ── */
#left-col {
  width: clamp(260px, 22vw, 320px);
  flex-shrink: 0;
  overflow-y: auto;
  padding: .75rem;
  display: flex;
  flex-direction: column;
  gap: .75rem;
  border-right: 1px solid var(--border);
  background: #fbf9f5;
}

/* ── middle column: chat ── */
#chat-col {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

main {
  flex: 1;
  overflow-y: auto;
  padding: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: .9rem;
  max-width: 860px;
  width: 100%;
  margin: 0 auto;
}

/* ── right column: gallery + description review ── */
#right-col {
  width: min(400px, 32vw);
  flex-shrink: 0;
  overflow-y: auto;
  background: var(--card);
  border-left: 1px solid var(--border);
  padding: 1rem 1.1rem 1.5rem;
  display: flex;
  flex-direction: column;
  gap: .9rem;
}
#right-tabs { display: flex; gap: .4rem; align-items: center; }
.tab-btn {
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--border);
  padding: .35rem .8rem;
  font-size: .85rem;
}
.tab-btn.active { background: var(--accent); color: #fff; }
#right-close { margin-left: auto; padding: .25rem .6rem; }

#right-col .review-product { margin: 0; color: var(--muted); font-size: .85rem; }
#review-sidebar { display: flex; flex-direction: column; gap: .9rem; }
#review-sidebar section { display: flex; flex-direction: column; gap: .4rem; }
#review-sidebar .label { color: var(--muted); font-size: .78rem; letter-spacing: .04em; }
#review-sidebar textarea {
  width: 100%;
  font-family: Consolas, "Courier New", monospace;
  font-size: .78rem;
  line-height: 1.4;
  padding: .55rem .7rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fffdf9;
  resize: vertical;
}
.save-btn { align-self: flex-start; }
.save-btn.copied { background: #4c7d4f; }
.panel-hint { margin: 0; font-size: .75rem; color: var(--muted); }

/* ── product gallery (right column) ── */
#gallery-panel { display: flex; flex-direction: column; gap: .7rem; }
#gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
  gap: .6rem;
}
.gallery-thumb {
  position: relative;
  border: 2px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
  padding: 0;
  background: #fffdf9;
}
.gallery-thumb img { width: 100%; height: 110px; object-fit: cover; display: block; }
.gallery-thumb.as-ref { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.gallery-thumb .cover-badge {
  position: absolute; top: 4px; left: 4px;
  background: rgba(182, 92, 63, .92); color: #fff;
  font-size: .65rem; padding: .1rem .4rem; border-radius: 6px;
}
.gallery-thumb .ref-badge {
  position: absolute; top: 4px; right: 4px;
  background: var(--accent); color: #fff;
  font-size: .7rem; padding: .1rem .35rem; border-radius: 6px;
  display: none;
}
.gallery-thumb.as-ref .ref-badge { display: block; }
.gallery-empty { color: var(--muted); font-style: italic; margin: .25rem 0; font-size: .85rem; }

/* ── reference chips above the composer ── */
#ref-chips {
  display: flex; flex-wrap: wrap; gap: .4rem;
  max-width: 860px; margin: 0 auto .55rem; width: 100%;
}
.ref-chip {
  display: inline-flex; align-items: center; gap: .35rem;
  background: #f3ece4; border: 1px solid var(--border);
  border-radius: 999px; padding: .15rem .3rem .15rem .6rem;
  font-size: .78rem; color: var(--text);
}
.ref-chip img { width: 24px; height: 24px; object-fit: cover; border-radius: 50%; }
.ref-chip button {
  background: transparent; color: var(--muted);
  padding: 0 .35rem; font-size: .85rem; line-height: 1;
}
.ref-chip button:hover { background: transparent; color: #a4302c; }

@media (max-width: 1100px) {
  #left-col {
    position: fixed; top: 0; bottom: 0; left: 0; z-index: 40;
    width: min(320px, 85vw);
    transform: translateX(-100%);
    transition: transform .2s ease;
    box-shadow: 4px 0 24px rgba(0,0,0,.15);
  }
  #left-col.open { transform: translateX(0); }
  #right-col { width: min(340px, 42vw); }
}
@media (min-width: 1101px) {
  #toggle-audits { display: none; }
}
@media (max-width: 900px) {
  #right-col {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
    width: 100%; max-height: 60vh;
    border-left: 0; border-top: 2px solid var(--accent);
    box-shadow: 0 -8px 24px rgba(0,0,0,.12);
  }
}

.msg {
  padding: .8rem 1rem;
  border-radius: 12px;
  white-space: pre-wrap;
  line-height: 1.45;
  max-width: 92%;
}
.msg.user { background: var(--accent); color: #fff; align-self: flex-end; }
.msg.agent { background: var(--card); border: 1px solid var(--border); align-self: flex-start; }

.platform-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-left: 4px solid var(--accent);
  border-radius: 10px;
  padding: .9rem 1rem;
  align-self: stretch;
}
.platform-card h3 { margin: 0 0 .5rem; font-size: 1rem; letter-spacing: .05em; }
.platform-card section { margin-bottom: .6rem; }
.platform-card .label {
  display: flex; justify-content: space-between; align-items: center;
  color: var(--muted); font-size: .8rem; margin-bottom: .2rem;
}
.platform-card pre {
  margin: 0; white-space: pre-wrap; font-family: inherit;
  background: #faf7f2; border-radius: 6px; padding: .5rem .65rem;
}
.copy-btn { font-size: .75rem; padding: .25rem .6rem; }
.copy-btn.copied { background: #4c7d4f; }

.image-card {
  align-self: flex-start;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: .75rem;
  max-width: 480px;
}
.image-card img { width: 100%; border-radius: 8px; display: block; }
.image-card .actions {
  display: flex; justify-content: space-between; align-items: center; margin-top: .5rem;
}
.image-card .review { font-size: .75rem; color: #8a6d3b; }

#status-line {
  max-width: 860px; width: 100%; margin: 0 auto;
  padding: .25rem 1.25rem .5rem;
  color: var(--muted); font-style: italic; font-size: .9rem;
}

/* ── agent activity trace (live sub-window under the chat) ── */
#trace-panel {
  max-width: 860px; width: 100%; margin: 0 auto;
  border: 1px solid var(--border);
  border-bottom: 0;
  border-radius: 10px 10px 0 0;
  background: #fffdf9;
  flex-shrink: 0;
}
#trace-panel .panel-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: .4rem .8rem;
  border-bottom: 1px solid var(--border);
}
#trace-panel .panel-head strong { font-size: .8rem; color: var(--muted); }
#trace-panel .panel-head button { padding: .15rem .6rem; font-size: .75rem; }
#trace-log {
  max-height: 26vh;
  overflow-y: auto;
  padding: .4rem .8rem .6rem;
  font-family: Consolas, "Courier New", monospace;
  font-size: .72rem;
  line-height: 1.45;
}
.trace-line { padding: .15rem 0; border-top: 1px dotted var(--border); }
.trace-line:first-child { border-top: 0; }
.trace-line .t-head { cursor: default; color: var(--text); }
.trace-line.has-detail .t-head { cursor: pointer; }
.trace-line.has-detail .t-head:hover { color: var(--accent-dark); }
.trace-line .t-time { color: var(--muted); margin-right: .4rem; }
.trace-line .t-detail {
  margin: .25rem 0 .15rem;
  padding: .35rem .55rem;
  background: #f5f1ea;
  border-radius: 6px;
  white-space: pre-wrap;
  word-break: break-word;
  color: var(--muted);
}
.trace-empty { color: var(--muted); font-style: italic; padding: .3rem 0; }

.header-actions { display: flex; gap: .5rem; }
button.secondary {
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--border);
}
button.secondary:hover { background: #f3ece4; }

footer {
  background: var(--card);
  border-top: 1px solid var(--border);
  padding: .75rem 1.25rem 1rem;
}

/* ── audit panels (left column) ── */
.audit-panel {
  background: var(--card);
  border: 1px solid var(--border);
  border-left: 4px solid #c98a2b;
  border-radius: 10px;
  padding: .7rem .9rem;
}
#needs-photos-panel { border-left-color: var(--accent); }
.audit-panel .panel-head {
  display: flex; justify-content: space-between; align-items: center;
  gap: .5rem;
}
.audit-panel .panel-head strong { font-size: .85rem; }
#needs-desc-list, #needs-photos-list { margin-top: .5rem; max-height: 40vh; overflow-y: auto; }
#needs-desc-list.collapsed, #needs-photos-list.collapsed { display: none; }

.needs-photo-row {
  display: flex; align-items: center;
  gap: .5rem;
  padding: .35rem 0;
  border-top: 1px solid var(--border);
  font-size: .85rem;
  cursor: pointer;
}
.needs-photo-row:hover { background: #f3ece4; }
.needs-photo-row.selected {
  background: #f3ece4;
  box-shadow: inset 3px 0 0 var(--accent);
}
.needs-photo-row .row-thumb {
  width: 36px; height: 36px; object-fit: cover;
  border-radius: 6px; border: 1px solid var(--border);
  flex-shrink: 0;
}
.needs-photo-row .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.needs-photo-row .count-badge {
  color: var(--accent-dark); font-size: .75rem; white-space: nowrap;
  border: 1px solid var(--border); border-radius: 999px; padding: .05rem .45rem;
  background: #fffdf9;
}
.needs-desc-row {
  display: flex; justify-content: space-between; align-items: center;
  gap: .6rem;
  padding: .35rem 0;
  border-top: 1px solid var(--border);
  font-size: .9rem;
}
.needs-desc-row .meta { color: var(--muted); font-size: .78rem; white-space: nowrap; }
.needs-desc-row .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fix-btn { padding: .25rem .8rem; font-size: .82rem; }

/* ── quick-action bar ── */
#action-bar {
  display: flex;
  gap: .6rem;
  max-width: 860px;
  margin: 0 auto .7rem;
  flex-wrap: wrap;
}
.action-btn {
  flex: 1 1 180px;
  background: #fffdf9;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: .65rem .8rem;
  text-align: left;
  font-size: .92rem;
  line-height: 1.3;
}
.action-btn:hover { background: #f3ece4; border-color: var(--accent); }
.action-btn .icon { font-size: 1.2rem; margin-right: .4rem; }

/* ── modal ── */
#modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(44, 39, 34, .45);
  display: flex; align-items: center; justify-content: center;
  z-index: 50;
}
#modal {
  background: var(--card);
  border-radius: 14px;
  width: min(560px, 94vw);
  max-height: 88vh;
  overflow-y: auto;
  box-shadow: 0 12px 48px rgba(0,0,0,.22);
}
#modal-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 1rem 1.25rem .25rem;
}
#modal-header h2 { margin: 0; font-size: 1.15rem; }
#modal-body { padding: .75rem 1.25rem 1.25rem; }

.modal-form { display: flex; flex-direction: column; gap: .8rem; }
.modal-form label { font-size: .85rem; color: var(--muted); display: block; margin-bottom: .25rem; }
.modal-form input[type="text"], .modal-form textarea, .modal-form select {
  width: 100%;
  padding: .55rem .7rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font: inherit;
  background: #fffdf9;
}
.modal-form .checks { display: flex; gap: 1rem; flex-wrap: wrap; }
.modal-form .checks label { color: var(--text); font-size: .9rem; display: flex; align-items: center; gap: .35rem; margin: 0; }
.modal-form .submit-row { display: flex; justify-content: flex-end; gap: .6rem; margin-top: .25rem; }
.field-error { color: #a4302c; font-size: .8rem; }

/* ── brand style panel ── */
.style-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: .7rem;
  margin-top: .75rem;
}
.style-thumb { position: relative; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.style-thumb img { width: 100%; height: 110px; object-fit: cover; display: block; }
.style-thumb button {
  position: absolute; top: 4px; right: 4px;
  padding: .1rem .45rem; font-size: .8rem;
  background: rgba(164, 48, 44, .9);
}
.style-empty { color: var(--muted); font-style: italic; margin: .5rem 0; }
#composer {
  display: flex; gap: .6rem; max-width: 860px; margin: 0 auto;
}
#composer textarea { flex: 1; resize: vertical; }
.hint {
  max-width: 860px; margin: .5rem auto 0; color: var(--muted); font-size: .78rem;
}
