/* ใช้ CSS ธรรมดา (ไม่ใช้ Tailwind) เพื่อให้เปิดได้แม้ไม่มีเน็ต */
* { box-sizing: border-box; }
body {
  margin: 0;
  min-height: 100vh;
  font-family: var(--font-ui, 'Sarabun'), sans-serif; /* ตัวอักษรทั้งเครื่อง เลือกได้ใน ⚙ */
  line-height: 1.3; /* = ระยะบรรทัดปกติของ Sarabun · ล็อกไว้ ฟอนต์อื่นที่บรรทัดสูงกว่า (Prompt, IBM Plex) จะได้ไม่ดันจอล้น */
  background: #e2e8f0;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  gap: 28px;
  padding: 32px 16px;
  flex-wrap: wrap;
}

/* ===== ตัวเครื่อง =====
   สีทั้งหมดมาจากตัวแปร (--xxx) → เปลี่ยนธีม = เปลี่ยนชุดตัวแปร ดู "ธีมสี" ด้านล่าง */
.calculator {
  /* สีทั้งหมดมาจาก skin (โฟลเดอร์ skins/) — ดู skins/_template.js */
  position: relative; /* ให้หน้าตั้งค่าซ้อนทับในตัวเครื่องได้ */
  width: 400px;
  background: var(--body);
  border-radius: 28px;
  padding: 20px;
  box-shadow: 0 20px 40px rgba(15, 23, 42, 0.35), inset 0 2px 0 var(--body-edge);
  transition: background 0.2s;
}


.brand {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 19px; /* สูงคงที่ ไม่ขยับตามฟอนต์ */
  color: var(--brand-sub);
  font-size: 13px;
  margin: 0 6px 10px;
}
.brand strong { color: var(--brand-text); letter-spacing: 1px; }
.settings-btn { background: none; border: none; color: var(--brand-sub); font-size: 16px; cursor: pointer; padding: 0 0 0 6px; }
.settings-btn:hover { color: var(--accent); }

/* หน้าตั้งค่า → css/settings.css */

/* เลขคีย์ลัดมุมซ้ายบนของปุ่มลัด */
.chip { position: relative; }
.chip .shortcut {
  position: absolute; top: 1px; left: 4px;
  font-size: 9px; font-weight: 400; color: var(--accent); opacity: 0.85;
}

