body {
  min-height: 100vh;
}

#app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

#app.hidden {
  display: none;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  padding: 16px 24px;
  border-bottom: 1px solid var(--border);
  background:
    linear-gradient(180deg, rgba(15, 20, 29, 0.96) 0%, rgba(11, 16, 24, 0.96) 100%);
  backdrop-filter: blur(12px);
}

.app-brand {
  display: grid;
  gap: 4px;
}

.app-brand-title {
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
}

.app-brand-subtitle {
  font-size: 12px;
  color: var(--dim);
}

.primary-nav {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.primary-nav-item {
  appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--dim);
  padding: 10px 14px;
  border-radius: 999px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}

/* 데스크톱은 텍스트 메뉴, 모바일에서만 아이콘으로 바뀐다. */
.primary-nav-icon,
.btn-logout-icon {
  display: none;
  align-items: center;
  justify-content: center;
  flex: none;
}

.icon-btn {
  appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  flex: none;
  padding: 0;
  border: 1px solid transparent;
  border-radius: 12px;
  background: transparent;
  color: var(--dim);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.icon-btn:hover,
.icon-btn:focus-visible {
  color: var(--text);
  background: rgba(255, 255, 255, 0.06);
  outline: none;
}

/* 데스크톱에서는 모바일 전용 컨트롤을 완전히 숨긴다. */
.nav-toggle,
.nav-backdrop,
.sidebar-mobile-bar {
  display: none;
}

/* 토프바 타이틀 옆 아이콘 토글(Advanced JSON / 목록). */
.topbar-inline-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: none;
}

.topbar-icon-btn {
  width: 34px;
  height: 34px;
  border-color: var(--border);
  border-radius: 9px;
  background: var(--surface2);
}

.topbar-icon-btn:hover:not(:disabled),
.topbar-icon-btn:focus-visible {
  color: var(--accent);
  border-color: var(--accent);
  background: var(--surface2);
}

.topbar-icon-btn.is-active {
  color: var(--accent);
  border-color: rgba(56, 139, 253, 0.5);
  background: rgba(56, 139, 253, 0.14);
}

.topbar-icon-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.primary-nav-item:hover {
  background: rgba(255, 255, 255, 0.04);
  color: var(--text);
}

.primary-nav-item.active {
  color: var(--accent);
  border-color: rgba(56, 139, 253, 0.3);
  background: rgba(56, 139, 253, 0.12);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

.header-actions .btn-logout {
  width: auto;
}

#app-body {
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
  flex: 1;
  min-height: 0;
}

#context-sidebar {
  display: flex;
  flex-direction: column;
  min-height: 0;
  border-right: 1px solid var(--border);
  background:
    linear-gradient(180deg, rgba(22, 27, 34, 0.96) 0%, rgba(12, 16, 23, 0.96) 100%);
}

#context-sidebar-header {
  padding: 20px 18px 14px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.04);
}

.context-sidebar-title {
  font-size: 13px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--dim);
}

.context-sidebar-subtitle {
  margin-top: 8px;
  font-size: 12px;
  line-height: 1.55;
  color: #a9b3bf;
}

#context-nav {
  padding: 12px 12px 24px;
  overflow: auto;
}

.context-nav-section + .context-nav-section {
  margin-top: 18px;
}

.context-nav-label {
  padding: 0 8px 8px;
  font-size: 11px;
  font-weight: 700;
  color: var(--dim);
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.context-nav-list {
  display: grid;
  gap: 6px;
}

.context-nav-item {
  appearance: none;
  width: 100%;
  text-align: left;
  border: 1px solid transparent;
  background: transparent;
  color: var(--dim);
  padding: 11px 12px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 13px;
  line-height: 1.4;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}

.context-nav-item:hover {
  background: rgba(255, 255, 255, 0.04);
  color: var(--text);
}

.context-nav-item.active {
  color: var(--accent);
  border-color: rgba(56, 139, 253, 0.24);
  background: rgba(56, 139, 253, 0.12);
}

.context-nav-item-dsl {
  font-size: 12px;
}

#main {
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
}

#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 18px 24px;
  background: rgba(11, 16, 24, 0.88);
}

#topbar .topbar-main {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

#topbar h1 {
  font-size: 18px;
  white-space: normal;
}

.topbar-context {
  display: inline-flex;
  align-items: center;
  min-width: 0;
  padding: 6px 12px;
  border: 1px solid rgba(56, 139, 253, 0.22);
  border-radius: 999px;
  background: rgba(56, 139, 253, 0.08);
  color: #d5e7ff;
  font-size: 12px;
  line-height: 1.2;
}

#topbar .topbar-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 12px;
  min-width: 0;
}

