* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Noto Sans JP", sans-serif;
  background: #11151d;
  color: #eee;
  overflow-x: hidden;
}

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 24px;
  background: #1b202a;
  border-bottom: 1px solid #39404d;
  position: sticky;
  top: 0;
  z-index: 50;
}

.header-top {
  display: flex;
  align-items: center;
  gap: 20px;
}

/* アプリ切替タブ */
.app-tabs {
  display: inline-flex;
  gap: 4px;
  background: #10151c;
  padding: 4px;
  border-radius: 10px;
  border: 1px solid #363e4c;
}

.tab-btn {
  background: transparent;
  border: none;
  color: #8993a3;
  padding: 8px 16px;
  border-radius: 7px;
  cursor: pointer;
  font-size: 13px;
  font-weight: bold;
  height: 34px;
  transition: all 0.2s ease;
}

.tab-btn:hover {
  color: #fff;
  background: rgba(255, 255, 255, 0.05);
  filter: none;
}

.tab-btn.active {
  background: #2563eb;
  color: #fff;
  box-shadow: 0 2px 8px rgba(37, 99, 235, 0.3);
}

.header-buttons {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.header-matrix-buff {
  display: flex;
  align-items: center;
  gap: 12px;
}

h1, h2 {
  margin: 0;
}

h1 {
  font-size: 22px;
}

h2 {
  font-size: 18px;
}

button, .file-label {
  border: 1px solid #555f70;
  background: #292f3b;
  color: #fff;
  border-radius: 9px;
  padding: 9px 13px;
  cursor: pointer;
  font-size: 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  white-space: nowrap;
  height: 38px;
  line-height: 1;
}

button:hover, .file-label:hover {
  filter: brightness(1.15);
}

button.selected {
  border-color: #e7b84c;
  background: #443a23;
}

button.primary {
  background: #a87816;
  border-color: #d8a83b;
}

.danger {
  background: #51252a;
  border-color: #8b424a;
}

label#loadJsonBtn {
  background-color: #3b82f6;
  border-color: #3b82f6;
  color: #ffffff;
}

.small {
  font-size: 12px;
  padding: 7px 10px;
}

main {
  max-width: 1600px;
  margin: auto;
  padding: 22px;
  display: block;
  width: 100%;
}

.panel {
  background: #1b202a;
  border: 1px solid #363e4c;
  border-radius: 14px;
  padding: 18px;
  box-shadow: 0 8px 25px rgba(0,0,0,.15);
  width: 100%;
  margin-bottom: 18px;
}

.preset-container {
  display: flex;
  flex-wrap: wrap;
  gap: 15px;
  align-items: center;
  justify-content: space-between;
  margin-top: 15px;
}

.preset-group {
  display: flex;
  gap: 8px;
  align-items: center;
  flex: 1;
  min-width: 260px;
}

.form-input, .form-select {
  padding: 8px 10px;
  border-radius: 6px;
  border: 1px solid #4c5665;
  background: #10151c;
  color: #fff;
  flex: 1;
  min-width: 0;
  height: 38px;
}

.add-row {
  display: grid;
  grid-template-columns: 1fr;
  gap: 20px;
  margin-top: 15px;
}

label {
  display: block;
  font-size: 13px;
  color: #aeb7c5;
  margin-bottom: 8px;
}

.character-list, .skill-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.character-btn {
  display: flex;
  align-items: center;
  gap: 7px;
  padding-top: 23px;
  padding-bottom: 23px;
}

.character-btn img {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  object-fit: cover;
}

.skill-list button {
  min-width: 70px;
  padding-top: 15px;
  padding-bottom: 15px;
}

.hint {
  color: #8993a3;
  font-size: 12px;
  margin: 8px 0 0;
}

.section-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}

.title-with-input {
  display: flex;
  align-items: center;
  gap: 10px;
}

.time-label {
  margin: 0;
}

.time-input {
  width: 60px;
  padding: 4px;
  border-radius: 4px;
  border: 1px solid #4c5665;
  background: #10151c;
  color: #fff;
}

.editor {
  min-height: 76px;
  margin-top: 14px;
  padding: 12px;
  border-radius: 10px;
  background: #11151d;
  border: 1px dashed #465061;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.empty {
  color: #778191;
  font-size: 13px;
}

