/* ============================================================
   IP / 子网掩码计算器 —— 响应式皮肤 v2
   ------------------------------------------------------------
   · 表单结构、字段名、onclick 绑定全部沿用原版，只重做外观
   · 分组换行由 layout.js 包出的 .grp 控制：
     一组输入框要么整组留下、要么整组换行，绝不从中间断开
   ============================================================ */

:root {
  --maxw: 1420px;
  --pad:  22px;

  --bg:        #f2f5f9;
  --card:      #ffffff;
  --text:      #16202e;
  --muted:     #5c6b7e;
  --faint:     #8a99aa;
  --border:    #dde4ec;
  --border-2:  #eef2f6;

  --accent:      #0b6f8a;
  --accent-2:    #0891b2;
  --accent-soft: #e3f4fa;
  --field:       #ffffff;
  --field-ro:    #f6f9fc;

  --nav-bg:  rgba(242, 245, 249, .84);
  --shadow:  0 1px 2px rgba(16, 24, 40, .04), 0 6px 16px rgba(16, 24, 40, .05);
  --shadow-h:0 4px 10px rgba(16, 24, 40, .06), 0 16px 34px rgba(16, 24, 40, .10);
  --radius:  14px;

  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg:        #0a111d;
    --card:      #111b2b;
    --text:      #e7eef9;
    --muted:     #9aabc2;
    --faint:     #77879c;
    --border:    #24334a;
    --border-2:  #1b2740;

    --accent:      #4dd6f0;
    --accent-2:    #22b8d8;
    --accent-soft: #14344a;
    --field:       #0d1729;
    --field-ro:    #16233a;

    --nav-bg:  rgba(10, 17, 29, .86);
    --shadow:  0 1px 2px rgba(0, 0, 0, .35);
    --shadow-h:0 8px 26px rgba(0, 0, 0, .5);
  }
}

* { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  scroll-behavior: smooth;
}

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

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.wrap {
  max-width: var(--maxw);
  margin: 0 auto;
  padding-left: var(--pad);
  padding-right: var(--pad);
}

/* =================== 页头 =================== */

.masthead {
  background: linear-gradient(180deg, var(--card) 0%, var(--bg) 100%);
  border-bottom: 1px solid var(--border);
}

.masthead-in {
  display: flex;
  align-items: center;
  gap: 16px;
  padding-top: 26px;
  padding-bottom: 24px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 0;
  color: inherit;
}
.brand:hover { text-decoration: none; }

.brand-mark {
  flex: 0 0 auto;
  width: 46px;
  height: 46px;
  border-radius: 13px;
  background: linear-gradient(140deg, var(--accent-2), var(--accent));
  color: #fff;
  font-family: var(--mono);
  font-size: 15px;
  font-weight: 700;
  letter-spacing: .5px;
  line-height: 46px;
  text-align: center;
  box-shadow: 0 8px 18px -8px var(--accent-2);
}

.brand-txt {
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 0;
  margin: 0;
}
.brand-txt .t1 {
  font-size: 22px;
  font-weight: 650;
  letter-spacing: -.01em;
  line-height: 1.3;
}
.brand-txt .t2 {
  font-size: 12.5px;
  font-weight: 400;
  color: var(--muted);
  line-height: 1.5;
}

.masthead-act { margin-left: auto; flex: 0 0 auto; }

.btn-ghost {
  display: inline-block;
  padding: 7px 13px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--card);
  color: var(--muted);
  font-size: 12.5px;
  white-space: nowrap;
  transition: color .15s ease, border-color .15s ease, transform .15s ease;
}
.btn-ghost:hover {
  color: var(--accent);
  border-color: var(--accent-2);
  text-decoration: none;
  transform: translateX(-2px);
}

/* =================== 顶部工具索引条（吸顶） =================== */

.nav {
  position: sticky;
  top: 0;
  z-index: 40;
  background: var(--nav-bg);
  border-bottom: 1px solid var(--border);
  -webkit-backdrop-filter: saturate(160%) blur(14px);
          backdrop-filter: saturate(160%) blur(14px);
}

/* 两侧淡出遮罩：还有内容可滚的时候才亮起 */
.nav::before,
.nav::after {
  content: '';
  position: absolute;
  top: 0;
  bottom: 0;
  width: 36px;
  z-index: 1;
  pointer-events: none;
  opacity: 0;
  transition: opacity .18s ease;
}
.nav::before { left: 0;  background: linear-gradient(90deg, var(--nav-bg), transparent); }
.nav::after  { right: 0; background: linear-gradient(270deg, var(--nav-bg), transparent); }
.nav.can-left::before,
.nav.can-right::after { opacity: 1; }

