:root {
  --bg: #f4f6fa;
  --card: #ffffff;
  --text: #1f2937;
  --muted: #6b7280;
  --line: #e5e7eb;
  --hover: #f1f5ff;
  --primary: #2563eb;
  --primary-text: #ffffff;
  --danger: #dc2626;
  --sel: #dbe7ff;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --card: #181b22;
    --text: #e5e7eb;
    --muted: #9ca3af;
    --line: #2a2f3a;
    --hover: #1f2531;
    --primary: #3b82f6;
    --danger: #ef4444;
    --sel: #1e2c4a;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", "Malgun Gothic", sans-serif;
  font-size: 15px;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 1.2em; margin: 6px 0 0; font-size: 14px; }

.btn {
  display: inline-flex; align-items: center; gap: 4px;
  border: 1px solid var(--line); background: var(--card); color: var(--text);
  padding: 8px 14px; border-radius: 8px; cursor: pointer; font: inherit; text-decoration: none;
  white-space: nowrap;
}
.btn:hover { background: var(--hover); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn.ghost { border-color: transparent; background: transparent; }
input, select {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; width: 100%;
}
.card { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 24px; }

/* 로그인 */
.auth { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.auth-card { width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: 10px; }
.auth-card h1 { margin: 0; font-size: 24px; }
.auth-card p { margin: 0 0 6px; }

/* 상단 */
.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px; background: var(--card); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
.brand { font-weight: 700; font-size: 17px; }
.disk { flex: 1; font-size: 13px; color: var(--muted); text-align: right; }
.menu-wrap { position: relative; }
.menu {
  position: absolute; right: 0; top: 110%; background: var(--card); border: 1px solid var(--line);
  border-radius: 10px; overflow: hidden; min-width: 150px; box-shadow: 0 8px 24px rgba(0,0,0,.15); z-index: 10;
}
.menu button { display: block; width: 100%; text-align: left; padding: 10px 14px; border: 0; background: none; color: var(--text); font: inherit; cursor: pointer; }
.menu button:hover { background: var(--hover); }

.toolbar {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 12px 16px;
}
.crumbs { flex: 1; min-width: 0; display: flex; flex-wrap: wrap; align-items: center; gap: 2px; font-size: 15px; }
.crumbs a { color: var(--primary); text-decoration: none; padding: 4px 6px; border-radius: 6px; cursor: pointer; }
.crumbs a:hover { background: var(--hover); }
.crumbs span.sep { color: var(--muted); }
.crumbs .cur { padding: 4px 6px; font-weight: 600; }
.actions { display: flex; gap: 8px; }

.selbar {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  margin: 0 16px 8px; padding: 8px 12px; background: var(--sel); border-radius: 10px;
}
.selbar span { flex: 1; font-weight: 600; }

main { position: relative; padding: 0 16px 100px; min-height: 60vh; }
.list { list-style: none; margin: 0; padding: 0; background: var(--card); border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
.list:empty { display: none; }
.item {
  display: grid; grid-template-columns: 28px 28px 1fr auto auto 36px; align-items: center; gap: 8px;
  padding: 8px 12px; border-bottom: 1px solid var(--line); cursor: pointer; user-select: none;
}
.item:last-child { border-bottom: 0; }
.item:hover { background: var(--hover); }
.item.selected { background: var(--sel); }
.item input[type=checkbox] { width: 18px; height: 18px; margin: 0; }
.item .icon { font-size: 20px; text-align: center; }
.item .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .size, .item .date { color: var(--muted); font-size: 13px; white-space: nowrap; }
.item .more { border: 0; background: none; color: var(--muted); font-size: 20px; cursor: pointer; border-radius: 6px; padding: 2px 0; }
.item .more:hover { background: var(--line); }
@media (max-width: 640px) {
  .item { grid-template-columns: 24px 26px 1fr auto 32px; }
  .item .date { display: none; }
  .disk { display: none; }
  .brand { flex: 1; }
  .toolbar .actions { width: 100%; }
  .toolbar .actions .btn { flex: 1; justify-content: center; }
}
.empty { text-align: center; color: var(--muted); padding: 60px 0; }
.drop-hint {
  position: absolute; inset: 0 16px; border: 3px dashed var(--primary); border-radius: 14px;
  background: color-mix(in srgb, var(--primary) 10%, transparent);
  display: grid; place-items: center; font-size: 20px; font-weight: 600; color: var(--primary); pointer-events: none;
}

/* 업로드 진행 */
.uploads {
  position: fixed; right: 16px; bottom: 16px; width: min(360px, calc(100% - 32px));
  background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 10px 14px;
  box-shadow: 0 8px 24px rgba(0,0,0,.2); max-height: 40vh; overflow: auto; z-index: 20;
}
.up-row { font-size: 13px; padding: 6px 0; }
.up-row .up-name { display: flex; justify-content: space-between; gap: 8px; }
.up-row .up-name span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar { height: 5px; background: var(--line); border-radius: 3px; margin-top: 4px; overflow: hidden; }
.bar div { height: 100%; width: 0; background: var(--primary); transition: width .2s; }
.up-row.err .bar div { background: var(--danger); }

/* 모달 */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.6); z-index: 30; display: flex; flex-direction: column; }
.modal.center { align-items: center; justify-content: center; padding: 16px; }
.dialog-card { width: 100%; max-width: 380px; }
.dialog-card.wide { max-width: 560px; }
.dialog-card h3 { margin: 0 0 14px; }
.dialog-card #dialog-body { display: flex; flex-direction: column; gap: 8px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.preview-head {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 10px 16px; background: var(--card); color: var(--text);
}
.preview-head span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.preview-head div { display: flex; gap: 8px; }
.preview-body { flex: 1; min-height: 0; display: grid; place-items: center; padding: 12px; }
.preview-body img, .preview-body video { max-width: 100%; max-height: 100%; object-fit: contain; }
.preview-body iframe { width: 100%; height: 100%; border: 0; background: #fff; border-radius: 8px; }
.preview-body pre {
  width: 100%; height: 100%; margin: 0; overflow: auto; background: var(--card); color: var(--text);
  padding: 16px; border-radius: 8px; white-space: pre-wrap; word-break: break-all; font-size: 13px;
}

.users { width: 100%; border-collapse: collapse; margin-bottom: 14px; }
.users th, .users td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--line); }
.users td:last-child { text-align: right; white-space: nowrap; }
.users .btn { padding: 4px 10px; font-size: 13px; }
.add-user { display: grid; grid-template-columns: 1fr 1fr auto auto; gap: 8px; }
@media (max-width: 640px) { .add-user { grid-template-columns: 1fr 1fr; } }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: #111827; color: #fff; padding: 10px 18px; border-radius: 999px; z-index: 40; font-size: 14px;
  max-width: calc(100% - 32px);
}

