:root {
  --accent: #4338ca; --accent2: #6366f1; --bg: #f5f5ff; --card: #ffffff;
  --text: #1e1b3a; --muted: #6b6b8a; --border: #e0e0f0; --bubble-user: #6366f1; --bubble-ai: #eef0fb;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Sans", "Noto Sans JP", sans-serif; background: var(--bg); color: var(--text); display: flex; flex-direction: column; }

.app-header { background: linear-gradient(135deg, var(--accent), var(--accent2)); color: #fff; padding: calc(env(safe-area-inset-top) + 10px) 14px 10px; flex-shrink: 0; }
.header-row { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.header-row-controls { flex-wrap: wrap; }
.icon-btn { border: none; background: rgba(255,255,255,.15); color: #fff; font-size: 1.1rem; width: 34px; height: 34px; border-radius: 50%; cursor: pointer; flex-shrink: 0; }
.help-btn { border: 1.5px solid rgba(255,255,255,.7); background: rgba(255,255,255,.15); color: #fff; font-weight: 700; width: 30px; height: 30px; border-radius: 50%; cursor: pointer; flex-shrink: 0; }
.draft-title { flex: 1; min-width: 0; padding: 8px 12px; border-radius: 10px; border: none; font-size: 1.05rem; font-weight: 700; background: rgba(255,255,255,.15); color: #fff; }
.draft-title::placeholder { color: rgba(255,255,255,.75); }

.field-inline { display: flex; align-items: center; gap: 6px; font-size: .82rem; color: rgba(255,255,255,.9); }
.field-inline span { white-space: nowrap; }
.field-inline select, .field-inline input { padding: 6px 8px; border-radius: 8px; border: none; font-size: .85rem; font-family: inherit; }
.field-grow { flex: 1; min-width: 160px; }
.field-grow input { width: 100%; }
.btn { padding: 8px 14px; border-radius: 8px; border: 1px solid var(--border); background: #fff; cursor: pointer; font-size: .85rem; font-family: inherit; font-weight: 600; white-space: nowrap; }
.btn-ghost { background: rgba(255,255,255,.15); color: #fff; border-color: rgba(255,255,255,.4); }
.btn-primary { background: #fbbf24; color: #422006; border: none; }

.drawer-scrim { position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 40; }
.drawer {
  position: fixed; top: 0; left: 0; bottom: 0; width: 300px; max-width: 85vw; background: #fff; z-index: 50;
  box-shadow: 4px 0 16px rgba(0,0,0,.15); display: flex; flex-direction: column;
  padding-top: env(safe-area-inset-top);
}
.drawer-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; border-bottom: 1px solid var(--border); }
.drawer-head h2 { margin: 0; font-size: 1.05rem; }
.draft-list { flex: 1; overflow-y: auto; padding: 8px; }
.draft-item { display: flex; align-items: center; gap: 8px; padding: 10px; border-radius: 8px; cursor: pointer; }
.draft-item:hover { background: var(--bg); }
.draft-item.is-active { background: #eef0fb; }
.draft-item-title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .9rem; }
.draft-item-status { font-size: .7rem; color: var(--muted); }
.draft-item .btn-delete { background: none; border: none; color: var(--muted); cursor: pointer; font-size: .85rem; }
.draft-empty { color: var(--muted); font-size: .85rem; padding: 12px; }

.columns { flex: 1; display: grid; grid-template-columns: 260px 1fr 320px; overflow: hidden; min-height: 0; }
.col { display: flex; flex-direction: column; min-height: 0; overflow: hidden; }
.col-head { padding: 10px 12px; border-bottom: 1px solid var(--border); background: var(--card); }
.col-head h2 { margin: 0 0 8px; font-size: .95rem; color: var(--muted); font-weight: 700; }
.col-head input { width: 100%; padding: 8px 10px; border-radius: 8px; border: 1px solid var(--border); font-size: .85rem; font-family: inherit; }

.col-sources { background: var(--card); border-right: 1px solid var(--border); }
.source-list { flex: 1; overflow-y: auto; padding: 8px; }
.source-card {
  background: #fbfbff; border: 1px solid var(--border); border-radius: 10px; padding: 10px; margin-bottom: 8px;
  cursor: grab; font-size: .8rem; line-height: 1.5;
}
.source-card:active { cursor: grabbing; }
.source-card.dragging { opacity: .4; }
.source-card-name { font-weight: 700; color: var(--accent); margin-bottom: 4px; }
.source-card-body { color: var(--text); display: -webkit-box; -webkit-line-clamp: 4; -webkit-box-orient: vertical; overflow: hidden; }
.source-empty { color: var(--muted); font-size: .85rem; padding: 12px; }

.col-body { background: var(--bg); overflow-y: auto; padding: 14px; gap: 14px; }
.section-block { background: var(--card); border-radius: 12px; border: 1px solid var(--border); padding: 12px; }
.section-block h2 { margin: 0 0 8px; font-size: .9rem; color: var(--accent); font-weight: 700; }
.section-input {
  width: 100%; min-height: 160px; border: 1px dashed var(--border); border-radius: 8px; padding: 10px;
  font-family: inherit; font-size: .92rem; line-height: 1.7; resize: vertical; color: var(--text); background: #fff;
}
.section-input:focus { outline: none; border-color: var(--accent2); border-style: solid; }
.section-input.drag-over { border-color: var(--accent2); border-style: solid; background: #eef0fb; }

.col-chat { background: var(--card); border-left: 1px solid var(--border); }
.chat-messages { flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 10px; }
.empty-state { margin: auto; text-align: center; color: var(--muted); font-size: .85rem; line-height: 1.6; padding: 12px; }
.msg { max-width: 92%; padding: 8px 12px; border-radius: 12px; font-size: .85rem; line-height: 1.6; white-space: pre-wrap; word-break: break-word; }
.msg.user { align-self: flex-end; background: var(--bubble-user); color: #fff; border-bottom-right-radius: 3px; }
.msg.assistant { align-self: flex-start; background: var(--bubble-ai); color: var(--text); border-bottom-left-radius: 3px; }
.msg.pending { opacity: .6; }
.chat-form { display: flex; gap: 6px; padding: 10px; border-top: 1px solid var(--border); }
.chat-form textarea {
  flex: 1; resize: none; max-height: 100px; padding: 8px 10px; border: 1px solid var(--border); border-radius: 14px;
  font-size: .85rem; font-family: inherit; line-height: 1.4;
}
.btn-send { width: 34px; height: 34px; border-radius: 50%; border: none; background: var(--accent); color: #fff; font-size: 1rem; cursor: pointer; flex-shrink: 0; }
.btn-send:disabled { opacity: .5; }

.help-panel { position: fixed; inset: 0; background: rgba(15,23,42,.5); z-index: 100; display: flex; align-items: center; justify-content: center; padding: 16px; }
.help-panel[hidden] { display: none; }
.help-card { background: #fff; border-radius: 14px; padding: 20px; max-width: 420px; max-height: 80vh; overflow-y: auto; color: var(--text); }
.help-card h2 { margin: 0 0 12px; font-size: 1.1rem; }
.help-card ul { margin: 0 0 16px; padding-left: 18px; }
.help-card li { margin-bottom: 8px; font-size: .9rem; line-height: 1.5; }

@media (max-width: 900px) {
  .columns { grid-template-columns: 1fr; grid-auto-rows: auto; overflow-y: auto; }
  .col { min-height: 320px; }
  .col-sources, .col-chat { border: none; border-bottom: 1px solid var(--border); }
}
