:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --text: #1c1f24;
  --muted: #6b7280;
  --line: #e2e5ea;
  --primary: #2563eb;
  --primary-text: #ffffff;
  --danger: #dc2626;
  --folder: #f2b33d;
  --shadow: 0 6px 24px rgb(0 0 0 / 0.14);
  --radius: 12px;
  --top: env(safe-area-inset-top, 0px);
  --bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121418;
    --surface: #1c1f25;
    --surface-2: #262a31;
    --text: #e8eaed;
    --muted: #9aa0a8;
    --line: #2f343c;
    --primary: #4f8cff;
    --danger: #f05252;
    --shadow: 0 6px 24px rgb(0 0 0 / 0.5);
    color-scheme: dark;
  }
}

* {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Hiragino Sans', 'Hiragino Kaku Gothic ProN', 'Noto Sans JP', 'Yu Gothic UI', Meiryo, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
}

body.no-scroll {
  overflow: hidden;
}

button,
input,
select {
  font: inherit;
  color: inherit;
}

.icon {
  width: 24px;
  height: 24px;
  fill: currentColor;
  flex: none;
}

.sr {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
}

.spacer {
  flex: 1;
}

.splash,
.empty {
  padding: 48px 24px;
  text-align: center;
  color: var(--muted);
}

.empty.small {
  padding: 16px;
}

.empty .btn {
  margin-top: 12px;
}

/* ---------- ボタン ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 8px 16px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  cursor: pointer;
}

.btn.small {
  min-height: 34px;
  padding: 4px 12px;
  font-size: 14px;
}

.btn.primary {
  background: var(--primary);
  border-color: var(--primary);
  color: var(--primary-text);
  font-weight: 600;
}

.btn.danger {
  color: var(--danger);
}

.btn.primary.danger,
.dialog-actions .btn.danger {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
}

.btn.wide {
  width: 100%;
}

.btn.disabled,
.btn:disabled {
  opacity: 0.4;
  pointer-events: none;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  cursor: pointer;
  flex: none;
}

.icon-btn:active {
  background: var(--surface-2);
}

.icon-btn:disabled {
  opacity: 0.3;
}

/* ---------- ログイン ---------- */

.login {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px 16px calc(24px + var(--bottom));
}

.login-card {
  width: 100%;
  max-width: 360px;
  background: var(--surface);
  border-radius: 20px;
  padding: 28px 22px;
  box-shadow: var(--shadow);
}

.login-logo {
  display: block;
  width: 72px;
  height: 72px;
  margin: 0 auto 8px;
}

.login h1 {
  text-align: center;
  font-size: 22px;
  margin: 0 0 20px;
}

.login-error {
  color: var(--danger);
  min-height: 1.5em;
  margin: 4px 0 12px;
  font-size: 14px;
}

.field {
  display: block;
  margin-bottom: 14px;
}

.field > span {
  display: block;
  font-size: 14px;
  color: var(--muted);
  margin-bottom: 4px;
}

.field input,
.field select {
  width: 100%;
  min-height: 46px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  font-size: 16px; /* iOS のズーム防止 */
}

.field input:focus,
.field select:focus {
  outline: 2px solid var(--primary);
  outline-offset: -1px;
}

.field small {
  display: block;
  color: var(--muted);
  font-size: 12px;
  margin-top: 4px;
}

