/* Mr.麻將 網頁版 —— 視覺語言取自甲方 Logo：黑線稿、art-deco 直角框、襯線小標。 */

:root {
  --paper: #f4efe3;
  --paper-dim: #e6ddc9;
  --ink: #14120f;
  --ink-soft: #4a443a;
  --felt: #1d5c46;
  --felt-dark: #123626;
  --gold: #c9a961;
  --wood: #1b1714;

  /* 桌面的保底值；真正在用的是 .felt 裡那組跟著牌桌盒子縮放的容器查詢單位 */
  --t-lg: clamp(38px, 4.4vw, 58px);
  --t-md: clamp(20px, 2.3vw, 30px);
  --t-sm: clamp(17px, 2vw, 26px);

  --disc-cols: 8;
  --panel-w: 336px;
  --serif: "BiauKai", "標楷體", "Kaiti TC", "Songti TC", serif;
}

* { box-sizing: border-box; }

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

body {
  /* dvh：iOS Safari 的網址列縮起來時，100% 會多出一截把牌桌頂出畫面 */
  min-height: 100dvh;
  height: 100dvh;
  background: var(--wood);
  color: var(--ink);
  font-family: var(--serif);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}

#app {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

/* ---- 頂列 --------------------------------------------------------------- */

.topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 8px 20px;
  background: var(--paper);
  border-bottom: 3px double var(--ink);
}

.brand {
  height: 52px;
  width: auto;
}

.topbar-meta {
  display: flex;
  gap: 20px;
  margin-left: auto;
  font-size: 15px;
  letter-spacing: 0.08em;
}

.meta-item b {
  display: block;
  font-size: 10px;
  letter-spacing: 0.34em;
  color: var(--ink-soft);
  font-weight: 400;
}

.topbar-actions { display: flex; gap: 10px; }