.topbar-actions-group {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

#content {
  flex: 1;
  min-height: 0;
  padding: 20px 24px 28px;
  overflow: auto;
}

#app.is-dsl-panel #content {
  overflow: hidden;
}

#panel-dsl {
  height: 100%;
  min-height: 0;
}

#panel-dsl .dsl-layout {
  display: block;
  height: 100%;
  min-height: 0;
}

#panel-dsl .dsl-editor-wrap {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
  width: 100%;
}

#panel-dsl .dsl-workspace {
  flex: 1;
  min-height: 0;
}

#panel-dsl .dsl-form-container,
#panel-dsl .dsl-json-wrap,
#panel-dsl #monaco-container {
  min-height: 0;
}

#panel-dsl .dsl-form-container {
  height: 100%;
}

#panel-dsl .dsl-form-split {
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

#panel-dsl .dsl-form-list {
  min-height: 0;
}

#panel-dsl .dsl-form-detail {
  min-height: 0;
}

#panel-dsl .dsl-form-toolbar {
  position: sticky;
  top: 0;
  z-index: 5;
}

#panel-dsl .dsl-form-toolbar:empty {
  display: none;
}

/* 목록 접기 — 데스크톱은 2단 그리드를 1단으로 줄이고, 모바일은 시트를 닫는다. */
#app.dsl-list-collapsed #panel-dsl .dsl-form-list {
  display: none;
}

#app.dsl-list-collapsed #panel-dsl .dsl-form-split {
  grid-template-columns: minmax(0, 1fr);
}

@media (max-width: 1200px) {
  #app-body {
    grid-template-columns: 240px minmax(0, 1fr);
  }
}

@media (max-width: 1024px) {
  .app-header {
    flex-wrap: wrap;
    align-items: flex-start;
  }

  #app-body {
    grid-template-columns: 220px minmax(0, 1fr);
  }
}

@media (max-width: 980px) {
  body,
  #app,
  #app-body,
  #main,
  #content {
    height: auto;
    min-height: 0;
    overflow: visible;
  }

  #app.is-dsl-panel #content {
    overflow: visible;
  }

  #panel-dsl,
  #panel-dsl .dsl-layout,
  #panel-dsl .dsl-editor-wrap,
  #panel-dsl .dsl-workspace,
  #panel-dsl .dsl-form-container {
    height: auto;
  }

  #panel-dsl .dsl-form-split {
    overflow: visible;
  }
}


/* ------------------------------------------------------------------
   모바일 셸 (<= 860px)
   - 상단 메뉴: 라벨 대신 아이콘만
   - 사이드 메뉴: 열고 닫는 오프캔버스 드로어
   - 가로 스크롤 금지: 모든 셸 컨테이너는 min-width 0 / max-width 100%
   ------------------------------------------------------------------ */
