:root {
  --bg: #f3f6fb;
  --card: #ffffff;
  --text: #111827;
  --muted: #667085;
  --line: #e5e7eb;
  --primary: #2563eb;
  --primary-dark: #1e40af;
  --soft: #eef4ff;
  --warn: #fffbeb;
}
* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans Thai", "Noto Sans SC", sans-serif;
  background: var(--bg);
  color: var(--text);
}
.app-header {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  padding: 18px 18px 12px;
  background: var(--card);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 5;
}
.header-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}
.lang-select {
  width: auto;
  min-width: 120px;
}
h1 { font-size: 22px; margin: 0; }
h2 { font-size: 20px; margin: 0 0 16px; }
p { margin: 4px 0 0; color: var(--muted); }
.tabs {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
  padding: 10px;
  background: var(--card);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 73px;
  z-index: 4;
}
.tab {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 12px;
  padding: 10px 6px;
  font-weight: 700;
  cursor: pointer;
}
.tab.active {
  background: var(--soft);
  color: var(--primary);
  border-color: #bfdbfe;
}
main {
  max-width: 820px;
  margin: 0 auto;
  padding: 14px;
}
.panel { display: none; }
.panel.active { display: block; }
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 18px;
  box-shadow: 0 8px 20px rgba(15, 23, 42, 0.04);
}
label {
  display: block;
  font-weight: 700;
  margin: 14px 0 6px;
}
input, select {
  width: 100%;
  min-height: 44px;
  border: 1px solid #d0d5dd;
  border-radius: 12px;
  padding: 10px 12px;
  font-size: 16px;
  background: #fff;
}
input[readonly] {
  background: #f8fafc;
  color: #475467;
}
.grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0 12px;
}
.result {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin: 18px 0;
}
.result > div, .single-result {
  background: var(--soft);
  border-radius: 16px;
  padding: 14px;
}
.result span, .single-result span {
  display: block;
  color: var(--muted);
  font-size: 13px;
  margin-bottom: 4px;
}
.result strong, .single-result strong { font-size: 22px; }
.vertical { grid-template-columns: 1fr; }
.primary, .ghost {
  width: 100%;
  min-height: 44px;
  border-radius: 12px;
  border: 0;
  font-weight: 800;
  cursor: pointer;
  margin-top: 10px;
}
.primary { background: var(--primary); color: white; }
.primary:hover { background: var(--primary-dark); }
.ghost { background: white; border: 1px solid var(--line); color: var(--text); }
.converter {
  display: grid;
  grid-template-columns: 1.1fr 1fr auto 1fr;
  gap: 8px;
  align-items: center;
}
.note, .formula {
  background: var(--warn);
  border: 1px solid #fde68a;
  border-radius: 12px;
  padding: 10px;
  font-size: 14px;
  margin-top: 12px;
}
.field-help {
  font-size: 12.5px;
  line-height: 1.4;
  margin-top: 6px;
}
.note-block {
  background: var(--warn);
  border: 1px solid #fde68a;
  border-radius: 12px;
  color: #334155;
  font-size: 13px;
  line-height: 1.5;
  margin: 18px 0 8px;
  padding: 12px;
}
.note-block p {
  color: inherit;
  margin: 0;
}
.note-block p + p {
  margin-top: 8px;
}
.formula {
  background: #f8fafc;
  border-color: var(--line);
  color: #334155;
}
.material-row {
  display: grid;
  grid-template-columns: 1fr 110px;
  gap: 10px;
  align-items: center;
}

.advanced {
  margin-top: 14px;
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 10px 12px 12px;
  background: #fcfcfd;
}
.advanced summary {
  cursor: pointer;
  font-weight: 800;
  color: #334155;
}

footer {
  text-align: center;
  color: var(--muted);
  font-size: 12px;
  padding: 24px;
}
.hidden { display: none; }

@media (max-width: 760px) {
  .app-header {
    align-items: flex-start;
    flex-direction: column;
  }
  .tabs {
    top: 124px;
  }
}
@media (max-width: 680px) {
  .grid, .result, .converter { grid-template-columns: 1fr; }
  .converter span { text-align: center; }
  .tabs { grid-template-columns: repeat(2, 1fr); }
}


