:root {
  --bg: #0f0f10;
  --surface: #161618;
  --surface2: #1e1e21;
  --border: #2a2a2e;
  --border2: #3a3a3f;
  --accent: #e8c547;
  --accent2: #c4a030;
  --text: #e8e8ea;
  --text2: #9898a5;
  --text3: #5a5a6a;
  --red: #e85555;
  --green: #55c47a;
  --blue: #5588e8;
  --mono: "IBM Plex Mono", monospace;
  --sans: "IBM Plex Sans JP", sans-serif;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 14px;
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* HEADER */
header {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
}

.logo {
  font-family: var(--mono);
  font-size: 13px;
  font-weight: 600;
  color: var(--accent);
  letter-spacing: 0.05em;
  white-space: nowrap;
}

.logo span {
  color: var(--text3);
  font-weight: 400;
}

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

.stat-badge {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text3);
  padding: 3px 8px;
  border: 1px solid var(--border);
  border-radius: 4px;
  transition: all 0.3s;
}

.stat-badge b {
  color: var(--accent);
  font-weight: 600;
}

.autosave-saving {
  border-color: var(--accent) !important;
  color: var(--accent) !important;
}
.autosave-ok {
  border-color: var(--green) !important;
  color: var(--green) !important;
}
.autosave-unsaved {
  border-color: var(--red) !important;
  color: var(--red) !important;
}

/* BUTTONS */
button {
  font-family: var(--sans);
  font-size: 13px;
  cursor: pointer;
  border: none;
  border-radius: 5px;
  padding: 7px 14px;
  transition: all 0.15s;
  font-weight: 500;
}

.btn-primary {
  background: var(--accent);
  color: #0f0f10;
}
.btn-primary:hover {
  background: var(--accent2);
}

.btn-ghost {
  background: transparent;
  color: var(--text2);
  border: 1px solid var(--border);
}
.btn-ghost:hover {
  border-color: var(--border2);
  color: var(--text);
  background: var(--surface2);
}

.btn-danger {
  background: transparent;
  color: var(--red);
  border: 1px solid transparent;
  padding: 4px 8px;
  font-size: 12px;
}
.btn-danger:hover {
  background: rgba(232, 85, 85, 0.1);
  border-color: var(--red);
}

.btn-icon {
  background: transparent;
  color: var(--text3);
  border: 1px solid var(--border);
  padding: 5px 9px;
  font-size: 14px;
  line-height: 1;
}
.btn-icon:hover {
  color: var(--text);
  border-color: var(--border2);
  background: var(--surface2);
}

.btn-sm {
  padding: 4px 10px;
  font-size: 12px;
}

.btn-green {
  background: transparent;
  color: var(--green);
  border: 1px solid var(--green);
}
.btn-green:hover {
  background: rgba(85, 196, 122, 0.1);
}

/* MAIN LAYOUT */
.main {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* LEFT PANEL — Doc list */
.panel-docs {
  width: 260px;
  flex-shrink: 0;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  background: var(--surface);
}

.panel-header {
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text3);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  display: flex;
  align-items: center;
  gap: 8px;
}

.panel-header button {
  margin-left: auto;
}

.doc-list {
  flex: 1;
  overflow-y: auto;
  padding: 6px;
}

.doc-item {
  padding: 9px 10px;
  border-radius: 5px;
  cursor: pointer;
  border: 1px solid transparent;
  margin-bottom: 2px;
  transition: all 0.12s;
  position: relative;
}

.doc-item:hover {
  background: var(--surface2);
  border-color: var(--border);
}
.doc-item.active {
  background: rgba(232, 197, 71, 0.07);
  border-color: rgba(232, 197, 71, 0.3);
}

.doc-item-id {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--accent);
  font-weight: 600;
  margin-bottom: 2px;
}

