/* ---
   Crab-Themed Flat Design System
   --- */

/* 1. Color & Font Variables (Light & Dark Mode) */
:root {
  /* Font */
  --font-main: -apple-system, BlinkMacSystemFont, "Helvetica Neue", "Segoe UI", "Hiragino Kaku Gothic ProN", "Hiragino Sans", Meiryo, sans-serif;

  /* Crab Color Palette */
  --c-accent-primary: hsl(14, 85%, 55%);
  /* Vibrant Orange */
  --c-accent-secondary: hsl(350, 75%, 58%);
  /* Strong Red */

  /* Light Mode Palette */
  --c-bg-light: hsl(30, 50%, 98%);
  --c-surface-light: hsl(0, 0%, 100%);
  --c-text-primary-light: hsl(215, 15%, 20%);
  --c-text-secondary-light: hsl(215, 10%, 45%);
  --c-border-light: hsl(215, 20%, 90%);

  /* Dark Mode Palette */
  --c-bg-dark: hsl(220, 15%, 12%);
  --c-surface-dark: hsl(220, 15%, 18%);
  --c-text-primary-dark: hsl(220, 15%, 95%);
  --c-text-secondary-dark: hsl(220, 10%, 70%);
  --c-border-dark: hsl(220, 15%, 30%);

  /* Sizing & Radius */
  --radius-l: 16px;
  --radius-m: 12px;
  --radius-s: 8px;
}

/* Default to Light Mode */
:root {
  --c-bg: var(--c-bg-light);
  --c-surface: var(--c-surface-light);
  --c-text-primary: var(--c-text-primary-light);
  --c-text-secondary: var(--c-text-secondary-light);
  --c-border: var(--c-border-light);
}

@media (prefers-color-scheme: dark) {
  :root {
    /* Set Dark Mode Colors */
    --c-bg: var(--c-bg-dark);
    --c-surface: var(--c-surface-dark);
    --c-text-primary: var(--c-text-primary-dark);
    --c-text-secondary: var(--c-text-secondary-dark);
    --c-border: var(--c-border-dark);
  }
}

/* 2. Base & Reset Styles */
*, *::before, *::after {
  box-sizing: border-box;
}

body {
  font-family: var(--font-main);
  background-color: var(--c-bg);
  color: var(--c-text-primary);
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  margin: 0;
  padding: 1rem;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* 3. Main Container Layout */
.container {
  background-color: var(--c-surface);
  padding: 2rem;
  border-radius: var(--radius-l);
  text-align: center;
  width: 100%;
  transition: all 0.3s ease;
  border: 1px solid var(--c-border);
}

/* 4. Typography */
.logo {
  font-size: 4rem;
  margin-bottom: 1rem;
  line-height: 1;
}

h1 {
  font-size: 2rem;
  font-weight: 700;
  margin: 0 0 0.5rem;
  color: var(--c-text-primary);
}

h2 {
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--c-text-secondary);
  border-bottom: 1px solid var(--c-border);
  padding-bottom: 0.75rem;
  margin: 2rem 0 1.5rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

p, .subtitle, #status {
  color: var(--c-text-secondary);
  margin-top: 0;
  margin-bottom: 1.5rem;
  line-height: 1.6;
}

p.subtitle {
  font-size: 1.1rem;
  margin-bottom: 2rem;
}

/* 5. Buttons & Interactive Elements */
button {
  border: 1px solid transparent;
  border-radius: var(--radius-s);
  cursor: pointer;
  font-size: 1rem;
  font-weight: 600;
  transition: all 0.2s ease-in-out;
  padding: 0.8rem 1.5rem;
  font-family: var(--font-main);
}

button:hover {
  transform: translateY(-2px);
}

button:active {
  transform: translateY(0);
  filter: brightness(0.9);
}

button:disabled, .icon-btn:disabled {
  background-color: var(--c-border) !important;
  color: var(--c-text-secondary) !important;
  cursor: not-allowed;
  transform: none;
  box-shadow: none !important;
  border-color: transparent !important;
  opacity: 0.7;
}

/* Main Call-to-Action Buttons */
#loginBtn, #registerBtn, .container>a {
  background-color: var(--c-accent-secondary);
  color: white;
  width: 100%;
  font-size: 1.1rem;
  padding: 1rem;
  border: none;
}

#loginBtn:hover, #registerBtn:hover, .container>a:hover {
  background-color: hsl(350, 75%, 50%);
}

