*, *::before, *::after { box-sizing: border-box; }

:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --text: #1f2328;
  --muted: #6b7280;
  --border: #e4e7ec;
  --accent: #2f6feb;
  --accent-soft: rgba(47, 111, 235, .09);
  --accent-line: rgba(47, 111, 235, .22);
  --accent-ring: rgba(47, 111, 235, .18);
  --danger: #dc2626;
  --danger-soft: rgba(220, 38, 38, .12);
  --shadow: 0 1px 2px rgba(16, 24, 40, .04), 0 4px 16px rgba(16, 24, 40, .06);
  --background-image: none;
  --background-blur: 0px;
  --background-overlay: rgba(245, 246, 248, .67);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16181d;
    --panel: #1f232b;
    --text: #e6e8eb;
    --muted: #9aa1ac;
    --border: #2e3440;
    --accent: #5b91f5;
    --accent-soft: rgba(91, 145, 245, .14);
    --accent-line: rgba(91, 145, 245, .28);
    --accent-ring: rgba(91, 145, 245, .22);
    --danger: #f87171;
    --danger-soft: rgba(248, 113, 113, .16);
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 4px 16px rgba(0, 0, 0, .3);
    --background-overlay: rgba(22, 24, 29, .70);
  }
}

html[data-theme="light"] {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --text: #1f2328;
  --muted: #6b7280;
  --border: #e4e7ec;
  --accent: #2f6feb;
  --accent-soft: rgba(47, 111, 235, .09);
  --accent-line: rgba(47, 111, 235, .22);
  --accent-ring: rgba(47, 111, 235, .18);
  --danger: #dc2626;
  --danger-soft: rgba(220, 38, 38, .12);
  --shadow: 0 1px 2px rgba(16, 24, 40, .04), 0 4px 16px rgba(16, 24, 40, .06);
  --background-overlay: rgba(245, 246, 248, .67);
}

html[data-theme="dark"] {
  --bg: #16181d;
  --panel: #1f232b;
  --text: #e6e8eb;
  --muted: #9aa1ac;
  --border: #2e3440;
  --accent: #5b91f5;
  --accent-soft: rgba(91, 145, 245, .14);
  --accent-line: rgba(91, 145, 245, .28);
  --accent-ring: rgba(91, 145, 245, .22);
  --danger: #f87171;
  --danger-soft: rgba(248, 113, 113, .16);
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 4px 16px rgba(0, 0, 0, .3);
  --background-overlay: rgba(22, 24, 29, .70);
}

body {
  position: relative;
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}

body::before {
  position: fixed;
  z-index: 0;
  inset: calc(-1 * var(--background-blur));
  display: block;
  content: "";
  background-image: var(--background-image);
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
  filter: blur(var(--background-blur));
  transform: scale(1.04);
  pointer-events: none;
}

body.has-background::after {
  position: fixed;
  z-index: 1;
  inset: 0;
  content: "";
  background: var(--background-overlay);
  pointer-events: none;
}

.page {
  position: relative;
  z-index: 2;
  max-width: 820px;
  margin: 0 auto;
  padding: 72px 24px 64px;
}

/* ---------- 时钟 ---------- */
.clock-block { text-align: center; margin-bottom: 40px; }

.clock {
  font-size: 56px;
  font-weight: 300;
  letter-spacing: 2px;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
}

.date {
  margin-top: 10px;
  font-size: 14px;
  color: var(--muted);
}

/* ---------- 搜索框 ---------- */
.search {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 6px 6px 8px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 999px;
  box-shadow: var(--shadow);
  transition: border-color .15s, box-shadow .15s;
}

.search:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-ring);
}

.engine-switch {
  position: relative;
  display: flex;
  gap: 2px;
  flex: none;
  padding: 2px;
  background: var(--bg);
  border-radius: 999px;
}

.engine-trigger,
.engine-option {
  display: grid;
  place-items: center;
  font: inherit;
  color: var(--muted);
  background: transparent;
  border: 0;
  cursor: pointer;
}

.engine-trigger {
  grid-template-columns: 16px 8px;
  gap: 3px;
  min-width: 35px;
  min-height: 28px;
  padding: 5px 7px;
  border-radius: 999px;
}

