/* ===== base ===== */
*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  min-width: 0;
  font-size: 16px;
  scroll-behavior: smooth;
  -webkit-text-size-adjust: 100%;
}

body {
  min-width: 0;
  margin: 0;
  font-family: 'Noto Sans TC', sans-serif;
  background: linear-gradient(180deg, #f5fafb 0%, #e6f3f4 100%);
  color: #1e293b;
  overflow-x: hidden;
}

body.app-loading {
  cursor: wait;
}

body,
main,
section,
footer,
.app-shell,
.page-section,
.card {
  min-width: 0;
}

img,
svg,
canvas,
video,
iframe {
  max-width: 100%;
  height: auto;
}

table {
  max-width: 100%;
}

button,
input,
select,
textarea {
  max-width: 100%;
  min-width: 0;
  font: inherit;
}

button,
select,
input,
textarea,
.btn-primary,
.calculator-nav-btn {
  min-height: 2.75rem;
}

button,
[role='button'],
.btn-primary,
.calculator-nav-btn {
  touch-action: manipulation;
}

h1,
h2,
h3,
h4 {
  color: #165f63;
  letter-spacing: 0;
  text-shadow: 0 1px 2px rgba(22, 95, 99, .15);
}

h1 {
  font-size: 1.75rem;
  line-height: 1.2;
}

h2 {
  font-size: 1.25rem;
  line-height: 1.25;
}

.text-accent { color: #1fb7ad; }
.text-gold { color: #d4a418; }
.text-warning { color: #d65a2d; }

input[type='number']::-webkit-inner-spin-button,
input[type='number']::-webkit-outer-spin-button {
  opacity: .4;
}

.app-loading-overlay {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  background:
    linear-gradient(135deg, rgba(247, 252, 252, .96) 0%, rgba(230, 244, 242, .96) 52%, rgba(248, 250, 252, .96) 100%),
    repeating-linear-gradient(90deg, rgba(15, 118, 110, .06) 0 1px, transparent 1px 5rem);
  backdrop-filter: blur(4px);
}

body.app-loading .app-loading-overlay {
  display: flex;
}

.app-loading-panel {
  display: flex;
  width: min(100%, 24rem);
  min-height: 20rem;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  padding: 2rem 1.5rem;
  border: 1px solid rgba(20, 184, 166, .28);
  border-radius: .5rem;
  background: rgba(255, 255, 255, .9);
  color: #164e63;
  text-align: center;
  box-shadow: 0 1.25rem 2.5rem rgba(15, 23, 42, .18);
}

.app-loading-panel p {
  margin: 0;
}

.app-loading-mark {
  position: relative;
  display: grid;
  width: 6rem;
  height: 6rem;
  place-items: center;
}

.app-loading-mark img {
  width: 4.25rem;
  height: 4.25rem;
  border-radius: .5rem;
  object-fit: cover;
  box-shadow: 0 .75rem 1.5rem rgba(15, 95, 99, .18);
}

.app-loading-ring {
  position: absolute;
  inset: 0;
  border: 3px solid rgba(15, 118, 110, .14);
  border-top-color: #0f766e;
  border-right-color: #d4a418;
  border-radius: 999px;
  animation: app-loading-spin 1.05s linear infinite;
}

.app-loading-copy {
  display: grid;
  gap: .35rem;
}

.app-loading-title {
  font-size: 1.15rem;
  font-weight: 800;
  line-height: 1.25;
  color: #165f63;
}

.app-loading-message {
  color: #475569;
  font-size: .95rem;
  font-weight: 700;
}

.app-loading-progress {
  position: relative;
  width: min(100%, 15rem);
  height: .5rem;
  overflow: hidden;
  border-radius: 999px;
  background: #d7eeed;
}

.app-loading-progress span {
  position: absolute;
  inset-block: 0;
  left: -45%;
  width: 45%;
  border-radius: inherit;
  background: linear-gradient(90deg, #0f766e, #1fb7ad, #d4a418);
  animation: app-loading-progress 1.25s ease-in-out infinite;
}

.app-loading-dots {
  display: flex;
  gap: .35rem;
  align-items: center;
  justify-content: center;
}

.app-loading-dots span {
  width: .45rem;
  height: .45rem;
  flex: 0 0 auto;
  border-radius: 999px;
  background: #0f766e;
  animation: app-loading-dot 1s ease-in-out infinite;
}

.app-loading-dots span:nth-child(2) {
  animation-delay: .14s;
}

.app-loading-dots span:nth-child(3) {
  animation-delay: .28s;
}

@keyframes app-loading-spin {
  to {
    transform: rotate(360deg);
  }
}

@keyframes app-loading-progress {
  0% {
    left: -45%;
  }

  55%,
  100% {
    left: 100%;
  }
}

@keyframes app-loading-dot {
  0%,
  80%,
  100% {
    opacity: .35;
    transform: translateY(0);
  }

  40% {
    opacity: 1;
    transform: translateY(-.25rem);
  }
}

@media (prefers-reduced-motion: reduce) {
  .app-loading-ring,
  .app-loading-progress span,
  .app-loading-dots span {
    animation: none;
  }
}

/* ===== layout ===== */
.app-shell {
  width: 100%;
  max-width: min(100%, 80rem);
  margin-inline: auto;
}

.app-layout {
  width: 100%;
  max-width: min(100%, 92rem);
  margin-inline: auto;
}

.section-side-nav {
  display: none;
}

.section-side-nav-title {
  margin: 0 0 .35rem;
  padding: 0 .5rem;
  color: #165f63;
  font-size: .78rem;
  font-weight: 800;
  line-height: 1.2;
}

.section-side-nav a {
  display: flex;
  min-height: 2.25rem;
  align-items: center;
  justify-content: center;
  border: 1px solid transparent;
  border-radius: .5rem;
  color: #0f766e;
  font-size: .86rem;
  font-weight: 800;
  line-height: 1.15;
  text-align: center;
  text-decoration: none;
  transition: background-color .18s ease, border-color .18s ease, color .18s ease, transform .18s ease;
}

.section-side-nav a:hover,
.section-side-nav a:focus-visible {
  border-color: #9ed6d8;
  background: #eefafa;
  color: #165f63;
  outline: none;
  transform: translateX(2px);
}

.form-shell {
  max-width: min(100%, 48rem);
}

.app-toolbar,
.selector-bar {
  width: 100%;
}

.app-toolbar > *,
.selector-bar > * {
  min-width: 0;
}

.selector-bar > div,
.app-toolbar .input-field,
.calculator-nav-btn {
  width: 100%;
}

#calculator-page-content,
#fragment-calculator-panel,
#target-time-form-panel {
  min-width: 0;
}

#target-time-card,
#primordial-star-card,
#target-levels-card,
#relic-card,
#character-exp-card,
#skill-card,
#equipment-card,
#pet-card,
#production-card,
#owned-materials-card,
#results-card,
#material-source-card {
  scroll-margin-top: 1rem;
}

#primary-content-grid {
  align-items: start;
}

.main-grid-column {
  display: flex;
  min-width: 0;
  flex-direction: column;
  gap: 1rem;
}

.responsive-scroll {
  max-height: none;
  overflow-y: visible !important;
}

.tooltip-safe-scroll {
  position: relative;
  z-index: 20;
  overflow: visible !important;
}

.responsive-table {
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  overscroll-behavior-x: contain;
  -webkit-overflow-scrolling: touch;
}

.responsive-table table {
  width: max-content;
  min-width: 100%;
}

.responsive-table th,
.responsive-table td {
  padding: .4rem .5rem;
  white-space: nowrap;
}

/* ===== components ===== */
.card {
  background: #ffffffd9;
  border: 1px solid #cde5e8;
  border-radius: 10px;
  box-shadow: 0 2px 8px rgba(38, 115, 119, .15);
  transition: all .25s ease;
}

.calculator-section-card {
  gap: 1rem;
}

.calculator-section-card > h2 {
  position: static !important;
  z-index: auto !important;
  padding: 0 0 .75rem !important;
  margin: 0 !important;
  border-bottom: 1px solid #cde5e8;
  background: transparent !important;
  backdrop-filter: none !important;
  font-size: 1.25rem;
  line-height: 1.25;
}

.input-field {
  width: 100%;
  background-color: #f7fbfc;
  color: #1e293b;
  border: 1px solid #b6d7da;
  transition: border-color .25s, box-shadow .25s, background-color .25s;
}

.input-field:focus {
  border-color: #2cb5ab;
  box-shadow: 0 0 6px rgba(44, 181, 171, .4);
  outline: none;
  background-color: #f0fafa;
}

.input-field:disabled {
  background-color: #eef3f4;
  color: #64748b;
  border-color: #cbd5e1;
  cursor: not-allowed;
}

.btn-primary {
  background: linear-gradient(135deg, #31c9be 0%, #1a9e95 100%);
  color: #fffef8;
  border: 1px solid #21b8ac;
  box-shadow: 0 2px 6px rgba(34, 180, 168, .25);
  transition: background .3s, transform .15s;
}

.btn-primary:hover {
  background: linear-gradient(135deg, #45dfd3 0%, #23b7ac 100%);
  transform: translateY(-2px);
}

.calculator-nav-btn,
#enable-levelup-notify-btn,
#enable-target-notify-btn,
#clear-local-data-btn,
.fragment-fee-mode-btn,
.relic-mode-btn {
  font-size: .875rem;
}

.result-item {
  background: #fffdf5;
  border-left: 4px solid #f48b45;
  box-shadow: inset 0 0 8px rgba(244, 139, 69, .2);
}

.info-item {
  min-width: 0;
  background: #f0faf9;
  border-left: 4px solid #2cb5ab;
  box-shadow: inset 0 0 8px rgba(44, 181, 171, .15);
}

.cost-item {
  background: #fffbee;
  border-left: 4px solid #facc15;
  box-shadow: inset 0 0 8px rgba(249, 209, 98, .2);
}

#results .flex {
  align-items: stretch;
}

#results .result-item {
  box-shadow: 0 1px 3px rgb(0 0 0 / .1);
}

#results .bg-green-100:hover,
#results .bg-blue-100:hover,
#results .bg-orange-100:hover,
#results .bg-red-100:hover {
  filter: brightness(.95);
  transition: .2s;
}

.result-summary-row {
  display: flex;
  flex-direction: column;
  gap: .5rem;
  overflow-wrap: anywhere;
}

.result-summary-values {
  margin-left: 0 !important;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: flex-start;
  gap: .25rem 1rem;
  overflow-wrap: anywhere;
}

.material-source-section {
  min-width: 0;
}

.material-source-input {
  width: min(100%, 7rem);
  min-width: 4.75rem;
  min-height: 2.5rem;
}

.material-source-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: .5rem;
  color: #0f766e;
  font-weight: 700;
}