/* Secondary Buttons */
#backToGameBtn, #shareScoreBtn, #backToGameFromCanvasBtn {
  background-color: var(--c-surface);
  color: var(--c-text-primary);
  border: 1px solid var(--c-border);
}

#backToGameBtn:hover, #shareScoreBtn:hover, #backToGameFromCanvasBtn:hover {
  border-color: var(--c-text-secondary);
  background-color: var(--c-bg);
}

#jankenStatus {
  font-size: 1.1rem;
  color: var(--c-text-secondary);
  margin-top: 1rem;
  margin-bottom: 2rem;
}

.button-group {
  display: flex;
  gap: 0.75rem;
  justify-content: center;
  flex-wrap: wrap;
  margin-top: 1rem;
}

input[type="text"] {
  padding: 1rem;
  margin-bottom: 1rem;
  width: 100%;
  border: 1px solid var(--c-border);
  border-radius: var(--radius-s);
  font-size: 1rem;
  background-color: var(--c-bg);
  color: var(--c-text-primary);
  font-family: inherit;
  transition: all 0.2s ease;
}

input[type="text"]::placeholder {
  color: var(--c-text-secondary);
}

input[type="text"]:focus {
  outline: none;
  border-color: var(--c-accent-primary);
  background-color: var(--c-surface);
  box-shadow: 0 0 0 3px hsla(14, 85%, 55%, 0.2);
}

/* 6. Icon Buttons (Janken, Gacha) */
.icon-btn {
  font-size: 2rem;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 70px;
  height: 70px;
  border-radius: var(--radius-m);
  background: var(--c-surface);
  color: var(--c-text-primary);
  border: 1px solid var(--c-border);
}

.icon-btn:hover {
  border-color: var(--c-accent-primary);
  color: var(--c-accent-primary);
}

.icon-btn.selected {
  background-color: var(--c-accent-secondary);
  color: white;
  border-color: var(--c-accent-secondary);
  transform: scale(1.1);
  box-shadow: 0 4px 10px hsla(350, 75%, 58%, 0.3);
}

#gachaBtn {
  background: var(--c-accent-primary);
  color: white;
  border: none;
}

#gachaBtn:hover {
  background-color: hsl(14, 85%, 48%);
}

#shareScoreBtn {
  font-size: 1rem;
}

/* 7. Lists (History & Ranking) */
hr {
  border: none;
  border-top: 1px solid var(--c-border);
  margin: 2rem 0;
}

#gameHistory, #rankingList {
  list-style: none;
  padding: 0;
  border-radius: var(--radius-m);
  text-align: left;
  background-color: var(--c-bg);
  border: 1px solid var(--c-border);
  overflow: hidden;
  margin: 1.5rem 0;
}

#gameHistory {
  max-height: 200px;
  overflow-y: auto;
}

#gameHistory li, #rankingList li {
  padding: 0.8rem 1.25rem;
  border-bottom: 1px solid var(--c-border);
  display: flex;
  align-items: center;
  gap: 1rem;
  transition: background-color 0.2s ease;
}

#gameHistory li {
  font-size: 0.7rem;
}

#rankingList li:hover {
  background-color: var(--c-surface);
}

#gameHistory li:last-child, #rankingList li:last-child {
  border-bottom: none;
}

#myRankSection {
  background-color: hsla(14, 85%, 55%, 0.1);
  padding: 1rem;
  border-radius: var(--radius-m);
  font-size: 1.1rem;
  border: 1px solid hsla(14, 85%, 55%, 0.3);
  font-weight: 600;
  color: var(--c-accent-primary);
}

#rankingList .rank {
  font-weight: 700;
  min-width: 40px;
  color: var(--c-text-secondary);
  font-size: 0.9em;
}

#rankingList .name {
  flex-grow: 1;
  font-weight: 600;
}

#rankingList .points {
  color: var(--c-text-secondary);
  font-size: 0.9em;
}

#rankingList li:nth-child(1) .name {
  color: gold;
  font-weight: 700;
}

#rankingList li:nth-child(2) .name {
  color: silver;
  font-weight: 700;
}

#rankingList li:nth-child(3) .name {
  color: #CD7F32;
  font-weight: 700;
}

#rankingList a {
  color: inherit;
}

/* 8. Header & Footer (Game Screen) */
.game-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2rem;
  flex-wrap: wrap;
  gap: 1rem;
}