@media (max-width: 860px) {
  :root {
    --mobile-header-h: 56px;
  }

  .app-header {
    position: sticky;
    top: 0;
    z-index: 1000;
    flex-wrap: nowrap;
    align-items: center;
    gap: 4px;
    min-height: var(--mobile-header-h);
    padding: 6px max(8px, env(safe-area-inset-right)) 6px max(8px, env(safe-area-inset-left));
  }

  .app-brand {
    display: none;
  }

  .nav-toggle {
    display: inline-flex;
  }

  .primary-nav {
    flex: 1 1 auto;
    min-width: 0;
    flex-wrap: nowrap;
    justify-content: space-evenly;
    gap: 2px;
  }

  .primary-nav-item {
    flex: 0 0 auto;
    width: 44px;
    height: 44px;
    padding: 0;
    border-radius: 12px;
    gap: 0;
  }

  .primary-nav-icon {
    display: inline-flex;
  }

  .primary-nav-label {
    display: none;
  }

  .header-actions {
    width: auto;
    flex: none;
    gap: 0;
  }

  .header-actions .btn-logout {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    padding: 0;
    border-radius: 12px;
  }

  .btn-logout-icon {
    display: inline-flex;
  }

  .btn-logout-label {
    display: none;
  }

  /* --- 사이드 메뉴 드로어 --- */
  #app-body {
    grid-template-columns: minmax(0, 1fr);
  }

  #context-sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    z-index: 1200;
    width: min(84vw, 320px);
    max-width: 100%;
    border-right: 1px solid var(--border);
    border-bottom: none;
    overflow: hidden;
    transform: translateX(-100%);
    transition: transform 0.22s ease;
    padding-bottom: env(safe-area-inset-bottom);
  }

  body.nav-open #context-sidebar {
    transform: none;
    box-shadow: 0 0 48px rgba(0, 0, 0, 0.6);
  }

  .nav-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 1100;
    background: rgba(1, 4, 9, 0.62);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease;
  }

  body.nav-open .nav-backdrop {
    opacity: 1;
    pointer-events: auto;
  }

  html.nav-open,
  body.nav-open {
    overflow: hidden;
  }

  .sidebar-mobile-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    flex: none;
    padding: 6px 8px 6px 16px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  }

  .sidebar-mobile-title {
    font-size: 13px;
    font-weight: 700;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--dim);
  }

  /* 드로어 안에서는 데스크톱과 같은 세로 목록을 그대로 쓴다. */
  #context-sidebar-header {
    display: block;
    flex: none;
    padding: 14px 16px 12px;
  }

  #context-nav {
    display: block;
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 12px 12px calc(24px + env(safe-area-inset-bottom));
  }

  .context-nav-section {
    display: block;
  }

  .context-nav-section + .context-nav-section {
    margin-top: 18px;
  }

  .context-nav-label {
    display: block;
  }

  .context-nav-list {
    display: grid;
    gap: 6px;
  }

  .context-nav-item {
    width: 100%;
    text-align: left;
    min-height: 44px;
    padding: 11px 12px;
    border-radius: 10px;
    white-space: normal;
    overflow-wrap: anywhere;
  }

  /* --- 본문 --- */
  #topbar {
    position: sticky;
    top: var(--mobile-header-h);
    z-index: 900;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px 10px;
    padding: 10px max(12px, env(safe-area-inset-right)) 10px max(12px, env(safe-area-inset-left));
    border-bottom: 1px solid var(--border);
    background: rgba(11, 16, 24, 0.96);
    backdrop-filter: blur(10px);
  }

  #topbar h1 {
    font-size: 16px;
  }

  /* Save 하나만 남아서 모바일에서도 타이틀과 같은 줄에 오른쪽 정렬로 둔다. */
  #topbar .topbar-main {
    flex: 1 1 auto;
    min-width: 0;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
  }

  #topbar .topbar-actions {
    flex: 0 0 auto;
    min-width: 0;
    justify-content: flex-end;
    flex-wrap: wrap;
    gap: 8px;
  }

  .topbar-actions-group {
    gap: 6px;
  }

  .topbar-actions-group .btn {
    min-height: 40px;
    padding: 8px 14px;
    white-space: nowrap;
  }

  /* 헤더/토프바가 이미 sticky 라 폼 툴바까지 붙으면 3중으로 겹친다. */
  #panel-dsl .dsl-form-toolbar {
    position: static;
  }

  #content {
    padding: 12px max(12px, env(safe-area-inset-right)) calc(28px + env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  }

  /* --- DSL 마스터-디테일: 목록은 시트로 접었다 편다 --- */
  .topbar-icon-btn {
    width: 40px;
    height: 40px;
  }

  /* 목록 시트: 가로 폭이 남으면 한 줄에 여러 개, 넘치면 다음 줄로 감싼다(세로 1열 강제 금지). */
  #app:not(.dsl-list-collapsed) #panel-dsl .dsl-form-list {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    gap: 6px;
    max-height: 60vh;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 10px;
    border-bottom: 1px solid var(--border);
  }

  #app:not(.dsl-list-collapsed) #panel-dsl .dsl-form-list-item {
    flex: 1 1 160px;
    width: auto;
  }

  /* 고급 JSON(Monaco) 편집기 높이 복구.
     위쪽 `#panel-dsl .dsl-json-wrap, #panel-dsl #monaco-container { min-height: 0 }` 는
     데스크톱 flex 레이아웃용인데, 모바일에선 부모가 높이를 안 줘서 에디터가 0으로 찌그러진다.
     같은 파일·같은 특이도(ID 2개)로 여기서 되돌린다(base.css 의 ID 1개 규칙은 이 규칙을 못 이긴다). */
  #panel-dsl .dsl-json-wrap {
    min-height: 70vh;
  }

  #panel-dsl #monaco-container {
    min-height: 62vh;
    height: 62vh;
  }
}

@media (max-width: 640px) {
  .app-header {
    padding-left: max(6px, env(safe-area-inset-left));
    padding-right: max(6px, env(safe-area-inset-right));
  }

  .primary-nav-item,
  .icon-btn,
  .header-actions .btn-logout {
    width: 42px;
    height: 42px;
  }

  #context-sidebar-header {
    padding: 12px 14px 10px;
  }

  .context-sidebar-subtitle {
    display: none;
  }

  #topbar {
    padding-top: 8px;
    padding-bottom: 8px;
  }

  .topbar-context {
    white-space: normal;
    overflow-wrap: anywhere;
  }
}

/* ── DSL 불러오기: 서버 스냅샷 목록 ───────────────────────────────────────── */
.backup-box-wide { grid-column: 1 / -1; }

