* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  font-family: "Segoe UI", "Microsoft YaHei", "PingFang SC", system-ui, sans-serif;
  font-size: 13px;
  overflow: hidden;
  color: var(--text);
  background: var(--chrome);
}

body {
  --chrome: #f3f3f3;
  --chrome-2: #ececec;
  --chrome-3: #e2e2e2;
  --border: #c9c9c9;
  --border-strong: #b0b0b0;
  --text: #202020;
  --text-dim: #5c5c5c;
  --accent: #0a64b4;
  --hover: #dbeafc;
  --editor-bg: #ffffff;
  --status-bg: #e9e9e9;
  --tab-active: #ffffff;
  --ok: #157347;
  --error: #c62828;
  --warn: #b26a00;
}

body.theme-dark {
  --chrome: #2b2b2b;
  --chrome-2: #333333;
  --chrome-3: #3a3a3a;
  --border: #404040;
  --border-strong: #4a4a4a;
  --text: #dcdcdc;
  --text-dim: #9b9b9b;
  --accent: #4c9aff;
  --hover: #36475d;
  --editor-bg: #1e1e1e;
  --status-bg: #262626;
  --tab-active: #1e1e1e;
  --ok: #4cd07d;
  --error: #ff7b72;
  --warn: #e0b252;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100%;
  height: 100dvh;
  background: var(--chrome);
}

.only-mobile {
  display: none;
}

.sidebar-mask {
  display: none;
}

/* ---------- 菜单栏 ---------- */
.menubar {
  display: flex;
  align-items: center;
  height: 27px;
  padding: 0 3px;
  background: var(--chrome);
  border-bottom: 1px solid var(--border);
  user-select: none;
  flex: 0 0 auto;
}

.menu-btn {
  border: 0;
  background: transparent;
  color: var(--text);
  font-size: 12.5px;
  padding: 3px 9px;
  border-radius: 2px;
  cursor: pointer;
  white-space: nowrap;
}

.brand {
  padding: 0 12px 0 4px;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.4px;
  color: var(--accent);
  user-select: none;
  white-space: nowrap;
}

.menu-btn:hover,
.menu-btn.open {
  background: var(--hover);
}

/* ---------- 工具栏 ---------- */
.toolbar {
  display: flex;
  align-items: center;
  gap: 4px;
  height: 39px;
  padding: 0 8px;
  background: var(--chrome-2);
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}

.tb {
  height: 27px;
  padding: 0 11px;
  font-size: 12.5px;
  color: var(--text);
  background: transparent;
  border: 1px solid transparent;
  border-radius: 3px;
  cursor: pointer;
  white-space: nowrap;
  font-family: inherit;
}

.tb:hover:not(:disabled) {
  background: var(--hover);
  border-color: var(--border);
}

.tb:active:not(:disabled) {
  background: var(--chrome-3);
}

.tb.primary {
  border-color: var(--border);
  background: var(--chrome);
  font-weight: 600;
  color: var(--accent);
}

.tb:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.tb-sep {
  width: 1px;
  height: 20px;
  margin: 0 5px;
  background: var(--border);
}

.tb-flex {
  flex: 1;
}

.tb-hint {
  font-size: 11.5px;
  color: var(--text-dim);
  padding-right: 4px;
}

/* ---------- 主区域 ---------- */
.main {
  display: flex;
  flex: 1;
  min-height: 0;
}

.sidebar {
  width: 192px;
  flex: 0 0 192px;
  display: flex;
  flex-direction: column;
  background: var(--chrome-2);
  border-right: 1px solid var(--border);
  overflow: hidden;
}

.side-title {
  height: 25px;
  line-height: 25px;
  padding-left: 9px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-dim);
  background: var(--chrome-3);
  border-bottom: 1px solid var(--border);
  border-top: 1px solid var(--border);
  user-select: none;
  flex: 0 0 auto;
}

.side-title:first-child {
  border-top: 0;
}

