:root {
  --bg: #f4f9ef;
  --ink: #26342a;
  --muted: #64736a;
  --line: #d5e2d0;
  --card: #ffffff;
  --primary: #2e9e5b;
  --primary-ink: #ffffff;
  --accent: #ff8a3d;
  --danger: #d64545;
  --in: #1e88e5;   /* 먹이에서 오는 화살표 */
  --out: #e53935;  /* 천적에게 가는 화살표 */
  --radius: 14px;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: "Malgun Gothic", "Apple SD Gothic Neo", "Noto Sans KR", sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 16px;
}
h1, h2, h3, .btn, .tab, .eco-card .name, .label, .hint, .legend, .gal-title { font-family: "Jua", "Malgun Gothic", sans-serif; font-weight: normal; }
.hidden { display: none !important; }
.small { font-size: 13px; color: var(--muted); }
.screen { min-height: 100%; }

/* ---------- 버튼 ---------- */
.btn {
  border: 2px solid var(--line); background: #fff; color: var(--ink);
  border-radius: 12px; padding: 8px 14px; font-size: 17px; cursor: pointer;
  white-space: nowrap; transition: transform .08s, box-shadow .15s;
}
.btn:hover { box-shadow: 0 2px 8px rgba(0,0,0,.12); }
.btn:active { transform: scale(.96); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn.share { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { background: #fff; border-color: var(--danger); color: var(--danger); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.big { font-size: 22px; padding: 14px 26px; border-radius: 16px; }
.btn.small { font-size: 14px; padding: 5px 10px; }
.btn.icon { width: 40px; padding: 6px 0; }
.btn.wide { width: 100%; margin-top: 12px; }
.btn.connect.on { background: var(--in); border-color: var(--in); color: #fff; box-shadow: 0 0 0 4px rgba(30,136,229,.25); }
.btn:disabled { opacity: .5; cursor: default; }

/* ---------- 처음 화면 ---------- */
#home { padding: 28px 20px 40px; max-width: 1100px; margin: 0 auto; }
.home-head { text-align: center; margin-bottom: 22px; }
.home-head h1 { font-size: clamp(30px, 5vw, 46px); margin: 0 0 6px; color: #1f6b3d; }
.home-head p { font-size: 19px; margin: 0; color: var(--muted); }
.eco-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 18px; }
.eco-card {
  position: relative; height: 190px; border: 4px solid #fff; border-radius: 22px; cursor: pointer;
  background-size: cover; background-position: center; overflow: hidden; padding: 0;
  box-shadow: 0 6px 18px rgba(0,0,0,.12); transition: transform .15s, box-shadow .15s; text-align: left;
}
.eco-card:hover { transform: translateY(-4px) rotate(-.5deg); box-shadow: 0 12px 26px rgba(0,0,0,.18); }
.eco-card .cap { position: absolute; left: 0; right: 0; bottom: 0; padding: 10px 14px; background: linear-gradient(transparent, rgba(0,0,0,.55)); color: #fff; }
.eco-card .name { font-size: 30px; display: block; text-shadow: 0 2px 4px rgba(0,0,0,.4); }
.eco-card .desc { font-size: 14px; opacity: .95; }
.eco-card .icon { position: absolute; top: 10px; left: 12px; font-size: 38px; filter: drop-shadow(0 2px 3px rgba(0,0,0,.3)); }
.eco-card .badge { position: absolute; top: 12px; right: 12px; background: #fff; color: var(--primary); border-radius: 999px; padding: 3px 10px; font-size: 13px; font-weight: bold; }
.home-actions { text-align: center; margin-top: 28px; }

/* ---------- 상단 막대 ---------- */
.topbar {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  padding: 8px 12px; background: #fff; border-bottom: 2px solid var(--line);
}
.spacer { flex: 1; }
.eco-badge { font-family: "Jua", sans-serif; font-size: 20px; padding: 4px 12px; background: #e8f5e0; border-radius: 999px; }
.title-input { font-size: 18px; padding: 7px 10px; border: 2px dashed var(--line); border-radius: 10px; min-width: 200px; flex: 0 1 300px; font-family: inherit; }
.title-input[readonly] { border-color: transparent; font-weight: bold; }
.view-by { color: var(--muted); }
.zoom { display: flex; gap: 2px; }
.hint { padding: 7px 14px; background: #fffbe6; border-bottom: 2px solid #f3e6a6; font-size: 17px; min-height: 36px; }
.hint.connect { background: #e3f1ff; border-color: #b6d8fb; }

body.readonly .edit-only { display: none !important; }
body:not(.readonly) .view-only { display: none !important; }

/* ---------- 작업 공간 ---------- */
#editor { display: flex; flex-direction: column; height: 100vh; }
.workspace { flex: 1; display: flex; min-height: 0; position: relative; }
.palette { width: 150px; background: #fff; border-right: 2px solid var(--line); padding: 10px 8px; overflow-y: auto; }
.palette h3 { margin: 0; font-size: 20px; text-align: center; }
.palette .small { text-align: center; margin: 2px 0 8px; }
.sticker-list { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.sticker-group { font-family: "Jua", sans-serif; font-size: 15px; margin: 10px 0 4px; padding-left: 6px; border-left: 5px solid var(--gc); }
.sticker.add-tile { width: 100%; border: 3px dashed var(--primary); border-left-width: 3px; background: #effaf2; color: var(--primary); font-family: "Jua", sans-serif; font-size: 15px; padding: 8px 2px; flex-direction: row; justify-content: center; gap: 4px; }
.sticker.add-tile .e { font-size: 22px; }

/* 크기 조절 */
.size-row { display: flex; align-items: center; gap: 6px; margin: 0 0 12px; padding: 6px 8px; background: #f6faf3; border-radius: 12px; font-family: "Jua", sans-serif; }
.size-row input[type=range] { flex: 1; min-width: 60px; accent-color: var(--primary); }
.size-row .btn.icon { width: 32px; padding: 3px 0; }
.size-val { width: 44px; text-align: right; font-size: 14px; }

/* 확인 창 */
.ask-box { width: 380px; text-align: center; }
.ask-msg { font-family: "Jua", sans-serif; font-size: 21px; white-space: pre-line; margin: 6px 0 4px; }
.ask-input { width: 100%; font-size: 18px; padding: 8px; border: 2px solid var(--line); border-radius: 10px; margin-top: 8px; }
.ask-box .row { justify-content: center; }
.ask-box .btn { min-width: 100px; font-size: 19px; }

/* 생물 추가 창 */
.wide-box { width: 560px; }
.mode-tabs { display: flex; gap: 6px; margin-bottom: 10px; }
.mode-tab { flex: 1; font-family: "Jua", sans-serif; font-size: 17px; padding: 8px; border: 2px solid var(--line); border-radius: 12px; background: #fff; cursor: pointer; }
.mode-tab.active { background: var(--primary); border-color: var(--primary); color: #fff; }
.icon-search { width: 100%; font-size: 16px; padding: 7px 10px; border: 2px solid var(--line); border-radius: 10px; margin-bottom: 8px; }
.icon-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(64px, 1fr)); gap: 5px; max-height: 230px; overflow-y: auto; padding: 2px; }
.icon-opt { border: 2px solid var(--line); border-radius: 10px; background: #fff; cursor: pointer; padding: 3px 1px; display: flex; flex-direction: column; align-items: center; font-size: 11px; }
.icon-opt .e { font-size: 28px; }
.icon-opt.on { border-color: var(--primary); background: #e3f6e8; box-shadow: 0 0 0 2px var(--primary); }
.add-fields { display: flex; gap: 12px; align-items: flex-end; margin-top: 10px; }
.add-fields .field { flex: 1; margin: 0; }
.add-chosen { width: 64px; height: 64px; flex: none; border-radius: 14px; border: 3px solid var(--line); display: flex; align-items: center; justify-content: center; font-size: 40px; overflow: hidden; background: #fff; }
.add-chosen img { width: 100%; height: 100%; object-fit: cover; }

/* 댓글 */
.comment-btn { background: #fff4e5; border-color: #ffcf99; }
.cmt-panel { display: flex; flex-direction: column; }
.cmt-panel.hidden { display: none; }
.cmt-title { margin: 0 0 10px; font-size: 22px; }
.cmt-list { flex: 1 0 160px; overflow-y: auto; display: flex; flex-direction: column; gap: 8px; }
.cmt-item { background: #f6faf3; border-radius: 14px; padding: 8px 12px; position: relative; }
.cmt-item .who { font-family: "Jua", sans-serif; font-size: 16px; }
.cmt-item .when { font-size: 12px; color: var(--muted); margin-left: 6px; }
.cmt-item .txt { margin: 3px 0 0; white-space: pre-wrap; word-break: break-word; }
.cmt-item .cx { position: absolute; top: 4px; right: 8px; border: none; background: none; color: var(--danger); cursor: pointer; font-size: 16px; }
.cmt-item.owner { background: #fff4e5; }
.cmt-empty { color: var(--muted); text-align: center; padding: 20px 0; }
.cmt-form { border-top: 2px solid var(--line); padding-top: 10px; margin-top: 8px; display: flex; flex-direction: column; gap: 6px; }
.cmt-form input { font-size: 16px; padding: 7px 10px; border: 2px solid var(--line); border-radius: 10px; }
.cmt-form textarea { min-height: 70px; }
.quick { display: flex; flex-wrap: wrap; gap: 4px; }
.quick button { border: 2px solid #ffcf99; background: #fff8ee; border-radius: 999px; padding: 3px 9px; font-size: 13px; cursor: pointer; }
.sticker {
  border: 2px solid var(--line); border-radius: 12px; background: #fff; cursor: pointer; padding: 4px 2px;
  display: flex; flex-direction: column; align-items: center; font-size: 13px; border-left-width: 5px;
}
.sticker .e { font-size: 30px; line-height: 1.15; }
.sticker:hover { background: #f3faef; }
.stage { flex: 1; position: relative; overflow: auto; background: #dfe9da; display: flex; min-width: 0; scrollbar-width: thin; }
.board-wrap { position: relative; margin: auto; flex: none; }
.board {
  position: absolute; left: 0; top: 0; width: 1600px; height: 1000px; transform-origin: 0 0;
  background-size: 100% 100%; overflow: hidden;
}
.links { position: absolute; left: 0; top: 0; pointer-events: none; }
.links .halo { fill: none; stroke: rgba(255,255,255,.85); stroke-width: 10; stroke-linecap: round; }
.links .line { fill: none; stroke: #2b2b2b; stroke-width: 4.5; stroke-linecap: round; }
.links .hit { fill: none; stroke: transparent; stroke-width: var(--hw, 26px); pointer-events: stroke; cursor: pointer; }
body.readonly .links .hit { cursor: default; }
.links .in .line { stroke: var(--in); stroke-width: 6; }
.links .out .line { stroke: var(--out); stroke-width: 6; }
.links .dim { opacity: .3; }
.links g:hover .halo { stroke: #fff59d; }
.empty-msg { position: absolute; left: 50%; top: 50%; transform: translate(-50%,-50%); background: rgba(255,255,255,.9); padding: 24px 34px; border-radius: 20px; font-family: "Jua", sans-serif; font-size: 30px; text-align: center; color: #3b5a44; pointer-events: none; }

/* 화살표 지우기 ✕ 버튼 */
.links .del { display: none; pointer-events: all; cursor: pointer; }
.links .del circle { fill: var(--danger); stroke: #fff; stroke-width: 3; }
.links .del text { fill: #fff; font-size: 20px; font-weight: bold; text-anchor: middle; dominant-baseline: central; pointer-events: none; }
.links .del:hover circle { fill: #b71c1c; }
body:not(.readonly) .links g.lk:hover .del, body:not(.readonly) .links g.lk.picked .del { display: block; }
.links g.picked .line { stroke: #ff9800; stroke-width: 7; }
.links g.picked .halo { stroke: #fff59d; }

/* ---------- 생물 카드 ---------- */
.card { --s: 1; position: absolute; width: calc(120px * var(--s)); display: flex; flex-direction: column; align-items: center; touch-action: none; cursor: grab; user-select: none; -webkit-user-select: none; }
.card .rs {
  display: none; position: absolute; left: calc(120px * var(--s) - 20px); top: calc(120px * var(--s) - 20px);
  width: 34px; height: 34px; border-radius: 50%; background: #ffd600; border: 3px solid #fff; box-shadow: 0 2px 6px rgba(0,0,0,.35);
  font-size: 18px; line-height: 28px; text-align: center; cursor: nwse-resize; z-index: 2; touch-action: none;
}
body:not(.readonly) .card.sel .rs { display: block; }
body.readonly .card { cursor: pointer; touch-action: auto; }
.card.dragging { cursor: grabbing; z-index: 10; }
.card.dragging .pic { transform: scale(1.06); }
.card .pic {
  width: calc(120px * var(--s)); height: calc(120px * var(--s)); border-radius: calc(20px * var(--s)); border: 6px solid var(--tc, #888); background: #fff;
  overflow: hidden; box-shadow: 0 4px 10px rgba(0,0,0,.25); display: flex; align-items: center; justify-content: center;
  transition: transform .1s;
}
.card .pic img { width: 100%; height: 100%; object-fit: cover; pointer-events: none; }
.card .emoji { font-size: calc(74px * var(--s)); line-height: 1; font-family: "Segoe UI Emoji", "Apple Color Emoji", "Noto Color Emoji", sans-serif; }
.card .label { margin-top: 6px; background: #fff; border: 2px solid #333; border-radius: 8px; padding: 1px 10px; font-size: clamp(17px, calc(15px + 6px * var(--s)), 32px); white-space: nowrap; box-shadow: 0 2px 4px rgba(0,0,0,.2); }
.card.sel .pic { outline: 6px solid #ffd600; outline-offset: 3px; }
.card.prey .pic { outline: 5px solid var(--in); outline-offset: 3px; }
.card.pred .pic { outline: 5px solid var(--out); outline-offset: 3px; }
.card.pending .pic { animation: pulse 1s infinite; outline: 6px solid var(--in); outline-offset: 3px; }
@keyframes pulse { 50% { transform: scale(1.08); } }

/* ---------- 자세히 보기 서랍 ---------- */
.drawer {
  position: absolute; top: 10px; right: 10px; bottom: 10px; width: 340px; max-width: calc(100% - 20px);
  background: #fff; border-radius: 18px; box-shadow: 0 8px 30px rgba(0,0,0,.25); padding: 16px; overflow-y: auto; z-index: 20;
}
.drawer-close { position: absolute; top: 8px; right: 10px; border: none; background: none; font-size: 22px; cursor: pointer; color: var(--muted); }
.d-top { display: flex; gap: 12px; align-items: center; margin-bottom: 12px; }
.d-pic { width: 96px; height: 96px; border-radius: 16px; border: 5px solid var(--tc, #888); overflow: hidden; flex: none; display: flex; align-items: center; justify-content: center; background: #fff; }
.d-pic img { width: 100%; height: 100%; object-fit: cover; }
.d-pic .emoji { font-size: 56px; font-family: "Segoe UI Emoji", "Apple Color Emoji", "Noto Color Emoji", sans-serif; }
.d-info { display: flex; flex-direction: column; gap: 6px; flex: 1; min-width: 0; }
.d-name { font-family: "Jua", sans-serif; font-size: 24px; padding: 4px 8px; border: 2px dashed var(--line); border-radius: 10px; width: 100%; }
.d-type { font-size: 15px; padding: 5px; border-radius: 8px; border: 2px solid var(--line); }
.d-name:disabled, .d-type:disabled, textarea:disabled { background: #fff; color: var(--ink); border-color: transparent; opacity: 1; -webkit-text-fill-color: var(--ink); }
.tabs { display: flex; gap: 4px; border-bottom: 3px solid var(--line); }
.tab { flex: 1; border: none; background: #eef4ea; border-radius: 12px 12px 0 0; padding: 9px 2px; font-size: 16px; cursor: pointer; color: var(--muted); }
.tab.active { background: var(--primary); color: #fff; }
.tab-body { padding-top: 8px; }
.tab-guide { margin: 8px 0 6px; font-size: 14px; color: var(--muted); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { display: inline-flex; align-items: center; gap: 4px; border: 2px solid var(--tc, #ccc); border-radius: 999px; padding: 2px 10px 2px 3px; background: #fff; cursor: pointer; font-size: 15px; }
.chip .mini { width: 26px; height: 26px; border-radius: 50%; overflow: hidden; display: inline-flex; align-items: center; justify-content: center; font-size: 18px; background: #f3f3f3; }
.chip .mini img { width: 100%; height: 100%; object-fit: cover; }
.chip .x { color: var(--danger); font-weight: bold; margin-left: 2px; padding: 0 2px; }
.chip-empty { color: var(--muted); font-size: 14px; }
.add-link { font-size: 14px; padding: 5px; border-radius: 999px; border: 2px dashed var(--in); color: var(--in); background: #f2f8ff; }
textarea { width: 100%; min-height: 90px; border: 2px solid var(--line); border-radius: 10px; padding: 8px; font-size: 16px; font-family: inherit; resize: vertical; }
textarea.tall { min-height: 200px; }

/* ---------- 범례 ---------- */
.legend { display: flex; flex-wrap: wrap; gap: 14px; align-items: center; padding: 6px 14px; background: #fff; border-top: 2px solid var(--line); font-size: 16px; }
.legend .dot { display: inline-block; width: 16px; height: 16px; border-radius: 5px; vertical-align: -2px; margin-right: 4px; }

/* ---------- 모달 ---------- */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: flex; align-items: center; justify-content: center; z-index: 100; padding: 16px; }
.modal-box { background: #fff; border-radius: 22px; padding: 22px; width: 420px; max-width: 100%; max-height: 100%; overflow-y: auto; }
.modal-box h2 { margin: 0 0 14px; }
.field { display: flex; flex-direction: column; gap: 4px; margin: 10px 0; font-weight: bold; }
.field input, .field select { font-size: 17px; padding: 8px 10px; border: 2px solid var(--line); border-radius: 10px; font-family: inherit; font-weight: normal; }
.row { display: flex; gap: 8px; justify-content: flex-end; margin-top: 14px; align-items: center; }
.row input { flex: 1; font-size: 14px; padding: 8px; border: 2px solid var(--line); border-radius: 10px; }
.pick-area { display: block; cursor: pointer; }
.add-preview { height: 200px; border: 3px dashed var(--line); border-radius: 16px; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; font-size: 18px; color: var(--muted); overflow: hidden; background: #fafcf8; }
.add-preview img { max-width: 100%; max-height: 100%; }
.add-preview.drag { border-color: var(--primary); background: #eaf7ee; }
.sh-result { background: #fff8e8; border-radius: 12px; padding: 10px 12px; margin-top: 8px; }
.sh-result p { margin: 0; }

/* ---------- 갤러리 ---------- */
.gal-title { margin: 0 0 0 8px; font-size: 26px; }
.gal-controls { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; padding: 16px 20px 4px; font-size: 17px; }
.gal-controls input, .gal-controls select { font-size: 17px; padding: 7px 10px; border: 2px solid var(--line); border-radius: 10px; font-family: inherit; }
.gal-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 18px; padding: 16px 20px 40px; }
.gal-item { background: #fff; border-radius: 18px; overflow: hidden; box-shadow: 0 4px 14px rgba(0,0,0,.1); cursor: pointer; border: none; padding: 0; text-align: left; font-family: inherit; transition: transform .15s; }
.gal-item:hover { transform: translateY(-3px); }
.gal-item img { width: 100%; aspect-ratio: 16 / 10; object-fit: cover; display: block; background: #e5eee0; }
.gal-item .meta { padding: 10px 12px; }
.gal-item .t { font-family: "Jua", sans-serif; font-size: 21px; display: block; }
.gal-item .s { color: var(--muted); font-size: 14px; }
.gal-item .mine { background: var(--accent); color: #fff; border-radius: 999px; padding: 1px 8px; font-size: 12px; margin-left: 4px; }
.gal-msg { grid-column: 1 / -1; text-align: center; font-size: 19px; color: var(--muted); padding: 40px 0; }

/* ---------- 알림 ---------- */
.toast { position: fixed; left: 50%; bottom: 60px; transform: translateX(-50%); background: rgba(30,40,34,.92); color: #fff; padding: 12px 22px; border-radius: 14px; font-size: 17px; z-index: 200; max-width: 90%; text-align: center; }

@media (max-width: 760px) {
  .palette { width: 96px; }
  .sticker.add-tile { flex-direction: column; }
  .sticker-list { grid-template-columns: 1fr; }
  .title-input { min-width: 120px; }
  .btn { font-size: 15px; padding: 7px 10px; }
}