/* 업로드 방식 선택 시트 (모바일) */
.modal.sheet-wrap { justify-content: flex-end; }
.sheet {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px;
  background: var(--card); border-radius: 18px 18px 0 0;
  padding: 18px 16px calc(18px + env(safe-area-inset-bottom));
}
.sheet button {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  border: 1px solid var(--line); background: var(--bg); color: var(--text);
  border-radius: 14px; padding: 16px 6px; font: inherit; font-weight: 600; cursor: pointer;
}
.sheet button span { font-size: 30px; }
.sheet button small { color: var(--muted); font-weight: 400; font-size: 12px; }

/* 메모 편집기 */
#editor { background: var(--bg); }
.editor-head {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 10px 16px; background: var(--card); border-bottom: 1px solid var(--line);
}
.editor-head input { flex: 1; min-width: 160px; font-weight: 600; }
.editor-status { font-size: 13px; color: var(--muted); white-space: nowrap; }
.editor-status.dirty { color: var(--danger); }
.editor-btns { display: flex; gap: 8px; }
#editor-text {
  flex: 1; width: 100%; resize: none; border: 0; outline: none; margin: 0;
  padding: 16px; background: var(--card); color: var(--text);
  font: 15px/1.6 ui-monospace, "D2Coding", Consolas, "Malgun Gothic", monospace;
  tab-size: 4;
}
@media (max-width: 640px) {
  .editor-head input { flex-basis: 100%; }
  .editor-status { flex: 1; }
}