/* UI refinement: compact horizontal nav and cleaner side-panel layout */
.app-header {
  align-items: flex-start;
}

.header-actions {
  flex-shrink: 0;
}

.tabs {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 10px 14px;
  top: 91px;
  scrollbar-width: none;
}

.tabs::-webkit-scrollbar {
  display: none;
}

.tab {
  flex: 0 0 auto;
  min-width: 92px;
  border-radius: 999px;
  padding: 9px 14px;
  white-space: nowrap;
}

.settings-tab {
  min-width: 78px;
}

.tab-icon {
  display: inline-block;
  margin-right: 5px;
}

.lang-select {
  width: 100%;
  max-width: 240px;
}

.converter {
  margin-top: 10px;
  gap: 12px;
}

.converter input,
.converter select {
  margin-bottom: 0;
}

.converter span {
  display: flex;
  justify-content: center;
  align-items: center;
  color: var(--muted);
  font-weight: 800;
}

.single-result {
  margin-top: 14px;
}

@media (max-width: 760px) {
  .app-header {
    flex-direction: column;
  }

  .tabs {
    top: 119px;
  }
}

@media (max-width: 680px) {
  .tabs {
    display: flex;
    grid-template-columns: none;
  }

  .tab {
    min-width: 104px;
  }
}


/* Hamburger navigation for narrow side panels */
.top-nav {
  display: flex;
  gap: 8px;
  padding: 10px 14px;
  background: var(--card);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 91px;
  z-index: 4;
}

.nav-button {
  min-height: 42px;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 12px;
  font-weight: 800;
  cursor: pointer;
}

.menu-button {
  flex: 1;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  gap: 10px;
  padding: 0 14px;
}

.hamburger-icon {
  font-size: 18px;
  line-height: 1;
}

.icon-button {
  width: 46px;
  flex: 0 0 46px;
  font-size: 18px;
}

.icon-button.active,
.menu-button[aria-expanded="true"] {
  background: var(--soft);
  color: var(--primary);
  border-color: #bfdbfe;
}

.nav-menu {
  position: sticky;
  top: 144px;
  z-index: 6;
  margin: 0 14px;
  padding: 8px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: 0 10px 24px rgba(15, 23, 42, 0.12);
  display: grid;
  gap: 6px;
}

.nav-menu.hidden {
  display: none;
}

.menu-item {
  min-height: 40px;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 10px;
  font-weight: 800;
  text-align: left;
  padding: 0 12px;
  cursor: pointer;
}

.menu-item.active {
  background: var(--soft);
  color: var(--primary);
  border-color: #bfdbfe;
}

@media (max-width: 760px) {
  .top-nav {
    top: 119px;
  }

  .nav-menu {
    top: 172px;
  }
}


/* UI fix 2: force hamburger/settings button styling and reduce subtitle weight */
.app-header p {
  font-size: 13px;
  line-height: 1.35;
  max-width: 560px;
}

.top-nav {
  display: flex !important;
  gap: 8px;
  padding: 10px 14px;
  background: var(--card);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 91px;
  z-index: 4;
}

.top-nav .nav-button {
  appearance: none;
  -webkit-appearance: none;
  min-height: 42px;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 12px;
  font: inherit;
  font-size: 14px;
  font-weight: 800;
  color: var(--text);
  cursor: pointer;
  margin: 0;
}

.top-nav .menu-button {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  gap: 10px;
  padding: 0 14px;
}

.top-nav .icon-button {
  width: 46px;
  flex: 0 0 46px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  font-size: 18px;
}

.hamburger-icon {
  font-size: 18px;
  line-height: 1;
}

#currentTabLabel {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

@media (max-width: 430px) {
  .app-header p {
    display: none;
  }

  .top-nav {
    top: 72px;
  }

  .nav-menu {
    top: 124px;
  }
}