.action-chip {
  display: flex;
  align-items: center;
  gap: 6px;
  background: #252c38;
  border: 1px solid #4b5565;
  border-radius: 9px;
  padding: 5px 7px;
  cursor: grab;
  user-select: none;
}

.action-chip.dragging {
  opacity: .4;
}

.action-chip img {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  object-fit: cover;
}

.action-chip .skill {
  font-size: 13px;
}

.action-chip .remove {
  border: 0;
  background: transparent;
  padding: 0 2px;
  color: #aab2bf;
  font-size: 17px;
}

.preview-wrap {
  overflow-x: auto;
  margin-top: 14px;
  background: #0b0e13;
  border-radius: 10px;
  padding: 8px;
  -webkit-overflow-scrolling: touch;
}

#previewCanvas {
  display: block;
  width: 100%;
  height: auto;
  min-width: 1050px;
}

.preview-controls {
  display: flex;
  gap: 10px;
  align-items: center;
}

.element-filter {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 12px;
}

.element-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 6px 10px;
  font-size: 13px;
  background: #202632;
  border: 1px solid #424b5a;
  border-radius: 8px;
  color: #ccc;
  cursor: pointer;
}

.element-btn img {
  width: 20px;
  height: 20px;
  object-fit: contain;
}

.element-btn.selected {
  background: #3b82f6;
  border-color: #60a5fa;
  color: #fff;
  font-weight: bold;
}

.element-btn:hover {
  filter: brightness(1.15);
}

#matrixView {
  width: 100%;
}

.matrix-layout {
  display: grid;
  grid-template-columns: 460px 1fr;
  gap: 18px;
  align-items: start;
}

.matrix-sticky-panel {
  position: sticky;
  top: 80px;
  max-height: calc(100vh - 100px);
  overflow-y: auto;
}

.selected-buff-summary {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.summary-label {
  font-size: 12px;
  color: #aeb7c5;
}

.selected-buff-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.buff-summary-badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: #252c38;
  border: 1px solid #e7b84c;
  padding: 3px 8px;
  border-radius: 6px;
  font-size: 12px;
  color: #fff;
}

.buff-summary-badge img {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  object-fit: cover;
}

.empty-text {
  font-size: 12px;
  color: #778191;
}

.buff-char-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.matrix-character-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(85px, 1fr));
  gap: 8px;
  margin-top: 10px;
}

.matrix-char-card {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 8px 4px;
  background: #292f3b;
  border: 1px solid #555f70;
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.2s ease;
  user-select: none;
}

.matrix-char-card:hover {
  filter: brightness(1.15);
  border-color: #e7b84c;
}

.matrix-char-card img {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  object-fit: cover;
  margin-bottom: 4px;
}

.matrix-char-card .char-name {
  font-size: 11px;
  color: #eee;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  width: 100%;
}

/* スタミナバッジ */
.stamina-badge {
  position: absolute;
  top: -4px;
  right: -4px;
  background: #a87816;
  border: 1px solid #d8a83b;
  color: #fff;
  font-size: 10px;
  font-weight: bold;
  padding: 1px 5px;
  border-radius: 10px;
  box-shadow: 0 2px 6px rgba(0,0,0,0.4);
}

.stamina-badge.empty {
  background: #51252a;
  border-color: #8b424a;
  color: #fca5a5;
}

.matrix-char-card.disabled {
  filter: grayscale(100%);
  opacity: 0.35;
  cursor: not-allowed;
  border-color: #363e4c;
}

.matrix-teams-container {
  display: flex;
  flex-direction: column;
  gap: 14px;
  margin-top: 14px;
}

.matrix-team-card {
  background: #11151d;
  border: 1px solid #363e4c;
  border-radius: 12px;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.team-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}

.team-title-input {
  background: #10151c;
  color: #fff;
  border: 1px solid #4c5665;
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 14px;
  font-weight: bold;
  width: 160px;
}

.grade-selector {
  display: flex;
  align-items: center;
  gap: 6px;
}

.grade-btn {
  padding: 4px 10px;
  font-size: 12px;
  font-weight: bold;
  border-radius: 6px;
  background: #1b202a;
  border: 1px solid #4c5665;
  color: #aeb7c5;
  height: 30px;
}