.doc-item-meta {
  font-size: 11px;
  color: var(--text3);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.doc-item-count {
  font-family: var(--mono);
  font-size: 10px;
  color: var(--text3);
  margin-top: 3px;
}

.doc-item-actions {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  display: none;
  gap: 3px;
}

.doc-item:hover .doc-item-actions {
  display: flex;
}

/* MIDDLE PANEL — Articles */
.panel-articles {
  width: 320px;
  flex-shrink: 0;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}

.article-list {
  flex: 1;
  overflow-y: auto;
  padding: 6px;
}

.article-item {
  padding: 8px 10px;
  border-radius: 5px;
  cursor: pointer;
  border: 1px solid transparent;
  margin-bottom: 2px;
  transition: all 0.12s;
  position: relative;
}

.article-item:hover {
  background: var(--surface2);
  border-color: var(--border);
}
.article-item.active {
  background: rgba(85, 136, 232, 0.08);
  border-color: rgba(85, 136, 232, 0.3);
}

.article-item-id {
  font-family: var(--mono);
  font-size: 10px;
  color: var(--blue);
  margin-bottom: 3px;
}

.article-item-preview {
  font-size: 12px;
  color: var(--text2);
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.article-item-actions {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  display: none;
  gap: 3px;
}

.article-item:hover .article-item-actions {
  display: flex;
}

/* RIGHT PANEL — Editor */
.panel-editor {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.editor-toolbar {
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface);
  flex-shrink: 0;
}

.editor-label {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text3);
  letter-spacing: 0.06em;
}

.editor-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  padding: 16px;
  gap: 12px;
}

/* FORM FIELDS */
label {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text3);
  letter-spacing: 0.06em;
  display: block;
  margin-bottom: 5px;
}

input[type="text"],
textarea {
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 5px;
  color: var(--text);
  font-family: var(--mono);
  font-size: 13px;
  padding: 8px 10px;
  outline: none;
  transition: border-color 0.15s;
  resize: none;
}

input[type="text"]:focus,
textarea:focus {
  border-color: var(--blue);
}

textarea.article-textarea {
  flex: 1;
  min-height: 0;
}

.field-group {
  display: flex;
  flex-direction: column;
}

.field-group-row {
  display: flex;
  gap: 10px;
}

.field-group-row .field-group {
  flex: 1;
}

/* EMPTY STATE */
.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text3);
  gap: 8px;
}

.empty-icon {
  font-size: 32px;
  opacity: 0.3;
}

.empty-text {
  font-family: var(--mono);
  font-size: 12px;
  letter-spacing: 0.05em;
}

/* JSON PREVIEW */
.json-panel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: 600px;
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  z-index: 100;
}

.json-panel.open {
  transform: translateX(0);
}

.json-panel-header {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
}

.json-panel-title {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--text2);
  letter-spacing: 0.06em;
}

pre {
  flex: 1;
  overflow: auto;
  padding: 16px;
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.6;
  color: var(--text2);
  white-space: pre-wrap;
  word-break: break-all;
}

/* Syntax highlight */
.json-key {
  color: #5588e8;
}
.json-str {
  color: #e8c547;
}
.json-num {
  color: #55c47a;
}
.json-bool {
  color: #e85555;
}

/* SCROLLBAR */
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}
::-webkit-scrollbar-track {
  background: transparent;
}
::-webkit-scrollbar-thumb {
  background: var(--border2);
  border-radius: 3px;
}
::-webkit-scrollbar-thumb:hover {
  background: #4a4a55;
}

/* MODAL */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(3px);
}

.modal {
  background: var(--surface);
  border: 1px solid var(--border2);
  border-radius: 8px;
  padding: 24px;
  width: 1040px;
  max-width: 95vw;
  max-height: 90vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.modal-title {
  font-family: var(--mono);
  font-size: 13px;
  color: var(--accent);
  font-weight: 600;
}

.modal-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  margin-top: 4px;
}

/* TOAST */
.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--surface2);
  border: 1px solid var(--border2);
  border-radius: 6px;
  padding: 10px 18px;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--text);
  opacity: 0;
  transition: all 0.2s;
  z-index: 300;
  pointer-events: none;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.toast.success {
  border-color: var(--green);
  color: var(--green);
}
.toast.error {
  border-color: var(--red);
  color: var(--red);
}