/* Info tooltip pattern */
.section-title {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 0 16px;
}

.section-title h2 {
  margin: 0;
}

.info-btn {
  position: relative;
  width: 24px;
  height: 24px;
  flex: 0 0 24px;
  border-radius: 50%;
  border: 1px solid #bfdbfe;
  background: var(--soft);
  color: var(--primary);
  font-weight: 900;
  font-size: 13px;
  line-height: 1;
  cursor: help;
  padding: 0;
}

.info-btn::after {
  content: attr(data-tooltip);
  position: absolute;
  left: 50%;
  top: calc(100% + 8px);
  transform: translateX(-50%);
  width: min(280px, calc(100vw - 48px));
  white-space: normal;
  background: #111827;
  color: #fff;
  border-radius: 12px;
  padding: 10px 12px;
  font-size: 12px;
  line-height: 1.45;
  font-weight: 600;
  text-align: left;
  opacity: 0;
  pointer-events: none;
  z-index: 30;
  box-shadow: 0 10px 24px rgba(15, 23, 42, 0.22);
}

.info-btn:hover::after,
.info-btn:focus::after,
.info-btn:active::after {
  opacity: 1;
}

.formula,
.note {
  display: none;
}


/* Tooltip position fix: keep info text inside narrow side panels */
.section-title {
  position: relative;
}

.info-btn::after {
  left: auto;
  right: auto;
  top: calc(100% + 8px);
  transform: none;
  width: clamp(220px, 72vw, 300px);
  max-width: calc(100vw - 48px);
  white-space: normal;
}

.section-title .info-btn::after {
  left: 50%;
  transform: translateX(-50%);
}

/* In narrow side panels, anchor tooltip to the card/title area instead of the button */
@media (max-width: 480px) {
  .section-title .info-btn::after {
    left: auto;
    right: -16px;
    transform: none;
    width: min(300px, calc(100vw - 44px));
    max-width: calc(100vw - 44px);
  }
}

/* Extremely narrow side-panel fallback */
@media (max-width: 380px) {
  .section-title .info-btn::after {
    right: -8px;
    width: min(280px, calc(100vw - 32px));
    max-width: calc(100vw - 32px);
    font-size: 11.5px;
  }
}


/* Inline info replaces tooltip popovers to prevent clipping in side panels */
.info-btn::after {
  display: none !important;
  content: none !important;
}

.info-btn {
  cursor: pointer;
}