.tree {
  padding: 5px 0;
  overflow-y: auto;
  flex: 0 1 auto;
  max-height: 45%;
}

.tree-item {
  display: flex;
  align-items: center;
  gap: 6px;
  height: 24px;
  padding: 0 10px;
  font-size: 12.5px;
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  border: 0;
  background: transparent;
  width: 100%;
  text-align: left;
  font-family: inherit;
}

.tree-item:hover {
  background: var(--hover);
}

.tree-item.active {
  background: var(--hover);
  box-shadow: inset 2px 0 0 var(--accent);
}

.tree-item .mark {
  color: var(--accent);
  font-size: 11px;
  flex: 0 0 auto;
}

.tree-item .label {
  overflow: hidden;
  text-overflow: ellipsis;
}

.side-tip {
  margin-top: auto;
  padding: 9px 10px;
  font-size: 11.5px;
  line-height: 1.5;
  color: var(--text-dim);
  border-top: 1px solid var(--border);
}

/* ---------- 工作区 ---------- */
.workspace {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-width: 0;
  background: var(--editor-bg);
}

.tabbar {
  display: flex;
  align-items: flex-end;
  height: 30px;
  padding: 0 4px;
  gap: 2px;
  background: var(--chrome-2);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  overflow-y: hidden;
  flex: 0 0 auto;
  scrollbar-width: none;
}

.tab {
  display: flex;
  align-items: center;
  gap: 7px;
  height: 26px;
  padding: 0 9px 0 11px;
  font-size: 12.5px;
  color: var(--text-dim);
  background: var(--chrome-3);
  border: 1px solid var(--border);
  border-bottom: 0;
  border-radius: 4px 4px 0 0;
  cursor: pointer;
  white-space: nowrap;
  font-family: inherit;
}

.tab.active {
  background: var(--tab-active);
  color: var(--text);
  box-shadow: inset 0 2px 0 var(--accent);
  font-weight: 600;
}

.tab .close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 15px;
  height: 15px;
  border-radius: 2px;
  font-size: 12px;
  line-height: 1;
  color: var(--text-dim);
}

.tab .close:hover {
  background: rgba(200, 60, 60, 0.18);
  color: var(--error);
}

.editor-wrap {
  flex: 1;
  min-height: 0;
  overflow: hidden;
  position: relative;
  background: var(--editor-bg);
}

.CodeMirror {
  height: 100%;
  font-family: Consolas, "Cascadia Mono", "SF Mono", Menlo, monospace;
  font-size: 14px;
  line-height: 1.5;
}

/* ---------- 底部面板 ---------- */
.panel {
  flex: 0 0 196px;
  display: flex;
  flex-direction: column;
  border-top: 1px solid var(--border);
  background: var(--chrome-2);
  min-height: 0;
}

.panel-head {
  display: flex;
  align-items: center;
  height: 28px;
  gap: 2px;
  padding: 0 6px;
  background: var(--chrome-3);
  border-bottom: 1px solid var(--border);
  user-select: none;
  flex: 0 0 auto;
}

.ptab {
  height: 22px;
  padding: 0 10px;
  font-size: 12px;
  color: var(--text-dim);
  background: transparent;
  border: 1px solid transparent;
  border-radius: 3px;
  cursor: pointer;
  font-family: inherit;
}

.ptab:hover {
  background: var(--hover);
}

.ptab.active {
  background: var(--chrome);
  border-color: var(--border);
  color: var(--text);
  font-weight: 600;
}

.panel-flex {
  flex: 1;
}

.panel-status {
  font-size: 11.5px;
  color: var(--text-dim);
  padding-right: 8px;
}

.panel-clear {
  height: 22px;
  padding: 0 9px;
  font-size: 11.5px;
  color: var(--text-dim);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 3px;
  cursor: pointer;
  font-family: inherit;
}

.panel-clear:hover {
  background: var(--hover);
  color: var(--text);
}

