/* ===== 테마 변수 ===== */
:root {
  --bg: #ffffff;
  --fg: #202124;
  --muted: #5f6368;
  --border: #e0e0e0;
  --card-bg: #ffffff;
  --hover: rgba(95, 99, 104, 0.1);
  --shadow: 0 1px 2px rgba(60, 64, 67, 0.3), 0 2px 6px rgba(60, 64, 67, 0.15);
  --accent: #f2a600;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #202124;
    --fg: #e8eaed;
    --muted: #9aa0a6;
    --border: #5f6368;
    --card-bg: #202124;
    --hover: rgba(232, 234, 237, 0.1);
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.5), 0 4px 8px rgba(0, 0, 0, 0.3);
  }
}

/* ===== 노트 색상 (라이트/다크) ===== */
.c-coral   { --note-bg: #faafa8; }
.c-peach   { --note-bg: #f39f76; }
.c-sand    { --note-bg: #fff8b8; }
.c-mint    { --note-bg: #e2f6d3; }
.c-sage    { --note-bg: #b4ddd3; }
.c-fog     { --note-bg: #d4e4ed; }
.c-storm   { --note-bg: #aeccdc; }
.c-dusk    { --note-bg: #d3bfdb; }
.c-blossom { --note-bg: #f6e2dd; }
.c-clay    { --note-bg: #e9e3d4; }
.c-chalk   { --note-bg: #efeff1; }

@media (prefers-color-scheme: dark) {
  .c-coral   { --note-bg: #77172e; }
  .c-peach   { --note-bg: #692b17; }
  .c-sand    { --note-bg: #7c4a03; }
  .c-mint    { --note-bg: #264d3b; }
  .c-sage    { --note-bg: #0c625d; }
  .c-fog     { --note-bg: #256377; }
  .c-storm   { --note-bg: #284255; }
  .c-dusk    { --note-bg: #472e5b; }
  .c-blossom { --note-bg: #6c394f; }
  .c-clay    { --note-bg: #4b443a; }
  .c-chalk   { --note-bg: #232427; }
}

/* ===== 기본 ===== */
* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
    "Noto Sans KR", "Malgun Gothic", sans-serif;
  font-size: 14px;
  line-height: 1.5;
}

button { font: inherit; color: inherit; background: none; border: none; cursor: pointer; }
input, textarea { font: inherit; color: inherit; background: transparent; border: none; outline: none; }
textarea { resize: none; }
[hidden] { display: none !important; }

/* ===== 상단바 ===== */
.topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 8px 16px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 20px;
  font-weight: 500;
  color: var(--muted);
}

.search { flex: 1; max-width: 640px; }

.search input {
  width: 100%;
  padding: 10px 16px;
  border-radius: 8px;
  background: var(--hover);
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-left: auto;
}

.sync-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border-radius: 16px;
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}
.sync-btn:hover { background: var(--hover); }

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--muted);
  flex-shrink: 0;
}
.dot.ok { background: #34a853; }
.dot.syncing { background: #4285f4; animation: pulse 1s infinite alternate; }
.dot.error { background: #ea4335; }

@keyframes pulse { from { opacity: 1; } to { opacity: 0.3; } }

/* ===== 탭 ===== */
.tabs {
  display: flex;
  gap: 8px;
  padding: 12px 16px 0;
  max-width: 1200px;
  margin: 0 auto;
}

.tab {
  padding: 6px 16px;
  border-radius: 16px;
  color: var(--muted);
  font-weight: 500;
}
.tab:hover { background: var(--hover); }
.tab.active { background: var(--note-bg, #feefc3); color: var(--fg); }
.tab.active { --note-bg: #feefc3; }
@media (prefers-color-scheme: dark) {
  .tab.active { --note-bg: #41331c; }
}

/* ===== 아이콘/텍스트 버튼 ===== */
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  color: var(--muted);
  opacity: 0.85;
}
.icon-btn:hover { background: var(--hover); opacity: 1; }
.icon-btn svg { width: 18px; height: 18px; }
.icon-btn.on { color: var(--fg); opacity: 1; }

.text-btn {
  padding: 8px 20px;
  border-radius: 4px;
  font-weight: 500;
}
.text-btn:hover { background: var(--hover); }
.text-btn.primary { color: var(--accent); }

/* ===== 작성창 (composer) ===== */
main { padding: 16px; max-width: 1200px; margin: 0 auto; }

.composer {
  max-width: 600px;
  margin: 16px auto 32px;
  padding: 12px 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--note-bg, var(--card-bg));
  box-shadow: var(--shadow);
}

.composer textarea { width: 100%; display: block; }

.note-title-input {
  width: 100%;
  font-size: 16px;
  font-weight: 500;
  margin-bottom: 8px;
}

.labels-input { width: 100%; font-size: 12px; color: var(--muted); margin-top: 8px; }

.note-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 8px;
}
.note-actions .left { display: flex; gap: 4px; }

/* ===== 노트 그리드 ===== */
.section-title {
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.8px;
  text-transform: uppercase;
  color: var(--muted);
  margin: 8px 4px;
}

.grid {
  columns: 240px;
  column-gap: 12px;
  margin-bottom: 24px;
}

.card {
  position: relative;
  break-inside: avoid;
  margin-bottom: 12px;
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--note-bg, var(--card-bg));
  cursor: default;
  transition: box-shadow 0.15s;
}
.card:hover { box-shadow: var(--shadow); }
.card.colored { border-color: transparent; }

.card h3 {
  font-size: 15px;
  font-weight: 500;
  margin-bottom: 6px;
  padding-right: 28px;
  word-break: break-word;
}

.card .content {
  word-break: break-word;
  display: -webkit-box;
  -webkit-line-clamp: 12;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.card .pin-btn {
  position: absolute;
  top: 6px;
  right: 6px;
  opacity: 0;
}
.card:hover .pin-btn, .card .pin-btn.on { opacity: 0.85; }

.card .labels { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 8px; }

.chip {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 10px;
  background: var(--hover);
  color: var(--muted);
}
.chip:hover { color: var(--fg); }

.card .card-actions {
  display: flex;
  gap: 2px;
  margin-top: 8px;
  opacity: 0;
  transition: opacity 0.15s;
}
.card:hover .card-actions { opacity: 1; }
.card .card-actions .icon-btn { width: 30px; height: 30px; }
.card .card-actions svg { width: 16px; height: 16px; }

.empty-state {
  text-align: center;
  color: var(--muted);
  padding: 64px 0;
  font-size: 16px;
}

/* ===== 모달 ===== */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 10vh 16px 16px;
  background: rgba(0, 0, 0, 0.55);
}

.modal-card {
  width: 100%;
  max-width: 600px;
  max-height: 80vh;
  overflow-y: auto;
  padding: 16px;
  border-radius: 8px;
  background: var(--note-bg, var(--card-bg));
  box-shadow: var(--shadow);
}

.editor-body {
  min-height: 120px;
  outline: none;
  padding: 4px 0;
  word-break: break-word;
}
.editor-body img,
.card .content img {
  max-width: 100%;
  display: block;
  border-radius: 6px;
  margin: 6px 0;
  background: var(--hover);
}
.editor-body img {
  cursor: default;
}
.editor-body img.missing,
.card .content img.missing {
  min-height: 48px;
}

.editor-meta {
  text-align: right;
  font-size: 11px;
  color: var(--muted);
  margin-top: 8px;
}

/* ===== 설정 모달 ===== */
.settings-card { background: var(--card-bg); }
.settings-card h2 { font-size: 18px; margin-bottom: 16px; }

.settings-card label {
  display: block;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 12px;
}

.settings-card label input {
  display: block;
  width: 100%;
  margin-top: 4px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 14px;
}
.settings-card label input:focus { border-color: var(--accent); }

.hint { font-size: 12px; color: var(--muted); margin: 8px 0 16px; }

.settings-actions { display: flex; align-items: center; gap: 8px; }
.settings-actions .spacer { flex: 1; }
.test-result { font-size: 12px; }
.test-result.ok { color: #34a853; }
.test-result.fail { color: #ea4335; }

/* ===== 색상 선택 팝오버 ===== */
.color-popover {
  position: fixed;
  z-index: 200;
  display: grid;
  grid-template-columns: repeat(6, 32px);
  gap: 6px;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--card-bg);
  box-shadow: var(--shadow);
}

.swatch {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 2px solid var(--border);
  background: var(--note-bg, var(--card-bg));
  display: flex;
  align-items: center;
  justify-content: center;
}
.swatch:hover { border-color: var(--fg); }
.swatch.selected { border-color: var(--accent); }
.swatch svg { width: 14px; height: 14px; color: var(--fg); }

/* ===== 이미지 ===== */
.note-images {
  margin: -14px -16px 8px;
  overflow: hidden;
  border-radius: 8px 8px 0 0;
}
.note-images img {
  width: 100%;
  display: block;
  min-height: 40px;
  background: var(--hover);
}

.images-strip {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 8px;
}
.images-strip:empty { display: none; }
.images-strip .thumb { position: relative; }
.images-strip img {
  width: 84px;
  height: 84px;
  object-fit: cover;
  border-radius: 6px;
  background: var(--hover);
}
.images-strip .rm {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--fg);
  color: var(--bg);
  font-size: 13px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}
img[data-path].failed { opacity: 0.35; }

/* ===== 마크다운 ===== */
.md p { margin: 0 0 6px; }
.md p:last-child { margin-bottom: 0; }
.md h1, .md h2, .md h3 { margin: 8px 0 4px; line-height: 1.3; }
.md h1:first-child, .md h2:first-child, .md h3:first-child { margin-top: 0; }
.md h1 { font-size: 1.3em; }
.md h2 { font-size: 1.15em; }
.md h3 { font-size: 1.05em; }
.md ul, .md ol { margin: 0 0 6px; padding-left: 20px; }
.md li.task { list-style: none; margin-left: -20px; }
.md li.task input[type="checkbox"] { vertical-align: middle; margin-right: 6px; accent-color: var(--accent); }
.md code {
  background: rgba(128, 128, 128, 0.25);
  padding: 1px 5px;
  border-radius: 4px;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 0.9em;
}
.md pre {
  background: rgba(128, 128, 128, 0.18);
  padding: 10px 12px;
  border-radius: 6px;
  overflow-x: auto;
  margin: 6px 0;
}
.md pre code { background: none; padding: 0; }
.md blockquote {
  border-left: 3px solid var(--muted);
  padding-left: 10px;
  margin: 6px 0;
  color: var(--muted);
}
.md hr { border: none; border-top: 1px solid var(--border); margin: 10px 0; }
.md a { color: inherit; text-decoration: underline; text-underline-offset: 2px; }

.preview { min-height: 60px; padding: 2px 0; margin-top: 4px; }

/* ===== 이미지 라이트박스 =====
 * fit: 화면에 맞춤 / zoom: 화면 폭 가득 (긴 이미지는 세로 스크롤) */
.lightbox {
  position: fixed;
  inset: 0;
  z-index: 400;
  background: rgba(0, 0, 0, 0.88);
  overflow: auto;
  cursor: zoom-out;
}
.lightbox img {
  border-radius: 4px;
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.6);
}
.lightbox.fit {
  display: flex;
  align-items: center;
  justify-content: center;
}
.lightbox.fit img {
  max-width: 96vw;
  max-height: 96vh;
  object-fit: contain;
  cursor: zoom-in;
}
.lightbox.zoom img {
  display: block;
  width: calc(100% - 32px);
  max-width: none;
  max-height: none;
  height: auto;
  margin: 16px auto;
  cursor: zoom-out;
}
.card .content img,
.note-images img,
.editor-body img {
  cursor: zoom-in;
}

/* ===== 토스트 ===== */
.toast {
  position: fixed;
  left: 50%;
  bottom: 32px;
  transform: translateX(-50%);
  z-index: 300;
  padding: 10px 20px;
  border-radius: 8px;
  background: var(--fg);
  color: var(--bg);
  font-size: 13px;
  box-shadow: var(--shadow);
  max-width: 90vw;
}

/* ===== 반응형 ===== */
@media (max-width: 600px) {
  .brand span { display: none; }
  .grid { columns: 2; }
  .sync-btn #sync-text { display: none; }
}