.restore-tabs { display: flex; gap: 8px; margin: 4px 0 12px; flex-wrap: wrap; }
.restore-tab.active { background: #2563eb; color: #fff; border-color: transparent; }

.restore-pane.hidden { display: none; }

.snapshot-list { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; }
.snapshot-row {
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px;
  padding: 10px 12px; border: 1px solid #2a3242; border-radius: 8px;
}
.snapshot-row-main { flex: 1 1 240px; min-width: 0; }
.snapshot-row-desc { font-weight: 600; word-break: break-word; }
.snapshot-row-meta { font-size: 12px; opacity: 0.7; margin-top: 2px; word-break: break-all; }
.snapshot-row-actions { display: flex; gap: 6px; flex-wrap: wrap; }

.snapshot-plan { margin-top: 12px; }
.snapshot-plan-box { padding: 12px; border: 1px solid #2a3242; border-radius: 8px; }
.snapshot-plan-title { font-weight: 600; margin-bottom: 8px; word-break: break-word; }
.snapshot-plan-scroll { overflow-x: auto; }
.snapshot-plan-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.snapshot-plan-table th, .snapshot-plan-table td { text-align: left; padding: 4px 8px; border-bottom: 1px solid #2a3242; white-space: nowrap; }
.snapshot-plan-loss { color: #f87171; font-weight: 600; }
.snapshot-plan-actions { display: flex; align-items: center; gap: 10px; margin-top: 12px; flex-wrap: wrap; }
.snapshot-plan-allow { display: flex; align-items: center; gap: 6px; font-size: 13px; color: #f87171; }

/* Editing status shares the workspace; lists remain usable at any page scroll position. */
.dsl-editing-status { flex: none; padding: 8px 12px; color: var(--accent, #8cc4ff); font-size: 13px; overflow-wrap: anywhere; }
.dsl-editing-status:empty { display: none; }
.dsl-list-close { display: none; }
#panel-dsl .dsl-form-container { flex: 1; min-height: 0; }
@media (max-width: 860px) {
  .dsl-list-filter-search, .dsl-skill-tab { min-height: 44px; font-size: 16px; }
  .dsl-list-filter-search { width: 100%; box-sizing: border-box; }
  .dsl-list-filter { flex: none; }
  .dsl-skill-tabs { gap: 6px; }
  .acct-input, .acct-select, #panel-mail input:not([type="checkbox"]), #panel-mail select, #panel-mail textarea { min-height: 44px; font-size: 16px; }
  body.dsl-mobile-list-open { overflow: hidden; }
  body.dsl-mobile-list-open #panel-dsl .dsl-form-container {
    position: fixed; top: var(--admin-list-top, 114px); left: 0; right: 0; bottom: 0;
    height: auto; z-index: 950; display: flex; flex-direction: column;
    border-radius: 0; background: var(--bg, #101620); padding-bottom: env(safe-area-inset-bottom);
  }
  body.dsl-mobile-list-open #panel-dsl .dsl-form-toolbar { flex: none; }
  body.dsl-mobile-list-open .dsl-form-toolbar-actions { display: none; }
  body.dsl-mobile-list-open .dsl-list-close { display: inline-flex; min-height: 44px; }
  body.dsl-mobile-list-open #panel-dsl .dsl-form-detail { display: none; }
  body.dsl-mobile-list-open #panel-dsl .dsl-form-split { display: flex; flex: 1; min-height: 0; overflow: hidden; }
  body.dsl-mobile-list-open #app:not(.dsl-list-collapsed) #panel-dsl .dsl-form-list {
    flex: 1; min-height: 0; max-height: none; overflow-y: auto; align-content: flex-start;
  }
  body.dsl-mobile-list-open .dsl-form-list-meta { display: block; font-size: 12px; }
  body.dsl-mobile-list-open .dsl-form-list-item { min-height: 58px; }
}
.dsl-form-list-summary { display: none; }
.dsl-list-availability { min-height: 36px; background: var(--bg); color: var(--text); border: 1px solid var(--border); border-radius: 6px; padding: 6px; }
@media (max-width: 860px) {
  .dsl-list-availability { min-height: 44px; font-size: 16px; }
  body.dsl-mobile-list-open .dsl-form-list-meta { display: none; }
  body.dsl-mobile-list-open .dsl-form-list-summary { display: block; color: var(--dim); font-size: 12px; margin-top: 6px; overflow-wrap: anywhere; }
  .dsl-collapsible > summary { min-height: 44px; box-sizing: border-box; }
}
@media (max-width: 860px) {
  .dsl-list-filter .dsl-skill-tabs { flex-wrap: nowrap; overflow-x: auto; max-width: 100%; padding-bottom: 4px; }
  .dsl-list-filter .dsl-skill-tab { flex: none; }
}
