/* แชร์เป็นรูป (js/sharecard.js) — จอคอม = กล่องกลางจอ · มือถือแนวตั้ง = แผ่นจากขอบล่าง วางเหนือเมนูล่าง (--bnav-h)
   มือถือแนวนอน = รูปซ้าย ปุ่มขวา */
.sc-dialog {
  box-sizing: border-box;
  width: min(520px, calc(100% - 24px));
  max-height: calc(100dvh - 32px);
  padding: 0 20px 18px;
  overflow: auto;
  overscroll-behavior: contain;
  color: var(--text);
  background: var(--panel-solid);
  border: 1px solid var(--line);
  border-radius: 20px;
  box-shadow: 0 20px 70px rgb(0 0 0 / 30%);
}

.sc-dialog::backdrop {
  background: rgb(0 0 0 / 55%);
}

.sc-dialog header {
  position: sticky;
  top: 0;
  z-index: 1;
  display: flex;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
  min-height: 56px;
  margin: 0 -20px;
  padding: 6px 10px 6px 20px;
  background: var(--panel-solid);
  border-bottom: 1px solid var(--line);
}

.sc-dialog h2 {
  margin: 0;
  font-size: 18px;
  line-height: 1.35;
}

.sc-dialog .icon-btn {
  flex: none;
  min-width: 44px;
  min-height: 44px;
}

/* พรีวิว: รูปทั้งใบ (4:5) ไม่ครอป · ระหว่างสร้างเป็นกรอบว่าง + ข้อความ */
.sc-preview {
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 14px 0 0;
}

.sc-preview[aria-busy="true"] {
  min-height: min(44dvh, 360px);
  background: var(--card);
  border-radius: 14px;
}

.sc-preview img {
  display: block;
  width: auto;
  max-width: 100%;
  height: auto;
  max-height: min(58dvh, 620px);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 6px 22px rgb(0 0 0 / 18%);
}

.sc-preview img[hidden],
.sc-making[hidden] {
  display: none;
}

.sc-making {
  margin: 0;
  font-size: 15px;
  color: var(--muted);
}

.sc-hint {
  margin: 10px 0 0;
  font-size: 13px;
  line-height: 1.55;
  color: var(--muted);
}

/* ปุ่ม: แชร์รูปเต็มแถว (ปุ่มหลัก) · บันทึกรูป + คัดลอกลิงก์ครึ่งแถว */
.sc-actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  margin-top: 12px;
}

.sc-actions .chip-btn {
  height: 46px;
  font-weight: 600;
}

.sc-actions .chip-btn[hidden] {
  display: none;
}

.sc-actions .chip-btn:disabled {
  cursor: progress;
  opacity: 0.5;
}

.sc-actions .sc-primary {
  grid-column: 1 / -1;
  color: var(--accent-ink);
  background: var(--accent);
  border-color: var(--accent);
}

.sc-actions .sc-primary > svg {
  /* .chip-btn > svg ใช้สี accent = สีเดียวกับพื้นปุ่ม → ไอคอนหาย */
  color: inherit;
}

.sc-status {
  min-height: 1.4em;
  margin: 8px 0 0;
  font-size: 14px;
  font-weight: 600;
  line-height: 1.5;
  overflow-wrap: anywhere;
}

.sc-status[data-kind="ok"] {
  color: var(--ok-t, var(--ok));
}

.sc-status[data-kind="error"] {
  color: var(--bad-t, var(--bad));
}

.sc-dialog :focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

/* ปุ่ม "รูป" ในหัวแผงขบวน (ข้างปุ่มแชร์ลิงก์) · ที่ไม่พอจริงๆ ข้อความในปุ่มย่อเป็น … (ไม่ดันแถวล้น) */
.train-head .dt-btn.sc-open {
  flex: 0 1 auto;
  gap: 5px;
  min-width: 40px;
  padding: 0 10px;
  overflow: hidden;
}