.game-header h1 {
  font-size: 1.25rem;
  margin: 0;
}

.game-header .header-actions {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

#pointsDisplay {
  font-size: 1rem;
  padding: 0.5rem 1rem;
  background: var(--c-bg);
  border-radius: var(--radius-s);
  font-weight: 600;
}

.header-actions button {
  font-size: 1.5rem;
  padding: 0.5rem;
  width: 44px;
  height: 44px;
  background: var(--c-bg);
  border: 1px solid transparent;
  display: flex;
  align-items: center;
  justify-content: center;
}

.header-actions button:hover {
  border-color: var(--c-text-secondary);
}

.game-footer {
  margin-top: 2rem;
}

.header-point-btn {
  font-size: 1.5rem;
  padding: 0.5rem;
  width: 88px;
  height: 44px;
  background: var(--c-bg);
  border: 1px solid transparent;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-s);
  color: var(--c-text-primary);
  text-decoration: none;
  transition: all 0.2s ease;
}

.header-link-btn {
  font-size: 1.5rem;
  padding: 0.5rem;
  width: 44px;
  height: 44px;
  background: var(--c-bg);
  border: 1px solid transparent;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-s);
  color: var(--c-text-primary);
  text-decoration: none;
  transition: all 0.2s ease;
}

.header-link-btn:hover {
  border-color: var(--c-text-secondary);
  text-decoration: none;
}

/* 9. Links */
a {
  color: var(--c-accent-secondary);
  text-decoration: none;
  font-weight: 500;
}

a:hover {
  text-decoration: underline;
}

.skip-link-wrapper {
  margin-top: 1.5rem;
}

/* 10. Share Card Specific Styles */
.card {
  padding: 2rem;
  margin-bottom: 1.5rem;
  border-radius: var(--radius-l);
  background-color: var(--c-surface);
  border: 1px solid var(--c-border);
}

.card .username {
  font-size: 2.5rem;
  font-weight: 700;
  margin: 1rem 0;
  color: var(--c-text-primary);
}

.card .points {
  font-size: 1.75rem;
  font-weight: 600;
  color: var(--c-accent-primary);
  margin-bottom: 2rem;
}

.card .footer {
  font-size: 1rem;
  color: var(--c-text-secondary);
}

.container>a {
  display: inline-block;
  text-decoration: none;
  font-weight: 600;
}

.container>a:hover {
  text-decoration: none;
}

.ws-space {
  position: relative;
  display: inline-block;
  width: 0.5em;
  height: 1em;
}

.ws-space::before {
  content: '';
  position: absolute;
  top: 80%;
  left: 50%;
  width: 0.3em;
  height: 0.3em;
  background-color: rgba(150, 150, 150, 0.3);
  border-radius: 50%;
  transform: translate(-50%, -50%);
  pointer-events: none;
}


/* 11. Canvas Section Styles */
#canvasSection h1 {
  margin-bottom: 1.5rem;
}

.canvas-wrapper {
  position: relative;
  /* 虫めがねの位置の基準にする */
  width: min(80vw, 80vh);
  /* 画面幅と高さの小さい方の80%をサイズとする */
  aspect-ratio: 1 / 1;
  /* 常に1:1の正方形を維持する */
  margin: 1rem auto;
  /* 上下マージンを保ちつつ中央揃え */
  border: 1px solid var(--c-border);
  cursor: crosshair;
  overflow: hidden;
  /* コンテナからはみ出した部分を非表示に */
}

#pixelCanvas {
  width: 100%;
  height: 100%;
  display: block;
  /* 不要な余白を防ぐ */
  image-rendering: pixelated;
  /* ドット絵をくっきり表示 */
  image-rendering: -moz-crisp-edges;
  image-rendering: crisp-edges;
}

.canvas-controls {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 1.5rem;
  flex-wrap: wrap;
  padding: 0.5rem;
  background-color: var(--c-bg);
  border-radius: var(--radius-s);
}

.control-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

input[type="color"] {
  width: 40px;
  height: 40px;
  border: none;
  padding: 0;
  background: none;
  border-radius: var(--radius-s);
  cursor: pointer;
}

.control-item .icon-btn {
  width: 40px;
  height: 40px;
  font-size: 1.5rem;
  padding: 0;
}

#drawPixelBtn {
  background-color: var(--c-accent-secondary);
  color: white;
}

