  /* ===================================================================
     街机面板统一样式
     tct2p / tenkai / mxsqy / sdmg2 / cjddz 五套共用，逐字节一致。
     各页面的差别只有 <title> 和 <body class="lay-*">：

       lay-mj    麻将面板（投币/押注/开始 + 5 动作键 + A~N）
       lay-ddz   斗地主面板（左：投币+← / 中：S1S2S3 / 右：→+开始）
       lay-joy   Two-way joystick panel with three action buttons
       lay-bar   单排面板（tct2p 的 6 个键）

     约定：投币永远在最左，开始永远在最右。
     改尺寸只动下面 :root 里那几个变量。
     =================================================================== */
  :root {
    --bg: #0d0f13; --panel: #171a21; --line: #262b36;
    --fg: #e6e8ee; --dim: #8b93a7; --accent: #6ea8fe;

    --keyH:  44px;   /* 按钮统一高度 */
    --keyR:  10px;   /* 统一圆角 */
    --keyFS: 15px;   /* 统一字号 */
    --gap:    6px;   /* 按钮间距 */

    --sideW: 84px;   /* 横屏侧边栏宽度 */
    --barH:  46px;   /* 横屏底栏按钮高度 */
  }
  * { box-sizing: border-box; }

  html, body {
    margin: 0;
    height: 100%;
    height: 100dvh;                  /* 手机地址栏收放时 100% 会跳，dvh 才跟手 */
    background: var(--bg); color: var(--fg);
    font: 14px/1.5 -apple-system, "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
    overflow: hidden;
    overscroll-behavior: none;       /* 关掉下拉刷新 / 橡皮筋 */
    -webkit-text-size-adjust: 100%;
  }
  body { display: flex; flex-direction: column; }

  main {
    flex: 1; min-height: 0;
    display: flex; align-items: center; justify-content: center;
    container-type: size;
  }

  /* 画面自适应：锁定 4:3，在可用空间内尽可能大。
     宽度取「可用宽度」和「可用高度换算出的宽度」中较小的一个，两个方向都不溢出。
     不能写 height:100% + aspect-ratio —— 窗口窄高时 height 会压过比例。

     这里写 4:3 不是笔误。这几台的帧缓冲是 512x240 / 448x224 之类的扁长尺寸，
     但**不能按帧缓冲比例摆 iframe**：MAME 的 -keepaspect 用的是显示器的物理
     宽高比，不是像素分辨率。横屏街机在驱动里就是普通 4:3 CRT，那些像素会被
     纵向拉伸填满 4:3 —— 真机上本来就长这样。按帧缓冲比例摆的话，MAME 会在
     里面再 letterbox 一次，左右两条大黑边、画面白白缩小一圈。 */
  #screen {
    display: block;
    aspect-ratio: 4 / 3;
    width: min(100cqw, calc(100cqh * 4 / 3));
    height: auto;
    border: 1px solid var(--line);
    background: #000;
  }
  @supports not (width: 1cqw) {
    #screen { width: auto; height: 100%; max-width: 100%; }
  }

  /* ------------------------------------------------------------- 面板 */
  footer {
    flex: none; width: 100%; background: var(--panel);
    border-top: 1px solid var(--line);
    color: var(--dim); text-align: center;
    padding: 10px 12px calc(10px + env(safe-area-inset-bottom, 0px));
    padding-left:  calc(12px + env(safe-area-inset-left, 0px));
    padding-right: calc(12px + env(safe-area-inset-right, 0px));
  }
  #pad { display: flex; flex-direction: column; gap: var(--gap); align-items: center; width: 100%; }
  .row { display: flex; flex-wrap: wrap; gap: var(--gap); justify-content: center; }
  .cluster { display: flex; align-items: center; justify-content: center; gap: 20px; }

  .key {
    font: inherit; color: var(--fg);
    background: #222734; border: 1px solid var(--line); border-radius: var(--keyR);
    height: var(--keyH); min-width: 72px; padding: 0 14px;
    display: flex; align-items: center; justify-content: center; gap: 8px;
    cursor: pointer;
    user-select: none; -webkit-user-select: none;
    touch-action: manipulation;                 /* 禁双击缩放，消除点击延迟 */
    -webkit-tap-highlight-color: transparent;   /* 去掉安卓蓝色高亮方块 */
    -webkit-touch-callout: none;                /* 长按不弹系统菜单 */
    transition: background .08s, border-color .08s, transform .08s;
  }
  .key b { font-weight: 600; font-size: var(--keyFS); letter-spacing: .5px; }
  .key span {
    color: var(--dim); font-size: 12px;
    border: 1px solid var(--line); border-radius: 5px; padding: 0 5px;
  }
  .key.hi { border-color: var(--accent); }      /* 只有 投币 / 开始 带高亮轮廓 */
  .key.down {
    background: var(--accent); color: #0d0f13; border-color: var(--accent);
    transform: translateY(1px);
  }
  .key.down span { color: #0d0f13; border-color: rgba(13, 15, 19, .35); }
  /* 只给真有指针的设备做 hover，触屏上 :hover 会粘住 */
  @media (hover: hover) { .key:hover { border-color: var(--accent); } }
  @media (hover: none)  { .key span { display: none; } }   /* 键帽标签在手机上没意义 */

  /* 一键多用：主标签一行，次要用途一行。
     斗地主的 S1/S2/S3 在不同画面是不同功能，一个标签说不清。
     次要行用 <i> 不用 <span> —— span 在触屏上是隐藏的（那是键盘提示），
     这行是真功能说明，手机上必须看得见。 */
  .key.multi   { flex-direction: column; gap: 1px; line-height: 1.15; }
  .key.multi b { display: flex; align-items: center; gap: 6px; font-size: 14px; }
  .key.multi i {
    font-style: normal; font-weight: 400; font-size: 11px;
    color: var(--dim); letter-spacing: 0;
  }
  .key.multi.down i { color: rgba(13, 15, 19, .72); }

  .key.t   { min-width: 0; padding: 0; }                   /* A~N 选牌键，宽度交给栅格 */
  .key.t b { font-family: ui-monospace, monospace; font-size: 19px; }
  .key.dir   { min-width: 88px; }                          /* 方向键 */
  .key.dir b { font-size: 26px; }

  /* Keep the three top-row controls on one line. */
  .row.top {
    display: grid; grid-template-columns: repeat(3, minmax(0, 96px));
    justify-content: center; width: 100%;
  }
  .row.top .key { min-width: 0; padding: 0; }

  /* A~N：宽屏 14 个一行；放不下切成 7 列两行。列数写死，永远不会有第三行 */
  .row.tiles {
    display: grid; grid-template-columns: repeat(7, minmax(0, 66px));
    justify-content: center; width: 100%;
  }
  @media (min-width: 1040px) {
    .row.tiles { grid-template-columns: repeat(14, minmax(0, 66px)); }
  }

  /* ================================================== 竖屏手机 */
  @media (max-width: 720px) {
    footer { padding-top: 8px; }
    #pad { gap: 8px; }

    .lay-mj .row.acts { display: grid; grid-template-columns: repeat(5, 1fr); width: 100%; }
    .lay-mj .row.acts .key { min-width: 0; padding: 0; }

    .lay-bar .row.bar { display: grid; grid-template-columns: repeat(3, 1fr); width: 100%; gap: 8px; }
    .lay-bar .row.bar .key { min-width: 0; padding: 0; }

    /* Two-way panels: side controls above three action buttons. */
    .cluster {
      display: grid; width: 100%; gap: 8px;
      grid-template-columns: 1fr 1fr;
      grid-template-areas: "left right"
                           "mid  mid";
    }
    .cluster .row.left  { grid-area: left; }
    .cluster .row.right { grid-area: right; }
    .cluster .row.mid   { grid-area: mid; grid-template-columns: repeat(3, 1fr); }
    .cluster .row.left, .cluster .row.right {
      display: grid; grid-template-columns: repeat(2, 1fr);
    }
    .cluster .row { gap: 8px; }
    .cluster .key { min-width: 0; padding: 0; }
  }

  /* ================================================== 横屏手机：街机布局
     左边一竖排 / 中间画面 / 右边一竖排 / 底部一条。
     只在「触屏 + 横屏 + 矮屏」三个条件同时满足时生效，竖屏和桌面不受影响。 */
  @media (orientation: landscape) and (max-height: 520px) and (hover: none) {

    body { display: block; }
    footer { position: static; background: none; border: 0; padding: 0; }
    #pad, .cluster { display: contents; }

    /* main 上的 container-type: size 还在，#screen 照样按 4:3 自适应 */
    main {
      position: fixed; top: 0;
      bottom: calc(var(--barH) + 10px + env(safe-area-inset-bottom, 0px));
      left: 0; right: 0;
    }
    .lay-mj main, .lay-ddz main, .lay-joy main {
      left:  calc(var(--sideW) + env(safe-area-inset-left, 0px));
      right: calc(var(--sideW) + env(safe-area-inset-right, 0px));
    }

    /* 两条侧边栏：按钮等分高度。
       注意 width 必须**把安全区加进去** —— 全局是 border-box，width 含 padding，
       而刘海屏横屏时 safe-area-inset-left 有 44~59px，
       写成 width:84px 的话内容只剩 84-47-8≈29px，按钮被挤成一条，中文标签竖着摞。
       加进去之后内容宽恒定 84-8=76px，而且背景铺到屏幕边缘，和 main 的留白严丝合缝。 */
    .lay-mj .row.top, .lay-mj .row.acts,
    .cluster .row.left, .cluster .row.right {
      position: fixed; top: 0; bottom: 0; z-index: 2;
      display: flex; flex-direction: column;
      justify-content: center; gap: 5px;
      padding: 4px;
      background: var(--panel);
    }
    .lay-mj .row.top, .cluster .row.left {
      left: 0;
      width: calc(var(--sideW) + env(safe-area-inset-left, 0px));
      padding-left: calc(4px + env(safe-area-inset-left, 0px));
      border-right: 1px solid var(--line);
    }
    .lay-mj .row.acts, .cluster .row.right {
      right: 0;
      width: calc(var(--sideW) + env(safe-area-inset-right, 0px));
      padding-right: calc(4px + env(safe-area-inset-right, 0px));
      border-left: 1px solid var(--line);
    }
    /* 右侧那组 DOM 顺序是「方向键, 开始」（横排时开始在最右），
       竖过来要反过来，让方向键落在底部拇指位 */
    .cluster .row.right { flex-direction: column-reverse; }

    .lay-mj .row.top .key, .lay-mj .row.acts .key,
    .cluster .row.left .key, .cluster .row.right .key {
      flex: 1; min-width: 0; height: auto; padding: 0;
    }
    .lay-mj .row.top .key b, .lay-mj .row.acts .key b,
    .cluster .row.left .key b, .cluster .row.right .key b { font-size: 13px; }
    .cluster .key.dir b { font-size: 24px; }
    .cluster .row.left .key.multi b, .cluster .row.right .key.multi b { font-size: 12px; }
    .cluster .row.left .key.multi i, .cluster .row.right .key.multi i { font-size: 10px; }

    /* 底栏：A~N / S1S2S3 / tct2p 的 6 个键，都是一条，永不换行 */
    .row.tiles, .row.mid, .row.bar {
      position: fixed; bottom: 0; left: 0; right: 0; z-index: 3; width: auto;
      display: grid; gap: 4px;
      padding: 5px 4px calc(5px + env(safe-area-inset-bottom, 0px)) 4px;
      background: var(--panel);
      border-top: 1px solid var(--line);
    }
    .lay-mj .row.tiles, .cluster .row.mid {
      left:  calc(var(--sideW) + env(safe-area-inset-left, 0px));
      right: calc(var(--sideW) + env(safe-area-inset-right, 0px));
    }
    .row.tiles { grid-template-columns: repeat(14, 1fr); }
    .row.mid   { grid-template-columns: repeat(3, 1fr); }
    .row.bar   { grid-template-columns: repeat(6, 1fr); }

    .row.tiles .key, .row.mid .key, .row.bar .key {
      min-width: 0; height: var(--barH); padding: 0;
    }
    .row.tiles .key b { font-size: 15px; }
    .row.mid   .key b { font-size: 15px; }
    .row.bar   .key b { font-size: 14px; }
    .row.mid .key.multi b { font-size: 13px; }
    .row.mid .key.multi i { font-size: 10px; }
  }

  /* ------------------------------------------------------------ 游戏列表
     不带 ?game= 时显示这个，而不是默认进某一个游戏。 */
  #menu {
    flex: 1; min-height: 0; overflow-y: auto;
    padding: 28px 16px calc(28px + env(safe-area-inset-bottom, 0px));
    -webkit-overflow-scrolling: touch;
  }
  #menu h1 {
    margin: 0 auto 20px; max-width: 900px;
    font-size: 17px; font-weight: 600; letter-spacing: .5px;
  }
  #menu .list {
    display: grid; gap: 10px; max-width: 900px; margin: 0 auto;
    grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
  }
  #menu a {
    display: block; text-decoration: none; color: var(--fg);
    background: #222734; border: 1px solid var(--line); border-radius: var(--keyR);
    padding: 14px 16px;
    -webkit-tap-highlight-color: transparent;
    transition: border-color .08s, background .08s;
  }
  #menu a:active { background: var(--accent); color: #0d0f13; border-color: var(--accent); }
  #menu a b { display: block; font-size: 15px; font-weight: 600; }
  #menu a em {
    display: block; margin-top: 3px; font-style: normal;
    font-family: ui-monospace, monospace; font-size: 12px; color: var(--dim);
  }
  #menu a:active em { color: rgba(13, 15, 19, .72); }
  @media (hover: hover) { #menu a:hover { border-color: var(--accent); } }