.nav-in {
  display: flex;
  align-items: center;
  gap: 5px;
  padding-top: 9px;
  padding-bottom: 9px;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
  -ms-overflow-style: none;
}
.nav-in::-webkit-scrollbar { display: none; }

.nav a {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  padding: 5px 9px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--card);
  color: var(--muted);
  font-size: 12.5px;
  line-height: 1.45;
  white-space: nowrap;
  transition: color .15s ease, border-color .15s ease, transform .15s ease;
}
.nav a:hover {
  color: var(--accent);
  border-color: var(--accent-2);
  text-decoration: none;
  transform: translateY(-1px);
}

/* =================== 出处条 =================== */

.credit-row { padding-top: 15px; }

.credit {
  margin: 0;
  color: var(--faint);
  font-size: 12px;
  line-height: 1.7;
}
.credit code {
  padding: 1px 5px;
  border-radius: 4px;
  background: var(--border-2);
  color: var(--accent);
  font-family: var(--mono);
  font-size: 11.5px;
}

/* =================== 卡片瀑布流 =================== */

#main {
  max-width: var(--maxw);
  margin: 0 auto;
  padding-left: var(--pad);
  padding-right: var(--pad);
}

#main > ul {
  margin: 15px 0 0;
  padding: 0;
  list-style: none;
  columns: 3 400px;
  column-gap: 18px;
}

#main > ul > li {
  break-inside: avoid;
  -webkit-column-break-inside: avoid;
  page-break-inside: avoid;
  margin: 0 0 18px;
  padding: 16px 18px 18px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  scroll-margin-top: 70px;
  transition: box-shadow .18s ease, border-color .18s ease, transform .18s ease;
}

#main > ul > li:hover {
  box-shadow: var(--shadow-h);
  border-color: var(--accent-2);
  transform: translateY(-2px);
}

/* =================== 卡片内部 =================== */

#main h2 {
  position: relative;
  margin: 0 0 13px;
  padding: 0 0 9px 11px;
  border-bottom: 1px solid var(--border-2);
  color: var(--accent);
  font-size: 15px;
  font-weight: 650;
  line-height: 1.45;
}
#main h2::before {
  content: '';
  position: absolute;
  left: 0;
  top: 2px;
  bottom: 9px;
  width: 3px;
  border-radius: 2px;
  background: linear-gradient(180deg, var(--accent-2), var(--accent));
}

#main b {
  color: var(--muted);
  font-weight: 600;
  font-size: 13px;
}

/* 原始 table 写死了 width="500"/"660"，一律拉满。
   ⚠️ 必须排除 .lb（标签格）：它的宽度由 layout.js 用 inline width 写死，
   而作者样式表里的 !important 是能压过 inline style 的。 */
#main table {
  width: 100% !important;
  border-collapse: collapse;
  table-layout: auto;
}

/* 通用：所有单元格共有的排版属性放这里，
   width 单独拆出去（见下），因为标签格的宽度要由 inline style 控制。 */
#main td {
  padding: 3px 0;
  vertical-align: middle;
  font-size: 13px;
  color: var(--text);
}

/* ⚠️ 两个 width 的坑（踩过一次，别再合并）：
   ① HTML 里很多格带 width="170"/"250" 属性，那是权重 0 的呈现性提示，
      必须被一条普通规则压掉，否则会盖住 JS 量的"最宽标签"；
   ② 作者样式表里的 !important 能压过 inline style —— 所以标签格
      绝不能被 `width: auto !important` 命中，否则 layout.js 写的宽度全废。 */
#main td:not(.lb) { width: auto !important; }
#main td.lb { width: auto; }

#main td[align="right"] {
  text-align: left;
  color: var(--muted);
}

/* 标签列不再用 nowrap 兜断字：宽度由 layout.js 定死（≥ 自然宽），
   本来就不会折行；nowrap 反而会在极端窄视口下把字顶出格子。 */

/* =================== 输入区：分组换行 =================== */

/* 原始表格行不会换行，窄卡片下单元格只会横向溢出。
   layout.js 把"含输入框的行"标成 .fr，改成 flex 行：
   一行放不下时，放不下的整格（整组输入 / 整组按钮）整体下移。 */
#main tr.fr {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

#main tr.fr > td {
  display: block;
  flex: 0 1 auto;
  min-width: 0;
}

/* 整行只有一个格子（colspan 标题行 / 标签与输入同格）→ 占满整宽 */
#main tr.fr > td:only-child { flex: 1 1 100%; }

