:root {
  --bg: #15171c;
  --panel: #1d2027;
  --panel2: #23272f;
  --line: #30353f;
  --text: #e7e9ee;
  --muted: #8b93a1;
  --accent: #ffb02e;
  --accent-d: #c98410;
  --good: #59d499;
  --bad: #ff6b6b;
  --link: #6db3ff;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: "Pretendard", "맑은 고딕", "Malgun Gothic", system-ui, sans-serif;
  font-size: 14px;
  line-height: 1.5;
}

header {
  position: sticky; top: 0; z-index: 10;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  padding: 14px 20px 0;
}
.titlebar { display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap; }
h1 { font-size: 19px; margin: 0; font-weight: 700; }
h1 .sub { font-size: 12px; color: var(--muted); font-weight: 400; margin-left: 8px; }

.toolbar { display: flex; align-items: center; gap: 10px; }
.srv { font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
select, input[type="text"], input[type="number"] {
  background: var(--panel2); color: var(--text);
  border: 1px solid var(--line); border-radius: 7px;
  padding: 7px 10px; font-size: 13px; font-family: inherit;
}
select:focus, input:focus { outline: none; border-color: var(--accent); }

button {
  background: var(--panel2); color: var(--text);
  border: 1px solid var(--line); border-radius: 7px;
  padding: 7px 13px; font-size: 13px; cursor: pointer; font-family: inherit;
  transition: background .12s, border-color .12s;
}
button:hover { background: #2c313b; }
button.primary { background: var(--accent); color: #20160a; border-color: var(--accent-d); font-weight: 700; }
button.primary:hover { background: #ffbe54; }
button:disabled { opacity: .5; cursor: default; }

#valueBasis { min-width: 0; }
.help-dot { width: 20px; height: 20px; padding: 0; border-radius: 50%; border: 1px solid var(--line); background: var(--panel2); color: var(--muted); font-size: 12px; line-height: 1; cursor: pointer; }
.help-dot:hover { color: var(--accent); border-color: var(--accent); background: #2c313b; }
.help-body { text-align: left; margin: 0 18px 16px; color: var(--muted); font-size: 13px; line-height: 1.7; }
.help-body p { margin: 8px 0; }
.help-body b { color: var(--text); }

.donate { border-color: #6e3b46; color: #ff9db0; font-weight: 600; }
.donate:hover { background: #34232a; border-color: #ff9db0; color: #ffc0cd; }
.auth-user { color: var(--muted); font-size: 12px; max-width: 160px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.auth-btn { font-weight: 600; }

.tabs { display: flex; align-items: center; gap: 4px; margin-top: 14px; }
.tabs button { border: none; border-bottom: 2px solid transparent; border-radius: 0; background: none; color: var(--muted); padding: 9px 14px; font-size: 14px; }
.tabs button:hover { color: var(--text); background: none; }
.tabs button.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 700; }
.status { margin-left: auto; font-size: 12px; color: var(--muted); padding-right: 4px; }
.status.err { color: var(--bad); }

main { padding: 20px; max-width: 1000px; margin: 0 auto; }
.tab { display: none; }
.tab.active { display: block; }

.addbar, .savebar { display: flex; gap: 8px; margin: 4px 0 16px; }
.add-hint { color: var(--bad); font-size: 13px; font-weight: 600; margin: -10px 0 14px; }
.add-hint[hidden] { display: none; }
.addbar input, .savebar input { flex: 1; }

/* 아이템 자동완성 드롭다운 */
.ac-wrap { position: relative; flex: 1; }
.ac-wrap > input { width: 100%; flex: none; }
.ac-list { position: absolute; left: 0; right: 0; top: calc(100% + 4px); z-index: 50; margin: 0; padding: 4px; list-style: none; background: var(--panel2); border: 1px solid var(--line); border-radius: 8px; max-height: 320px; overflow-y: auto; box-shadow: 0 10px 28px rgba(0,0,0,.45); }
.ac-list[hidden] { display: none; }
.ac-item { display: flex; align-items: center; gap: 9px; padding: 6px 8px; border-radius: 6px; cursor: pointer; font-size: 13px; }
.ac-item:hover, .ac-item.active { background: #2c313b; }
.ac-item > span { flex: 1 1 auto; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ac-icon { width: 22px; height: 22px; object-fit: contain; flex: 0 0 auto; }
/* '.addbar input{flex:1}'(0,1,1)에 안 지도록 특이도 높임 — 안 그러면 체크박스가 늘어남 */
.ac-list .ac-check { flex: 0 0 auto; width: 16px; height: 16px; margin: 0; cursor: pointer; accent-color: var(--accent); }
.ac-foot { position: sticky; bottom: -4px; margin: 4px -4px -4px; padding: 6px; background: var(--panel2); border-top: 1px solid var(--line); list-style: none; }
.ac-add-btn { width: 100%; padding: 8px; border: none; border-radius: 6px; background: var(--accent); color: #fff; font-size: 13px; font-weight: 600; cursor: pointer; }
.ac-add-btn:disabled { background: #3a3f4a; color: #888; cursor: default; }
.savebar { margin: 18px 0 0; }

table.grid { width: 100%; border-collapse: collapse; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.grid th, .grid td { padding: 10px 9px; text-align: left; border-bottom: 1px solid var(--line); }
.grid thead th { background: var(--panel2); color: var(--muted); font-size: 12px; font-weight: 600; }
.grid tbody tr:last-child td { border-bottom: 1px solid var(--line); }
.grid tbody tr:hover { background: #20242c; }
/* 가계부 아이템 표 지브라(짝수행 살짝 밝게) — hover는 뒤에 재선언해 우선 적용 */
#itemRows tr:nth-child(even) { background: rgba(255,255,255,.022); }
#itemRows tr:hover { background: #20242c; }
/* '.grid td'(0,1,1)보다 특이도를 높여야 우측정렬이 적용됨. 평가액·수량·기록숫자만 우측, 가격 3칸은 좌측 유지 */
.grid td.num, .grid td.c-value, .grid td.c-qty { text-align: right; }
.grid th.num { text-align: right; } /* 숫자 헤더(항목·총 평가액)도 내용에 맞춰 우측 */
.c-price, .c-avg, .c-buy, .c-npc { white-space: nowrap; }
.c-buy, .c-npc { font-variant-numeric: tabular-nums; }
.c-buy .buy { color: #8fb0d8; }
.c-npc .npc { color: #d3b46a; }
.npc-edit { background: none; border: none; color: var(--muted); cursor: pointer; font-size: 11px; padding: 0 2px; margin-left: 3px; opacity: 0; }
.c-npc:hover .npc-edit { opacity: .65; }
.npc-edit:hover { color: var(--accent); opacity: 1; }
.npc-edit.npc-add { opacity: .85; text-decoration: underline; margin: 0; } /* 빈 칸은 '입력' 항상 보이게 */
.npc-input { width: 100%; box-sizing: border-box; text-align: right; padding: 3px 5px; font-size: 12px; background: var(--panel); color: var(--text); border: 1px solid var(--accent); border-radius: 5px; }
/* 평가 기준: 사용중 가격칸에 점(●) 표시 + 클릭으로 아이템별 전환 */
tr:not(.manual-row) td.c-price, tr:not(.manual-row) td.c-buy, tr:not(.manual-row) td.c-avg, tr:not(.manual-row) td.c-npc { cursor: pointer; }
.last-refresh { color: var(--muted); font-size: 12px; white-space: nowrap; }
td.basis-active::before { content: "●"; color: var(--accent); font-size: 8px; margin-right: 7px; vertical-align: middle; }
.avg { color: var(--muted); font-variant-numeric: tabular-nums; }
.c-price, .c-value { font-variant-numeric: tabular-nums; }
.c-name { width: 38%; }
.c-qty { width: 110px; }
.c-time { color: var(--muted); font-size: 12px; white-space: nowrap; }
.c-del { width: 36px; text-align: center; }
.c-grip { width: 26px; text-align: center; padding-left: 8px; padding-right: 0; }
.grip { cursor: grab; color: var(--muted); font-size: 15px; line-height: 1; user-select: none; padding: 3px 4px; border-radius: 4px; }
.grip:hover { color: var(--text); background: #2c313b; }
.grip:active { cursor: grabbing; }
tr.dragging { opacity: .4; }
tr.drop-before td { box-shadow: inset 0 2px 0 0 var(--accent); }
tr.drop-after td { box-shadow: inset 0 -2px 0 0 var(--accent); }

.c-qty input { width: 92px; text-align: right; padding: 5px 8px; }
.c-value { font-weight: 600; white-space: normal; overflow-wrap: anywhere; }
/* 가계부 표: 칼럼 폭 고정(값이 바뀌어도 너비 불변, 길면 줄바꿈) */
#tab-ledger table.grid { table-layout: fixed; }
#tab-ledger thead th { white-space: normal; line-height: 1.25; } /* 헤더는 칸 넘치면 줄바꿈(겹침 방지), 데이터 셀은 nowrap 유지 */
#tab-ledger thead .c-grip { width: 4%; }
#tab-ledger thead .c-name { width: 24%; }
#tab-ledger thead .c-price { width: 10%; }
#tab-ledger thead .c-buy { width: 10%; }
#tab-ledger thead .c-avg { width: 10%; }
#tab-ledger thead .c-npc { width: 10%; }
#tab-ledger thead .c-qty { width: 11%; }
#tab-ledger thead .c-value { width: 17%; text-align: right; }
#tab-ledger thead .c-del { width: 4%; }
/* 가격 4칸은 비교하기 쉽게 좌우 여백 줄여 붙임 */
#tab-ledger .c-price, #tab-ledger .c-buy, #tab-ledger .c-avg, #tab-ledger .c-npc { padding-left: 5px; padding-right: 5px; }

tfoot td { background: var(--panel2); font-weight: 700; }
.grid td.total-label { text-align: right; color: var(--muted); }
.total-value { font-size: 16px; color: var(--accent); }

.muted { color: var(--muted); }
.bad { color: var(--bad); cursor: help; }
.good { color: var(--good); }

button.del { background: none; border: none; color: var(--muted); font-size: 16px; padding: 2px 6px; line-height: 1; }
button.del:hover { color: var(--bad); background: none; }
button.etc-undo { background: none; border: none; color: var(--muted); font-size: 15px; padding: 2px 6px; line-height: 1; cursor: pointer; }
button.etc-undo:hover:not(:disabled) { color: var(--accent); }
button.etc-undo:disabled { opacity: .35; cursor: default; }
/* 숫자 입력칸 위아래 화살표(스피너) 제거 */
input[type="number"] { -moz-appearance: textfield; }
input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
button.link { background: none; border: none; color: var(--link); padding: 2px 6px; }
button.link:hover { background: none; text-decoration: underline; }
button.del-rec { background: none; border: none; color: var(--muted); padding: 2px 6px; }
button.del-rec:hover { color: var(--bad); background: none; }

.empty { color: var(--muted); text-align: center; padding: 26px; font-size: 13px; }

/* 기록 표: 상세 펼침 시 칼럼 너비 흔들림 방지(고정 레이아웃 + 너비) */
#tab-history table.grid { table-layout: fixed; }
#tab-history thead th:nth-child(1) { width: 13%; }
#tab-history thead th:nth-child(2) { width: 29%; }
#tab-history thead th:nth-child(3) { width: 17%; }
#tab-history thead th:nth-child(4) { width: 9%; }
#tab-history thead th:nth-child(5) { width: 17%; }
#tab-history thead th:nth-child(6) { width: 15%; }
#recordRows tr:not(.rec-detail) td:nth-child(2) { overflow-wrap: anywhere; } /* 긴 제목은 줄바꿈(잘림 없이) */
#recordRows tr:not(.rec-detail) td:last-child { white-space: nowrap; } /* 상세·삭제 한 줄 유지 */
.rec-detail > td { padding: 0 12px 12px; background: #181b21; }
table.subgrid { width: 100%; border-collapse: collapse; margin: 4px 0; table-layout: fixed; } /* 칸 너비 고정(수량 수정 시 흔들림 방지) */
.subgrid th, .subgrid td { padding: 6px 10px; border-bottom: 1px solid var(--line); font-size: 12px; text-align: right; overflow: hidden; text-overflow: ellipsis; }
.subgrid th { color: var(--muted); font-weight: 600; }
.subgrid th:first-child, .subgrid td:first-child { text-align: left; }
.subgrid th:nth-child(1) { width: 42%; }
.subgrid th:nth-child(2) { width: 20%; }
.subgrid th:nth-child(3) { width: 16%; }
.subgrid th:nth-child(4) { width: 22%; }
.subgrid tbody tr:nth-child(even) td { background: rgba(255,255,255,.03); } /* 상세 표 지브라 */
/* 기록 표 지브라 — 상세행이 끼어 nth-child 불가라 짝수 레코드에 .rec-alt 부여. hover는 뒤에 재선언해 우선 */
#recordRows tr.rec-alt { background: rgba(255,255,255,.022); }
#recordRows tr:hover { background: #20242c; }
.rec-edit-bar { display: flex; justify-content: flex-end; gap: 4px; margin-top: 2px; }
.rec-qty-input { width: 100%; max-width: 72px; box-sizing: border-box; text-align: right; padding: 3px 6px; font-size: 12px; background: var(--panel); color: var(--text); border: 1px solid var(--accent); border-radius: 5px; }

/* 기록 표 - 리스트 칩 */
.rec-list { white-space: nowrap; }
.rec-list .chip { display: inline-block; padding: 1px 9px; border: 1px solid var(--line); border-radius: 11px; background: var(--panel2); font-size: 12px; color: var(--text); white-space: nowrap; }

/* 자산 추이 그래프 */
.chart-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 12px; margin: 0 0 10px; flex-wrap: wrap; }
.chart-title { font-size: 15px; margin: 0; font-weight: 700; }
.chart-summary { margin: 4px 0 0; color: var(--muted); font-size: 13px; }
.chart-summary b { color: var(--text); }
.chart-host { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 10px; margin-bottom: 18px; }
.chart { width: 100%; height: auto; display: block; }
.ch-grid { stroke: var(--line); stroke-width: 1; }
.ch-ylabel { fill: var(--muted); font-size: 10px; text-anchor: end; font-variant-numeric: tabular-nums; }
.ch-xlabel { fill: var(--muted); font-size: 10px; }
.ch-area { fill: var(--accent); opacity: 0.10; }
.ch-line { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.ch-dot { fill: var(--accent); }
.ch-dot.last { fill: #fff; stroke: var(--accent); stroke-width: 2; }

/* 리스트 관리 바 */
.listbar { display: flex; gap: 8px; align-items: center; margin: 0 0 16px; flex-wrap: wrap; }
.listbar .srv select { min-width: 220px; }
.lb-spacer { flex: 1 1 auto; }

/* 가격 변동 플래시 (갱신 시) */
.c-price.lvl-high { color: var(--good); }  /* 현재가 > 기록 평균 (평소보다 비쌈) */
.c-price.lvl-low { color: var(--bad); }    /* 현재가 < 기록 평균 (평소보다 쌈) */
.c-price.flash-up { animation: flashUp 2s ease-out; }
.c-price.flash-down { animation: flashDown 2s ease-out; }
/* 100% 미지정 → 애니메이션이 현재 글자색(평균 대비 색)으로 자연스럽게 복귀 */
@keyframes flashUp { 0%, 30% { color: var(--good); } }
@keyframes flashDown { 0%, 30% { color: var(--bad); } }
.c-price { position: relative; }
.delta { position: absolute; right: 12px; bottom: 2px; font-size: 10px; font-weight: 700; white-space: nowrap; line-height: 1; pointer-events: none; animation: deltaFade 2s ease-out forwards; }
.delta.up { color: var(--good); }
.delta.down { color: var(--bad); }
@keyframes deltaFade { 0%, 55% { opacity: 1; } 100% { opacity: 0; } }

/* 아이템 아이콘 */
.name-cell { display: flex; align-items: center; gap: 9px; min-width: 0; }
.name-cell > span:last-child { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item-icon { width: 26px; height: 26px; object-fit: contain; flex: 0 0 auto; }
.item-icon.placeholder { background: #181b21; border: 1px dashed var(--line); border-radius: 5px; }
.item-icon-sm { width: 18px; height: 18px; object-fit: contain; vertical-align: middle; margin-right: 6px; border-radius: 3px; }

/* 기타(잡템) 수동 입력 항목 */
.manual-row { background: #1b1e26; }
.manual-row td { border-left: 0; }
.manual-row .c-name { border-left: 3px solid var(--accent-d); }
.c-name input.manual-name { flex: 1; min-width: 0; width: auto; padding: 5px 8px; }
.c-value input.manual-value { width: 92px; text-align: right; padding: 5px 8px; }

/* 자리표시 슬롯 (placeholder — 라이브 시 광고 스니펫으로 교체. 이름에 'ad' 미사용 = 차단기에 안 숨겨짐) */
.box-slot { display: flex; align-items: center; justify-content: center; min-height: 90px; margin: 16px 0; overflow: hidden; }
.box-slot .slot-label { color: var(--muted); font-size: 12px; letter-spacing: .3px; }
#slotTop { margin-top: 0; }

/* 사이드(스카이스크래퍼) — 넓은 화면에서 스크롤해도 고정 노출 */
.slot-side { display: none; }
#slotBottom { display: flex; }              /* 기본 노출: 사이드(≥1360)가 없는 좁은 화면 폴백 */
@media (max-width: 767px) {                 /* 모바일: 상단(728)이 안 들어가므로 숨김 */
  #slotTop { display: none; }
}
@media (min-width: 1360px) {                /* 넓은 PC: 사이드(160) 스크롤 고정 + 하단 숨김 */
  .slot-side { display: flex; position: fixed; top: 50%; transform: translateY(-50%); left: calc(50% + 510px); width: 160px; height: 600px; margin: 0; z-index: 20; }
  #slotBottom { display: none; }
}

/* 푸터 (백업 도구 + 안내) */
.appfoot { max-width: 1000px; margin: 28px auto 40px; padding: 16px 20px; border-top: 1px solid var(--line); color: var(--muted); }
.foot-tools { display: flex; gap: 8px; margin-bottom: 10px; flex-wrap: wrap; }
.foot-note { font-size: 12px; margin: 4px 0; line-height: 1.6; }
.foot-note b { color: var(--text); }
.foot-src { font-size: 11px; margin: 6px 0 0; opacity: .7; }
.foot-src a { color: var(--accent); text-decoration: none; }
.foot-src a:hover { text-decoration: underline; }
.foot-copy { font-size: 11px; margin: 4px 0 0; opacity: .55; }

/* 토스트 알림 (기록 완료 등) */
.toast { position: fixed; left: 50%; bottom: 36px; transform: translate(-50%, 16px); z-index: 200; max-width: 90vw; padding: 13px 22px; background: var(--good); color: #08231a; font-size: 14px; font-weight: 700; border-radius: 10px; box-shadow: 0 8px 28px rgba(0,0,0,.45); opacity: 0; pointer-events: none; transition: opacity .25s ease, transform .25s ease; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* 광고 차단기 감지 팝업 */
.abk-modal { max-width: 380px; text-align: center; padding: 8px 8px 18px; }
.abk-icon { font-size: 42px; margin: 16px 0 4px; }
.abk-title { margin: 6px 16px 10px; font-size: 17px; font-weight: 700; }
.abk-msg { margin: 0 20px 18px; color: var(--muted); font-size: 13px; line-height: 1.7; }
.abk-msg b { color: var(--text); }
.abk-allow { display: block; width: calc(100% - 40px); margin: 0 20px 8px; background: var(--good); color: #08231a; border: none; border-radius: 9px; font-weight: 700; padding: 12px; font-size: 15px; cursor: pointer; text-align: center; text-decoration: none; box-sizing: border-box; }
.abk-allow[hidden] { display: none; }
.abk-allow:hover { background: #6fe0a9; }
.abk-dismiss { background: none; border: none; color: var(--muted); font-size: 12px; padding: 4px; cursor: pointer; }
.abk-dismiss:hover { color: var(--text); background: none; text-decoration: underline; }

/* 모달(팝업) */
.modal-overlay { position: fixed; inset: 0; z-index: 100; background: rgba(0,0,0,.55); display: flex; align-items: center; justify-content: center; padding: 20px; }
.modal-overlay[hidden] { display: none; }
.modal { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; width: 100%; max-width: 440px; max-height: 80vh; display: flex; flex-direction: column; box-shadow: 0 20px 60px rgba(0,0,0,.5); }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.modal-head h3 { margin: 0; font-size: 16px; font-weight: 700; }
.modal-x { background: none; border: none; color: var(--muted); font-size: 22px; line-height: 1; padding: 0 6px; }
.modal-x:hover { color: var(--text); background: none; }
.modal-body { padding: 8px 16px; overflow-y: auto; }
.modal-foot { display: flex; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--line); }
.modal-foot input { flex: 1; }
.ml-row { display: flex; align-items: center; gap: 8px; padding: 9px 4px; border-bottom: 1px solid var(--line); }
.ml-row:last-child { border-bottom: none; }
.ml-label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.ml-row.active .ml-label { color: var(--accent); font-weight: 700; }
.ml-row .ml-name { flex: 1; border-color: var(--accent); }
.ml-count { color: var(--muted); font-size: 12px; min-width: 34px; text-align: right; }
.ml-row button { padding: 5px 11px; font-size: 12px; }
.ml-edit:hover { color: var(--accent); border-color: var(--accent); }
.ml-del { color: var(--muted); }
.ml-del:hover:not(:disabled) { color: var(--bad); border-color: var(--bad); }
