* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: #f5f4f0;
  color: #222;
}

.topbar {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  padding: 12px 24px;
  background: #2b3a4a;
  color: #f5f4f0;
}
.topbar h1 { margin: 0; font-size: 18px; font-weight: 500; }
.topbar .phase { font-size: 12px; opacity: 0.6; }

main {
  display: grid;
  grid-template-columns: 420px 1fr;
  gap: 24px;
  padding: 24px;
  align-items: start;
}

main:has(.chat-panel:not([hidden])) {
  grid-template-columns: 420px 1fr 360px;
}

.panel {
  background: #fff;
  border: 1px solid #d8d4ca;
  border-radius: 6px;
  padding: 16px 20px;
}
.panel h2 { margin: 0 0 12px 0; font-size: 16px; font-weight: 600; }
.panel h3 { margin: 16px 0 6px 0; font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; color: #6a6660; }
.panel-head { display: flex; justify-content: space-between; align-items: center; }

.envelope-row { display: flex; gap: 12px; flex-wrap: wrap; margin-bottom: 8px; }
.envelope-row label { font-size: 12px; color: #444; display: flex; flex-direction: column; gap: 2px; }
.envelope-row input, .envelope-row select { padding: 4px 6px; border: 1px solid #c7c2b8; border-radius: 3px; }

table { width: 100%; border-collapse: collapse; font-size: 12px; }
table th { text-align: left; font-weight: 500; color: #6a6660; padding: 4px 6px; border-bottom: 1px solid #d8d4ca; }
table td { padding: 3px 4px; border-bottom: 1px solid #f0ede6; }
table input, table select { width: 100%; padding: 3px 4px; border: 1px solid #d8d4ca; border-radius: 3px; font-size: 12px; }
table .del { background: none; border: none; color: #b34b4b; cursor: pointer; font-size: 14px; }

.row-actions { margin-top: 6px; }
.row-actions button { background: #ece8de; border: 1px solid #c7c2b8; border-radius: 3px; padding: 3px 10px; font-size: 12px; cursor: pointer; }

.generate-row { display: flex; align-items: center; gap: 12px; margin-top: 16px; padding-top: 12px; border-top: 1px solid #e6e2d8; }
.generate-row label { font-size: 12px; color: #444; display: flex; align-items: center; gap: 6px; }
.generate-row input { width: 60px; padding: 4px 6px; border: 1px solid #c7c2b8; border-radius: 3px; }

button.primary { background: #2b3a4a; color: #f5f4f0; border: none; border-radius: 3px; padding: 6px 14px; cursor: pointer; font-weight: 500; }
button.primary:hover { background: #3a4d62; }
button#btn-load-reference { background: none; border: 1px solid #c7c2b8; border-radius: 3px; padding: 3px 10px; font-size: 12px; cursor: pointer; }

#status-line { font-size: 12px; color: #6a6660; }

#results {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(360px, 1fr));
  gap: 16px;
}
.layout-card {
  background: #fbfaf6;
  border: 1px solid #d8d4ca;
  border-radius: 4px;
  padding: 12px;
}
.layout-card .layout-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 6px; }
.layout-card .layout-head .score { font-weight: 600; }
.layout-card .breakdown { display: flex; gap: 8px; font-size: 11px; color: #6a6660; flex-wrap: wrap; margin-top: 6px; }
.layout-card .breakdown span { padding: 1px 6px; background: #ece8de; border-radius: 2px; }
.layout-card svg { width: 100%; height: auto; background: #fff; border: 1px solid #e6e2d8; border-radius: 3px; }

.empty { color: #888; font-style: italic; padding: 16px 0; }

.layout-card .layout-head .actions { display: flex; gap: 6px; }
.layout-card .layout-head .actions button { font-size: 11px; padding: 2px 8px; background: none; border: 1px solid #c7c2b8; border-radius: 3px; cursor: pointer; }
.layout-card .layout-head .actions button:hover { background: #ece8de; }

/* Chat panel — docks to right of results when open */
.chat-panel {
  grid-column: 3;
  grid-row: 1 / 3;
  display: flex;
  flex-direction: column;
  height: calc(100vh - 100px);
  position: sticky;
  top: 24px;
}
.chat-head { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; margin-bottom: 10px; }
.chat-head h2 { margin: 0; font-size: 16px; }
.chat-target { font-size: 11px; color: #6a6660; flex: 1; }
button.ghost { background: none; border: none; font-size: 18px; color: #6a6660; cursor: pointer; padding: 0 4px; }
button.ghost:hover { color: #222; }

.chat-log {
  flex: 1;
  overflow-y: auto;
  border: 1px solid #e6e2d8;
  border-radius: 4px;
  padding: 8px;
  background: #fbfaf6;
  font-size: 13px;
  margin-bottom: 8px;
}
.chat-msg { margin-bottom: 10px; }
.chat-msg.user { text-align: right; }
.chat-msg.user .bubble { background: #d8e4f0; color: #1a2a3a; }
.chat-msg.assistant .bubble { background: #ece8de; color: #2a2620; }
.chat-msg .bubble {
  display: inline-block;
  padding: 6px 10px;
  border-radius: 8px;
  max-width: 90%;
  white-space: pre-wrap;
  text-align: left;
}
.chat-msg.system { color: #6a6660; font-style: italic; font-size: 11px; padding: 4px 0; }
.chat-msg .edits { font-size: 11px; color: #6a6660; margin-top: 4px; padding-left: 6px; border-left: 2px solid #d8d4ca; }

.chat-form { display: flex; flex-direction: column; gap: 6px; }
.chat-form textarea {
  resize: vertical;
  min-height: 56px;
  border: 1px solid #c7c2b8;
  border-radius: 3px;
  padding: 6px 8px;
  font-family: inherit;
  font-size: 13px;
}
.chat-row { display: flex; gap: 6px; align-items: center; }
#btn-mic {
  width: 36px; height: 32px;
  border: 1px solid #c7c2b8;
  background: #fff;
  border-radius: 3px;
  cursor: pointer;
  font-size: 16px;
}
#btn-mic.listening { background: #d44; color: #fff; border-color: #b22; animation: pulse 1.2s infinite; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.6; } }
#btn-chat-send { flex: 1; }
.chat-status { font-size: 11px; color: #6a6660; min-height: 14px; }
.chat-status.error { color: #b34b4b; }