.material-source-toggle input {
  width: 1rem;
  min-width: 1rem;
  height: 1rem;
  min-height: 1rem;
  accent-color: #0f766e;
}

.material-source-total,
.material-source-derived {
  display: inline-block;
  max-width: 100%;
  overflow-wrap: anywhere;
}

.seo-explanation-copy {
  display: grid;
  gap: .75rem;
}

.custom-scrollbar::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

.custom-scrollbar::-webkit-scrollbar-track {
  background: #f2f8f9;
}

.custom-scrollbar::-webkit-scrollbar-thumb {
  background: linear-gradient(180deg, #b3dede 0%, #89cfcf 100%);
  border-radius: 4px;
}

.custom-scrollbar::-webkit-scrollbar-thumb:hover {
  background: linear-gradient(180deg, #6dcacb 0%, #4fbebf 100%);
}

.label-with-help {
  display: inline-flex;
  align-items: center;
  gap: .4rem;
}

.tooltip {
  position: relative;
  display: inline-flex;
  align-items: center;
}

.tooltip-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1rem;
  height: 1rem;
  border-radius: 9999px;
  border: 1px solid #7db8bd;
  background: #eef8f8;
  color: #24767b;
  font-size: .7rem;
  font-weight: 700;
  line-height: 1;
  cursor: help;
}

.tooltip-text {
  position: absolute;
  left: 0;
  bottom: calc(100% + 8px);
  width: max-content;
  min-width: min(16rem, calc(100vw - 2rem));
  max-width: min(18rem, calc(100vw - 2rem));
  padding: .6rem .75rem;
  border-radius: .65rem;
  background: rgba(15, 23, 42, .94);
  color: #f8fafc;
  font-size: .75rem;
  line-height: 1.45;
  box-shadow: 0 10px 30px rgba(15, 23, 42, .22);
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transform: none;
  transition: opacity .18s ease, transform .18s ease, visibility .18s ease;
  z-index: 9999;
}

.tooltip:not(.tooltip-active):not(:hover):not(:focus-within) .tooltip-text {
  display: none;
}

.tooltip-text::after {
  content: '';
  position: absolute;
  left: 1rem;
  top: 100%;
  border-width: 6px;
  border-style: solid;
  border-color: rgba(15, 23, 42, .94) transparent transparent transparent;
}

.tooltip.tooltip-active .tooltip-text,
.tooltip:hover .tooltip-text,
.tooltip:focus-within .tooltip-text {
  opacity: 1;
  visibility: visible;
  transform: translateY(-2px);
}

.tooltip.tooltip-active {
  z-index: 10000;
}

.tooltip-text.tooltip-floating {
  position: fixed;
  left: var(--tooltip-left, 0);
  top: var(--tooltip-top, 0);
  bottom: auto;
  transform: none !important;
}

.tooltip-text.tooltip-floating::after {
  display: none;
}

/* Compact density keeps the original visual style while respecting touch size. */
body.compact .card {
  border-radius: 8px;
}

body.compact .card > .custom-scrollbar {
  padding: 1rem !important;
}

body.compact h2 {
  padding: .5rem 1rem !important;
}

body.compact .input-field {
  padding: .5rem .625rem !important;
}

body.compact label {
  margin-bottom: .25rem !important;
}

body.compact .mb-6 {
  margin-bottom: 1rem !important;
}

body.compact .space-y-6 > * + * {
  margin-top: 1rem !important;
}

body.compact .gap-6 {
  gap: 1rem !important;
}

body.compact .gap-4 {
  gap: .75rem !important;
}

body.compact .result-item,
body.compact .info-item,
body.compact .cost-item {
  padding: .75rem !important;
}

body.compact #results .grid {
  gap: .75rem !important;
}