/* 控件格：能收缩，但不会缩到"最小的一组控件"以下 ——
   4 个八位组要么整组完整显示，要么整块换行，绝不挤成一团 */
#main tr.fr > td.f { min-width: auto; }

/* 空壳格：原站的 &nbsp; 占位格、以及按钮被搬走后剩下的按钮格。
   不收掉的话每格连间距要白占一二十像素，把按钮往右推。 */
#main tr.fr > td.void { display: none; }

/* 兜底：极窄视口连"整组输入框不许压缩"都放开（layout.js 判定溢出后加 .tight） */
#main.tight td.f > .grp { min-width: 0; }
#main.tight td input[size="8"] { min-width: 3.4em; }

/* 标签格（"掩码:" / "第一个可用:" …）：宽度由 layout.js 按卡片量出最宽标签后
   写进 inline width，右对齐 → 冒号落在同一条竖线上。
   flex 0 0 auto：标签绝不参与收缩，行里放不下时让右边的控件整块换行，
   而不是把标签挤扁（挤扁会让冒号重新参差）。 */
#main tr.fr > td.lb {
  flex: 0 0 auto;
  max-width: 100%;
  text-align: right;
}

#main td.f {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 7px;
}

/* layout.js 包出的组：整组同进同退，绝不从中间断开。
   这里**不设** min-width:0 —— 组的自动最小尺寸就取内容宽度，
   td.f 因此不会把一组输入框压扁（4 个 size=8 的二进制框合计 262px 是硬底线），
   放不下时由整格换行解决；真的要压（320px 以下的极端视口）
   才由 .tight 兜底放开。 */
#main td.f > .grp {
  display: inline-flex;
  flex-wrap: nowrap;
  align-items: center;
  gap: 4px;
  flex: 0 1 auto;
}
/* 按钮组（由 layout.js 从按钮格搬进来）。跟输入组之间只靠 td.f 的 gap
   分隔就行，不再额外加 margin —— 卡片窄的时候每一像素都要省。 */
#main td.f > .grp-b { gap: 6px; }

/* =================== 表单控件 =================== */

#main input[type="text"],
#main input:not([type]) {
  min-width: 3em;
  max-width: 100%;
  margin: 0;
  padding: 5px 5px;
  background: var(--field);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 7px;
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.4;
  text-align: center;
  transition: border-color .15s ease, box-shadow .15s ease, background .15s ease;
}

#main input[type="text"]:focus,
#main input:not([type]):focus {
  outline: none;
  border-color: var(--accent-2);
  background: var(--card);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent-2) 20%, transparent);
}

/* 只读字段 = 计算结果，一眼要能区分 */
#main input[readonly] {
  background: var(--field-ro);
  border-style: dashed;
  border-color: var(--border);
  color: var(--accent);
  font-weight: 600;
  cursor: default;
}

#main input[type="button"] {
  margin: 0;
  padding: 6px 13px;
  border: 0;
  border-radius: 8px;
  background: linear-gradient(180deg, var(--accent-2), var(--accent));
  color: #fff;
  font-family: inherit;
  font-size: 12.5px;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
  box-shadow: 0 2px 6px -2px color-mix(in srgb, var(--accent-2) 70%, transparent);
  transition: filter .15s ease, transform .1s ease;
}
#main input[type="button"]:hover  { filter: brightness(1.1); }
#main input[type="button"]:active { transform: scale(.97); }

#main select {
  margin: 0;
  padding: 5px 8px;
  max-width: 100%;
  background: var(--field);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 7px;
  font-family: inherit;
  font-size: 13px;
}

/* size=3 的八位组框（192.168.1.1 那种）：
   原站固有宽约 42px，4 个连排就吃掉 180px，是"标签 + 4 框 + 按钮"
   这一行放不放得下的关键。收窄到 2.9em 并少留一点左右内边距，
   3 位数字仍然宽松放得下，整行的宽度余量却多出约 11px ——
   很多行就是从"差 2px 换行"变成"恰好一行"。 */
#main td input[size="3"] {
  width: 2.9em;
  min-width: 2.9em;
  padding-left: 3px;
  padding-right: 3px;
}

/* size=8 的二进制字段（bits_1~4 / bin1b~4b）：
   原站按 8 字符定宽，一行 4 个合计 262px，是整页最宽的一组。
   min-width 与 width 一致 = 这一组不许被压扁（8 位二进制要完整看得见），
   放不下就让整块（连同后面的按钮）换行。 */
#main td input[size="8"] {
  width: 5.2em;
  min-width: 5.2em;
  padding-left: 3px;
  padding-right: 3px;
  font-size: 12px;
}