/* ===== จอ LCD ===== */
.lcd {
  background: var(--lcd-bg);
  border-radius: 12px;
  padding: 10px 14px;
  color: var(--lcd-text);
  box-shadow: inset 0 3px 8px rgba(0, 0, 0, 0.35);
  margin-bottom: 18px;
  height: 172px;           /* สูงคงที่เหมือนจอเครื่องจริง */
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
/* โหมดมีผลหลายบรรทัด → ตัวเลขหลักเล็กลง ซ่อนแถบขั้น (ไม่ได้ใช้) ให้พอดีจอ */
.lcd.compact .lcd-step, .lcd.compact .lcd-entered { display: none; }
.lcd.compact .lcd-main { font-size: 30px; min-height: 36px; margin-top: 2px; }
.lcd-prompt { font-size: 13px; min-height: 18px; }
.lcd-step { display: flex; gap: 4px; margin-top: 4px; min-height: 6px; }
.lcd-entered { font-size: 13px; margin-top: 6px; min-height: 18px; color: var(--lcd-sub); }
/* ขั้นแตะเลือก → แถบปุ่มลัดกะพริบกรอบ ให้รู้ว่าต้องแตะตรงนี้ */
.touchbar.pick { outline: 2px solid var(--accent); animation: pick-pulse 1.2s ease-in-out infinite; }
@keyframes pick-pulse { 50% { outline-color: transparent; } }
.lcd-step span { flex: 1; height: 4px; border-radius: 2px; background: var(--lcd-dim); }
.lcd-step span.done { background: var(--lcd-text); }
.lcd-main {
  font-family: var(--font-lcd, 'Courier New'), monospace; /* ตัวเลขบนจอ เลือกได้ใน ⚙ */
  font-size: 42px;
  font-weight: 700;
  text-align: right;
  line-height: 1.2;
  min-height: 50px; /* จอว่างก็ไม่ยุบ */
  margin-top: auto; /* ดันตัวเลขลงล่าง ชิดบรรทัดหมายเหตุ */
  overflow: hidden;
  white-space: nowrap;
}
.lcd-bottom { display: flex; justify-content: space-between; font-size: 13px; min-height: 18px; }
/* คำถามกลางจอ (ขั้นที่ยังไม่ได้พิมพ์) — ฟอนต์ตัวหนังสือ ไม่ใช่ฟอนต์ตัวเลข · ยาวได้ 2 บรรทัด */
.lcd.question .lcd-main { margin-top: 0; flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; white-space: normal; }
.lcd-question { font-family: var(--font-ui, 'Sarabun'), sans-serif; font-size: 22px; font-weight: 700; line-height: 1.25; text-align: center; }
/* คำแนะนำเฉพาะขั้น (เช่น "กด = ข้าม") → แถบเข้ม ให้เห็นว่าเป็นทางเลือกของขั้นนี้ */
#lcd-note.step-hint { background: var(--lcd-text); color: var(--lcd-bg); border-radius: 4px; padding: 0 6px; }
.lcd-unit { font-weight: 700; }
/* ผลทันทีจากขนาด — ตาราง 2 คอลัมน์ใต้ตัวเลขหลัก */
.lcd-results { display: grid; grid-template-columns: 1fr 1fr; gap: 2px 14px; font-size: 13px; }
.lcd-results:not(:empty) { border-top: 1px dashed var(--lcd-dim); margin-top: 8px; padding-top: 6px; }
.lcd-results div { display: flex; justify-content: space-between; gap: 6px; }
.lcd-results b { font-weight: 700; white-space: nowrap; }
.lcd.single-results .lcd-bottom { display: none; } /* หน่วยอยู่ในแถว "ผลลัพธ์" แล้ว → เว้นที่ให้แถว */
.lcd-results.single { grid-template-columns: 1fr; }
/* ผลขนาด (แบบการ์ด 2×2): บรรทัดบน = พท. ของหน่วยที่กรอก + ขนาดตัวใหญ่ · การ์ด 4 หน่วยอื่น (กว้าง × ยาว = พื้นที่) */
.lcd.size-results .lcd-bottom { display: none; }                  /* หน่วยอยู่ท้ายตัวเลขใหญ่แล้ว */
.lcd.size-results .lcd-main { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; font-size: 28px; min-height: 0; line-height: 1.1; }
/* ขนาดเป็น em ของตัวเลขใหญ่ → ย่อทั้งบรรทัดได้พร้อมกันเมื่อยาวเกิน */
.lcd.size-results .main-side { font-family: var(--font-ui, 'Sarabun'), sans-serif; font-weight: 400; font-size: 0.5em; white-space: nowrap; }
.lcd.size-results .main-side b { font-size: 1.15em; }
.lcd.size-results .main-unit { font-family: var(--font-ui, 'Sarabun'), sans-serif; font-size: 0.54em; margin-left: 0.6em; } /* เว้นช่องก่อนหน่วย แบบภาพร่าง (50 mm) */
.lcd.size-results .lcd-main > span:last-child { white-space: nowrap; }
.lcd.size-results .lcd-results { grid-template-columns: 1fr 1fr; grid-auto-rows: 1fr; gap: 5px; flex: 1; min-height: 0; } /* การ์ดยืดเต็มที่เหลือ ไม่เหลือขอบล่าง */
.lcd.size-results .lcd-results:not(:empty) { margin-top: 4px; padding-top: 0; border-top: none; }
.lcd.size-results .size-card { display: flex; flex-direction: column; justify-content: center; border: 1px solid var(--lcd-dim); border-radius: 6px; padding: 2px 8px 3px; line-height: 1.2; min-width: 0; }
.lcd.size-results .size-card div { display: block; }
.lcd.size-results .size-card-unit { font-size: 12px; }
.lcd.size-results .size-card-line { font-size: 15px; white-space: nowrap; overflow: hidden; } /* ยาวเกิน → JS ย่อตัวอักษร ไม่ตกบรรทัด */
.lcd.size-results .size-card-line b { font-size: 1.15em; }
.prompt-note { float: right; font-size: 11px; color: var(--lcd-sub); }  /* หมายเหตุสั้นท้ายหัวข้อ (เช่น คลี่กล่อง = ค่าประมาณ) */
/* ตารางเทียบหน่วย — ใช้พื้นที่จอทั้งหมด (ซ่อนตัวเลขใหญ่) */
.lcd.table-results .lcd-main, .lcd.table-results .lcd-bottom { display: none; }
.lcd.preview-units .lcd-step, .lcd.preview-units .lcd-entered { display: none; }
.lcd.preview-hide-entered .lcd-entered { display: none; }
.lcd-results.table { display: block; }
.lcd.table-results .lcd-results:not(:empty) { margin-top: 2px; padding-top: 0; border-top: none; } /* ตารางใช้ที่ทั้งจอ */
.conv-table { width: 100%; border-collapse: collapse; font-size: 12.5px; font-variant-numeric: tabular-nums; }
.conv-table th, .conv-table td { padding: 1px 3px; text-align: right; white-space: nowrap; }
.conv-table tr > th:first-child { text-align: left; font-weight: 400; }
.conv-table tr:first-child th { font-weight: 700; border-bottom: 1px dashed currentColor; }
.conv-table td { font-weight: 700; }
.conv-table tr.highlight { background: var(--lcd-dim); }
.conv-table.tight th, .conv-table.tight td { line-height: 1.15; } /* ตาราง 7 แถว (ราคาขาย) → บรรทัดชิดขึ้นให้พอดีจอ */
/* ตารางแปลงหน่วย: คอลัมน์แรกแคบ (แค่ "80 ↓" / หน่วย) → ที่เหลือให้ตัวเลขตัวใหญ่ อ่านง่าย */
.unit-table { font-size: 14px; }
.unit-table tr:first-child th { border-bottom: none; font-size: 12px; }
.unit-table tr:first-child th:first-child { font-size: 10px; }          /* "80 ↓" ตัวเล็ก → คอลัมน์แรกแคบ */
.unit-table tr:first-child { border-bottom: 1px dashed currentColor; } /* เส้นใต้หัวตาราง ต่อกันเส้นเดียว */
.unit-table tr > th:first-child { width: 1%; padding-right: 6px; }      /* คอลัมน์แรกกว้างเท่าที่จำเป็น */
.unit-table .read-arrow { font-weight: 400; margin-left: 2px; }
.unit-table.pair { font-size: 10.5px; line-height: 1; }                 /* คู่ กว้าง×ยาว: ช่องละ 2 บรรทัด กว้างบน ×ยาวล่าง */
.unit-table.pair tr:first-child th { font-size: inherit; }
.unit-table.pair td { padding: 1px 3px; }
.unit-table.pair tr + tr + tr { border-top: 1px dotted var(--lcd-dim); }
.unit-table.dense { font-size: 10px; }                                   /* เลขยาวมาก → เล็กลงให้พอดีจอ */
.unit-table.pair.dense { font-size: 9.5px; }
.unit-table.dense tr:first-child th:first-child { white-space: normal; }  /* "25,000×99,999 ↓" ยาว → ↓ ขึ้นบรรทัดใหม่ */

/* ===== แถบปุ่มลัด (เปลี่ยนตามฟังก์ชัน — แนว Touch Bar / Stream Deck) ===== */
.touchbar {
  background: var(--bar-bg);
  border-radius: 14px;
  padding: 8px;
  margin-bottom: 18px;
  display: flex;
  gap: 6px;
  height: 62px;            /* สูงคงที่เหมือนเครื่องจริง ไม่ยืด ไม่หด */
  align-items: stretch;
  overflow-x: auto;        /* ปุ่มเยอะ → เลื่อนซ้าย-ขวาแบบ Touch Bar */
  overflow-y: hidden;
  scrollbar-width: none;   /* ซ่อนแถบเลื่อน (Firefox) */
  box-shadow: inset 0 2px 6px rgba(0, 0, 0, 0.45);
}
.touchbar::-webkit-scrollbar { display: none; } /* ซ่อนแถบเลื่อน (Chrome/Safari) */
/* ยังมีปุ่มต่อทางขวา → ขอบขวาจางลง เป็นสัญญาณให้เลื่อน */
.touchbar.more-right { mask-image: linear-gradient(to right, #000 85%, transparent); }
.touchbar-hint { color: var(--chip-sub); font-size: 13px; align-self: center; margin: 0 auto; }
.chip {
  flex: 0 0 80px;
  width: 80px;             /* ปุ่มลัดกว้างเท่ากันทุกปุ่ม ทุกชุด (ข้อความต้องสั้นพอดีปุ่ม) · 4 ปุ่มพอดีแถบ */
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  border: 1px solid var(--chip-border);
  border-radius: 10px;
  background: var(--chip-bg);
  color: var(--chip-text);
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
  padding: 8px 4px 2px;    /* ดันชื่อลงใต้เลขคีย์ลัดมุมซ้ายบน ไม่ให้ทับกัน */
  cursor: pointer;
  line-height: 1.15;
  transition: background 0.1s, transform 0.05s;
}
.chip-long { font-size: 11px; }   /* ชื่อยาว (ตัวเลขใหญ่) ย่อให้พอดีปุ่ม 80px */
.chip small { display: block; font-size: 10px; font-weight: 400; color: var(--chip-sub); overflow: hidden; text-overflow: ellipsis; }
.chip:hover { background: var(--chip-hover); border-color: var(--accent); }
.chip:active { transform: scale(0.94); background: var(--accent); color: #111; }
.chip.size { border-color: var(--func-bg); } /* ปุ่มขนาด = กรอกให้ 2 ช่องในครั้งเดียว */
.chip.armed { background: var(--accent); color: #111; transform: scale(0.95); } /* กำลังกดค้างเลือกปุ่มนี้ */
.chip.kb-focus { background: var(--accent); color: #111; } /* เลือกด้วยลูกศร ← → (Enter = กด) */
.chip.kb-focus small, .chip.kb-focus .shortcut { color: #333; }
.chip.armed small, .chip.armed .shortcut { color: #333; }
.key[data-digit] { touch-action: manipulation; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
.chip.go { border-color: #22c55e; } /* ได้ผลทันที */
/* สลับหน่วยที่พิมพ์ (mm / cm / นิ้ว) */
.touchbar-label {
  flex: 0 0 auto; align-self: center; color: var(--chip-sub); font-size: 12px; padding: 0 4px;
  position: sticky; left: -8px; z-index: 1; background: var(--bar-bg); align-self: stretch; display: flex; align-items: center; /* ป้ายติดซ้ายเสมอ ไม่เลื่อนหายตามปุ่ม */
}
.chip.manual { border-style: dashed; }   /* ✎ กรอกเอง */
.chip.back { color: var(--chip-sub); }   /* ↩ กลับไปขนาดสำเร็จ */
.chip.back.kb-focus { color: #111; }      /* เลือกด้วยลูกศร → ตัวเข้ม อ่านออกบนพื้นเหลือง */
.unit-toggle { flex: 0 0 auto; display: flex; gap: 2px; padding: 3px; border: 1px solid var(--chip-border); border-radius: 10px; background: var(--chip-bg); }
.unit-toggle button { border: none; background: transparent; color: var(--chip-sub); font-family: inherit; font-size: 13px; font-weight: 600; padding: 0 9px; border-radius: 7px; cursor: pointer; }
.unit-toggle button.on { background: var(--accent); color: #111; }
.unit-toggle button.kb-focus { box-shadow: inset 0 0 0 2px var(--accent); } /* เลือกด้วยลูกศร ← → (Enter = เปลี่ยนหน่วย) */
/* แถบเลื่อน (เช่น ช่องไฟ) — ลากแล้วผลบนจอเปลี่ยนทันที */
.slider-chip { flex: 1 1 0; min-width: 0; /* หดให้ปุ่ม ✓ ด้านขวาอยู่ในแถบเสมอ */ display: flex; align-items: center; gap: 6px; padding: 0 10px; border: 1px solid var(--chip-border); border-radius: 10px; background: var(--chip-bg); color: var(--chip-text); }
.slider-chip input[type="range"] { flex: 1; min-width: 60px; accent-color: var(--accent); cursor: pointer; }
.slider-chip button { border: none; background: transparent; color: var(--chip-text); font-size: 13px; padding: 6px 4px; cursor: pointer; }
.slider-name { font-size: 12px; color: var(--chip-sub); }
.slider-value { flex: 0 0 auto; min-width: 54px; text-align: right; font-size: 15px; white-space: nowrap; }
.chip.ask { border-style: dashed; border-color: var(--accent); } /* ปุ่มที่ต้องถามข้อมูลต่อ */
.chip.guess { border: 2px solid var(--accent); } /* เมนูเลข: ตัวที่เดาว่าใช่ */

/* ===== ปุ่ม =====
   แต่ละชนิดปุ่มตั้ง --kb (พื้น) --kt (ตัวอักษร) --ks (ตัวเล็ก) --ke (ขอบล่าง) แล้ว .key ใช้ค่าพวกนี้
   → "รูปแบบปุ่ม" (data-keys) เปลี่ยนแค่รูปทรง/เงา ไม่ต้องรู้สี */
.section-label { color: var(--label); font-size: 11px; margin: 0 4px 6px; }
.keys { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 14px; }
.keys.three { grid-template-columns: repeat(3, 1fr); } /* ฟังก์ชัน 9 ปุ่ม = 3 × 3 */
.key {
  --kb: var(--key-bg); --kt: var(--key-text); --ks: var(--key-text); --ke: var(--key-edge);
  --ko: var(--key-outline, var(--kb)); /* สีเส้นในแบบ "ขอบเส้น" */
  border: none;
  border-radius: 12px;
  padding: 12px 4px;
  font-family: inherit;
  font-size: 20px;
  font-weight: 600;
  line-height: 1.3; /* ล็อกความสูงปุ่ม — ปุ่มไม่รับระยะบรรทัดจากตัวเครื่อง เปลี่ยนฟอนต์แล้วปุ่มต้องไม่สูง/เตี้ยลง */
  cursor: pointer;
  background: var(--kb);
  color: var(--kt);
  box-shadow: 0 3px 0 var(--ke);
  transition: transform 0.05s;
}
.key:active, .key.pressed { transform: translateY(3px); box-shadow: 0 0 0 var(--ke); }
.key.operator { --kb: var(--op-bg); --kt: var(--op-text); --ke: var(--op-edge); --ko: var(--kb); }
.key.equals { --kb: var(--eq-bg); --kt: var(--eq-text); --ke: var(--eq-edge); --ko: var(--kb); }
.key.clear { --kb: var(--clear-bg); --kt: var(--clear-text); --ke: var(--clear-edge); --ko: var(--kb); }
/* ===== รูปแบบแป้นตัวเลข (เลือกใน ⚙) — วาดตำแหน่งปุ่มเป็น "แผนที่" ===== */
[data-area="ac"] { grid-area: ac; }   [data-area="back"] { grid-area: back; } [data-area="pct"] { grid-area: pct; }
[data-area="div"] { grid-area: div; } [data-area="mul"] { grid-area: mul; }   [data-area="sub"] { grid-area: sub; }
[data-area="add"] { grid-area: add; } [data-area="eq"] { grid-area: eq; }     [data-area="dot"] { grid-area: dot; }
[data-area="d0"] { grid-area: d0; } [data-area="d1"] { grid-area: d1; } [data-area="d2"] { grid-area: d2; } [data-area="d3"] { grid-area: d3; }
[data-area="d4"] { grid-area: d4; } [data-area="d5"] { grid-area: d5; } [data-area="d6"] { grid-area: d6; }
[data-area="d7"] { grid-area: d7; } [data-area="d8"] { grid-area: d8; } [data-area="d9"] { grid-area: d9; }

/* เครื่องคิดเลข (ค่าเริ่มต้น) */
.keypad {
  grid-template-areas:
    "ac  back pct div"
    "d7  d8   d9  mul"
    "d4  d5   d6  sub"
    "d1  d2   d3  add"
    "d0  d0   dot eq";
}
/* Numpad คีย์บอร์ด PC (IBM): + และ Enter สูง 2 ช่อง · แถวบนบาง = ⌫ % */
.calculator[data-keypad="pc"] .keypad {
  grid-template-areas:
    "back back pct pct"
    "ac   div  mul sub"
    "d7   d8   d9  add"
    "d4   d5   d6  add"
    "d1   d2   d3  eq"
    "d0   d0   dot eq";
}
.calculator[data-keypad="pc"] .keypad [data-area="back"],
.calculator[data-keypad="pc"] .keypad [data-area="pct"] { padding: 5px 4px; font-size: 15px; }
/* โทรศัพท์: 1 2 3 อยู่บน */
.calculator[data-keypad="phone"] .keypad {
  grid-template-areas:
    "ac  back pct div"
    "d1  d2   d3  mul"
    "d4  d5   d6  sub"
    "d7  d8   d9  add"
    "d0  d0   dot eq";
}
/* ปุ่มเลขที่ใช้เป็นคีย์ลัดได้ตอนนี้ — วงกลมเส้นบาง ๆ มุมขวาบน */
.key { position: relative; }
.key.shortcut-on::after {
  content: ''; position: absolute; top: 7px; right: 9px;
  width: 7px; height: 7px; border-radius: 50%;
  border: 1.5px solid var(--accent);
}

/* ปุ่มฟังก์ชันงานพิมพ์ — 2 บรรทัด: ชื่อ + คำอธิบายสั้น */
.key.func {
  --kb: var(--func-bg); --kt: var(--func-text); --ks: var(--func-sub); --ke: var(--func-edge); --ko: var(--kb);
  font-size: 14px;
  padding: 8px 2px;
  line-height: 1.2;
}
.key.func small { display: block; font-size: 10px; font-weight: 400; color: var(--ks); }
/* ปุ่มฟังก์ชันแบบมีไอคอน (js/keys.js): ไอคอนซ้าย · ชื่อ + คำอธิบายขวา · สูงคงที่ ไม่ขยับตามฟอนต์/กลุ่ม */
.function-keys .key.func {
  height: 44.8px; padding: 0 8px;
  display: flex; align-items: center; gap: 7px; text-align: left; min-width: 0;
}
.key.func .icon { width: 19px; height: 19px; flex: none; color: var(--ks); }
.fn-text { display: flex; flex-direction: column; min-width: 0; }
.fn-text b, .fn-text small { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
/* ช่องว่าง (กลุ่มนี้ยังมีไม่ครบ 9 ปุ่ม) */
.key-slot {
  height: 44.8px; display: flex; align-items: center; justify-content: center;
  border-radius: 12px; box-shadow: inset 0 0 0 1.5px var(--chip-border); color: var(--label); font-size: 11px;
}
/* เปลี่ยนกลุ่ม → ปุ่มพลิกเข้ามาทีละปุ่ม แบบ Stream Deck */
.flip-in { animation: key-flip-in 0.22s ease-out both; }
@keyframes key-flip-in { from { transform: rotateX(90deg); opacity: 0; } to { transform: none; opacity: 1; } }
@media (prefers-reduced-motion: reduce) { .flip-in { animation: none; } }

/* แถบดำตอนว่าง = แท็บกลุ่ม */
.group-tabs { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 5px; width: 100%; height: 100%; }
.group-tab {
  border: none; border-radius: 9px; background: transparent; cursor: pointer;
  color: var(--chip-sub); font-family: inherit; font-size: 12.5px; font-weight: 600; line-height: 1.2;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px; min-width: 0;
}
.group-tab .icon { width: 18px; height: 18px; }
.group-tab:hover { color: var(--chip-text); }
.group-tab.active { background: var(--chip-bg); color: var(--chip-text); box-shadow: inset 0 0 0 1.5px var(--accent); }
.group-tab.active .icon { color: var(--accent); }
.group-tab:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
/* บรรทัดใต้แถบ: กลุ่มที่เลือก (ซ้าย) · วิธีเปลี่ยน (ขวา) */
#group-label { display: flex; justify-content: space-between; gap: 8px; }
#group-label b { color: var(--brand-sub); font-weight: 600; }
.key.instant { --kb: var(--instant-bg); --kt: var(--instant-text); --ks: var(--instant-sub); --ke: var(--instant-edge); --ko: var(--kb); }
.key.func.smart { --kb: var(--smart-bg); --kt: var(--smart-text); --ks: var(--smart-sub); --ke: var(--smart-edge); --ko: var(--kb); }
.key.func.active { outline: 3px solid var(--accent); }

/* ===== รูปแบบปุ่ม ===== */
.calculator[data-keys="flat"] .key { box-shadow: none; border-radius: 10px; }
.calculator[data-keys="flat"] .key:active { transform: scale(0.96); filter: brightness(0.9); }
.calculator[data-keys="round"] .key { border-radius: 999px; }
.calculator[data-keys="round"] .key.func { border-radius: 18px; }
.calculator[data-keys="square"] .key { border-radius: 3px; box-shadow: 0 2px 0 var(--ke); }
.calculator[data-keys="outline"] .key { background: transparent; color: var(--ko); box-shadow: inset 0 0 0 2px var(--ko); }
.calculator[data-keys="outline"] .key small { color: var(--ko); opacity: 0.8; }
.calculator[data-keys="outline"] .key:active { background: var(--ko); color: var(--body); transform: none; }
.calculator[data-keys="soft"] .key { border-radius: 16px; box-shadow: 0 6px 14px -6px var(--ke), inset 0 -3px 0 rgba(0, 0, 0, 0.12); }
.calculator[data-keys="soft"] .key:active { transform: translateY(1px); box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.2); }

/* ===== แผงขวา: ใบเสร็จเทอร์มอล (เต็มความสูง) + ปุ่มคัดลอกด้านล่าง ===== */
.side-panel { width: 320px; display: flex; flex-direction: column; gap: 12px; height: 900px; /* JS ปรับให้สูงเท่าตัวเครื่อง */ }
.panel-head { display: flex; justify-content: space-between; align-items: center; color: #475569; font-size: 14px; gap: 8px; }
.panel-head button { border: 1px solid #94a3b8; background: #fff; border-radius: 8px; padding: 3px 10px; cursor: pointer; font-family: inherit; font-size: 13px; }
.panel-head button:disabled { opacity: 0.35; cursor: default; }
.history-nav { display: flex; align-items: center; gap: 4px; font-size: 13px; }

/* ใบเสร็จเทอร์มอล: ขาว-ดำล้วน ฟอนต์ตัวเลขแบบเครื่องพิมพ์ใบเสร็จ ขอบบน/ล่างหยัก */
.receipt {
  flex: 1 1 auto;
  min-height: 0;
  background: #fdfdfb;
  color: #111;
  padding: 18px 18px 22px;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  overflow-y: auto;
  scrollbar-width: none;
  box-shadow: 0 8px 20px rgba(15, 23, 42, 0.15);
  mask: conic-gradient(from 135deg at top, #0000, #000 1deg 89deg, #0000 90deg) top / 14px 7px repeat-x,
        linear-gradient(#000 0 0) center / 100% calc(100% - 14px) no-repeat,
        conic-gradient(from -45deg at bottom, #0000, #000 1deg 89deg, #0000 90deg) bottom / 14px 7px repeat-x;
}
.receipt::-webkit-scrollbar { display: none; }
/* มีเนื้อหาซ่อนด้านล่าง → ส่วนล่างจางลง (แทนแถบเลื่อน) */
.receipt.more-below::after {
  content: ''; position: sticky; bottom: -22px; display: block; height: 48px; margin-top: -48px;
  background: linear-gradient(rgba(253, 253, 251, 0), #fdfdfb); pointer-events: none;
}
.receipt-empty { color: #9ca3af; text-align: center; margin-top: 40%; }
.receipt-head { text-align: center; font-family: 'Courier New', monospace; font-weight: 700; letter-spacing: 3px; }
.receipt-head small { display: block; font-weight: 400; letter-spacing: 0; font-size: 11px; color: #444; }
.receipt-title { text-align: center; font-weight: 700; font-size: 15px; margin: 8px 0 4px; border-top: 1px dashed #111; padding-top: 8px; }
.receipt-visual { display: block; width: 100%; height: auto; margin: 4px 0 8px; }
.receipt-line { display: flex; justify-content: space-between; gap: 8px; }
.receipt-line span:last-child { text-align: right; }
.receipt-result { display: flex; justify-content: space-between; font-weight: 700; font-size: 16px; border-top: 1px dashed #111; margin-top: 6px; padding-top: 6px; }
.receipt-note { font-size: 12px; color: #333; margin-top: 2px; }
.receipt-sub { font-size: 11px; color: #555; margin: -2px 0 3px; }
.receipt-conv { font-size: 11.5px; color: #333; margin: 0 0 5px 10px; }   /* แปลงหน่วย: หน่วยอื่นต่อบรรทัด */
.receipt-highlight { background: #e5e5e0; }
.receipt-waiting { color: #9ca3af; text-align: center; margin-top: 16px; border-top: 1px dashed #ccc; padding-top: 10px; }

/* ปุ่มคัดลอก (LINE / แชท) */
.share-copy { flex: 0 0 auto; width: 100%; border: none; border-radius: 10px; padding: 10px; cursor: pointer; font-family: inherit; font-size: 15px; font-weight: 600; background: #06c755; color: #fff; box-shadow: 0 4px 12px rgba(15, 23, 42, 0.12); }
.share-copy:disabled { opacity: 0.4; cursor: default; }
.share-copy.done { background: #0f172a; }

.hint { width: 100%; text-align: center; color: #64748b; font-size: 13px; }
.receipt-toggle, .mobile-only { display: none; }

/* ===== มือถือ: เต็มจอพอดี ไม่ต้องเลื่อน · ใบผลลัพธ์ = แผ่นดึงขึ้นจากด้านล่าง ===== */
@media (max-width: 600px) {
  body { display: block; padding: 0; min-height: 0; }
  .calculator {
    width: 100%; height: 100vh; height: 100dvh; /* dvh = ความสูงจอจริง (ไม่รวมแถบเบราว์เซอร์) */
    border-radius: 0; padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
    display: flex; flex-direction: column;
  }
  .brand { margin: 0 4px 6px; align-items: center; }
  .receipt-toggle {
    display: inline-block; border: 1px solid var(--chip-border); background: var(--chip-bg); color: var(--chip-text);
    border-radius: 8px; padding: 2px 8px; margin-left: 6px; font-family: inherit; font-size: 13px; cursor: pointer;
  }
  .lcd { height: 168px; flex: 0 0 auto; margin-bottom: 8px; padding: 8px 12px; }
  .lcd-main { font-size: 36px; }
  .touchbar { height: 54px; flex: 0 0 auto; margin-bottom: 8px; padding: 6px; }
  .section-label, #group-label { display: none; } /* มือถือ: แถบดำบอกกลุ่มอยู่แล้ว ประหยัดความสูง */
  .keys { gap: 6px; margin-bottom: 8px; }
  .key.func { padding: 7px 2px; font-size: 13px; }
  .key.func small { display: none; }         /* เหลือแต่ชื่อปุ่ม ประหยัดความสูง */
  .function-keys .key.func, .key-slot { height: 29.6px; padding: 0 6px; gap: 5px; } /* = ความสูงเดิมบนมือถือ */
  .key.func .icon { width: 16px; height: 16px; }
  .group-tab { font-size: 11.5px; }
  .group-tab .icon { width: 16px; height: 16px; }
  .keypad { flex: 1 1 auto; min-height: 0; margin-bottom: 0; grid-auto-rows: 1fr; }
  .keypad .key { padding: 0; }               /* ปุ่มเลขยืดเต็มพื้นที่ที่เหลือ */
  .hint { display: none; }
  .side-panel {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 50;
    width: auto; height: 88dvh !important;  /* ชนะความสูงที่ JS ตั้งตามตัวเครื่อง */
    padding: 12px 12px calc(12px + env(safe-area-inset-bottom));
    background: #e2e8f0; border-radius: 18px 18px 0 0; box-shadow: 0 -10px 30px rgba(15, 23, 42, 0.35);
    transform: translateY(105%); transition: transform 0.25s ease;
  }
  .side-panel.open { transform: none; }
  .mobile-only { display: inline-block; }
}
/* จอเตี้ย (เช่น iPhone SE) → จอ LCD เตี้ยลงอีก */
@media (max-width: 600px) and (max-height: 700px) {
  .lcd { height: 166px; } /* พอดีตารางแปลงหน่วยแบบ กว้าง×ยาว */
  .unit-table.pair { font-size: 10px; }        /* จอเตี้ย: ตารางคู่เล็กลงนิด ให้ 5 แถวพอดี */
  .unit-table.pair.dense { font-size: 9px; }
  .unit-table.dense tr:first-child th:first-child { font-size: 9px; }
  .lcd-main { font-size: 30px; min-height: 36px; }
  .lcd-entered { display: none; }
  .touchbar { height: 52px; }
  .key.func { padding: 5px 2px; }
  .function-keys .key.func, .key-slot { height: 25.6px; } /* = ความสูงเดิมบนจอเตี้ย (iPhone SE) */
}

/* ===== แผงขวา: สลับ ใบผลลัพธ์ | ประวัติ ===== */
.panel-tabs { display: inline-flex; background: #cbd5e1; border-radius: 8px; padding: 2px; flex: none; }
.panel-head .panel-tabs button { border: none; background: none; border-radius: 6px; padding: 2px 10px; font-size: 13px; color: #334155; }
.panel-head .panel-tabs button.active { background: #fff; color: #0f172a; font-weight: 600; }
.history-count { font-size: 12px; color: #64748b; margin-left: auto; }
.history-view {
  flex: 1; min-height: 0; display: flex; flex-direction: column;
  background: #fdfdfb; border-radius: 4px; box-shadow: 0 1px 3px rgba(15, 23, 42, 0.12); overflow: hidden;
}
.history-view[hidden], .receipt[hidden], .share-copy[hidden], .history-nav[hidden], .history-count[hidden] { display: none; }
.history-search {
  display: flex; align-items: center; gap: 6px; margin: 10px 12px 4px;
  border: 1px solid #e2e8f0; border-radius: 8px; padding: 4px 10px; color: #94a3b8;
}
.history-search .icon { width: 15px; height: 15px; flex: none; }
.history-search input { border: none; outline: none; flex: 1; min-width: 0; font: inherit; font-size: 13px; color: #0f172a; background: transparent; }
.history-search:focus-within { border-color: #4f46e5; }
.history-list { flex: 1; min-height: 0; overflow-y: auto; scrollbar-width: none; }
.history-list::-webkit-scrollbar { display: none; }
.history-day { font-size: 12px; font-weight: 600; color: #64748b; padding: 10px 14px 4px; }
.history-item { border-top: 1px solid #f1f5f9; }
.history-item.open { background: #f8fafc; }
.history-row {
  width: 100%; border: none; background: none; cursor: pointer; text-align: left; font: inherit; color: #0f172a;
  display: grid; grid-template-columns: 28px minmax(0, 1fr) auto; gap: 8px; align-items: center; padding: 8px 14px;
}
.history-row:hover { background: #f8fafc; }
.history-icon { width: 28px; height: 28px; border-radius: 8px; background: #e0e7ff; color: #4f46e5; display: flex; align-items: center; justify-content: center; }
.history-icon.plain { background: #fef3c7; color: #b45309; }
.history-icon .icon { width: 16px; height: 16px; }
.history-text { min-width: 0; display: flex; flex-direction: column; }
.history-text b { font-size: 13.5px; font-weight: 600; }
.history-text small, .history-result small { font-size: 12px; color: #64748b; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.history-result { text-align: right; font: 700 14px 'Courier New', monospace; display: flex; flex-direction: column; max-width: 110px; }
.history-result small { font-family: inherit; font-weight: 400; font-size: 11px; color: #94a3b8; }
.history-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; padding: 0 14px 10px 50px; }
.history-actions button { border: 1px solid #cbd5e1; background: #fff; border-radius: 7px; padding: 3px 10px; font: inherit; font-size: 12.5px; color: #334155; cursor: pointer; }
.history-actions button.primary { background: #4f46e5; border-color: #4f46e5; color: #fff; font-weight: 600; }
.history-hint { flex-basis: 100%; font-size: 11.5px; color: #94a3b8; }
.history-empty { padding: 24px 16px; text-align: center; color: #94a3b8; font-size: 13px; }
.history-foot { display: flex; justify-content: space-between; align-items: center; gap: 8px; border-top: 1px solid #e2e8f0; padding: 8px 14px; font-size: 12px; color: #64748b; }
.history-foot button { border: none; background: none; color: #dc2626; font: inherit; font-size: 12px; cursor: pointer; padding: 0; }
.history-foot button.armed { font-weight: 700; }