.row {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* ---------- 上部バー ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 2px;
  padding: calc(4px + var(--top)) 4px 4px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.title {
  flex: 1;
  min-width: 0;
  margin: 0 4px;
  font-size: 18px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.search-form {
  flex: 1;
  display: flex;
  align-items: center;
}

.search-form[hidden] {
  display: none;
}

.search-input {
  flex: 1;
  min-width: 0;
  height: 40px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 20px;
  background: var(--bg);
  font-size: 16px;
}

.topbar.searching .title,
.topbar.searching .search-btn {
  display: none;
}

.app-head {
  position: sticky;
  top: 0;
  z-index: 10;
}

.crumbs {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 4px 8px;
  overflow-x: auto;
  white-space: nowrap;
  background: var(--surface);
  scrollbar-width: none;
}

.crumbs::-webkit-scrollbar {
  display: none;
}

.crumb {
  display: inline-flex;
  align-items: center;
  border: 0;
  background: transparent;
  padding: 6px 8px;
  border-radius: 8px;
  color: var(--primary);
  font-size: 14px;
  cursor: pointer;
}

.crumb .icon {
  width: 20px;
  height: 20px;
}

.crumb:last-child {
  color: var(--text);
  font-weight: 600;
}

.crumb-sep {
  color: var(--muted);
}

.toolbar {
  display: flex;
  align-items: center;
  padding: 0 4px 0 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.count {
  font-size: 13px;
  color: var(--muted);
}

/* ---------- ファイル一覧 ---------- */

.content {
  padding-bottom: calc(110px + var(--bottom));
}

.files.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(104px, 1fr));
  gap: 6px;
  padding: 8px;
}

.item {
  position: relative;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.item img {
  -webkit-touch-callout: none;
  pointer-events: none;
}

.thumb {
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
  background: var(--surface-2);
  color: var(--muted);
}

.thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.thumb.kind-dir {
  color: var(--folder);
}
.thumb.kind-image {
  color: #22a06b;
}
.thumb.kind-video {
  color: #8b5cf6;
}
.thumb.kind-audio {
  color: #e05297;
}
.thumb.kind-pdf {
  color: #e5484d;
}
.thumb.kind-doc {
  color: #3b82f6;
}
.thumb.kind-archive {
  color: #a16207;
}

.badge {
  position: absolute;
  right: 4px;
  bottom: 4px;
  padding: 0 5px;
  border-radius: 4px;
  background: rgb(0 0 0 / 0.55);
  color: #fff;
  font-size: 11px;
}

.files.grid .item {
  background: var(--surface);
  border-radius: 10px;
  overflow: hidden;
}

.files.grid .thumb {
  aspect-ratio: 1;
}

.files.grid .thumb .icon {
  width: 48px;
  height: 48px;
}

.files.grid .info {
  padding: 4px 6px 6px;
}

.files.grid .name {
  font-size: 12px;
  line-height: 1.35;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  word-break: break-all;
}

.files.grid .meta {
  display: none;
}

.files.list .item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 4px 8px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.files.list .thumb {
  width: 44px;
  height: 44px;
  border-radius: 8px;
}

.files.list .info {
  flex: 1;
  min-width: 0;
}

.files.list .name {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.meta {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.check {
  display: none;
  position: absolute;
  top: 6px;
  left: 6px;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 2px solid #fff;
  background: rgb(0 0 0 / 0.25);
  color: #fff;
  align-items: center;
  justify-content: center;
}

.check .icon {
  width: 18px;
  height: 18px;
  visibility: hidden;
}

.files.selecting .check {
  display: flex;
}

.files.list.selecting .check {
  position: static;
  order: -1;
  border-color: var(--muted);
}

.item.checked .check {
  background: var(--primary);
  border-color: var(--primary);
}

.item.checked .check .icon {
  visibility: visible;
}

.files.grid .item.checked {
  outline: 3px solid var(--primary);
  outline-offset: -3px;
}

.files.list .item.checked {
  background: color-mix(in srgb, var(--primary) 12%, var(--surface));
}

/* ---------- 右下ボタン・選択バー・アップロード ---------- */

.fab {
  position: fixed;
  right: 18px;
  bottom: calc(20px + var(--bottom));
  z-index: 20;
  width: 60px;
  height: 60px;
  border: 0;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.fab .icon {
  width: 30px;
  height: 30px;
}

.fab[hidden] {
  display: none;
}

.selbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 20;
  background: var(--surface);
  border-top: 1px solid var(--line);
  box-shadow: var(--shadow);
  padding: 4px 8px calc(8px + var(--bottom));
}

.selbar[hidden] {
  display: none;
}

.selbar-top {
  display: flex;
  align-items: center;
  gap: 4px;
}

.sel-count {
  font-weight: 600;
}

.selbar-actions {
  display: flex;
  gap: 6px;
  overflow-x: auto;
}

.selbar-actions .btn {
  flex: 1;
  flex-direction: column;
  gap: 0;
  min-width: 60px;
  padding: 6px 4px;
  font-size: 12px;
  border: 0;
}

.upload-panel {
  position: fixed;
  left: 8px;
  right: 8px;
  bottom: calc(90px + var(--bottom));
  z-index: 21;
  max-width: 520px;
  margin: 0 auto;
  padding: 10px 12px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  font-size: 14px;
}

.upload-panel[hidden] {
  display: none;
}

.upload-head {
  display: flex;
  align-items: center;
  gap: 8px;
}

.progress {
  height: 6px;
  margin: 8px 0 4px;
  border-radius: 3px;
  background: var(--surface-2);
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  width: 0;
  background: var(--primary);
  transition: width 0.2s;
}

.upload-current,
.upload-error {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.upload-error {
  color: var(--danger);
}

/* ---------- オーバーレイ ---------- */

.backdrop {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgb(0 0 0 / 0.4);
  animation: fade 0.15s;
}

.backdrop.center {
  align-items: center;
  padding: 16px;
}

@keyframes fade {
  from {
    opacity: 0;
  }
}

@keyframes rise {
  from {
    transform: translateY(40px);
    opacity: 0;
  }
}

.sheet {
  width: 100%;
  max-width: 520px;
  max-height: 85dvh;
  overflow-y: auto;
  background: var(--surface);
  border-radius: 16px 16px 0 0;
  padding: 8px 8px calc(8px + var(--bottom));
  animation: rise 0.18s ease-out;
}

.sheet-title {
  padding: 10px 12px;
  font-size: 13px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  border-bottom: 1px solid var(--line);
}

.sheet-note {
  padding: 10px 12px;
  font-size: 13px;
  color: var(--muted);
}

.sheet-item {
  display: flex;
  align-items: center;
  gap: 14px;
  width: 100%;
  min-height: 52px;
  padding: 10px 14px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  text-align: left;
  cursor: pointer;
}

.sheet-item:active {
  background: var(--surface-2);
}

.sheet-item .icon {
  color: var(--muted);
}

.sheet-item.danger,
.sheet-item.danger .icon {
  color: var(--danger);
}

.sheet-item.cancel {
  justify-content: center;
  margin-top: 4px;
  background: var(--surface-2);
  font-weight: 600;
}

.dialog {
  width: 100%;
  max-width: 420px;
  max-height: 90dvh;
  overflow-y: auto;
  background: var(--surface);
  border-radius: 16px;
  padding: 20px;
  box-shadow: var(--shadow);
  animation: rise 0.18s ease-out;
}

.dialog h2 {
  margin: 0 0 12px;
  font-size: 18px;
}

.dialog-message {
  margin: 0 0 16px;
  color: var(--muted);
  word-break: break-all;
}

.dialog-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  margin-top: 16px;
}

.dialog-actions .btn {
  flex: 1;
}

.dialog .btn.danger.wide {
  margin-top: 8px;
}

.info-list {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 6px 12px;
  margin: 0;
  font-size: 14px;
}

.info-list dt {
  color: var(--muted);
}

.info-list dd {
  margin: 0;
  word-break: break-all;
}

.help p {
  margin: 0 0 10px;
  font-size: 14px;
}

.picker {
  display: flex;
  flex-direction: column;
  height: min(80dvh, 640px);
}

.picker-path {
  font-size: 13px;
  color: var(--muted);
  padding-bottom: 8px;
  border-bottom: 1px solid var(--line);
  word-break: break-all;
}

.picker-list {
  flex: 1;
  overflow-y: auto;
}

.picker-item {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  min-height: 48px;
  padding: 8px;
  border: 0;
  border-bottom: 1px solid var(--line);
  background: transparent;
  text-align: left;
  cursor: pointer;
}

.picker-item .icon {
  color: var(--folder);
}

.picker-item:disabled {
  opacity: 0.35;
}

/* ---------- 全画面パネル（アカウント管理） ---------- */

.panel {
  position: fixed;
  inset: 0;
  z-index: 90;
  background: var(--bg);
  overflow-y: auto;
  animation: fade 0.15s;
}

.panel-body {
  max-width: 640px;
  margin: 0 auto;
  padding: 16px 16px calc(24px + var(--bottom));
}

.hint {
  font-size: 13px;
  color: var(--muted);
  margin: 0 0 12px;
}

.user-list {
  margin-top: 16px;
}

.user-item {
  display: block;
  width: 100%;
  padding: 12px 14px;
  margin-bottom: 8px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  text-align: left;
  cursor: pointer;
}

.user-name {
  font-weight: 600;
}

.tag {
  margin-left: 8px;
  padding: 1px 8px;
  border-radius: 10px;
  background: var(--surface-2);
  font-size: 12px;
  font-weight: 400;
}

/* ---------- ビューア ---------- */

.viewer {
  position: fixed;
  inset: 0;
  z-index: 95;
  background: #000;
  color: #fff;
}

.viewer-stage {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.viewer-stage img,
.viewer-stage video {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  -webkit-touch-callout: default;
}

.viewer-audio {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 24px;
}

.viewer-audio .icon {
  width: 96px;
  height: 96px;
  opacity: 0.6;
}

.viewer-bar {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  padding: calc(4px + var(--top)) 4px 8px;
  background: linear-gradient(rgb(0 0 0 / 0.7), transparent);
  transition: opacity 0.2s;
}

.viewer-caption {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  padding: 0 4px;
}

.viewer-name {
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.viewer-pos {
  font-size: 12px;
  opacity: 0.7;
}

.viewer .icon-btn:active {
  background: rgb(255 255 255 / 0.15);
}

.viewer-nav {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  background: rgb(0 0 0 / 0.35);
  transition: opacity 0.2s;
}

.viewer-nav.prev {
  left: 8px;
}

.viewer-nav.next {
  right: 8px;
}

.viewer-nav[hidden] {
  display: none;
}

.viewer.chrome-hidden .viewer-bar,
.viewer.chrome-hidden .viewer-nav {
  opacity: 0;
  pointer-events: none;
}

/* ---------- トースト ---------- */

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(96px + var(--bottom));
  z-index: 200;
  max-width: calc(100% - 32px);
  padding: 10px 16px;
  border-radius: 10px;
  background: #2b2f36;
  color: #fff;
  font-size: 14px;
  transform: translate(-50%, 20px);
  opacity: 0;
  pointer-events: none;
  transition:
    opacity 0.2s,
    transform 0.2s;
}

#toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

#toast.error {
  background: var(--danger);
}

@media (min-width: 700px) {
  .files.grid {
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 10px;
    padding: 12px;
  }
}

/* アカウント管理: 見られる共有フォルダ */
.share-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 4px 12px;
  margin: 4px 0 0 28px;
}

.share-opt {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 40px;
  font-size: 15px;
  overflow-wrap: anywhere;
}

.field .share-opt input {
  width: 20px;
  height: 20px;
  min-height: 0;
  padding: 0;
  flex: none;
  accent-color: var(--primary);
}

.share-list.dim {
  opacity: 0.45;
}

.dialog-error {
  color: var(--danger);
  font-size: 14px;
  margin: 0 0 8px;
}

.field .share-opt input[type='radio'] {
  accent-color: var(--primary);
}

/* パスワードの再設定画面 */
.login-note {
  color: var(--muted);
  font-size: 14px;
  line-height: 1.6;
  margin: 0 0 16px;
  text-align: left;
}

.login-sub {
  margin-top: 10px;
}

/* hidden 属性を付けた要素は、display の指定があっても隠す */
[hidden] {
  display: none !important;
}

/* アップロードの進み具合 */
.upload-note {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
  margin-top: 2px;
}

.upload-current {
  display: flex;
  gap: 8px;
  margin-top: 6px;
}

.upload-current-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

.upload-current-pct {
  flex: none;
  font-variant-numeric: tabular-nums;
}

.progress.small {
  height: 4px;
  margin: 4px 0 2px;
}

.progress.failed .progress-bar {
  background: var(--danger);
}

/* 進み具合がまだ分からないあいだは、左右に動くバーで「処理中」を示す */
.progress.indeterminate .progress-bar {
  width: 35%;
  animation: progress-slide 1.1s ease-in-out infinite;
}

@keyframes progress-slide {
  from {
    transform: translateX(-100%);
  }
  to {
    transform: translateX(290%);
  }
}

.spinner {
  flex: none;
  width: 12px;
  height: 12px;
  border: 2px solid var(--surface-2);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@media (prefers-reduced-motion: reduce) {
  .progress.indeterminate .progress-bar {
    animation-duration: 3s;
  }
  .spinner {
    animation-duration: 2.4s;
  }
}

/* メニューの項目の下の補足（iPhone の写真選択の待ち時間など） */
.sheet-item-text {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  min-width: 0;
  text-align: left;
}

.sheet-sub {
  font-size: 12px;
  line-height: 1.45;
  color: var(--muted);
  white-space: normal;
}

/* 確認画面の説明は改行をそのまま表示する */
.dialog-message {
  white-space: pre-line;
}

/* 外からのアクセスを遮断中の帯 */
.lockdown-banner {
  padding: 6px 12px;
  background: var(--danger);
  color: #fff;
  font-size: 13px;
  text-align: center;
}

/* PDF などの読み込み中の画面 */
.open-card .open-name {
  margin: 0 0 12px;
  font-weight: 600;
  overflow-wrap: anywhere;
}

.open-actions {
  display: grid;
  gap: 8px;
  margin-top: 16px;
}

.open-actions a.btn {
  text-align: center;
  text-decoration: none;
}

/* パスキー・2 段階認証 */
.login-or {
  margin: 12px 0;
  text-align: center;
  color: var(--muted);
  font-size: 13px;
}

.login-hint {
  margin-top: 12px;
  font-size: 12px;
}

.code-input {
  font-size: 24px !important;
  letter-spacing: 0.3em;
  text-align: center;
}

.totp-key {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 15px;
  word-break: break-all;
  padding: 8px;
  border-radius: 8px;
  background: var(--surface-2);
  user-select: all;
}

.passkey-list {
  display: grid;
  gap: 8px;
  margin-bottom: 12px;
}

.passkey-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 0;
  border-bottom: 1px solid var(--line);
}

.field .btn.small {
  margin: 6px 8px 0 0;
}

/* ログイン画面: どこからつないでいるか */
.login-place {
  margin: -4px 0 14px;
  text-align: center;
  color: var(--muted);
  font-size: 13px;
}