body.compact #results .grid > div {
  padding: .75rem !important;
}

/* ===== responsive ===== */
#relic-card > .absolute {
  position: static;
  display: flex;
  justify-content: center;
  margin-bottom: 1rem;
}

#notify-time-select,
#enable-levelup-notify-btn,
#enable-target-notify-btn,
#clear-local-data-btn,
#relay-submit-btn,
#target-time-form-panel a[data-i18n='relay_open_google_form'],
main a[data-i18n='relay_open_google_form'] {
  width: 100%;
}

#char-bed-inputs .input-field,
#char-bed-inputs .w-full,
#char-bed-inputs .flex,
#char-bed-inputs label {
  min-width: 0;
}

#char-bed-inputs .label-with-help {
  align-items: flex-start;
}

#char-bed-inputs .tooltip-text {
  max-width: min(15rem, calc(100vw - 2rem));
}

@media (min-width: 768px) {
  h1 {
    font-size: 2.25rem;
  }

  h2 {
    font-size: 1.5rem;
  }

  .app-toolbar .input-field,
  .selector-bar > div,
  .calculator-nav-btn,
  #notify-time-select,
  #enable-levelup-notify-btn,
  #enable-target-notify-btn,
  #clear-local-data-btn,
  #relay-submit-btn,
  #target-time-form-panel a[data-i18n='relay_open_google_form'],
  main a[data-i18n='relay_open_google_form'] {
    width: auto;
  }

  .result-summary-row {
    flex-direction: row;
    align-items: center;
  }

  .result-summary-values {
    margin-left: auto !important;
    align-items: center;
    justify-content: flex-end;
  }

  #relic-card > .absolute {
    position: absolute;
    right: 1rem;
    top: 1rem;
    margin-bottom: 0;
  }

  .tooltip-text {
    left: 50%;
    bottom: calc(100% + 10px);
    min-width: 220px;
    max-width: 320px;
    transform: translateX(-50%);
  }

  .tooltip-text::after {
    left: 50%;
    transform: translateX(-50%);
  }

  .tooltip.tooltip-active .tooltip-text,
  .tooltip:hover .tooltip-text,
  .tooltip:focus-within .tooltip-text {
    transform: translateX(-50%) translateY(-2px);
  }
}

