/* App shell — sidebar + header + main layout */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html { scroll-behavior: smooth; }

body {
  font-family: var(--font-sans);
  font-size: var(--text-base);
  line-height: var(--leading);
  color: var(--text-primary);
  background: var(--bg-page);
  -webkit-font-smoothing: antialiased;
}

a { color: var(--text-link); text-decoration: none; }
a:hover { color: var(--accent-hover); }

/* ===== Header ===== */
.app-header {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: var(--header-height);
  background: var(--bg-sidebar);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 16px 0 0;
  z-index: 100;
}

.header-brand {
  width: var(--sidebar-width);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 20px;
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--text-primary);
  text-decoration: none;
}
.header-brand:hover { color: var(--text-primary); }

.header-brand .logo {
  width: 28px; height: 28px;
  background: var(--accent);
  border-radius: 6px;
  display: flex; align-items: center; justify-content: center;
  color: #fff;
  font-size: 13px;
  font-weight: 700;
}

.header-search {
  flex: 1;
  max-width: 360px;
  position: relative;
}
.header-search input {
  width: 100%;
  height: 32px;
  padding: 0 12px 0 32px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-subtle);
  font-size: var(--text-sm);
  font-family: inherit;
  color: var(--text-primary);
  outline: none;
  transition: border-color .15s, background .15s;
}
.header-search input:focus {
  border-color: var(--accent);
  background: #fff;
}
.header-search input::placeholder { color: var(--text-tertiary); }
.header-search .icon {
  position: absolute;
  left: 10px; top: 50%;
  transform: translateY(-50%);
  color: var(--text-tertiary);
  font-size: 14px;
  pointer-events: none;
}

.header-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: var(--text-sm);
  color: var(--text-secondary);
}

.header-promo {
  flex: 1;
  min-width: 0;
  max-width: none;
  margin: 0 20px;
  padding: 6px 14px;
  border: none;
  border-radius: 0;
  background: transparent;
  color: var(--danger);
  font-family: inherit;
  font-size: 17px;
  font-weight: 700;
  line-height: 1.35;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: center;
  cursor: pointer;
  transition: opacity .15s;
}
.header-promo:hover {
  opacity: 0.82;
}

.header-author {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 10px 4px 4px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg-subtle);
  color: inherit;
  font-family: inherit;
  cursor: pointer;
  transition: border-color .15s, background .15s;
}
.header-author:hover {
  border-color: var(--accent);
  background: #fff;
  color: var(--text-primary);
}
.header-author-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
  display: block;
}
.header-author-meta {
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
  line-height: 1.2;
  text-align: left;
}
.header-author-name {
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--text-primary);
  white-space: nowrap;
}
.header-author-id {
  font-size: 11px;
  color: var(--text-tertiary);
  white-space: nowrap;
}

/* Author modal */
.author-modal {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.author-modal[hidden] { display: none; }
.author-modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
}
.author-modal-panel {
  position: relative;
  width: min(360px, 100%);
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: 0 18px 50px rgba(15, 23, 42, 0.18);
  padding: 28px 24px 22px;
  text-align: center;
}
.author-modal-close {
  position: absolute;
  top: 10px;
  right: 12px;
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  color: var(--text-tertiary);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  border-radius: 8px;
}
.author-modal-close:hover {
  background: var(--bg-subtle);
  color: var(--text-primary);
}
.author-modal-avatar {
  width: 88px;
  height: 88px;
  border-radius: 50%;
  object-fit: cover;
  display: block;
  margin: 0 auto 14px;
  border: 2px solid var(--border);
}
.author-modal-name {
  margin: 0 0 6px;
  font-size: 18px;
  font-weight: 700;
  color: var(--text-primary);
}
.author-modal-id {
  margin: 0 0 18px;
  font-size: var(--text-sm);
  color: var(--text-secondary);
}
.author-modal-copy {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 140px;
  height: 36px;
  padding: 0 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-subtle);
  color: var(--text-primary);
  font-family: inherit;
  font-size: var(--text-sm);
  font-weight: 600;
  cursor: pointer;
  transition: border-color .15s, background .15s, color .15s;
}
.author-modal-copy:hover {
  border-color: var(--accent);
  background: #fff;
}
.author-modal-copy.is-copied {
  border-color: var(--accent);
  color: var(--accent);
}

.sidebar-toggle {
  display: none;
  background: none;
  border: none;
  padding: 6px;
  cursor: pointer;
  color: var(--text-secondary);
  font-size: 18px;
}

/* ===== Shell layout ===== */
.app-body {
  display: flex;
  padding-top: var(--header-height);
  min-height: 100vh;
}

/* ===== Sidebar ===== */
.app-sidebar {
  position: fixed;
  top: var(--header-height);
  left: 0;
  width: var(--sidebar-width);
  height: calc(100vh - var(--header-height));
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  z-index: 50;
  transition: transform .2s;
}