.no-select {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  gap: 10px;
  color: var(--text3);
}

.divider {
  height: 1px;
  background: var(--border);
  margin: 4px 0;
}

.panel-footer {
  padding: 8px 10px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 6px;
}

/* ── HTML→MD DRAWER ── */
.md-drawer {
  position: fixed;
  top: 0;
  bottom: 0;
  right: 0;
  width: 680px;
  background: var(--surface);
  border-left: 2px solid var(--accent);
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
  z-index: 150;
  box-shadow: -8px 0 40px rgba(0, 0, 0, 0.5);
}
.md-drawer.open {
  transform: translateX(0);
}

.md-drawer-header {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
  background: var(--surface2);
}

.md-drawer-title {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--accent);
  font-weight: 600;
  letter-spacing: 0.06em;
}

.md-drawer-doc {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text3);
  margin-left: 4px;
}

.md-drawer-body {
  display: flex;
  flex-direction: column;
  flex: 1;
  overflow: hidden;
}

.md-input-zone {
  display: flex;
  flex-direction: column;
  padding: 12px 16px;
  gap: 8px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.md-input-zone textarea {
  height: 100px;
  font-size: 12px;
  resize: none;
}

.md-input-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

.md-viewer-zone {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
}

.md-viewer-label {
  font-family: var(--mono);
  font-size: 10px;
  color: var(--text3);
  letter-spacing: 0.08em;
  padding: 8px 16px 4px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 10px;
}

.md-selection-hint {
  font-family: var(--mono);
  font-size: 10px;
  color: var(--text3);
  padding: 0 16px 6px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 8px;
}

.md-selection-hint .hint-active {
  color: var(--green);
  font-weight: 600;
}

#mdContent {
  flex: 1;
  overflow-y: auto;
  padding: 8px 16px 16px;
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.7;
  color: var(--text2);
  white-space: pre-wrap;
  word-break: break-word;
  cursor: text;
  user-select: text;
  outline: none;
}

#mdContent:focus {
  outline: none;
}

/* hint when anchor is set */
.anchor-set-hint {
  font-family: var(--mono);
  font-size: 10px;
  color: var(--blue);
  padding: 0 16px 4px;
  flex-shrink: 0;
}

/* Floating "Add as Article" button that appears on selection */
.add-selection-btn {
  position: fixed;
  background: var(--green);
  color: #0f0f10;
  border: none;
  border-radius: 6px;
  padding: 7px 14px;
  font-family: var(--mono);
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
  display: none;
  z-index: 9999;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.5);
  transition:
    transform 0.1s,
    background 0.1s;
  white-space: nowrap;
}
.add-selection-btn:hover {
  background: #44b36a;
  transform: scale(1.03);
}

/* Floating selection popup menu */
.selection-popup {
  position: fixed;
  display: none;
  flex-direction: column;
  z-index: 9999;
  background: var(--surface2);
  border: 1px solid var(--border2);
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.6);
  min-width: 190px;
}
.selection-popup.show {
  display: flex;
}

.sel-popup-btn {
  padding: 9px 16px;
  font-family: var(--mono);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  border: none;
  background: transparent;
  text-align: left;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: background 0.12s;
  border-radius: 0;
  white-space: nowrap;
}
.sel-popup-btn:hover {
  background: var(--surface);
}
.sel-popup-btn.btn-article {
  color: var(--green);
}
.sel-popup-btn.btn-basis {
  color: var(--accent);
}
.sel-popup-btn.btn-basis.done {
  color: var(--text3);
  text-decoration: line-through;
  cursor: not-allowed;
  opacity: 0.5;
}
.sel-popup-divider {
  height: 1px;
  background: var(--border);
  margin: 0;
}

.md-drawer-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  z-index: 149;
  display: none;
}
.md-drawer-overlay.show {
  display: block;
}