@media (min-width: 1024px) {
  .app-shell {
    max-width: 80rem;
  }

  .form-shell {
    max-width: 48rem;
  }

  .responsive-table th,
  .responsive-table td {
    padding: .45rem .6rem;
  }
}

@media (min-width: 1280px) {
  .app-layout {
    display: grid;
    grid-template-columns: 7.5rem minmax(0, 1fr);
    align-items: start;
    gap: 1rem;
  }

  .app-layout .app-shell {
    max-width: none;
  }

  .section-side-nav {
    position: sticky;
    top: 1rem;
    z-index: 80;
    display: flex;
    flex-direction: column;
    gap: .25rem;
    max-height: calc(100vh - 2rem);
    overflow-y: auto;
    padding: .75rem;
    border: 1px solid #cde5e8;
    border-radius: .5rem;
    background: rgba(255, 255, 255, .9);
    box-shadow: 0 2px 8px rgba(38, 115, 119, .15);
  }

  .section-side-nav.is-hidden {
    display: none;
  }

  .responsive-scroll {
    overflow-y: visible !important;
  }

  .tooltip-safe-scroll {
    overflow-y: visible !important;
  }
}

@media (min-width: 1536px) {
  .app-layout {
    grid-template-columns: 8.75rem minmax(0, 1fr);
  }
}