.grade-btn.selected.grade-S {
  background: #a87816;
  border-color: #d8a83b;
  color: #fff;
  box-shadow: 0 0 10px rgba(216, 168, 59, 0.4);
}

.grade-btn.selected.grade-A {
  background: #6b21a8;
  border-color: #a855f7;
  color: #fff;
}

.grade-btn.selected.grade-B {
  background: #1e40af;
  border-color: #3b82f6;
  color: #fff;
}

.grade-btn.selected.grade-C {
  background: #374151;
  border-color: #6b7280;
  color: #fff;
}

.team-slots {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.team-slot {
  position: relative;
  height: 64px;
  background: #1b202a;
  border: 2px dashed #363e4c;
  border-radius: 10px;
  display: flex;
  align-items: center;
  padding: 6px 10px;
  gap: 10px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.team-slot:hover {
  border-color: #555f70;
  background: #202632;
}

.team-slot.active-slot {
  border: 2px solid #3b82f6 !important;
  background: rgba(59, 130, 246, 0.1);
  box-shadow: 0 0 10px rgba(59, 130, 246, 0.3);
}

.team-slot.filled {
  border-style: solid;
  border-color: #4b5565;
  background: #252c38;
}

.team-slot img {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  object-fit: cover;
}

.team-slot span {
  font-size: 13px;
  font-weight: bold;
  color: #fff;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.empty-slot-text {
  color: #6b7280 !important;
  text-align: center;
  font-size: 12px !important;
  font-weight: normal !important;
}

.slot-remove-btn {
  background: transparent;
  border: none;
  color: #aab2bf;
  font-size: 18px;
  padding: 0 4px;
  height: auto;
  cursor: pointer;
}

.slot-remove-btn:hover {
  color: #f87171;
}

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(0, 0, 0, 0.75);
  backdrop-filter: blur(4px);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.modal-content {
  background: #1b202a;
  border: 1px solid #363e4c;
  border-radius: 14px;
  width: 100%;
  max-width: 800px;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 10px 30px rgba(0,0,0,0.5);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid #363e4c;
}

.modal-header h3 {
  margin: 0;
  font-size: 18px;
  color: #fff;
}

.modal-close-btn {
  background: transparent;
  border: none;
  color: #aab2bf;
  font-size: 24px;
  height: auto;
  padding: 0 4px;
}

.modal-body {
  padding: 20px;
  overflow-y: auto;
  flex: 1;
}

.modal-footer {
  padding: 12px 20px;
  border-top: 1px solid #363e4c;
  display: flex;
  justify-content: flex-end;
}

@media screen and (max-width: 1024px) {
  .matrix-layout {
    grid-template-columns: 1fr;
  }
  
  .matrix-sticky-panel {
    position: relative;
    top: 0;
    max-height: none;
  }
}

@media screen and (max-width: 768px) {
  header {
    flex-direction: column;
    gap: 12px;
    align-items: center;
    text-align: center;
    padding: 16px 12px;
  }

  .header-top {
    flex-direction: column;
    gap: 10px;
    width: 100%;
  }

  .app-tabs {
    width: 100%;
    justify-content: center;
  }

  .tab-btn {
    flex: 1;
  }

  .header-buttons, .header-matrix-buff {
    justify-content: center;
    width: 100%;
  }

  main {
    padding: 12px 8px;
  }

  .panel {
    padding: 12px;
  }

  .matrix-character-grid {
    grid-template-columns: repeat(auto-fill, minmax(75px, 1fr));
  }

  .team-slots {
    grid-template-columns: 1fr;
  }
}

.matrix-char-card.unowned img {
  filter: grayscale(100%);
  opacity: 0.3;
}

.matrix-char-card.unowned {
  border-color: #2e3542;
  background: #1a1e26;
}

.unowned-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.6);
  border-radius: 9px;
  display: flex;
  align-items: center;
  justify-content: center;
  
  color: #ff4d4d;
  font-size: 14px;
  font-weight: 900;
  text-shadow: 0 0 4px #000;
  
  letter-spacing: 1px;
  pointer-events: none;
}