/* =================== 页脚 =================== */

.page-foot {
  margin-top: 32px;
  padding-top: 30px;
  padding-bottom: 26px;
  background: var(--card);
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 12.5px;
}

.foot-grid {
  display: grid;
  grid-template-columns: 1.8fr 1fr;
  gap: 30px;
}

.foot-block h2 {
  margin: 0 0 10px;
  color: var(--text);
  font-size: 13px;
  font-weight: 650;
  letter-spacing: .02em;
}
.foot-block p {
  margin: 0;
  line-height: 1.9;
}
.foot-block code {
  padding: 1px 5px;
  border-radius: 4px;
  background: var(--border-2);
  color: var(--accent);
  font-family: var(--mono);
  font-size: 11.5px;
}

.foot-links {
  display: flex;
  flex-direction: column;
  gap: 7px;
  margin: 0;
  padding: 0;
  list-style: none;
}
.foot-links a { color: var(--accent); }

.foot-bar {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 8px 16px;
  margin-top: 22px;
  padding-top: 16px;
  border-top: 1px solid var(--border-2);
  color: var(--faint);
  font-size: 11.5px;
}

/* =================== 回到顶部 =================== */

.to-top {
  position: fixed;
  right: 20px;
  bottom: 22px;
  z-index: 50;
  padding: 9px 15px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--card);
  color: var(--muted);
  font-family: inherit;
  font-size: 12.5px;
  line-height: 1;
  cursor: pointer;
  box-shadow: var(--shadow);
  opacity: 0;
  transform: translateY(10px);
  pointer-events: none;
  transition: opacity .2s ease, transform .2s ease, color .15s ease, border-color .15s ease;
}
.to-top.on { opacity: 1; transform: none; pointer-events: auto; }
.to-top:hover { color: var(--accent); border-color: var(--accent-2); }

/* =================== 响应式 =================== */

@media (max-width: 1240px) {
  #main > ul { columns: 2 360px; }
}

@media (max-width: 760px) {
  :root { --pad: 15px; }

  /* 单列时不要用多列（multicol）排版：多列容器在窄屏下会把列宽
     撑到内容的 min-content（bits_1~4 那组 4 个 size=8 输入框约 332px），
     直接横向溢出。改成块级后卡片宽度 = 容器宽度，组内靠 flex 收缩。 */
  #main > ul { display: block; columns: auto; }
  #main > ul > li { padding: 15px 15px 17px; }

  .masthead-in { gap: 12px; padding-top: 18px; padding-bottom: 16px; }
  .brand { gap: 11px; }
  .brand-mark { width: 38px; height: 38px; border-radius: 11px; font-size: 13px; line-height: 38px; }
  .brand-txt .t1 { font-size: 17px; }
  .brand-txt .t2 { font-size: 11.5px; }
  .btn-ghost { padding: 6px 11px; font-size: 12px; }

  .nav-in { padding-top: 8px; padding-bottom: 8px; }

  #main td { font-size: 13.5px; }
  #main td.lb { font-size: 13.5px; }
  #main h2 { font-size: 14.5px; }
  /* 手机上控件放大，方便点按（按钮横向只放到 12px：再宽就会把
     "标签 + 4 个输入框 + 按钮"这种整行顶到下一行去） */
  #main input[type="text"],
  #main input:not([type]) { padding: 7px 5px; font-size: 13.5px; }
  #main input[type="button"] { padding: 8px 12px; font-size: 13px; }
  #main select { padding: 7px 8px; }

  .foot-grid { grid-template-columns: 1fr; gap: 20px; }
  .page-foot { padding-top: 24px; }
  .to-top { right: 14px; bottom: 16px; }
}

@media (max-width: 460px) {
  .brand-txt .t2 { display: none; }
  .masthead-act .btn-ghost { padding: 6px 10px; }
}

/* 超窄屏再收一档内边距，保证最宽的那组输入框放得下 */
@media (max-width: 380px) {
  :root { --pad: 12px; }
  #main > ul > li { padding: 12px; }
  /* 5.2em 跟着字号缩：4 个二进制框从 262px 收到 241px，8 位仍然完整 */
  #main td input[size="8"] { font-size: 11px; }
}

/* =================== 打印 =================== */

@media print {
  body { background: #fff; }
  .nav, .to-top, .masthead-act { display: none; }
  .nav::before, .nav::after { display: none; }
  #main > ul { columns: 1; }
  #main > ul > li { break-inside: avoid; box-shadow: none; border-color: #ccc; }
  .page-foot { border-top: 1px solid #ccc; }
}