.engine-trigger:hover,
.engine-trigger[aria-expanded="true"] {
  color: var(--accent);
  background: var(--panel);
  box-shadow: 0 1px 2px rgba(16, 24, 40, .08);
}

.engine-trigger .iconfont,
.engine-option .iconfont { font-size: 16px; }

.engine-chevron {
  width: 6px;
  height: 6px;
  border-right: 1.5px solid currentColor;
  border-bottom: 1.5px solid currentColor;
  transform: rotate(45deg) translateY(-2px);
  transition: transform .15s;
}

.engine-trigger[aria-expanded="true"] .engine-chevron {
  transform: rotate(225deg) translate(-1px, -1px);
}

.engine-menu {
  position: absolute;
  top: calc(100% + 8px);
  left: 0;
  z-index: 5;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 150px;
  padding: 4px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow);
}

.engine-menu[hidden] { display: none; }

.engine-option {
  display: flex;
  justify-content: flex-start;
  gap: 9px;
  width: 100%;
  height: 30px;
  padding: 0 8px;
  border-radius: 7px;
}

.engine-option-label { font-size: 12px; }

.engine-option:hover,
.engine-option.active {
  color: var(--accent);
  background: var(--accent-soft);
}

/* 兼容已保存页面或旧版样式中使用的引擎按钮。 */
.engine-btn {
  padding: 5px 10px;
  font: inherit;
  font-size: 12px;
  color: var(--muted);
  background: transparent;
  border: 0;
  border-radius: 999px;
  cursor: pointer;
  transition: background .15s, color .15s;
}

.engine-btn:hover { color: var(--text); }

.engine-btn.active {
  color: var(--accent);
  font-weight: 600;
  background: var(--panel);
  box-shadow: 0 1px 2px rgba(16, 24, 40, .08);
}

.search-input {
  flex: 1;
  min-width: 0;
  padding: 8px 4px;
  font: inherit;
  font-size: 15px;
  color: var(--text);
  background: transparent;
  border: 0;
  outline: none;
}

.search-input::placeholder { color: var(--muted); }

.search-btn {
  flex: none;
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  color: #fff;
  background: var(--accent);
  border: 0;
  border-radius: 50%;
  cursor: pointer;
  transition: filter .15s;
}

.search-btn:hover { filter: brightness(1.08); }

/* ---------- 区块标题 ---------- */
.block { margin-top: 44px; }

.background-control {
  position: fixed;
  right: 24px;
  bottom: 24px;
  z-index: 3;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 10px;
}

.background-toggle {
  padding: 8px 14px;
  font: inherit;
  font-size: 12px;
  color: var(--muted);
  background: color-mix(in srgb, var(--panel) 88%, transparent);
  border: 1px solid var(--border);
  border-radius: 999px;
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: color .15s, border-color .15s, background .15s;
}

.background-toggle:hover {
  color: var(--accent);
  border-color: var(--accent-line);
  background: var(--panel);
}

.background-toggle.icon-button {
  min-width: 38px;
  min-height: 34px;
  padding: 8px 11px;
}

.background-settings {
  width: min(360px, calc(100vw - 32px));
  padding: 18px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
}

.background-title {
  margin: 0 0 18px;
  font-size: 16px;
  font-weight: 600;
}

.theme-settings {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 0 0 18px;
  padding: 0;
  border: 0;
}

.theme-settings legend {
  width: 100%;
  margin-bottom: 8px;
  padding: 0;
  font-size: 12px;
  color: var(--muted);
}

.theme-option {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 6px 9px;
  font-size: 12px;
  color: var(--muted);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
}

.theme-option:has(input:checked) {
  color: var(--accent);
  background: var(--accent-soft);
  border-color: var(--accent-line);
}

.theme-option input { margin: 0; accent-color: var(--accent); }
.theme-option-icon { font-size: 14px; }

.theme-follow-icon {
  width: 12px;
  height: 12px;
  border: 1.5px solid currentColor;
  border-radius: 50%;
  background: linear-gradient(90deg, currentColor 50%, transparent 50%);
}

.background-toggle[aria-expanded="true"] {
  font-size: 22px;
  font-weight: 300;
  line-height: 1;
  color: var(--muted);
}