.sidebar-section { padding: 16px 0 8px; }

.sidebar-section-title {
  padding: 0 20px 8px;
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--text-tertiary);
  text-transform: uppercase;
  letter-spacing: .5px;
}

.sidebar-nav { list-style: none; }

.sidebar-nav a {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 20px;
  font-size: var(--text-sm);
  color: var(--text-secondary);
  text-decoration: none;
  border-left: 2px solid transparent;
  transition: background .12s, color .12s;
}
.sidebar-nav a:hover {
  background: var(--bg-subtle);
  color: var(--text-primary);
}
.sidebar-nav a.active {
  background: var(--accent-bg);
  color: var(--accent);
  border-left-color: var(--accent);
  font-weight: 500;
}
.sidebar-nav a .rank {
  flex-shrink: 0;
  width: 30px;
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}
.sidebar-nav a.active .rank { color: var(--accent); }
.sidebar-nav a .title {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.sidebar-nav a .diff-label {
  flex-shrink: 0;
  font-size: 10px;
  font-weight: 500;
  padding: 1px 5px;
  border-radius: 3px;
  line-height: 1.4;
}
.sidebar-nav a .diff-label.easy   { color: var(--easy);   background: var(--success-bg); }
.sidebar-nav a .diff-label.medium { color: var(--medium); background: var(--warning-bg); }
.sidebar-nav a .diff-label.hard   { color: var(--hard);   background: var(--danger-bg); }
.sidebar-nav a .status {
  flex-shrink: 0;
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--border);
}
.sidebar-nav a .status.done { background: var(--success); }
.sidebar-nav a.disabled {
  opacity: .45;
  pointer-events: none;
}

.sidebar-filter {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-light);
}
.sidebar-filter select {
  width: 100%;
  height: 30px;
  padding: 0 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #fff;
  font-size: var(--text-xs);
  font-family: inherit;
  color: var(--text-secondary);
  outline: none;
}

/* ===== Main content area ===== */
.app-main {
  flex: 1;
  margin-left: var(--sidebar-width);
  min-width: 0;
  background: var(--bg-page);
}

body[data-page="problem"] .app-main {
  background: var(--bg-doc);
}

/* ===== Doc page: adaptive content + TOC pinned right ===== */
.doc-layout {
  display: flex;
  align-items: flex-start;
  width: 100%;
  min-height: calc(100vh - var(--header-height));
}

/* ===== Right TOC (problem pages) ===== */
.doc-toc {
  position: sticky;
  top: calc(var(--header-height) + 28px);
  width: var(--toc-width);
  flex-shrink: 0;
  order: 2;
  padding: 40px 32px 80px 20px;
  max-height: calc(100vh - var(--header-height) - 28px);
  overflow-y: auto;
  align-self: flex-start;
  border-left: 1px solid var(--border-light);
  background: var(--bg-doc);
}

.doc-toc-title {
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--text-tertiary);
  margin-bottom: 12px;
  letter-spacing: .02em;
}

.doc-toc ol {
  list-style: none;
  font-size: var(--text-xs);
  line-height: 1.55;
}
.doc-toc a {
  display: block;
  padding: 5px 0 5px 12px;
  color: var(--text-tertiary);
  text-decoration: none;
  border-left: 2px solid transparent;
  transition: color .12s, border-color .12s;
}
.doc-toc a:hover {
  color: var(--text-secondary);
}
.doc-toc a.active {
  color: var(--accent);
  font-weight: 500;
  border-left-color: var(--accent);
}

/* ===== Index page ===== */
.index-content {
  width: 100%;
  max-width: 960px;
  margin: 0 auto;
  padding: 32px 40px 80px;
}

.index-hero {
  margin-bottom: 20px;
}
.index-hero h1 {
  font-size: var(--text-2xl);
  font-weight: 600;
  margin-bottom: 6px;
  letter-spacing: -.02em;
}
.index-hero p {
  color: var(--text-secondary);
  font-size: var(--text-base);
  max-width: 62ch;
}
.index-hero .official-note {
  margin-top: 8px;
  font-size: var(--text-xs);
  color: var(--text-tertiary);
  max-width: 72ch;
}

.official-rank-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: var(--text-xs);
  color: var(--text-secondary);
  background: var(--bg-subtle);
  padding: 3px 10px;
  border-radius: 999px;
  margin-bottom: 10px;
}
.official-rank-badge b { color: var(--accent); font-weight: 600; }