#loupe {
  position: absolute;
  width: 128px;
  height: 128px;
  border-radius: 50%;
  border: 2px solid #333;
  background-color: white;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
  z-index: 10;
  pointer-events: none;
  background-repeat: no-repeat;
  image-rendering: pixelated;
  image-rendering: -moz-crisp-edges;
  image-rendering: crisp-edges;
}

#loupe::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 8px;
  /* 拡大後の中央1ピクセルのサイズ */
  height: 8px;
  transform: translate(-50%, -50%);
  box-shadow: 0 0 0 1px rgba(255, 0, 0, 0.7);
  /* 中央のピクセルを赤い枠で示す */
  background: none;
}

/* 12. Board Section Styles */
#boardSection h1 {
  margin-bottom: 1.5rem;
}

#messageListContainer {
  max-height: 40vh;
  /* 少し高さを確保 */
  overflow-y: auto;
  border: 1px solid var(--c-border);
  border-radius: var(--radius-m);
  margin-bottom: 2rem;
  background-color: var(--c-bg);
}

#messageList {
  list-style: none;
  padding: 0;
  margin: 0;
  text-align: left;
}

#messageList li {
  display: flex;
  /* アイコンとテキストを横並びに */
  gap: 0.75rem;
  /* アイコンとテキストの間の余白 */
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--c-border);
}

#messageList li:last-child {
  border-bottom: none;
}

.message-content {
  display: flex;
  flex-direction: column;
  /* 名前と本文を縦に並べる */
  flex-grow: 1;
}

.message-meta {
  display: flex;
  justify-content: space-between;
  /* 名前と時刻を両端に配置 */
  align-items: baseline;
  font-size: 0.8em;
  color: var(--c-text-secondary);
  margin-bottom: 0.25rem;
}

.message-meta strong {
  color: var(--c-accent-primary);
  font-weight: 600;
  font-size: 1.2em;
  /* 少し名前を大きく */
}

.message-meta a {
  color: inherit;
  text-decoration: none;
}

.message-meta a:hover {
  text-decoration: underline;
}

.message-text {
  font-size: 0.9rem;
  word-break: break-all;
  /* 長い投稿がはみ出ないように */
}

.post-area, .add-phrase-area {
  margin-bottom: 1rem;
}

.form-group {
  display: flex;
  gap: 0.5rem;
}

.form-group select, .form-group input {
  flex-grow: 1;
  margin-bottom: 0;
}

.form-group button {
  flex-shrink: 0;
  background-color: var(--c-accent-primary);
  color: white;
  border: none;
}

/* 14. Profile Styles */
.welcome-container {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.profile-image-sm {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  object-fit: cover;
  background-color: var(--c-border);
}

.profile-image-md {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  object-fit: cover;
  background-color: var(--c-border);
  flex-shrink: 0;
}

.profile-image-lg {
  width: 128px;
  height: 128px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid var(--c-border);
}

.game-footer {
  display: flex;
  justify-content: center;
  gap: 1rem;
  margin-top: 2rem;
}

#profileSection .profile-form {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
}

#imageInput {
  display: none;
}

/* ファイル選択を非表示に */
.button-like-label {
  display: inline-block;
  padding: 0.8rem 1.5rem;
  background-color: var(--c-surface);
  color: var(--c-text-primary);
  border: 1px solid var(--c-border);
  border-radius: var(--radius-s);
  cursor: pointer;
  transition: all 0.2s ease;
}

.button-like-label:hover {
  background-color: var(--c-bg);
}

#uploadImageBtn {
  background-color: var(--c-accent-primary);
  color: white;
  border: none;
}

/* 15. Profile Page Styles */
.profile-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
}

.profile-username {
  font-size: 2.5rem;
  margin: 0;
}

.profile-points {
  font-size: 1.5rem;
  font-weight: 600;
  color: var(--c-accent-primary);
  margin: 0 0 1rem;
}

.button-like-link {
  display: inline-block;
  padding: 0.8rem 1.5rem;
  background-color: var(--c-surface);
  color: var(--c-text-primary);
  border: 1px solid var(--c-border);
  border-radius: var(--radius-s);
  text-decoration: none;
  font-weight: 600;
  transition: all 0.2s ease;
}

.button-like-link:hover {
  background-color: var(--c-bg);
  border-color: var(--c-text-secondary);
  text-decoration: none;
}