.background-settings .field { margin-bottom: 16px; }

.background-url-field {
  display: flex;
  gap: 8px;
}

.background-url-field input { min-width: 0; }

.background-file-btn {
  flex: none;
  padding: 7px 10px;
  white-space: nowrap;
}

.background-file-status {
  min-height: 18px;
  margin: -8px 0 12px;
  font-size: 12px;
  line-height: 1.5;
  color: var(--muted);
}

.range-field { display: block; }

.range-field > span {
  display: flex;
  justify-content: space-between;
  margin-bottom: 8px;
  font-size: 12px;
  color: var(--muted);
}

.range-field output { color: var(--text); font-variant-numeric: tabular-nums; }

.range-field input { width: 100%; accent-color: var(--accent); }

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

.background-hint {
  margin: 14px 0 0;
  font-size: 12px;
  line-height: 1.6;
  color: var(--muted);
}

.background-hint code {
  padding: 1px 4px;
  color: var(--text);
  background: var(--bg);
  border-radius: 4px;
}

.background-credit {
  margin: 14px 0 0;
  font-size: 12px;
  color: var(--muted);
  text-align: center;
}

.background-credit a {
  color: var(--accent);
  text-decoration: none;
}

.background-credit a:hover { text-decoration: underline; }

.block-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}

.section-title {
  margin: 0 0 0 2px;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--muted);
}

.edit-toggle {
  padding: 4px 12px;
  font: inherit;
  font-size: 12px;
  color: var(--muted);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 999px;
  cursor: pointer;
  transition: color .15s, border-color .15s, background .15s;
}

.edit-toggle:hover {
  color: var(--accent);
  border-color: var(--accent-line);
  background: var(--accent-soft);
}

.edit-toggle.active {
  color: #fff;
  background: var(--accent);
  border-color: var(--accent);
}

.icon-button {
  display: inline-grid;
  place-items: center;
  min-width: 32px;
  min-height: 28px;
  padding: 4px 8px;
}

.icon-button .iconfont { font-size: 15px; }

/* ---------- 常用网站 ---------- */
.site-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(84px, 1fr));
  gap: 10px;
}

.site-card {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 14px 8px 12px;
  color: var(--text);
  text-decoration: none;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  transition: transform .15s, box-shadow .15s, border-color .15s;
}

.site-card:hover {
  transform: translateY(-2px);
  border-color: var(--accent-line);
  box-shadow: var(--shadow);
}

.site-icon {
  position: relative;
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  font-size: 15px;
  font-weight: 600;
  border-radius: 9px;
}

.site-favicon {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  padding: 2px;
  object-fit: contain;
  border-radius: inherit;
  background: transparent;
  opacity: 0;
  transition: opacity .15s;
}

.site-icon.has-favicon .site-favicon { opacity: 1; }
.site-icon.has-favicon .site-icon-fallback { visibility: hidden; }
.site-icon.has-favicon { background: transparent !important; }
.site-icon-fallback { border-radius: 50%; }

.site-name {
  max-width: 100%;
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.site-del {
  position: absolute;
  top: 2px;
  right: 4px;
  display: grid;
  place-items: center;
  width: 18px;
  height: 18px;
  font-size: 14px;
  line-height: 1;
  color: var(--muted);
  background: transparent;
  border: 0;
  border-radius: 50%;
  cursor: pointer;
  transition: background .15s, color .15s;
}

.site-del:hover {
  color: var(--danger);
  background: var(--danger-soft);
}

.site-add {
  font: inherit;
  color: var(--muted);
  background: transparent;
  border-style: dashed;
  cursor: pointer;
}

.site-add:hover { color: var(--accent); }

.site-add .site-icon {
  font-size: 18px;
  font-weight: 400;
  color: var(--muted);
  background: transparent !important;
  border: 1px dashed var(--border);
}

/* ---------- 调试入口 ---------- */
.debug-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.debug-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
}

.debug-ip {
  margin-right: 6px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px;
  font-weight: 600;
}