.index-stats {
  display: flex;
  gap: 28px;
  align-items: center;
  margin-bottom: 20px;
  padding: 14px 20px;
  background: var(--bg-doc);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
}
.index-stat .value {
  font-size: var(--text-xl);
  font-weight: 600;
  color: var(--text-primary);
  font-variant-numeric: tabular-nums;
}
.index-stat .label {
  font-size: var(--text-xs);
  color: var(--text-tertiary);
  margin-top: 2px;
}
/* Progress stat spans remaining width with a slim bar. */
.index-stat-progress {
  flex: 1;
  min-width: 140px;
}
.index-stat-progress .progress-track {
  margin-top: 8px;
  height: 6px;
  background: var(--bg-subtle);
  border-radius: 999px;
  overflow: hidden;
}
.index-stat-progress .progress-bar {
  height: 100%;
  width: 0;
  background: var(--accent);
  border-radius: 999px;
  transition: width .4s ease;
}

/* ===== Filter toolbar (sticky) ===== */
.index-toolbar {
  position: sticky;
  top: var(--header-height);
  z-index: 20;
  margin-bottom: 20px;
  padding: 12px 0;
  background: var(--bg-page);
  border-bottom: 1px solid var(--border-light);
}
.filter-row {
  display: flex;
  align-items: flex-start;
  gap: 10px;
}
.filter-row + .filter-row { margin-top: 8px; }
.filter-label {
  flex-shrink: 0;
  width: 30px;
  padding-top: 5px;
  font-size: var(--text-xs);
  color: var(--text-tertiary);
  font-weight: 600;
}

.index-filters {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  flex: 1;
  min-width: 0;
}
/* Tag filter collapses to a single row until expanded. */
.tag-filters.collapsed {
  max-height: 30px;
  overflow: hidden;
}
.tag-toggle {
  flex-shrink: 0;
  align-self: flex-start;
  height: 26px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--bg-subtle);
  font-size: var(--text-xs);
  font-family: inherit;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all .12s;
}
.tag-toggle:hover { border-color: var(--accent); color: var(--accent); }
.filter-chip {
  padding: 4px 12px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: #fff;
  font-size: var(--text-xs);
  color: var(--text-secondary);
  cursor: pointer;
  font-family: inherit;
  transition: all .12s;
}
.filter-chip:hover { border-color: var(--accent); color: var(--accent); }
.filter-chip.active {
  background: var(--accent-bg);
  border-color: var(--accent);
  color: var(--accent);
  font-weight: 500;
}

.problem-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 12px;
  align-items: start;
}

.grid-empty {
  grid-column: 1 / -1;
  padding: 32px 16px;
  text-align: center;
  color: var(--text-tertiary);
}

.problem-card {
  display: block;
  padding: 16px;
  background: var(--bg-doc);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  text-decoration: none;
  color: inherit;
  transition: border-color .15s, box-shadow .15s;
}
.problem-card:hover {
  border-color: var(--accent);
  box-shadow: var(--shadow-sm);
}
.problem-card.disabled {
  opacity: .5;
  pointer-events: none;
}
.problem-card-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}
.problem-card .rank {
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
  min-width: 28px;
}
.problem-card .num {
  font-size: 10px;
  color: var(--text-tertiary);
  font-variant-numeric: tabular-nums;
  min-width: 28px;
}
.problem-card .title {
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--text-primary);
  flex: 1;
}
.problem-card .tags {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 8px;
}
.problem-card .tag {
  font-size: 11px;
  padding: 1px 6px;
  background: var(--bg-subtle);
  color: var(--text-tertiary);
  border-radius: 3px;
}
.problem-card .difficulty {
  font-size: 11px;
  font-weight: 500;
  padding: 1px 6px;
  border-radius: 3px;
}
.problem-card .difficulty.easy   { color: var(--easy);   background: var(--success-bg); }
.problem-card .difficulty.medium { color: var(--medium); background: var(--warning-bg); }
.problem-card .difficulty.hard   { color: var(--hard);   background: var(--danger-bg); }

/* ===== Responsive ===== */
@media (max-width: 1100px) {
  .doc-toc { padding-right: 24px; }
}

@media (max-width: 960px) {
  .doc-toc { display: none; }
  .doc { padding: 32px 24px 80px; }
}

@media (max-width: 768px) {
  .sidebar-toggle { display: block; }
  .app-sidebar {
    transform: translateX(-100%);
  }
  .app-sidebar.open { transform: translateX(0); }
  .app-main { margin-left: 0; }
  .header-brand { width: auto; padding: 0 12px; }
  .header-brand span { display: none; }
  .header-author-meta { display: none; }
  .header-author { padding: 2px; gap: 0; }
  .header-promo {
    flex: 1;
    margin: 0 8px;
    font-size: 14px;
    padding: 4px 6px;
    white-space: normal;
    line-height: 1.25;
    text-align: center;
  }
  .header-search { max-width: 140px; }
  .index-content { padding: 20px 16px 60px; }
}