.panel-body {
  flex: 1;
  min-height: 0;
  overflow: auto;
  background: var(--editor-bg);
  padding: 8px 10px;
  font-family: Consolas, "Cascadia Mono", "SF Mono", Menlo, monospace;
  font-size: 12.5px;
  line-height: 1.55;
  color: var(--text);
}

.panel-body.hidden {
  display: none;
}

.panel-body pre {
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: inherit;
}

.log-line {
  white-space: pre-wrap;
  word-break: break-word;
}

.log-error {
  color: var(--error);
}

.log-warn {
  color: var(--warn);
}

.log-ok {
  color: var(--ok);
}

.log-meta {
  color: var(--text-dim);
}

.log-run {
  color: var(--text);
}

.log-stderr {
  color: var(--error);
}

.err-link {
  color: var(--accent);
  text-decoration: underline;
  cursor: pointer;
}

.placeholder {
  color: var(--text-dim);
  font-family: inherit;
  font-size: 12.5px;
}

#stdinBox {
  display: block;
  width: 100%;
  height: 100%;
  min-height: 120px;
  resize: none;
  border: 1px solid var(--border);
  border-radius: 3px;
  background: var(--editor-bg);
  color: var(--text);
  padding: 7px 9px;
  font-family: Consolas, "Cascadia Mono", Menlo, monospace;
  font-size: 12.5px;
  line-height: 1.5;
  outline: none;
}

#stdinBox:focus {
  border-color: var(--accent);
}

/* ---------- 交互式输入栏 ---------- */
.interactive-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 8px;
  background: var(--chrome-2);
  border-top: 1px solid var(--border);
  flex: 0 0 auto;
}

.interactive-bar.hidden {
  display: none;
}

.ia-prompt {
  color: var(--accent);
  font-family: Consolas, "Cascadia Mono", Menlo, monospace;
  font-size: 13px;
  font-weight: 700;
}

.ia-input {
  flex: 1;
  height: 27px;
  padding: 0 9px;
  border: 1px solid var(--border);
  border-radius: 3px;
  background: var(--editor-bg);
  color: var(--text);
  font-family: Consolas, "Cascadia Mono", Menlo, monospace;
  font-size: 12.5px;
  outline: none;
}

.ia-input:focus {
  border-color: var(--accent);
}

.ia-btn {
  height: 27px;
  padding: 0 13px;
  border: 1px solid var(--border);
  border-radius: 3px;
  background: var(--chrome);
  color: var(--text);
  font-size: 12.5px;
  font-family: inherit;
  cursor: pointer;
  white-space: nowrap;
}

.ia-btn:hover {
  background: var(--hover);
}

.ia-btn.danger {
  color: var(--error);
}

.stream-out,
.stream-err {
  margin: 0;
  font-family: inherit;
  font-size: inherit;
  line-height: inherit;
  white-space: pre-wrap;
  word-break: break-word;
}

.stream-err {
  color: var(--error);
}

/* ---------- 状态栏 ---------- */
.statusbar {
  display: flex;
  align-items: center;
  height: 24px;
  background: var(--status-bg);
  border-top: 1px solid var(--border);
  flex: 0 0 auto;
  font-size: 11.5px;
  color: var(--text-dim);
  user-select: none;
}

.status-item {
  padding: 0 10px;
  height: 100%;
  display: flex;
  align-items: center;
  border-left: 1px solid var(--border);
  white-space: nowrap;
}

.status-item:first-child {
  border-left: 0;
}

.status-item.msg {
  color: var(--text);
  min-width: 120px;
}

.status-flex {
  flex: 1;
}

/* ---------- 下拉菜单 ---------- */
.dropdown {
  position: fixed;
  z-index: 100;
  min-width: 190px;
  padding: 4px;
  background: var(--chrome);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: 3px;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.22);
  font-size: 12.5px;
}

.dropdown.hidden {
  display: none;
}

.menu-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  width: 100%;
  height: 26px;
  padding: 0 10px;
  border: 0;
  border-radius: 2px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  text-align: left;
  font-family: inherit;
  font-size: 12.5px;
  white-space: nowrap;
}

