* { box-sizing: border-box; }

html, body {
  width: 100%;
  max-width: 100%;
  overflow-x: hidden;
}

body {
  margin: 0;
  font-family: system-ui, sans-serif;
  background: #111;
  color: #eee;
}

/* ---- Library locked state ---- */
body.isLocked #library,
body.isLocked #unlock {
  filter: blur(2px);
  opacity: 0.55;
  pointer-events: none;
  user-select: none;
}

header {
  display: flex;
  justify-content: space-between;
  align-items: center;

  /* ✅ Safe-area aware top padding so header never clips on iOS */
  padding:
    calc(env(safe-area-inset-top) + 0.75rem)
    1rem
    0.75rem
    1rem;

  background: #1b1b1b;
  border-bottom: 1px solid #222;
}

.version {
  padding: 0 1rem;
  font-size: 0.85rem;
  color: #9aa;
}

main {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.5rem;
}

section { padding: 0.75rem; }

button, input {
  width: 100%;
  padding: 0.6rem;
  margin-bottom: 0.5rem;
  border-radius: 10px;
  border: 1px solid #333;
  background: #161616;
  color: #eee;

  max-width: 100%;   /* ✅ NEW: never exceed container */
}

button {
  cursor: pointer;
  background: #202020;
}
button:hover { background: #262626; }
button:disabled { opacity: 0.55; cursor: not-allowed; }

.btnSecondary { background: #151515; border-color: #2b2b2b; }
.btnSecondary:hover { background: #1b1b1b; }
.btnDanger { background: #2a1414; border-color: #5a2222; }
.btnDanger:hover { background: #341818; }

.btnSmall,
.btnTiny {
  width: auto;
  margin: 0;

  flex: 0 1 auto;   /* ✅ NEW: allow buttons to shrink/wrap */
  max-width: 100%;  /* ✅ NEW: prevent overflow on very narrow widths */
}

.btnSmall {
  padding: 0.45rem 0.6rem;
  border-radius: 10px;
  font-size: 0.85rem;
}

.btnTiny {
  padding: 0.35rem 0.5rem;
  border-radius: 10px;
  font-size: 0.82rem;
}

#apps { padding: 0; margin: 0; }

#apps li {
  list-style: none;
  padding: 0.55rem 0.6rem;
  cursor: pointer;
  border: 1px solid #2a2a2a;
  border-radius: 12px;
  margin-bottom: 0.5rem;
  background: #141414;

  display: flex;
  flex-wrap: wrap;          /* ✅ NEW: allow wrapping */
  align-items: center;
  gap: 0.6rem;
}

#apps li.selected {
  outline: 2px solid #3a3a3a;
  background: #1b1b1b;
}

.appIcon {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  flex: 0 0 auto;
  object-fit: cover;
  background: #0b0b0b;
  border: 1px solid #2a2a2a;
}

.appMeta {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
  min-width: 0;

  flex: 1 1 260px;          /* ✅ CHANGED: can wrap under actions */
}

.appName {
  font-weight: 650;
  font-size: 0.95rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.appSub {
  font-size: 0.8rem;
  color: #aab;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.appActions {
  display: flex;
  align-items: center;
  gap: 0.4rem;

  flex-wrap: wrap;          /* ✅ NEW: buttons wrap */
  justify-content: flex-end;/* ✅ NEW: keep them to the right */
  min-width: 0;             /* ✅ NEW: critical for flex shrink */

  flex: 1 1 220px;          /* ✅ CHANGED: can drop to next line */
}

.appLast {
  font-size: 0.78rem;
  color: #9aa;
  margin-top: 2px;
  white-space: normal;
  overflow-wrap: anywhere;
}

.badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.18rem 0.45rem;
  border-radius: 999px;
  font-size: 0.75rem;
  border: 1px solid #2a2a2a;
  background: #111;
  color: #ccd;
  white-space: nowrap;
}

.badgeGreen { border-color: #275c35; background: #0f1a12; color: #9fe3b2; }
.badgeGray { border-color: #2a2a2a; background: #111; color: #bcc; }

.miniToggle {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.75rem;
  opacity: 0.9;
  user-select: none;
}
.miniToggle input { width: 14px; height: 14px; margin: 0; }

/* Progress */
#progressWrap {
  margin-top: 0.35rem;
  padding: 0.5rem 0.5rem 0.25rem;
  border-radius: 12px;
  border: 1px solid #2a2a2a;
  background: #121212;
}

#progressBar {
  width: 100%;
  height: 10px;
  border-radius: 999px;
  background: #222;
  overflow: hidden;
  border: 1px solid #2a2a2a;
}

#progressBarFill {
  height: 100%;
  width: 0%;
  background: #39d353;
  transition: width 0.25s ease;
}

#progressText {
  margin-top: 0.45rem;
  font-size: 0.85rem;
  color: #cdd;
}

.status {
  margin-top: 0.35rem;
  font-size: 0.85rem;
  color: #cdd;
  min-height: 1.1em;
}

/* Tablet */
@media (min-width: 768px) {
  main { grid-template-columns: 1fr; }
}

/* Splash */
#splash {
  position: fixed;
  inset: 0;
  background: #0b0b0b;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  opacity: 1;
  transition: opacity 0.6s ease;
  pointer-events: none;
}
#splash.hidden { opacity: 0; }
#splash img { max-width: 60vw; max-height: 60vh; }

/* ---------------- Modal ---------------- */

.modalHost.hidden { display: none; }
.modalHost {
  position: fixed;
  inset: 0;
  z-index: 9999;
}
.modalBackdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.65);
}
.modalPanel {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: min(980px, calc(100vw - 24px));
  max-height: min(82vh, 860px);
  overflow: auto;
  background: #0f0f11;
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 16px;
  box-shadow: 0 12px 42px rgba(0,0,0,0.55);
}
.modalHeader {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 12px 12px 10px;
  border-bottom: 1px solid #222;
}
.modalTitle { font-weight: 700; }
.modalClose {
  width: auto;
  padding: 0.35rem 0.6rem;
  margin: 0;
  border-radius: 10px;
}
.modalBody { padding: 12px; }
.modalFooter { padding: 12px; border-top: 1px solid #222; }

.modalFooterRow {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
.modalFooterRow button { width: auto; margin: 0; }

.stack { display: flex; flex-direction: column; gap: 10px; }
.modalText { margin: 0; line-height: 1.35; color: #d6d6df; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }

/* Inspector */
.inspectorTop {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
}
@media (min-width: 820px) {
  .inspectorTop { grid-template-columns: 1fr 1fr; }
}
.inspectorCard {
  padding: 10px 12px;
  border: 1px solid #222;
  border-radius: 14px;
  background: #121214;
}
.inspectorLabel { font-size: 0.85rem; color: #aab; }
.inspectorValue { font-size: 1.05rem; font-weight: 700; margin-top: 4px; }
.inspectorHint { font-size: 0.8rem; color: #99a; margin-top: 4px; }

.inspectorTableWrap { overflow: auto; border-radius: 14px; border: 1px solid #222; }
.inspectorTable {
  width: 100%;
  border-collapse: collapse;
  min-width: 860px;
}
.inspectorTable th, .inspectorTable td {
  padding: 10px 10px;
  border-bottom: 1px solid #222;
  vertical-align: top;
}
.inspectorTable th {
  text-align: left;
  font-size: 0.85rem;
  color: #bbc;
  background: #121214;
  position: sticky;
  top: 0;
}
.cellTitle { font-weight: 650; }
.cellSub { font-size: 0.82rem; color: #99a; margin-top: 2px; }
.cellActions { white-space: nowrap; display: flex; gap: 8px; }