.train-head .dt-btn.sc-open > span {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* จอแคบ (Fold 344 / iPhone SE) และแผ่นด้านข้างของมือถือแนวนอน: ปุ่มในแถวชิดกันขึ้นเล็กน้อยให้พอดีแถวเดียว */
@media (max-width: 379px), (max-width: 899px) and (max-height: 500px) and (orientation: landscape) and (min-aspect-ratio: 4/3) {
  .train-head .dt-bar {
    gap: 4px;
  }

  .train-head .dt-btn.wide {
    gap: 5px;
    padding: 0 9px;
  }

  .train-head .dt-btn.sc-open {
    padding: 0 8px;
  }
}

/* แถวปุ่มแคบกว่า ~310px (จอ 320 · แผ่นด้านข้างของมือถือแนวนอนจอเล็ก เช่น 667×375): ข้อความไว้ใต้ไอคอน
   แบบปุ่มบนแผนที่ (ทั้งไทย/แบบแผนที่) — ยังมีทั้งไอคอนและข้อความ ไม่ถูกตัด */
@media (max-width: 341px), (max-width: 759px) and (max-height: 500px) and (orientation: landscape) and (min-aspect-ratio: 4/3) {
  .train-head .dt-btn.sc-open {
    --i: 17px;

    flex-direction: column;
    flex-shrink: 0;
    gap: 1px;
    min-width: 44px;
    padding: 0 5px;
    font-size: 11px;
    line-height: 1.15;
  }
}

/* มือถือแนวตั้ง: แผ่นจากขอบล่าง เหนือเมนูล่าง */
@media (max-width: 899px) and (min-height: 501px), (max-width: 899px) and (max-aspect-ratio: 4/3) {
  .sc-dialog {
    inset: auto 0 var(--bnav-h);
    width: 100%;
    max-width: none;
    max-height: calc(100dvh - var(--bnav-h) - 12px);
    margin: 0;
    padding: 0 16px 14px;
    border-radius: 20px 20px 0 0;
  }

  .sc-dialog header {
    margin: 0 -16px;
    padding-left: 16px;
  }

  .sc-preview img {
    max-height: calc(100dvh - var(--bnav-h) - 280px);
  }

  /* มีปุ่มแชร์รูป (แถวปุ่มเพิ่ม 1 แถว) → รูปเล็กลงอีกนิด ปุ่มทั้งหมดยังเห็นโดยไม่ต้องเลื่อน */
  .sc-dialog:has(.sc-primary:not([hidden])) .sc-preview img {
    max-height: calc(100dvh - var(--bnav-h) - 336px);
  }

  /* จอเตี้ย (iPhone SE + แถบเบราว์เซอร์): ซ่อนคำอธิบาย ให้รูปใหญ่ขึ้น */
  @media (max-height: 620px) {
    .sc-hint {
      display: none;
    }

    .sc-preview img {
      max-height: calc(100dvh - var(--bnav-h) - 226px);
    }

    .sc-dialog:has(.sc-primary:not([hidden])) .sc-preview img {
      max-height: calc(100dvh - var(--bnav-h) - 282px);
    }
  }

  /* โหมดเด็ก/คีย์บอร์ด: ไม่มีเมนูล่าง → เว้นขอบล่างของ iPhone เอง */
  .kid .sc-dialog,
  html.kb-open .sc-dialog {
    padding-bottom: calc(14px + env(safe-area-inset-bottom));
  }
}

/* มือถือแนวนอน: รูปซ้าย ปุ่มขวา (จอเตี้ย รูปเรียงต่อกันจะเล็กเกินไป) */
@media (max-width: 899px) and (max-height: 500px) and (orientation: landscape) and (min-aspect-ratio: 4/3) {
  .sc-dialog {
    width: min(760px, calc(100% - 24px));
    max-height: calc(100dvh - 16px);
  }

  .sc-dialog[open] {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    grid-template-rows: auto auto auto auto 1fr;
    column-gap: 16px;
    align-content: start;
  }

  .sc-dialog header {
    grid-column: 1 / -1;
  }

  .sc-preview {
    grid-row: 2 / span 4;
    margin-bottom: 4px;
  }

  .sc-preview[aria-busy="true"] {
    width: 200px;
    min-height: 250px;
  }

  .sc-preview img {
    max-height: calc(100dvh - 100px);
  }

  .sc-hint,
  .sc-actions {
    margin-top: 14px;
  }

  .sc-actions {
    grid-template-columns: minmax(0, 1fr);
  }
}