.menu-item:hover {
  background: var(--hover);
  color: var(--accent);
}

.menu-item .shortcut {
  color: var(--text-dim);
  font-size: 11.5px;
}

.menu-sep {
  height: 1px;
  margin: 4px 6px;
  background: var(--border);
}

/* ---------- 博客提交条 ---------- */
.submit-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 10px;
  background: linear-gradient(90deg, rgba(10, 100, 180, 0.12), rgba(10, 100, 180, 0.04));
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}

.submit-bar.hidden {
  display: none;
}

.sb-badge {
  padding: 2px 8px;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font-size: 11.5px;
  font-weight: 700;
  white-space: nowrap;
}

.sb-text {
  font-size: 12.5px;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sb-flex {
  flex: 1;
}

.sb-name {
  height: 27px;
  width: 132px;
  padding: 0 8px;
  border: 1px solid var(--border);
  border-radius: 3px;
  background: var(--bg);
  color: var(--text);
  font-size: 12.5px;
  font-family: inherit;
  outline: none;
}

.sb-name:focus {
  border-color: var(--accent);
}

.sb-name.hidden {
  display: none;
}

.sb-btn {
  height: 27px;
  padding: 0 12px;
  border: 1px solid var(--accent);
  border-radius: 3px;
  background: var(--accent);
  color: #fff;
  font-size: 12.5px;
  font-family: inherit;
  cursor: pointer;
  white-space: nowrap;
}

.sb-btn:hover:not(:disabled) {
  filter: brightness(1.08);
}

.sb-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.sb-btn.ghost {
  background: transparent;
  color: var(--accent);
}

/* ---------- 排行榜弹窗 ---------- */
.modal-mask {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(0, 0, 0, 0.45);
}

.modal-mask.hidden {
  display: none;
}

.modal {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 660px;
  max-height: 82vh;
  background: var(--chrome);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.35);
  overflow: hidden;
}

.modal-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--chrome-2);
}

.modal-title {
  font-size: 13.5px;
  font-weight: 700;
  white-space: nowrap;
}

.modal-tabs {
  display: flex;
  gap: 2px;
}

.mtab {
  height: 24px;
  padding: 0 10px;
  border: 1px solid transparent;
  border-radius: 3px;
  background: transparent;
  color: var(--text-dim);
  font-size: 12px;
  font-family: inherit;
  cursor: pointer;
}

.mtab:hover {
  background: var(--hover);
}

.mtab.active {
  background: var(--chrome);
  border-color: var(--border);
  color: var(--text);
  font-weight: 600;
}

.modal-flex {
  flex: 1;
}

.modal-close {
  width: 26px;
  height: 26px;
  border: 0;
  border-radius: 3px;
  background: transparent;
  color: var(--text-dim);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
}

.modal-close:hover {
  background: var(--hover);
  color: var(--error);
}

.modal-body {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding: 12px;
  background: var(--editor-bg);
  font-size: 12.5px;
}

.modal-foot {
  padding: 8px 12px;
  border-top: 1px solid var(--border);
  background: var(--chrome-2);
  color: var(--text-dim);
  font-size: 11.5px;
}

.board-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12.5px;
}