/* 외부(Funnel) 접속: 업로드·메모만 허용 */
.ro-badge { display: none; font-size: 12px; font-weight: 600; color: #b45309; background: #fef3c7; padding: 3px 10px; border-radius: 999px; white-space: nowrap; }
body.external .ro-badge { display: inline-block; }
body.external .tailnet-only { display: none !important; }

/* ===================== 메모 앱 ===================== */
.btn.sm { padding: 5px 10px; font-size: 13px; }
.btn.on { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.modal.notes { flex-direction: row; background: var(--bg); }
.notes-side {
  width: 320px; flex-shrink: 0; display: flex; flex-direction: column;
  background: var(--card); border-right: 1px solid var(--line); min-height: 0;
}
.notes-head { display: flex; align-items: center; gap: 8px; padding: 10px 12px; }
.notes-head strong { flex: 1; font-size: 17px; }
.notes-new-wrap { position: relative; }
.notes-tabs { display: flex; gap: 4px; padding: 0 12px 8px; }
.notes-tab {
  flex: 1; padding: 8px; border: 1px solid var(--line); background: var(--bg); color: var(--text);
  border-radius: 8px; font: inherit; cursor: pointer;
}
.notes-tab.active { background: var(--primary); border-color: var(--primary); color: var(--primary-text); font-weight: 600; }
#notes-search { margin: 0 12px 8px; width: auto; padding: 8px 10px; }
.notes-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.note-item { padding: 10px 14px; border-top: 1px solid var(--line); cursor: pointer; }
.note-item:hover { background: var(--hover); }
.note-item.active { background: var(--sel); }
.note-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.note-meta { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 2px; }
.notes-side .empty { padding: 30px 16px; font-size: 14px; }

.notes-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.note-empty { margin: auto; color: var(--muted); }
.note-pane { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.note-head {
  display: flex; align-items: center; gap: 8px; padding: 8px 12px;
  background: var(--card); border-bottom: 1px solid var(--line);
}
#note-back { display: none; }
.note-scope { font-size: 12px; padding: 2px 8px; border-radius: 999px; background: var(--sel); white-space: nowrap; }
#note-title { flex: 1; min-width: 80px; font-weight: 600; font-size: 16px; border-color: transparent; background: transparent; }
#note-title:focus { border-color: var(--line); background: var(--bg); }
.note-status { font-size: 12px; color: var(--muted); white-space: nowrap; }
.note-status.dirty { color: #d97706; }
.note-status.saving { color: var(--primary); }
.note-body { flex: 1; min-height: 0; display: flex; flex-direction: column; }
#note-text {
  flex: 1; width: 100%; resize: none; border: 0; outline: none; margin: 0; padding: 16px;
  background: var(--card); color: var(--text); font: 15px/1.7 system-ui, "Malgun Gothic", sans-serif;
}

/* ----- 엑셀 모드 ----- */
.sheet-root { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.sheet-toolbar {
  display: flex; gap: 6px; padding: 6px 10px; overflow-x: auto; background: var(--card);
  border-bottom: 1px solid var(--line); flex-shrink: 0;
}
.sheet-toolbar .sep { width: 1px; background: var(--line); margin: 2px 2px; flex-shrink: 0; }
.formula-bar { display: flex; align-items: center; gap: 6px; padding: 4px 10px; background: var(--card); border-bottom: 1px solid var(--line); }
.cell-name { min-width: 64px; font: 600 13px ui-monospace, Consolas, monospace; color: var(--muted); text-align: center; }
.formula-input { padding: 5px 8px; font: 14px ui-monospace, Consolas, "Malgun Gothic", monospace; }
.grid-wrap { flex: 1; min-height: 0; overflow: auto; position: relative; background: var(--card); outline: none; -webkit-overflow-scrolling: touch; }
.grid { border-collapse: separate; border-spacing: 0; font-size: 13px; table-layout: fixed; }
.grid th, .grid td {
  border-right: 1px solid var(--line); border-bottom: 1px solid var(--line);
  height: 26px; padding: 0 6px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.grid td { min-width: 96px; max-width: 96px; width: 96px; cursor: cell; background: var(--card); user-select: none; }
.grid td.num { text-align: right; font-variant-numeric: tabular-nums; }
.grid td.err { color: var(--danger); }
.grid td.sel { background: var(--sel); }
.grid td.active { outline: 2px solid var(--primary); outline-offset: -2px; }
.grid th {
  position: sticky; top: 0; z-index: 2; background: var(--bg); color: var(--muted);
  font-weight: 500; font-size: 12px; text-align: center; cursor: pointer; user-select: none;
}
.grid tbody th { left: 0; z-index: 1; min-width: 44px; width: 44px; }
.grid th.corner { left: 0; z-index: 3; min-width: 44px; width: 44px; }
.grid th.hl { background: var(--sel); color: var(--text); font-weight: 700; }
.cell-editor {
  position: absolute; z-index: 4; margin: 0; padding: 3px 6px; resize: none; overflow: hidden;
  border: 2px solid var(--primary); background: var(--card); color: var(--text);
  font: 13px system-ui, "Malgun Gothic", sans-serif; line-height: 18px; white-space: pre; box-shadow: 0 2px 8px rgba(0,0,0,.2);
  border-radius: 0; width: auto;
}
.cell-editor.idle { opacity: 0; pointer-events: none; caret-color: transparent; }
.sheet-tabs {
  display: flex; gap: 4px; padding: 6px 10px; overflow-x: auto; background: var(--bg);
  border-top: 1px solid var(--line); flex-shrink: 0;
}
.sheet-tab {
  padding: 5px 14px; border: 1px solid var(--line); background: var(--card); color: var(--text);
  border-radius: 6px; font: inherit; font-size: 13px; cursor: pointer; white-space: nowrap;
}
.sheet-tab.active { border-color: var(--primary); color: var(--primary); font-weight: 700; }
.sheet-tab.add { font-weight: 700; }
.sheet-tab.tool { color: var(--muted); }
.sheet-tab.tool.danger { color: var(--danger); }

@media (max-width: 760px) {
  .notes-side { width: 100%; border-right: 0; }
  .notes-main { display: none; }
  .modal.notes.show-editor .notes-side { display: none; }
  .modal.notes.show-editor .notes-main { display: flex; }
  #note-back { display: inline-flex; }
  .note-head { flex-wrap: wrap; }
  #note-title { order: 5; flex-basis: 100%; }
  .grid td { min-width: 84px; max-width: 84px; width: 84px; }
}

/* ----- 일반 메모 (글 + 사진) ----- */
.rich-root { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.rich-toolbar {
  display: flex; align-items: center; gap: 10px; padding: 6px 12px;
  background: var(--card); border-bottom: 1px solid var(--line); flex-shrink: 0;
}
.rich-hint { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#note-rich {
  flex: 1; overflow-y: auto; padding: 16px; outline: none; background: var(--card); color: var(--text);
  font: 15px/1.7 system-ui, "Malgun Gothic", sans-serif; white-space: pre-wrap; word-break: break-word;
}
#note-rich:empty::before { content: attr(data-placeholder); color: var(--muted); }
#note-rich img {
  max-width: min(100%, 640px); max-height: 480px; height: auto; vertical-align: bottom;
  border-radius: 8px; border: 1px solid var(--line); margin: 4px 0; cursor: zoom-in;
}
#note-rich img.uploading { opacity: .45; cursor: progress; }
@media (max-width: 640px) { .rich-hint { display: none; } }