.info-btn.active {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

.inline-info {
  margin: -6px 0 14px;
  padding: 10px 12px;
  border: 1px solid #bfdbfe;
  border-radius: 12px;
  background: var(--soft);
  color: #334155;
  font-size: 12.5px;
  line-height: 1.5;
  font-weight: 600;
}

.inline-info.hidden {
  display: none;
}


/* Desktop workbench layout */
body {
  min-height: 100vh;
}

.app-header {
  padding: 18px 28px;
  position: sticky;
  top: 0;
}

.app-shell {
  display: grid;
  grid-template-columns: 220px minmax(520px, 1fr) 360px;
  gap: 18px;
  max-width: 1560px;
  margin: 0 auto;
  padding: 18px 24px 10px;
  align-items: start;
}

.sidebar,
.history-panel {
  position: sticky;
  top: 96px;
}

.sidebar {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  box-shadow: 0 8px 20px rgba(15, 23, 42, 0.04);
}

.workspace-main {
  max-width: none;
  width: 100%;
  margin: 0;
  padding: 0;
}

.top-nav {
  position: static !important;
  border-bottom: 0;
  padding: 0;
  background: transparent;
}

.top-nav .menu-button {
  display: none;
}

.top-nav .icon-button {
  width: 100%;
  flex-basis: auto;
  min-height: 40px;
  margin-bottom: 8px;
}

.nav-menu,
.nav-menu.hidden {
  display: grid;
  position: static;
  margin: 0;
  padding: 0;
  border: 0;
  box-shadow: none;
  background: transparent;
  gap: 6px;
}

.menu-item {
  border-radius: 8px;
  min-height: 42px;
}

.card {
  border-radius: 8px;
  padding: 22px;
}

.result {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.result.vertical {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.result > div,
.single-result {
  border-radius: 8px;
}

.action-row {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin-top: 10px;
}

.primary,
.ghost,
.secondary {
  border-radius: 8px;
  margin-top: 0;
}

.secondary {
  width: 100%;
  min-height: 44px;
  border: 1px solid #bfdbfe;
  background: var(--soft);
  color: var(--primary);
  font-weight: 800;
  cursor: pointer;
}

.secondary:hover {
  border-color: var(--primary);
}

.compact {
  width: auto;
  min-height: 34px;
  padding: 6px 10px;
  font-size: 12px;
}

.history-panel {
  max-height: calc(100vh - 116px);
  overflow: auto;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
  box-shadow: 0 8px 20px rgba(15, 23, 42, 0.04);
}

.history-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}

.history-header h2 {
  margin: 0;
  font-size: 18px;
}

.history-list {
  display: grid;
  gap: 10px;
}

.history-empty {
  padding: 18px 10px;
  text-align: center;
}

.history-item {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  background: #fff;
}

.history-item-header {
  display: grid;
  gap: 6px;
}

.history-item h3 {
  margin: 0;
  font-size: 14px;
}

.history-item time {
  display: block;
  color: var(--muted);
  font-size: 11px;
  margin-top: 2px;
}

.history-item strong {
  color: var(--primary-dark);
  font-size: 16px;
  line-height: 1.35;
}

.history-lines {
  margin: 10px 0 0;
  padding: 0;
  list-style: none;
  color: #475467;
  font-size: 12px;
  line-height: 1.45;
}

.history-lines li + li {
  margin-top: 4px;
}

.history-actions {
  display: flex;
  gap: 8px;
  margin-top: 12px;
}

.danger-lite {
  color: #b42318;
  border-color: #fecdca;
}

@media (max-width: 1180px) {
  .app-shell {
    grid-template-columns: 190px minmax(0, 1fr);
  }

  .history-panel {
    grid-column: 1 / -1;
    position: static;
    max-height: none;
  }

  .history-list {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 760px) {
  .app-header {
    position: static;
    padding: 16px;
  }

  .app-shell {
    grid-template-columns: 1fr;
    padding: 12px;
  }

  .sidebar {
    position: static;
  }

  .top-nav .menu-button {
    display: flex;
  }

  .top-nav .icon-button {
    width: 46px;
    flex: 0 0 46px;
    margin-bottom: 0;
  }

  .top-nav {
    display: flex !important;
  }

  .nav-menu.hidden {
    display: none;
  }

  .nav-menu {
    margin-top: 8px;
  }

  .grid,
  .result,
  .result.vertical,
  .converter,
  .action-row,
  .history-list {
    grid-template-columns: 1fr;
  }
}


/* Factory suite visual alignment */
html,
body {
  width: 100%;
  height: 100%;
}

body {
  overflow: hidden;
  background: #eef2f6;
  color: #172033;
}

.app-header {
  display: none;
}

.app-shell {
  display: grid;
  grid-template-columns: 260px minmax(0, 1fr) 280px;
  gap: 0;
  width: 100%;
  max-width: none;
  height: 100vh;
  margin: 0;
  padding: 0;
  background: #eef2f6;
}

.sidebar,
.history-panel {
  position: static;
  top: auto;
  height: 100vh;
  max-height: none;
  overflow: auto;
  border-radius: 0;
  box-shadow: none;
}

.sidebar {
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 12px;
  background: #f8fafc;
  border: 0;
  border-right: 1px solid #cad3df;
}

.sidebar-brand {
  display: grid;
  gap: 5px;
  margin-bottom: 12px;
}

.sidebar-brand strong {
  font-size: 18px;
  font-weight: 800;
  line-height: 1.2;
}

.sidebar-brand span {
  color: #64748b;
  font-size: 12px;
  line-height: 1.35;
}

.top-nav {
  display: none !important;
  margin: 0;
  padding: 0;
}

.nav-menu,
.nav-menu.hidden {
  display: grid;
  gap: 6px;
}

.menu-item {
  min-height: 36px;
  border: 1px solid #c7d0df;
  border-radius: 5px;
  background: #fff;
  color: #172033;
  font-size: 13px;
  font-weight: 800;
  padding: 0 10px;
}

.menu-item.active {
  border-color: #2563eb;
  background: #dbeafe;
  color: #1d4ed8;
}

.workspace-main {
  display: grid;
  grid-template-rows: 42px minmax(0, 1fr);
  min-width: 0;
  min-height: 0;
  width: 100%;
  padding: 0;
}

.workspace-topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  min-width: 0;
  border-bottom: 1px solid #cad3df;
  background: #fff;
  padding: 0 14px;
}

.workspace-topbar strong {
  flex: 0 0 auto;
  font-size: 14px;
  font-weight: 900;
}

.workspace-topbar span {
  min-width: 0;
  overflow: hidden;
  color: #64748b;
  font-size: 12px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.calculator-stage {
  min-width: 0;
  min-height: 0;
  overflow: auto;
  padding: 16px;
}

.panel.active {
  display: block;
}

.card {
  max-width: 880px;
  min-height: auto;
  margin: 0 auto;
  border: 1px solid #d7dee9;
  border-radius: 6px;
  background: #fff;
  box-shadow: 0 2px 12px rgba(15, 23, 42, 0.1);
  padding: 18px;
}

.section-title {
  border-bottom: 1px solid #e2e8f0;
  margin-bottom: 12px;
  padding-bottom: 10px;
}

.section-title h2 {
  font-size: 18px;
}

label {
  color: #475569;
  font-size: 13px;
  font-weight: 800;
  margin: 10px 0 5px;
}

input,
select {
  min-height: 32px;
  border: 1px solid #c7d0df;
  border-radius: 4px;
  padding: 5px 8px;
  font-size: 14px;
}

.grid {
  gap: 0 12px;
}

.result,
.result.vertical {
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin: 14px 0;
}

.result > div,
.single-result {
  border: 1px solid #dbeafe;
  border-radius: 5px;
  background: #eff6ff;
  padding: 10px 12px;
}

.result span,
.single-result span {
  font-size: 12px;
}

.result strong,
.single-result strong {
  font-size: 20px;
}

.primary,
.secondary,
.ghost {
  min-height: 34px;
  border-radius: 4px;
  font-size: 13px;
}

.primary {
  border: 1px solid #2563eb;
  background: #2563eb;
}

.secondary {
  border: 1px solid #c7d0df;
  background: #fff;
  color: #172033;
}

.secondary:hover,
.ghost:hover {
  background: #f4f7fb;
}

.advanced,
.note-block,
.inline-info {
  border-radius: 5px;
}

.history-panel {
  border: 0;
  border-left: 1px solid #cad3df;
  background: #f8fafc;
  padding: 12px;
}

.history-header {
  margin-bottom: 10px;
}

.history-header h2 {
  font-size: 16px;
}

.history-list {
  grid-template-columns: 1fr;
}

.history-item {
  border: 1px solid #d7dee9;
  border-radius: 5px;
  box-shadow: none;
}

.history-item strong {
  color: #1d4ed8;
  font-size: 14px;
}

.history-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
}

footer {
  display: none;
}

@media (max-width: 1100px) {
  .app-shell {
    grid-template-columns: 220px minmax(0, 1fr);
  }

  .history-panel {
    display: none;
  }
}

@media (max-width: 760px) {
  body {
    overflow: auto;
  }

  .app-shell {
    grid-template-columns: 1fr;
    height: auto;
    min-height: 100vh;
  }

  .sidebar,
  .history-panel {
    height: auto;
  }

  .history-panel {
    display: block;
    border-left: 0;
    border-top: 1px solid #cad3df;
  }

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

  .workspace-topbar {
    min-height: 42px;
  }

  .result,
  .result.vertical {
    grid-template-columns: 1fr;
  }
}