.profile-stats {
  font-size: 1.1rem;
  color: var(--c-text-secondary);
  margin-bottom: 1.5rem;
  border-top: 1px solid var(--c-border);
  border-bottom: 1px solid var(--c-border);
  padding: 1rem 0;
  width: 100%;
  text-align: center;
}

.profile-stats span {
  font-weight: 600;
  color: var(--c-text-primary);
  margin: 0 0.5rem;
}

.profile-card .button-group {
  display: flex;
  gap: 1rem;
  justify-content: center;
  width: 100%;
  margin-top: 1rem;
}

/* プロフィール画像に重ねるデバイスアイコンのコンテナ */
.device-icon-stack {
  position: absolute;
  bottom: 2px;  /* 位置を微調整 */
  right: 2px;   /* 位置を微調整 */
  display: flex;
  flex-direction: row-reverse; /* 右から左に並べる */
}

/* 個々のデバイスアイコンバッジ */
.device-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  background-color: var(--c-surface); /* カードの背景色 */
  border: 2px solid var(--c-surface); /* 重なり部分を綺麗に見せるための枠線 */
  border-radius: 50%;
  font-size: 16px;
  margin-right: -10px; /* アイコンを重ねる */
  box-shadow: 0 1px 3px rgba(0,0,0,0.2);
  transition: all 0.2s ease;
}

.device-badge:hover {
  transform: translateY(-2px); /* ホバー時に少し浮き上がらせる */
  margin-right: -4px; /* ホバー時に重なりを少し緩める */
}

/* リンクの基本色を調整 */
#gameHistory a, #messageList a {
  color: var(--c-accent-primary);
  font-weight: 600;
}

.profile-avatar-container {
  position: relative;
  width: 128px;
  height: 128px;
  margin-bottom: 1rem; /* image-lgから移設 */
  cursor: pointer; /* wrapperから移設 */
}

.profile-image-wrapper {
  position: relative;
  width: 128px;
  height: 128px;
  border-radius: 50%;
  overflow: hidden;
}

.profile-image-lg.editable {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: filter 0.2s ease;
}

.profile-image-wrapper:hover .profile-image-lg.editable {
  filter: brightness(0.7);
}

.edit-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  color: white;
  display: flex;
  justify-content: center;
  align-items: center;
  font-weight: bold;
  line-height: 1;
  opacity: 0.5;
  transition: opacity 0.2s ease;
  pointer-events: none;
}

.profile-image-wrapper:hover .edit-overlay {
  opacity: 1;
}

.rank .crown {
  display: inline-block;
  width: 1.5em;
  /* 絵文字用のスペースを確保 */
  text-align: left;
}

#rankingList li, #myRankSection {
  display: flex;
  align-items: center;
  gap: 1rem;
}

#rankingList li:hover {
  background-color: var(--c-surface);
}


.rank-icon-container {
  position: relative;
  /* 王冠を重ねるための基準位置 */
  flex-shrink: 0;
  /* コンテナが縮まないように */
}

.rank-crown {
  position: absolute;
  width: 20px;
  /* 王冠のサイズ */
  height: 20px;
  /* 王冠のサイズ */
  bottom: -5px;
  /* アイコンの下端からの位置 */
  right: -5px;
  /* アイコンの右端からの位置 */
  z-index: 2;
  /* アイコンより手前に表示 */
}

.stock-selector {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 1.5rem;
  border-bottom: 1px solid var(--c-border);
  flex-wrap: wrap;
}

.stock-selector button {
  background: none;
  border: none;
  padding: 0.5rem 1rem;
  cursor: pointer;
  border-radius: var(--radius-s) var(--radius-s) 0 0;
  color: var(--c-text-primary);
}

.stock-selector button.active {
  background-color: var(--c-surface);
  border: 1px solid var(--c-border);
  border-bottom: 1px solid var(--c-surface);
  font-weight: bold;
}

.price-display {
  margin-bottom: 1.5rem;
  text-align: left;
}

.price-display h2 {
  margin: 0 0 0.5rem;
  font-size: 1.2rem;
}

.price-display p {
  font-size: 2rem;
  font-weight: bold;
  margin: 0;
  color: var(--c-accent-primary);
}

.user-assets {
  text-align: right;
  margin-bottom: 1.5rem;
}

.trade-form {
  border-top: 1px solid var(--c-border);
  padding-top: 1.5rem;
}

.trade-note {
  font-size: 0.8em;
  color: var(--c-text-secondary);
  margin-top: 0.75rem;
  text-align: center;
}