.port-btn {
  display: inline-flex;
  align-items: baseline;
  gap: 3px;
  padding: 4px 10px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
  color: var(--accent);
  text-decoration: none;
  background: var(--accent-soft);
  border: 1px solid var(--accent-line);
  border-radius: 7px;
  transition: background .15s, color .15s, border-color .15s;
}

.port-btn:hover {
  color: #fff;
  background: var(--accent);
  border-color: var(--accent);
}

.port-proto {
  display: inline-grid;
  place-items: center;
  width: 14px;
  height: 14px;
  opacity: .8;
}

.port-proto .iconfont {
  font-size: 14px;
  line-height: 1;
}

.port-name { font-size: 12px; }

.port-num { opacity: .82; }

/* ---------- 调试入口：编辑态 ---------- */
.debug-ip-input,
.port-proto-select,
.port-name-input,
.port-num-input {
  font: inherit;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 7px;
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}

.debug-ip-input:focus,
.port-proto-select:focus,
.port-name-input:focus,
.port-num-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-ring);
}

.debug-ip-input {
  width: 140px;
  padding: 6px 9px;
  font-weight: 600;
}

.port-editor {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 4px 3px 6px;
  background: var(--bg);
  border: 1px dashed var(--accent-line);
  border-radius: 8px;
}

.port-proto-select {
  padding: 5px 4px;
  cursor: pointer;
}

.port-name-input {
  width: 110px;
  padding: 5px 8px;
}

.port-num-input {
  width: 66px;
  padding: 5px 8px;
}

.port-del,
.del-row-btn,
.add-port-btn,
.add-row-btn {
  font: inherit;
  font-size: 12px;
  border-radius: 7px;
  cursor: pointer;
  transition: background .15s, color .15s, border-color .15s;
}

.port-del {
  display: grid;
  place-items: center;
  width: 22px;
  height: 22px;
  font-size: 14px;
  line-height: 1;
  color: var(--muted);
  background: transparent;
  border: 0;
}

.port-del:hover {
  color: var(--danger);
  background: var(--danger-soft);
}

.add-port-btn {
  padding: 5px 10px;
  color: var(--accent);
  background: var(--accent-soft);
  border: 1px dashed var(--accent-line);
}

.add-port-btn:hover {
  color: #fff;
  background: var(--accent);
  border-style: solid;
}

.del-row-btn {
  margin-left: auto;
  padding: 5px 10px;
  color: var(--muted);
  background: transparent;
  border: 1px solid var(--border);
}

.del-row-btn:hover {
  color: var(--danger);
  border-color: var(--danger);
  background: var(--danger-soft);
}

.add-row-btn {
  align-self: flex-start;
  padding: 8px 14px;
  color: var(--muted);
  background: transparent;
  border: 1px dashed var(--border);
}

.add-row-btn:hover {
  color: var(--accent);
  border-color: var(--accent-line);
  background: var(--accent-soft);
}

/* ---------- 弹窗 ---------- */
.modal-mask {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(15, 18, 23, .45);
}

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

.modal {
  width: 100%;
  max-width: 360px;
  padding: 22px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, .25);
}

.modal-title {
  margin: 0 0 16px;
  font-size: 15px;
}

.field {
  display: block;
  margin-bottom: 12px;
}

.field span {
  display: block;
  margin-bottom: 6px;
  font-size: 12px;
  color: var(--muted);
}

.field input {
  width: 100%;
  padding: 9px 11px;
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}

.field input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-ring);
}

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

.btn {
  padding: 8px 16px;
  font: inherit;
  font-size: 13px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  transition: filter .15s;
}

.btn:hover { filter: brightness(.97); }

.btn.primary {
  color: #fff;
  font-weight: 500;
  background: var(--accent);
  border-color: var(--accent);
}

.btn.primary:hover { filter: brightness(1.08); }

/* ---------- 小屏 ---------- */
@media (max-width: 520px) {
  .page { padding: 44px 16px 48px; }
  .background-control { right: 16px; bottom: 16px; }
  .clock { font-size: 42px; }
  .site-grid { grid-template-columns: repeat(auto-fill, minmax(74px, 1fr)); }
  .debug-row { padding: 10px; }
  .port-name-input { width: 84px; }
  .port-num-input { width: 58px; }
  .debug-ip-input { width: 116px; }
}