.board-table th,
.board-table td {
  padding: 7px 8px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.board-table th {
  color: var(--text-dim);
  font-size: 11.5px;
  font-weight: 600;
  position: sticky;
  top: -12px;
  background: var(--editor-bg);
}

.board-table td.rank {
  width: 42px;
  font-weight: 700;
  color: var(--accent);
}

.board-table tr.top1 td.rank {
  color: #d4a017;
}

.board-table td.name {
  max-width: 170px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.board-table td.points {
  font-weight: 700;
}

.board-table .num {
  font-family: Consolas, "Cascadia Mono", Menlo, monospace;
}

.board-table tr.me td {
  background: rgba(10, 100, 180, 0.1);
}

.board-empty {
  padding: 12px 2px;
  color: var(--text-dim);
}

@media (max-width: 860px) {
  .submit-bar {
    flex-wrap: wrap;
    gap: 6px;
  }

  .sb-text {
    flex: 1 1 100%;
    white-space: normal;
  }

  .sb-btn {
    height: 33px;
    flex: 1 1 auto;
  }

  .sb-name {
    height: 33px;
    width: 100%;
    flex: 1 1 100%;
  }

  .modal-mask {
    padding: 10px;
  }

  .modal {
    max-height: 88vh;
  }

  .modal-head {
    flex-wrap: wrap;
  }

  .modal-body {
    padding: 8px;
  }

  .board-table th,
  .board-table td {
    padding: 6px 5px;
    font-size: 12px;
  }
}

/* ---------- 窄屏适配 ---------- */
@media (max-width: 860px) {
  .brand {
    display: none;
  }

  .menubar {
    height: 32px;
    overflow-x: auto;
    overflow-y: hidden;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }

  .menubar::-webkit-scrollbar {
    display: none;
  }

  .menu-btn {
    padding: 6px 10px;
    font-size: 13px;
    flex: 0 0 auto;
  }

  .toolbar {
    height: auto;
    flex-wrap: wrap;
    gap: 6px;
    padding: 7px 8px;
  }

  .tb {
    height: 33px;
    padding: 0 12px;
    font-size: 13px;
    flex: 0 1 auto;
    min-width: 0;
  }

  .tb-sep,
  .tb-flex,
  .tb-hint {
    display: none;
  }

  .only-mobile {
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  .main {
    position: relative;
  }

  .sidebar {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    z-index: 60;
    width: 78%;
    max-width: 300px;
    flex-basis: auto;
    border-right: 1px solid var(--border-strong);
    box-shadow: 4px 0 16px rgba(0, 0, 0, 0.25);
    transform: translateX(-102%);
    transition: transform 0.22s ease;
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .tree {
    max-height: none;
    flex: 0 1 auto;
  }

  .tree-item {
    height: 40px;
    font-size: 13.5px;
  }

  .sidebar-mask {
    display: block;
    position: absolute;
    inset: 0;
    z-index: 55;
    background: rgba(0, 0, 0, 0.42);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease;
  }

  .sidebar-mask.show {
    opacity: 1;
    pointer-events: auto;
  }

  .tabbar {
    height: 34px;
  }

  .tab {
    height: 29px;
    font-size: 13px;
  }

  .CodeMirror {
    font-size: 13.5px;
    line-height: 1.55;
  }

  .panel {
    flex-basis: 42vh;
    max-height: 55vh;
  }

  .panel.collapsed {
    flex-basis: 32px;
    max-height: 32px;
  }

  .panel.collapsed .panel-body,
  .panel.collapsed .interactive-bar {
    display: none;
  }

  .panel-head {
    height: 32px;
  }

  .ptab {
    height: 26px;
    padding: 0 8px;
    font-size: 12.5px;
  }

  .panel-clear {
    height: 26px;
    padding: 0 8px;
    font-size: 12px;
  }

  .panel-status {
    display: none;
  }

  #stdinBox {
    font-size: 16px;
  }

  .ia-input {
    height: 36px;
    font-size: 16px;
  }

  .ia-btn {
    height: 36px;
    padding: 0 14px;
    font-size: 13px;
  }

  .statusbar {
    height: 28px;
    font-size: 11px;
    padding-bottom: env(safe-area-inset-bottom);
  }

  .status-item {
    padding: 0 8px;
  }

  .status-item:nth-last-child(2),
  .status-item:nth-last-child(3) {
    display: none;
  }

  .dropdown {
    min-width: 168px;
    font-size: 13.5px;
  }

  .menu-item {
    height: 38px;
    font-size: 13.5px;
  }
}

@media (max-width: 400px) {
  .tb {
    padding: 0 9px;
    font-size: 12.5px;
  }

  .tb.primary {
    flex: 1 1 45%;
  }
}