.topbar button,
.panel button {
  font-family: var(--serif);
  font-size: 14px;
  letter-spacing: 0.12em;
  padding: 7px 14px;
  color: var(--ink);
  background: transparent;
  border: 1px solid var(--ink);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

@media (hover: hover) {
  .topbar button:hover,
  .panel button:hover { background: var(--ink); color: var(--paper); }
}

.topbar button, .panel button, .act, .preset, .gtile { touch-action: manipulation; }

/* ---- 版面 --------------------------------------------------------------- */

.stage {
  flex: 1 1 auto;
  display: grid;
  grid-template-columns: 1fr 0;
  min-height: 0;
  transition: grid-template-columns 0.2s ease;
}

body.panel-open .stage { grid-template-columns: 1fr var(--panel-w); }

.table {
  position: relative;
  min-width: 0;
  min-height: 0;
  padding: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  /* 牌桌的尺寸由外層 grid 決定，牌就照這個盒子縮 —— 不是照 viewport。
     手機直握時 viewport 很高但牌桌很窄，用 vw/vh 會算出離譜的大小。 */
  container-type: size;
}

/* ---- 牌桌 --------------------------------------------------------------- */

.felt {
  /* 兩軸都限一次：寬的螢幕不會把牌撐到爆高，矮的螢幕不會把牌撐到爆寬 */
  --t-lg: clamp(22px, min(4.4cqw, 7.4cqh), 58px);
  --t-md: clamp(13px, min(2.3cqw, 3.9cqh), 30px);
  --t-sm: clamp(11px, min(2cqw, 3.4cqh), 26px);

  position: relative;
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: minmax(90px, auto) 1fr minmax(90px, auto);
  grid-template-rows: minmax(70px, auto) 1fr minmax(70px, auto);
  /* 上下家橫跨整排 —— 左右家轉了 90 度，版面盒子還是「橫的」，
     若讓它們跟中央同排分欄，會把中間擠掉 400 多 px。 */
  grid-template-areas:
    "top  top     top"
    "left center  right"
    "bottom bottom bottom";
  padding: 12px 12px 20px;
  background:
    radial-gradient(ellipse at 50% 42%, #27735699 0%, transparent 62%),
    linear-gradient(160deg, var(--felt) 0%, var(--felt-dark) 100%);
  border: 1px solid #ffffff1f;
  box-shadow: inset 0 0 90px #00000066, 0 18px 50px #0009;
}

/* Logo 上那組 art-deco 直角括號，四個角各一 */
.frame i {
  position: absolute;
  width: 46px;
  height: 46px;
  border: 2px solid #ffffff33;
  pointer-events: none;
}
.frame i:nth-child(1) { top: 10px; left: 10px; border-right: 0; border-bottom: 0; }
.frame i:nth-child(2) { top: 10px; right: 10px; border-left: 0; border-bottom: 0; }
.frame i:nth-child(3) { bottom: 10px; left: 10px; border-right: 0; border-top: 0; }
.frame i:nth-child(4) { bottom: 10px; right: 10px; border-left: 0; border-top: 0; }

/* ---- 動畫層 ------------------------------------------------------------- */

.anim-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 5;
}

.anim-tile {
  position: absolute;
  top: 0;
  left: 0;
  will-change: transform;
}

.anim-tile .tile { width: 100%; }

/* 開場動畫進行中，牌桌本體先讓位 */
.felt.is-dealing .seat,
.felt.is-dealing .center { opacity: 0; }

.felt .seat,
.felt .center { transition: opacity 0.35s ease; }

/* ---- 座位 --------------------------------------------------------------- */

.seat {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  overflow: visible;
}

.seat-bottom { grid-area: bottom; }
.seat-top { grid-area: top; transform: rotate(180deg); }
.seat-left { grid-area: left; }
.seat-right { grid-area: right; }

/* 左右家整塊轉 90 度：版面盒子還是橫的，視覺上是直的 */
.seat-left, .seat-right {
  justify-content: center;
  width: 100%;
}
.seat-left { transform: rotate(90deg); }
.seat-right { transform: rotate(-90deg); }

.hand {
  display: flex;
  align-items: flex-end;
  gap: 2px;
}

/* 自己這一排：手牌那疊 + 右邊動作鈕，手牌張數多時整排等比縮小 */
.seat-bottom {
  flex-direction: row;
  align-items: flex-end;
  justify-content: center;
  gap: 20px;
}

.stack {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  flex: 1 1 auto;   /* 吃掉動作鈕以外的寬度，手牌就有明確的可用寬度可以縮 */
  min-width: 0;
}

.seat-bottom .hand {
  gap: 3px;
  width: 100%;
  justify-content: center;
}

.seat-bottom .hand .tile {
  flex: 0 1 var(--t-lg);
  min-width: 0;
}

.gap { flex: 0 0 14px; }

.melds {
  display: flex;
  gap: 10px;
  align-items: flex-end;
  min-height: 1px;
}

.meld { display: flex; gap: 1px; }
.meld.flowers { opacity: 0.92; }

/* ---- 名牌 --------------------------------------------------------------- */

.plate {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 3px 12px;
  color: var(--paper);
  background: #0e2b20cc;
  border: 1px solid #ffffff2e;
  font-size: 13px;
  letter-spacing: 0.1em;
  white-space: nowrap;
}

.plate.is-turn {
  background: var(--paper);
  color: var(--ink);
  border-color: var(--gold);
  box-shadow: 0 0 0 2px #c9a96155, 0 0 18px #c9a96144;
}

.plate .wind {
  font-size: 17px;
  line-height: 1;
}

.plate .tag {
  font-size: 11px;
  padding: 1px 5px;
  border: 1px solid currentColor;
}

.plate .score { opacity: 0.75; font-size: 12px; }

/* ---- 中央：棄牌與局數牌 -------------------------------------------------- */

.center {
  grid-area: center;
  display: grid;
  grid-template-columns: auto minmax(120px, 1fr) auto;
  grid-template-rows: auto minmax(90px, 1fr) auto;
  grid-template-areas:
    ".      dtop    ."
    "dleft  plaque  dright"
    ".      dbottom .";
  place-items: center;
  gap: 6px;
  padding: 8px;
}

.discards {
  display: flex;
  flex-wrap: wrap;
  gap: 1px;
  width: calc(var(--t-sm) * var(--disc-cols) + 8px);
}

.discards-bottom { grid-area: dbottom; align-content: flex-start; }
.discards-top { grid-area: dtop; align-content: flex-end; transform: rotate(180deg); }
.discards-left { grid-area: dleft; transform: rotate(90deg); align-content: flex-end; }
.discards-right { grid-area: dright; transform: rotate(-90deg); align-content: flex-end; }

.plaque {
  grid-area: plaque;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 10px 18px;
  color: var(--paper);
  background: #0d2b1fbb;
  border: 1px solid #ffffff2b;
  box-shadow: inset 0 0 0 3px #0d2b1fbb, inset 0 0 0 4px #ffffff1a;
  text-align: center;
}

.plaque-round {
  font-size: clamp(20px, 2.4vw, 30px);
  line-height: 1;
  letter-spacing: 0.06em;
}
.plaque-round span { font-size: 0.5em; margin-left: 4px; opacity: 0.8; }
.plaque-wall { font-size: 11px; letter-spacing: 0.22em; opacity: 0.72; }

/* ---- 牌 ----------------------------------------------------------------- */

.tile {
  display: block;
  width: var(--t-md);
  height: auto;
  filter: drop-shadow(0 1px 1px #00000055);
}

.tile.lg { width: var(--t-lg); }
.tile.md { width: var(--t-md); }
.tile.sm { width: var(--t-sm); }

.hand-tile {
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.12s ease, filter 0.12s ease;
}
/* 觸控裝置沒有 hover：留著的話第一下點擊會卡在 hover 態不放 */
@media (hover: hover) {
  .hand-tile:hover { transform: translateY(-8px); }
}
.hand-tile.is-selected {
  transform: translateY(-14px);
  filter: drop-shadow(0 4px 6px #000a) drop-shadow(0 0 6px var(--gold));
}
.hand-tile.is-drawn { filter: drop-shadow(0 2px 3px #000a) drop-shadow(0 0 5px #ffffff66); }

/* ---- 動作列 ------------------------------------------------------------- */

.actions {
  display: flex;
  gap: 8px;
  flex: 0 0 auto;
  margin-bottom: 2px;
}

.act {
  font-family: var(--serif);
  font-size: 17px;
  letter-spacing: 0.1em;
  width: 46px;
  height: 46px;
  flex: 0 0 auto;
  border-radius: 50%;
  color: var(--paper);
  background: #0e2b20cc;
  border: 1px solid #ffffff33;
  cursor: pointer;
}
.act:disabled { opacity: 0.32; cursor: default; }
@media (hover: hover) {
  .act:not(:disabled):hover { background: var(--paper); color: var(--ink); }
}
.act-win:not(:disabled) { border-color: var(--gold); color: var(--gold); }

/* ---- 設定面板 ----------------------------------------------------------- */

.panel {
  background: var(--paper);
  border-left: 3px double var(--ink);
  overflow-y: auto;
  overflow-x: hidden;
  padding: 16px 18px 40px;
}

body:not(.panel-open) .panel { display: none; }

.panel-head h2 {
  margin: 0 0 4px;
  font-size: 19px;
  letter-spacing: 0.2em;
}
.panel-head p, .hint {
  margin: 0 0 6px;
  font-size: 12px;
  line-height: 1.6;
  color: var(--ink-soft);
}

.pgroup {
  margin-top: 20px;
  padding-top: 14px;
  border-top: 1px solid #0000001f;
}

.pgroup h3 {
  margin: 0 0 10px;
  font-size: 12px;
  letter-spacing: 0.34em;
  font-weight: 400;
  color: var(--ink-soft);
}

.presets { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

.preset {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 9px;
  font-family: var(--serif);
  font-size: 13px;
  background: transparent;
  border: 1px solid #00000026;
  cursor: pointer;
  text-align: left;
}
.preset.is-on { border-color: var(--ink); box-shadow: inset 0 0 0 1px var(--ink); }

.preset-swatch {
  display: flex;
  gap: 2px;
  align-items: center;
  padding: 3px;
  border: 1px solid;
  border-radius: 2px;
}
.preset-swatch i { width: 6px; height: 12px; display: block; }

.fields { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 10px; }

.field {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 12px;
}
.field input[type="color"] {
  width: 30px;
  height: 24px;
  padding: 0;
  border: 1px solid #00000033;
  background: none;
  cursor: pointer;
}

.row {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 13px;
  margin-bottom: 8px;
}
.row > span { min-width: 42px; color: var(--ink-soft); }
.row select, .row input[type="range"] { flex: 1; font-family: var(--serif); }
.row b { font-weight: 400; min-width: 18px; text-align: right; }

.gallery {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
}

.gtile {
  position: relative;
  padding: 2px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 3px;
  cursor: pointer;
}
.gtile:hover { border-color: var(--ink); }
.gtile .tile { width: 100%; filter: none; }

.gtile-face.has-custom::after {
  content: "";
  position: absolute;
  top: 1px;
  right: 1px;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--gold);
  box-shadow: 0 0 0 1px #0006;
}

.btns { display: flex; flex-wrap: wrap; gap: 8px; }
.btns .warn { border-color: #8a1c12; color: #8a1c12; }
.btns .warn:hover { background: #8a1c12; color: var(--paper); }

/* ---- 對家的手牌張數：窄畫面把整排牌背收成一顆數字 ------------------------ */

.hand-count { display: none; }

/* ---- 抽屜遮罩 ----------------------------------------------------------- */

.scrim, .panel-close { display: none; }

/* ---- 窄畫面：面板改成蓋在牌桌上的抽屜 ------------------------------------
   舊版是「開面板就把牌桌 display:none」，而 body 預設帶 panel-open ——
   手機一進來看到的是一片設定面板，牌桌根本沒被畫出來。 */

@media (max-width: 900px) {
  .stage,
  body.panel-open .stage { grid-template-columns: 1fr; }

  .panel {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    z-index: 30;
    width: min(360px, 88vw);
    border-left: 3px double var(--ink);
    box-shadow: -10px 0 34px #0008;
    padding: calc(52px + env(safe-area-inset-top)) 16px calc(40px + env(safe-area-inset-bottom));
    transition: transform 0.22s ease, visibility 0.22s;
    overscroll-behavior: contain;
  }

  /* 收起的樣子全掛在這條上 —— 開啟態就不必再回頭跟 .panel 搶同一個屬性。
     桌面版是靠 display:none 收面板，這裡改成滑出去，才有動畫也才不吃版面。 */
  body:not(.panel-open) .panel {
    display: block;
    transform: translateX(100%);
    visibility: hidden;
  }

  body.panel-open .scrim {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 20;
    background: #000000a6;
  }

  body.panel-open .panel-close {
    display: block;
    position: fixed;
    z-index: 31;
    top: calc(8px + env(safe-area-inset-top));
    right: 10px;
    width: 40px;
    height: 40px;
    font-family: var(--serif);
    font-size: 17px;
    line-height: 1;
    color: var(--ink);
    background: var(--paper);
    border: 1px solid var(--ink);
    cursor: pointer;
  }

  .topbar {
    gap: 10px;
    padding: 6px 10px;
    padding-right: calc(10px + env(safe-area-inset-right));
  }
  .brand { height: 34px; }
  .topbar-meta { gap: 0; font-size: 13px; }
  .topbar-meta .meta-item + .meta-item { display: none; }
  .topbar button { padding: 0 12px; min-height: 40px; font-size: 13px; }
  .table { padding: 8px; }
}

/* ---- 手機：直握、或橫握但很矮 --------------------------------------------
   對家那三排牌背在這個尺寸只是在吃空間，收成張數；動作鈕移到手牌下面一排。 */

@media (max-width: 780px), (max-height: 500px) {
  .felt {
    --t-lg: clamp(28px, min(8.6cqw, 13cqh), 46px);
    --t-md: clamp(12px, min(2.6cqw, 4cqh), 24px);
    --t-sm: clamp(10px, min(2.4cqw, 3.6cqh), 20px);
    --disc-cols: 6;

    grid-template-columns: minmax(0, auto) 1fr minmax(0, auto);
    /* 上下兩排給實 auto、中央才是可壓縮的那個 —— 反過來的話牌桌一矮，
       中央的最小高度會把自家那排壓扁，手牌跟動作鈕就溢出牌桌下緣。 */
    grid-template-rows: auto minmax(0, 1fr) auto;
    padding: 6px 6px 10px;
  }

  .center { grid-template-rows: auto minmax(0, 1fr) auto; }

  .seat:not(.seat-bottom) .hand { display: none; }

  .hand-count {
    display: inline-block;
    padding: 0 5px;
    font-size: 11px;
    border: 1px solid currentColor;
    opacity: 0.8;
  }

  .plate {
    gap: 5px;
    padding: 2px 7px;
    font-size: 11px;
  }
  .plate .wind { font-size: 14px; }
  .plate .score { display: none; }

  /* 動作鈕改排到手牌下面一排，橫向就不必再分它 270px */
  .seat-bottom {
    flex-direction: column;
    align-items: center;
    gap: 6px;
  }
  .seat-bottom .hand { gap: 2px; }
  .gap { flex: 0 0 8px; }
  .melds { gap: 6px; }

  .actions { gap: 6px; margin-bottom: 0; }
  .act {
    width: 44px;
    height: 44px;
    font-size: 15px;
  }

  .frame i { width: 22px; height: 22px; }
  .frame i:nth-child(1), .frame i:nth-child(2) { top: 5px; }
  .frame i:nth-child(3), .frame i:nth-child(4) { bottom: 5px; }
  .frame i:nth-child(1), .frame i:nth-child(3) { left: 5px; }
  .frame i:nth-child(2), .frame i:nth-child(4) { right: 5px; }

  .center { gap: 3px; padding: 3px; }
  .plaque { padding: 6px 10px; }
  .plaque-round { font-size: clamp(15px, 4.5cqw, 22px); }
  .plaque-wall { font-size: 10px; letter-spacing: 0.12em; }
}

/* 牌桌真的很窄時棄牌堆再收一欄。用容器查詢而不是 vw ——
   直握模式下整頁轉了 90 度，viewport 寬度跟牌桌寬度根本是兩回事。 */
@container (max-width: 560px) {
  .felt { --disc-cols: 5; }
}

/* 牌桌夠高才讓手牌換行 —— 那是直握平板這種形狀。矮牌桌（手機橫著、或直握
   轉向後）一排縮著放才對，換行會把動作鈕擠出牌桌外。 */
@container (min-height: 420px) {
  .seat-bottom .hand { flex-wrap: wrap; }
  .seat-bottom .hand .tile { flex: 0 0 auto; }
}

/* SE 這種矮機再擠一次，不然自家那排會頂出牌桌下緣 */
@container (max-height: 280px) {
  .felt { padding: 4px 4px 6px; }
  .seat-bottom { gap: 3px; }
  .act { width: 38px; height: 38px; font-size: 13px; }
}

@media (max-width: 430px) {
  .fields, .presets { grid-template-columns: 1fr; }
  .gallery { grid-template-columns: repeat(6, 1fr); }
}

/* ---- 直握手機：整頁轉 90 度 ---------------------------------------------
   #app 先往上推自己一個身高，再繞左上角轉 90 度，剛好貼齊整個畫面：
   寬給 100dvh、高給 100dvw，轉完就對調回來。
   注意這之後 #app 變成子孫 position:fixed 的容器塊 —— 抽屜和遮罩因此
   會定位在「轉過的畫面」裡，那正是我們要的。
   安全區也跟著轉：實體上緣落在內容的左邊，所以四個 inset 要輪一格。 */

html[data-rotate] body { padding: 0; }

html[data-rotate] #app {
  position: fixed;
  top: 0;
  left: 0;
  width: 100dvh;
  height: 100dvw;
  transform-origin: top left;
  transform: rotate(90deg) translateY(-100%);
  padding:
    env(safe-area-inset-right)
    env(safe-area-inset-bottom)
    env(safe-area-inset-left)
    env(safe-area-inset-top);
}

/* 轉向後畫面的「寬」是 100dvh，抽屜寬度得跟著那一軸算，用 vw 會算到短邊 */
html[data-rotate] .panel { width: min(360px, 70dvh); }
